Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Better proxy prototype
Author:
Committer:
Parent: 74d08d66f9fcfb267b4fc6b38b62c3fd0317847c (Merge branch '1.12.x' of https://github.com/MinecraftForge/MinecraftForge into emissive_items)
Branch:
Follows:
Precedes:
Local uncommitted changes, not checked in to index
-------- src/main/java/net/minecraftforge/fml/common/ProxyInjector.java --------
index 89465af3c..7ac74a2a5 100644
@@ -23,8 +23,10 @@ import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.Set;
+import net.minecraftforge.fml.common.SidedProxy.ProxyClass;
import net.minecraftforge.fml.common.discovery.ASMDataTable;
import net.minecraftforge.fml.common.discovery.ASMDataTable.ASMData;
+import net.minecraftforge.fml.common.discovery.asm.ModAnnotation.EnumHolder;
import net.minecraftforge.fml.relauncher.Side;
import org.apache.logging.log4j.Level;
@@ -44,6 +46,7 @@ public class ProxyInjector
SetMultimap<String, ASMData> modData = data.getAnnotationsFor(mod);
Set<ASMData> mods = modData.get(Mod.class.getName());
Set<ASMData> targets = modData.get(SidedProxy.class.getName());
+ Set<ASMData> impls = modData.get(ProxyClass.class.getName());
ClassLoader mcl = Loader.instance().getModClassLoader();
for (ASMData targ : targets)
@@ -80,7 +83,16 @@ public class ProxyInjector
String targetType = side.isClient() ? annotation.clientSide() : annotation.serverSide();
if(targetType.equals(""))
{
- targetType = targ.getClassName() + (side.isClient() ? "$ClientProxy" : "$ServerProxy");
+ ASMData impl = impls.stream()
+ .filter(a -> a.getAnnotationInfo().get("modId").equals(mod.getModId()))
+ .filter(a -> ((EnumHolder) a.getAnnotationInfo().get("side")).getValue().equals(side.toString()))
+ .findFirst().orElse(null);
+
+ if (impl == null) {
+ targetType = targ.getClassName() + (side.isClient() ? "$ClientProxy" : "$ServerProxy");
+ } else {
+ targetType = impl.getClassName();
+ }
}
Object proxy=Class.forName(targetType, true, mcl).newInstance();
--------- src/main/java/net/minecraftforge/fml/common/SidedProxy.java ---------
index 011946d1b..337381c05 100644
@@ -23,6 +23,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
+import net.minecraftforge.fml.relauncher.Side;
/**
* Sided proxies are loaded based on the specific environment they find themselves loaded into.
@@ -34,20 +35,21 @@ import java.lang.annotation.Target;
* <p>
* This example will load a CommonProxy on the server side, and a ClientProxy on the client side.
*
- * <pre>{@code
+ * <pre>
* public class MySidedProxyHolder {
- * {@literal @}SidedProxy(modId="MyModId",clientSide="mymod.ClientProxy", serverSide="mymod.CommonProxy")
+ * {@literal @}SidedProxy(modId="MyModId")
* public static CommonProxy proxy;
* }
*
+ * {@literal @}ProxyClass(Side.SERVER)
* public class CommonProxy {
* // Common or server stuff here that needs to be overridden on the client
* }
- *
+ *
+ * {@literal @}ProxyClass(Side.CLIENT)
* public class ClientProxy extends CommonProxy {
* // Override common stuff with client specific stuff here
* }
- * }
* </pre>
* @author cpw
*
@@ -59,13 +61,19 @@ public @interface SidedProxy
/**
* The full name of the client side class to load and populate.
* Defaults to the nested class named "ClientProxy" in the current class.
+ *
+ * @deprecated Use {@link ProxyClass}
*/
+ @Deprecated
String clientSide() default "";
/**
* The full name of the server side class to load and populate.
* Defaults to the nested class named "ServerProxy" in the current class.
+ *
+ * @deprecated Use {@link ProxyClass}
*/
+ @Deprecated
String serverSide() default "";
/**
@@ -73,4 +81,18 @@ public @interface SidedProxy
* Or there is no other way to determine the mod this annotation belongs to. When in doubt, add this value.
*/
String modId() default "";
+
+ /**
+ * Use this to define your client and server proxy implementations.
+ * <p>
+ * {@code @ProxyClass(Side.CLIENT)} or {@code @ProxyClass(Side.SERVER)}
+ */
+ @Retention(RetentionPolicy.RUNTIME)
+ @Target(ElementType.TYPE)
+ public @interface ProxyClass {
+
+ String modId();
+
+ Side side();
+ }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment