Handle the minecraft object registry better- now the client compiles too

Also a quickie astyle run over the code..
This commit is contained in:
Christian Weeks 2012-04-05 12:36:54 -04:00
parent ae927a3cf8
commit 8b1f984c53
6 changed files with 220 additions and 84 deletions

View file

@ -31,21 +31,13 @@ public class FMLCommonHandler
private Map<String, ModContainer> modChannels = new HashMap<String, ModContainer>();
private Map<Object, Set<String>> activeChannels = new HashMap<Object, Set<String>>();
private IFMLSidedHandler sidedDelegate;
private FMLCommonHandler() {
try {
Class.forName("net.minecraft.client.Minecraft");
sidedDelegate=(IFMLSidedHandler) Class.forName("cpw.mods.fml.client.FMLClientHandler").newInstance();
} catch (Exception ex) {
try {
sidedDelegate=(IFMLSidedHandler) Class.forName("cpw.mods.fml.server.FMLServerHandler").newInstance();
} catch (Exception ex2) {
Loader.log.severe("A severe installation issue has occured with FML, we cannot continue");
throw new LoaderException(ex2);
}
}
public void registerSidedDelegate(IFMLSidedHandler handler)
{
sidedDelegate = handler;
}
public void gameTickStart()
{
for (ModContainer mod : Loader.getModList())
@ -167,12 +159,14 @@ public class FMLCommonHandler
{
return activeChannels.get(player).contains(channel);
}
public Logger getFMLLogger() {
public Logger getFMLLogger()
{
return Loader.log;
}
public Logger getMinecraftLogger() {
public Logger getMinecraftLogger()
{
return sidedDelegate.getMinecraftLogger();
}

View file

@ -13,131 +13,90 @@
*/
package net.minecraft.src;
import java.util.Collections;
import java.util.List;
public class CommonRegistry
{
private static IMinecraftRegistry instance;
public static void registerRegistry(IMinecraftRegistry registry)
{
if (instance != null)
{
throw new RuntimeException("Illegal attempt to replace FML registry");
}
instance = registry;
}
public static void addRecipe(ItemStack output, Object... params)
{
CraftingManager.func_20151_a().func_20153_a(output, params);
instance.addRecipe(output, params);
}
public static void addShapelessRecipe(ItemStack output, Object... params)
{
CraftingManager.func_20151_a().func_21146_b(output, params);
instance.addShapelessRecipe(output, params);
}
public static void addSmelting(int input, ItemStack output)
{
FurnaceRecipes.func_21162_a().func_21160_a(input, output);
instance.addSmelting(input, output);
}
public static void registerBlock(Block block)
{
registerBlock(block, ItemBlock.class);
instance.registerBlock(block);
}
public static void registerBlock(Block block, Class <? extends ItemBlock > itemclass)
{
try
{
assert block != null : "registerBlock: block cannot be null";
assert itemclass != null : "registerBlock: itemclass cannot be null";
int blockItemId = block.field_573_bc - 256;
itemclass.getConstructor(int.class).newInstance(blockItemId);
}
catch (Exception e)
{
//HMMM
}
instance.registerBlock(block, itemclass);
}
public static void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id)
{
EntityList.addNewEntityListMapping(entityClass, entityName, id);
instance.registerEntityID(entityClass, entityName, id);
}
public static void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id, int backgroundEggColour, int foregroundEggColour)
{
EntityList.addNewEntityListMapping(entityClass, entityName, id, backgroundEggColour, foregroundEggColour);
instance.registerEntityID(entityClass, entityName, id, backgroundEggColour, foregroundEggColour);
}
public static void registerTileEntity(Class <? extends TileEntity > tileEntityClass, String id)
{
TileEntity.addNewTileEntityMapping(tileEntityClass, id);
instance.registerTileEntity(tileEntityClass, id);
}
public static void addBiome(BiomeGenBase biome)
{
//NOOP because the implementation idea is broken. Creating a BiomeGenBase adds the biome already.
instance.addBiome(biome);
}
public static void addSpawn(Class <? extends EntityLiving > entityClass, int weightedProb, int min, int max, EnumCreatureType typeOfCreature, BiomeGenBase... biomes)
{
for (BiomeGenBase biome : biomes)
{
@SuppressWarnings("unchecked")
List<SpawnListEntry> spawns = biome.func_25055_a(typeOfCreature);
for (SpawnListEntry entry : spawns)
{
//Adjusting an existing spawn entry
if (entry.field_25145_a == entityClass)
{
entry.field_35483_d = weightedProb;
entry.field_35484_b = min;
entry.field_35485_c = max;
break;
}
}
spawns.add(new SpawnListEntry(entityClass, weightedProb, min, max));
}
instance.addSpawn(entityClass, weightedProb, min, max, typeOfCreature, biomes);
}
@SuppressWarnings("unchecked")
public static void addSpawn(String entityName, int weightedProb, int min, int max, EnumCreatureType spawnList, BiomeGenBase... biomes)
{
Class <? extends Entity > entityClazz = EntityList.getEntityToClassMapping().get(entityName);
if (EntityLiving.class.isAssignableFrom(entityClazz))
{
addSpawn((Class <? extends EntityLiving >) entityClazz, weightedProb, min, max, spawnList, biomes);
}
instance.addSpawn(entityName, weightedProb, min, max, spawnList, biomes);
}
public static void removeBiome(BiomeGenBase biome)
{
// NOOP because broken
instance.removeBiome(biome);
}
public static void removeSpawn(Class <? extends EntityLiving > entityClass, EnumCreatureType typeOfCreature, BiomeGenBase... biomes)
{
for (BiomeGenBase biome : biomes)
{
@SuppressWarnings("unchecked")
List<SpawnListEntry> spawns = biome.func_25055_a(typeOfCreature);
for (SpawnListEntry entry : Collections.unmodifiableList(spawns))
{
if (entry.field_25145_a == entityClass)
{
spawns.remove(entry);
}
}
}
instance.removeSpawn(entityClass, typeOfCreature, biomes);
}
@SuppressWarnings("unchecked")
public static void removeSpawn(String entityName, EnumCreatureType spawnList, BiomeGenBase... biomes)
{
Class <? extends Entity > entityClazz = EntityList.getEntityToClassMapping().get(entityName);
if (EntityLiving.class.isAssignableFrom(entityClazz))
{
removeSpawn((Class <? extends EntityLiving >) entityClazz, spawnList, biomes);
}
instance.removeSpawn(entityName, spawnList, biomes);
}
}

View file

@ -0,0 +1,34 @@
package net.minecraft.src;
public interface IMinecraftRegistry
{
public abstract void removeSpawn(String entityName, EnumCreatureType spawnList, BiomeGenBase... biomes);
public abstract void removeSpawn(Class <? extends EntityLiving > entityClass, EnumCreatureType typeOfCreature, BiomeGenBase... biomes);
public abstract void removeBiome(BiomeGenBase biome);
public abstract void addSpawn(String entityName, int weightedProb, int min, int max, EnumCreatureType spawnList, BiomeGenBase... biomes);
public abstract void addSpawn(Class <? extends EntityLiving > entityClass, int weightedProb, int min, int max, EnumCreatureType typeOfCreature, BiomeGenBase... biomes);
public abstract void addBiome(BiomeGenBase biome);
public abstract void registerTileEntity(Class <? extends TileEntity > tileEntityClass, String id);
public abstract void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id, int backgroundEggColour, int foregroundEggColour);
public abstract void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id);
public abstract void registerBlock(Block block, Class <? extends ItemBlock > itemclass);
public abstract void registerBlock(Block block);
public abstract void addSmelting(int input, ItemStack output);
public abstract void addShapelessRecipe(ItemStack output, Object... params);
public abstract void addRecipe(ItemStack output, Object... params);
}

