From deab31fb03839b759f1dac6c63bc0cf81cbab81f Mon Sep 17 00:00:00 2001 From: Christian Date: Mon, 13 Aug 2012 15:26:29 -0400 Subject: [PATCH] Add in a sided variant of the packethandlers in the annotation - also, a bit more explicit warning on when to register blocks - if you do it after pre-init, you'll reset the achievements --- .../cpw/mods/fml/common/LoadController.java | 5 + fml/common/cpw/mods/fml/common/Loader.java | 10 ++ .../mods/fml/common/network/NetworkMod.java | 30 +++- .../fml/common/network/NetworkModHandler.java | 135 +++++++++++++++--- .../fml/common/registry/GameRegistry.java | 68 ++++++++- .../net/minecraft/src/RenderEngine.java.patch | 2 +- 6 files changed, 228 insertions(+), 22 deletions(-) diff --git a/fml/common/cpw/mods/fml/common/LoadController.java b/fml/common/cpw/mods/fml/common/LoadController.java index caa4f22e6c..e7e0664799 100644 --- a/fml/common/cpw/mods/fml/common/LoadController.java +++ b/fml/common/cpw/mods/fml/common/LoadController.java @@ -193,4 +193,9 @@ public class LoadController { return ImmutableBiMap.copyOf(modObjectList); } + + public boolean isInState(LoaderState state) + { + return this.state == state; + } } diff --git a/fml/common/cpw/mods/fml/common/Loader.java b/fml/common/cpw/mods/fml/common/Loader.java index 9fc0e27ab6..ce14116ae0 100644 --- a/fml/common/cpw/mods/fml/common/Loader.java +++ b/fml/common/cpw/mods/fml/common/Loader.java @@ -621,4 +621,14 @@ public class Loader { return getModObjectList().inverse(); } + + public ModContainer activeModContainer() + { + return modController.activeContainer(); + } + + public boolean isInState(LoaderState state) + { + return modController.isInState(state); + } } diff --git a/fml/common/cpw/mods/fml/common/network/NetworkMod.java b/fml/common/cpw/mods/fml/common/network/NetworkMod.java index 3d8095fa1c..c4b9bbdefb 100644 --- a/fml/common/cpw/mods/fml/common/network/NetworkMod.java +++ b/fml/common/cpw/mods/fml/common/network/NetworkMod.java @@ -24,7 +24,9 @@ public @interface NetworkMod */ boolean serverSideRequired() default false; /** - * A list of Packet250 network channels to register for this mod + * A list of Packet250 network channels to register for this mod - these channels + * will be universal and will require a universal packethandler to handle them + * * @return */ String[] channels() default {}; @@ -36,6 +38,8 @@ public @interface NetworkMod /** * A packet handler implementation for channels registered through this annotation + * - this packet handler will be universal and handle both client and server + * requests. * * @return */ @@ -47,12 +51,26 @@ public @interface NetworkMod * @return */ Class connectionHandler() default NULL.class; + /** + * A packet handler and channels to register for the client side + * + * @return + */ + SidedPacketHandler clientPacketHandlerSpec() default @SidedPacketHandler(channels = {}, packetHandler = NULL.class ); + + /** + * A packet handler and channels to register for the server side + * @return + */ + SidedPacketHandler serverPacketHandlerSpec() default @SidedPacketHandler(channels = {}, packetHandler = NULL.class ); + /** * Special dummy class for handling stupid annotation default values * @author cpw * */ static interface NULL extends IPacketHandler, IConnectionHandler {}; + /** * A marker for a method that will be offered the client's version string * if more sophisticated version rejection handling is required: @@ -64,4 +82,14 @@ public @interface NetworkMod @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface VersionCheckHandler { } + + /** + * Bundles together a packet handler and it's associated channels for the sided packet handlers + * @author cpw + * + */ + public @interface SidedPacketHandler { + String[] channels(); + Class packetHandler(); + } } diff --git a/fml/common/cpw/mods/fml/common/network/NetworkModHandler.java b/fml/common/cpw/mods/fml/common/network/NetworkModHandler.java index 09824a51be..0337d4dace 100644 --- a/fml/common/cpw/mods/fml/common/network/NetworkModHandler.java +++ b/fml/common/cpw/mods/fml/common/network/NetworkModHandler.java @@ -24,6 +24,11 @@ import cpw.mods.fml.common.versioning.VersionRange; public class NetworkModHandler { + private static Object connectionHandlerDefaultValue; + private static Object packetHandlerDefaultValue; + private static Object clientHandlerDefaultValue; + private static Object serverHandlerDefaultValue; + private static int assignedIds = 1; private int localId; @@ -92,30 +97,17 @@ public class NetworkModHandler } } - if (!mod.packetHandler().getName().equals(NetworkMod.NULL.class.getName())) + tryCreatingPacketHandler(container, mod.packetHandler(), mod.channels()); + if (mod.clientPacketHandlerSpec() != getClientHandlerSpecDefaultValue()) { - IPacketHandler instance; - try - { - instance = mod.packetHandler().newInstance(); - } - catch (Exception e) - { - FMLLog.log(Level.SEVERE, e, "Unable to create packet handler instance %s", mod.packetHandler().getName()); - throw new FMLNetworkException(e); - } - - for (String channel : mod.channels()) - { - NetworkRegistry.instance().registerChannel(instance, channel); - } + tryCreatingPacketHandler(container, mod.clientPacketHandlerSpec().packetHandler(), mod.clientPacketHandlerSpec().channels()); } - else if (mod.channels().length > 0) + if (mod.serverPacketHandlerSpec() != getServerHandlerSpecDefaultValue()) { - FMLLog.warning("The mod id %s attempted to register channels without specifying a valid packet handler", container.getModId()); + tryCreatingPacketHandler(container, mod.serverPacketHandlerSpec().packetHandler(), mod.serverPacketHandlerSpec().channels()); } - if (!mod.connectionHandler().getName().equals(NetworkMod.NULL.class.getName())) + if (mod.connectionHandler() != getConnectionHandlerDefaultValue()) { IConnectionHandler instance; try @@ -131,7 +123,112 @@ public class NetworkModHandler NetworkRegistry.instance().registerConnectionHandler(instance); } } + /** + * @param container + */ + private void tryCreatingPacketHandler(ModContainer container, Class clazz, String[] channels) + { + if (clazz!=getPacketHandlerDefaultValue()) + { + if (channels.length==0) + { + FMLLog.log(Level.WARNING, "The mod id %s attempted to register a packet handler without specifying channels for it", container.getModId()); + } + else + { + IPacketHandler instance; + try + { + instance = clazz.newInstance(); + } + catch (Exception e) + { + FMLLog.log(Level.SEVERE, e, "Unable to create a packet handler instance %s for mod %s", clazz.getName(), container.getModId()); + throw new FMLNetworkException(e); + } + + for (String channel : channels) + { + NetworkRegistry.instance().registerChannel(instance, channel); + } + } + } + else if (channels.length > 0) + { + FMLLog.warning("The mod id %s attempted to register channels without specifying a packet handler", container.getModId()); + } + } + /** + * @return + */ + private Object getConnectionHandlerDefaultValue() + { + try { + if (connectionHandlerDefaultValue == null) + { + connectionHandlerDefaultValue = NetworkMod.class.getMethod("connectionHandler").getDefaultValue(); + } + return connectionHandlerDefaultValue; + } + catch (NoSuchMethodException e) + { + throw new RuntimeException("Derp?", e); + } + } + + /** + * @return + */ + private Object getPacketHandlerDefaultValue() + { + try { + if (packetHandlerDefaultValue == null) + { + packetHandlerDefaultValue = NetworkMod.class.getMethod("packetHandler").getDefaultValue(); + } + return packetHandlerDefaultValue; + } + catch (NoSuchMethodException e) + { + throw new RuntimeException("Derp?", e); + } + } + + /** + * @return + */ + private Object getClientHandlerSpecDefaultValue() + { + try { + if (clientHandlerDefaultValue == null) + { + clientHandlerDefaultValue = NetworkMod.class.getMethod("clientPacketHandler").getDefaultValue(); + } + return clientHandlerDefaultValue; + } + catch (NoSuchMethodException e) + { + throw new RuntimeException("Derp?", e); + } + } + /** + * @return + */ + private Object getServerHandlerSpecDefaultValue() + { + try { + if (serverHandlerDefaultValue == null) + { + serverHandlerDefaultValue = NetworkMod.class.getMethod("serverPacketHandler").getDefaultValue(); + } + return serverHandlerDefaultValue; + } + catch (NoSuchMethodException e) + { + throw new RuntimeException("Derp?", e); + } + } public boolean requiresClientSide() { return mod.clientSideRequired(); diff --git a/fml/common/cpw/mods/fml/common/registry/GameRegistry.java b/fml/common/cpw/mods/fml/common/registry/GameRegistry.java index 837f4a5c9e..946077917e 100644 --- a/fml/common/cpw/mods/fml/common/registry/GameRegistry.java +++ b/fml/common/cpw/mods/fml/common/registry/GameRegistry.java @@ -24,12 +24,15 @@ import com.google.common.collect.Lists; import com.google.common.collect.Multimap; import com.google.common.collect.Sets; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.ICraftingHandler; import cpw.mods.fml.common.IDispenseHandler; import cpw.mods.fml.common.IFuelHandler; import cpw.mods.fml.common.IWorldGenerator; +import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.LoaderException; +import cpw.mods.fml.common.LoaderState; import cpw.mods.fml.common.Mod.Block; import cpw.mods.fml.common.ModContainer; @@ -42,11 +45,26 @@ public class GameRegistry private static List craftingHandlers = Lists.newArrayList(); private static List dispenserHandlers = Lists.newArrayList(); + /** + * Register a world generator - something that inserts new block types into the world + * + * @param generator + */ public static void registerWorldGenerator(IWorldGenerator generator) { worldGenerators.add(generator); } + /** + * Callback hook for world gen - if your mod wishes to add extra mod related generation to the world + * call this + * + * @param chunkX + * @param chunkZ + * @param world + * @param chunkGenerator + * @param chunkProvider + */ public static void generateWorld(int chunkX, int chunkZ, World world, IChunkProvider chunkGenerator, IChunkProvider chunkProvider) { long worldSeed = world.func_72905_C(); @@ -61,12 +79,30 @@ public class GameRegistry } } + /** + * Register a handler for dispensers + * + * @param handler + */ public static void registerDispenserHandler(IDispenseHandler handler) { dispenserHandlers.add(handler); } + /** + * Callback hook for dispenser activities - if you add a block and want mods to be able + * to extend their dispenser related activities to it call this + * + * @param world + * @param x + * @param y + * @param z + * @param xVelocity + * @param zVelocity + * @param item + * @return + */ public static int tryDispense(World world, double x, double y, double z, int xVelocity, int zVelocity, ItemStack item) { for (IDispenseHandler handler : dispenserHandlers) @@ -79,6 +115,14 @@ public class GameRegistry } return -1; } + /** + * Internal method for creating an @Block instance + * @param container + * @param type + * @param annotation + * @return + * @throws Exception + */ public static Object buildBlock(ModContainer container, Class type, Block annotation) throws Exception { Object o = type.getConstructor(int.class).newInstance(findSpareBlockId()); @@ -86,18 +130,40 @@ public class GameRegistry return o; } + /** + * Private and not yet working properly + * + * @return + */ private static int findSpareBlockId() { return BlockTracker.nextBlockId(); } + /** + * Register a block with the world + * + */ public static void registerBlock(net.minecraft.src.Block block) { registerBlock(block, ItemBlock.class); } + /** + * Register a block with the world, with the specified item class + * + * @param block + * @param itemclass + */ public static void registerBlock(net.minecraft.src.Block block, Class itemclass) { + if (Loader.instance().isInState(LoaderState.CONSTRUCTING)) + { + FMLLog.warning("The mod %s is attempting to register a block whilst it it being constructed. This is bad modding practice - please use a proper mod lifecycle event.", Loader.instance().activeModContainer()); + } else if (!Loader.instance().isInState(LoaderState.PREINITIALIZATION)) + { + FMLLog.warning("The mod %s is attempting to register a block after the statistics have loaded. This will reset the statistics for the game", Loader.instance().activeModContainer().getModId()); + } try { assert block != null : "registerBlock: block cannot be null"; @@ -110,7 +176,7 @@ public class GameRegistry FMLLog.log(Level.SEVERE, e, "Caught an exception during block registration"); throw new LoaderException(e); } - BlockTracker.reserveBlockId(block.field_71990_ca); + blockRegistry.put(Loader.instance().activeModContainer(), (BlockProxy) block); } public static void addRecipe(ItemStack output, Object... params) diff --git a/fml/patches/minecraft/net/minecraft/src/RenderEngine.java.patch b/fml/patches/minecraft/net/minecraft/src/RenderEngine.java.patch index 9a861a1276..9827584ce9 100644 --- a/fml/patches/minecraft/net/minecraft/src/RenderEngine.java.patch +++ b/fml/patches/minecraft/net/minecraft/src/RenderEngine.java.patch @@ -25,7 +25,7 @@ { @@ -30,6 +37,7 @@ public boolean field_78361_b = false; - private TexturePackList field_78366_k; + public TexturePackList field_78366_k; private BufferedImage field_78364_l = new BufferedImage(64, 64, 2); + public static Logger log = FMLLog.getLogger();