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
This commit is contained in:
Christian committed 2012-08-13 15:26:29 -04:00
1 parent c393299a91
commit deab31fb03
6 files changed
+228 -22

No files matched your search

@@ -193,4 +193,9 @@ public class LoadController
{
return ImmutableBiMap.copyOf(modObjectList);
}
public boolean isInState(LoaderState state)
{
return this.state == state;
}
}
@@ -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);
}
}
@@ -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<? extends IConnectionHandler> 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<? extends IPacketHandler> packetHandler();
}
}
@@ -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<? extends IPacketHandler> 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();
@@ -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<ICraftingHandler> craftingHandlers = Lists.newArrayList();
private static List<IDispenseHandler> 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<? extends ItemBlock> 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)
@@ -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();