View file

@ -20,6 +20,7 @@ import java.util.logging.Logger;
import net.minecraft.server.MinecraftServer;
import net.minecraft.src.BaseMod;
import net.minecraft.src.BiomeGenBase;
import net.minecraft.src.CommonRegistry;
import net.minecraft.src.EntityItem;
import net.minecraft.src.EntityPlayer;
import net.minecraft.src.IChunkProvider;
@ -29,6 +30,7 @@ import net.minecraft.src.NetworkManager;
import net.minecraft.src.Packet1Login;
import net.minecraft.src.Packet250CustomPayload;
import net.minecraft.src.Packet3Chat;
import net.minecraft.src.ServerRegistry;
import net.minecraft.src.World;
import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.IFMLSidedHandler;
@ -46,6 +48,8 @@ public class FMLServerHandler implements IFMLSidedHandler
public void onPreLoad(MinecraftServer minecraftServer)
{
server = minecraftServer;
FMLCommonHandler.instance().registerSidedDelegate(this);
CommonRegistry.registerRegistry(new ServerRegistry());
Loader.instance().loadMods();
}

View file

@ -0,0 +1,145 @@
package net.minecraft.src;
import java.util.Collections;
import java.util.List;
public class ServerRegistry implements IMinecraftRegistry
{
@Override
public void addRecipe(ItemStack output, Object... params)
{
CraftingManager.func_20151_a().func_20153_a(output, params);
}
@Override
public void addShapelessRecipe(ItemStack output, Object... params)
{
CraftingManager.func_20151_a().func_21146_b(output, params);
}
@Override
public void addSmelting(int input, ItemStack output)
{
FurnaceRecipes.func_21162_a().func_21160_a(input, output);
}
@Override
public void registerBlock(Block block)
{
registerBlock(block, ItemBlock.class);
}
@Override
public void registerBlock(Block block, Class <? extends ItemBlock > itemclass)
{
try
{
assert block != null : "registerBlock: block cannot be null";
assert itemclass != null : "registerBlock: itemclass cannot be null";
int blockItemId = block.field_573_bc - 256;
itemclass.getConstructor(int.class).newInstance(blockItemId);
}
catch (Exception e)
{
//HMMM
}
}
@Override
public void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id)
{
EntityList.addNewEntityListMapping(entityClass, entityName, id);
}
@Override
public void registerEntityID(Class <? extends Entity > entityClass, String entityName, int id, int backgroundEggColour, int foregroundEggColour)
{
EntityList.addNewEntityListMapping(entityClass, entityName, id, backgroundEggColour, foregroundEggColour);
}
@Override
public void registerTileEntity(Class <? extends TileEntity > tileEntityClass, String id)
{
TileEntity.addNewTileEntityMapping(tileEntityClass, id);
}
@Override
public void addBiome(BiomeGenBase biome)
{
//NOOP because the implementation idea is broken. Creating a BiomeGenBase adds the biome already.
}
@Override
public void addSpawn(Class <? extends EntityLiving > entityClass, int weightedProb, int min, int max, EnumCreatureType typeOfCreature, BiomeGenBase... biomes)
{
for (BiomeGenBase biome : biomes)
{
@SuppressWarnings("unchecked")
List<SpawnListEntry> spawns = biome.func_25055_a(typeOfCreature);
for (SpawnListEntry entry : spawns)
{
//Adjusting an existing spawn entry
if (entry.field_25145_a == entityClass)
{
entry.field_35483_d = weightedProb;
entry.field_35484_b = min;
entry.field_35485_c = max;
break;
}
}
spawns.add(new SpawnListEntry(entityClass, weightedProb, min, max));
}
}
@Override
@SuppressWarnings("unchecked")
public void addSpawn(String entityName, int weightedProb, int min, int max, EnumCreatureType spawnList, BiomeGenBase... biomes)
{
Class <? extends Entity > entityClazz = EntityList.getEntityToClassMapping().get(entityName);
if (EntityLiving.class.isAssignableFrom(entityClazz))
{
addSpawn((Class <? extends EntityLiving >) entityClazz, weightedProb, min, max, spawnList, biomes);
}
}
@Override
public void removeBiome(BiomeGenBase biome)
{
// NOOP because broken
}
@Override
public void removeSpawn(Class <? extends EntityLiving > entityClass, EnumCreatureType typeOfCreature, BiomeGenBase... biomes)
{
for (BiomeGenBase biome : biomes)
{
@SuppressWarnings("unchecked")
List<SpawnListEntry> spawns = biome.func_25055_a(typeOfCreature);
for (SpawnListEntry entry : Collections.unmodifiableList(spawns))
{
if (entry.field_25145_a == entityClass)
{
spawns.remove(entry);
}
}
}
}
@Override
@SuppressWarnings("unchecked")
public void removeSpawn(String entityName, EnumCreatureType spawnList, BiomeGenBase... biomes)
{
Class <? extends Entity > entityClazz = EntityList.getEntityToClassMapping().get(entityName);
if (EntityLiving.class.isAssignableFrom(entityClazz))
{
removeSpawn((Class <? extends EntityLiving >) entityClazz, spawnList, biomes);
}
}
}