Toyed with a new way to register TileEntitySpecialRenderers

The idea is to let the object being registered control all aspects of
how its registered. This started with the blocks adding config files,
disabling themselves, and loading their own tile entities. Now i'm
working on having the tile entities register their own renderers, and
have configs as well. Give that the registration is actually handled by
the ModObjectRegistry and the object just provided the data instead of
the mod class.
This commit is contained in:
DarkGuardsman 2013-09-24 11:22:12 -04:00
parent fea5d199b9
commit 11a2ac41b1
7 changed files with 155 additions and 85 deletions

View file

@ -3,9 +3,13 @@ package dark.core.common.machines;
import java.util.EnumSet;
import micdoodle8.mods.galacticraft.API.ISolarLevel;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.electricity.ElectricityPack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.client.renders.RenderBlockSolarPanel;
import dark.core.prefab.machine.TileEntityEnergyMachine;
public class TileEntitySolarPanel extends TileEntityEnergyMachine
@ -84,4 +88,11 @@ public class TileEntitySolarPanel extends TileEntityEnergyMachine
{
return 0;
}
@Override
@SideOnly(Side.CLIENT)
public TileEntitySpecialRenderer getClientTileEntityRenderer()
{
return new RenderBlockSolarPanel();
}
}

View file

@ -1,8 +1,15 @@
package dark.core.common.transmit;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraftforge.common.Configuration;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.client.renders.RenderBlockSolarPanel;
import dark.core.client.renders.RenderBlockWire;
import dark.core.prefab.IExtraInfo.IExtraTileEntityInfo;
import universalelectricity.compatibility.TileEntityUniversalConductor;
public class TileEntityWire extends TileEntityUniversalConductor
public class TileEntityWire extends TileEntityUniversalConductor implements IExtraTileEntityInfo
{
int updateTick = 0;
@ -35,4 +42,24 @@ public class TileEntityWire extends TileEntityUniversalConductor
return true;
}
@Override
@SideOnly(Side.CLIENT)
public TileEntitySpecialRenderer getClientTileEntityRenderer()
{
return new RenderBlockWire();
}
@Override
public boolean hasExtraConfigs()
{
return false;
}
@Override
public void loadExtraConfigs(Configuration config)
{
}
}

View file

@ -2,15 +2,19 @@ package dark.core.prefab;
import java.util.Set;
import com.builtbroken.common.Pair;
import net.minecraft.block.ITileEntityProvider;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.Configuration;
import com.builtbroken.common.Pair;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
/** Used to handle info about the block that would normally be handled by the mod main class. Use the
* BlockRegistry in order for these methods to be called on load of the mod.
*
*
* @author DarkGuardsman */
public interface IExtraInfo
{
@ -36,6 +40,8 @@ public interface IExtraInfo
public static interface IExtraTileEntityInfo extends IExtraInfo
{
@SideOnly(Side.CLIENT)
public TileEntitySpecialRenderer getClientTileEntityRenderer();
}
}

View file

@ -4,6 +4,7 @@ import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.IOException;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.ISidedInventory;
import net.minecraft.nbt.NBTTagCompound;
@ -11,6 +12,7 @@ import net.minecraft.network.INetworkManager;
import net.minecraft.network.packet.Packet;
import net.minecraft.network.packet.Packet250CustomPayload;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.common.ForgeDirection;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.network.IPacketReceiver;
@ -23,8 +25,9 @@ import dark.core.common.DarkMain;
import dark.core.interfaces.IExternalInv;
import dark.core.interfaces.IInvBox;
import dark.core.network.PacketHandler;
import dark.core.prefab.IExtraInfo.IExtraTileEntityInfo;
public class TileEntityMachine extends TileEntityInv implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IRotatable
public abstract class TileEntityMachine extends TileEntityInv implements ISidedInventory, IExternalInv, IDisableable, IPacketReceiver, IRotatable, IExtraTileEntityInfo
{
protected int disabledTicks = 0, playersUsingMachine = 0;
protected boolean functioning = false, prevFunctioning = false, hasGUI = false, rotateByMetaGroup = false, canBeDisabled = false;
@ -206,7 +209,7 @@ public class TileEntityMachine extends TileEntityInv implements ISidedInventory,
}
/** Handles reduced data from the main packet method
*
*
* @param id - packet ID
* @param dis - data
* @param player - player
@ -289,4 +292,24 @@ public class TileEntityMachine extends TileEntityInv implements ISidedInventory,
{
return !this.canBeDisabled && this.disabledTicks > 0;
}
@Override
public boolean hasExtraConfigs()
{
// TODO Auto-generated method stub
return false;
}
@Override
public void loadExtraConfigs(Configuration config)
{
// TODO Auto-generated method stub
}
@Override
public TileEntitySpecialRenderer getClientTileEntityRenderer()
{
return null;
}
}

View file

@ -1,16 +1,10 @@
package dark.core.registration;
import java.io.File;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Set;
import com.builtbroken.common.Pair;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
@ -19,8 +13,11 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import com.builtbroken.common.Pair;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.SidedProxy;
import dark.core.common.DarkMain;
import dark.core.prefab.IExtraInfo;
import dark.core.prefab.IExtraInfo.IExtraBlockInfo;
@ -28,10 +25,14 @@ import dark.core.prefab.ModPrefab;
import dark.core.prefab.machine.BlockMachine;
/** Handler to make registering all parts of a mod's objects that are loaded into the game by forge
*
*
* @author DarkGuardsman */
public class ModObjectRegistry
{
@SidedProxy(clientSide = "dark.core.registration.RegistryProxyClient", serverSide = "dark.core.registration.RegistryProxy")
public static RegistryProxy proxy;
public static Configuration masterBlockConfig = new Configuration(new File(Loader.instance().getConfigDir(), "Dark/EnabledBlocks.cfg"));
public static Block createNewBlock(String name, String modID, Class<? extends Block> blockClass)
@ -64,7 +65,7 @@ public class ModObjectRegistry
}
if (block != null)
{
ModObjectRegistry.registerBlock(block, itemClass, name, modID);
proxy.registerBlock(block, itemClass, name, modID);
ModObjectRegistry.finishCreation(block, null);
}
}
@ -111,7 +112,7 @@ public class ModObjectRegistry
}
}
ModObjectRegistry.registerBlock(block, buildData.itemBlock, buildData.blockName, modID);
proxy.registerBlock(block, buildData.itemBlock, buildData.blockName, modID);
ModObjectRegistry.finishCreation(block, buildData);
}
}
@ -128,69 +129,12 @@ public class ModObjectRegistry
{
if (block != null)
{
BlockTileEntityInfo blockTileEntityInfo = block.getClass().getAnnotation(BlockTileEntityInfo.class);
if (blockTileEntityInfo != null)
{
System.out.println("\n\n\n\n[ModObjectRegistry] Reading tile entities for " + block.getUnlocalizedName());
Class<? extends TileEntity>[] tileEntities = blockTileEntityInfo.tileEntities();
String[] tileEntitiesNames = blockTileEntityInfo.tileEntitiesNames();
if (tileEntities != null && tileEntities.length > 0 && tileEntitiesNames != null && tileEntitiesNames.length > 0)
{
for (int i = 0; i < tileEntities.length && i < tileEntitiesNames.length; i++)
{
GameRegistry.registerTileEntityWithAlternatives(tileEntities[i], tileEntitiesNames[i], "DM" + tileEntitiesNames[i]);
}
}
}
// Read threw the block class looking for annotions on fields
for (Method method : block.getClass().getMethods())
{
System.out.println("[ModObjectRegistry] Reading class methods " + method.toGenericString());
for (Annotation annotian : method.getDeclaredAnnotations())
{
System.out.println("[ModObjectRegistry] Reading annotion " + annotian.toString());
if (annotian instanceof BlockConfigFile)
{
System.out.println("[ModObjectRegistry] Loading config file for " + block.getUnlocalizedName());
Type[] types = method.getParameterTypes();
if (types.length == 1 && types[0] instanceof Configuration)
{
Configuration extraBlockConfig = new Configuration(new File(Loader.instance().getConfigDir(), "Dark/blocks/" + block.getUnlocalizedName() + ".cfg"));
extraBlockConfig.load();
try
{
method.setAccessible(true);
method.invoke(extraBlockConfig);
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
catch (IllegalArgumentException e)
{
e.printStackTrace();
}
catch (InvocationTargetException e)
{
e.printStackTrace();
}
extraBlockConfig.save();
}
break;
}
}
}
// Load data from BlockBuildData
if (data.tiles != null)
{
for (Pair<String, Class<? extends TileEntity>> par : data.tiles)
{
GameRegistry.registerTileEntityWithAlternatives(par.right(), par.left(), "DM" + par.left());
proxy.regiserTileEntity(par.left(), par.right());
}
}
if (data.creativeTab != null)
@ -215,7 +159,7 @@ public class ModObjectRegistry
((IExtraBlockInfo) block).getTileEntities(block.blockID, tileListNew);
for (Pair<String, Class<? extends TileEntity>> par : tileListNew)
{
GameRegistry.registerTileEntityWithAlternatives(par.right(), par.left(), "DM" + par.left());
proxy.regiserTileEntity(par.left(), par.right());
}
}
}
@ -238,7 +182,7 @@ public class ModObjectRegistry
if (fluidActual.getBlockID() == -1 && masterBlockConfig.get("Enabled_List", "Enabled_" + fluid.getName() + "Block", true).getBoolean(true))
{
fluidBlock = new BlockFluid(modDomainPrefix, fluidActual, config).setUnlocalizedName("tile.Fluid." + fluid.getName());
GameRegistry.registerBlock(fluidBlock, "DMBlockFluid" + fluid.getName());
proxy.registerBlock(fluidBlock, null, "DMBlockFluid" + fluid.getName(), modDomainPrefix);
}
else
{
@ -249,14 +193,6 @@ public class ModObjectRegistry
return fluidBlock;
}
public static void registerBlock(Block block, Class<? extends ItemBlock> itemClass, String name, String modID)
{
if (block != null && name != null)
{
GameRegistry.registerBlock(block, itemClass == null ? ItemBlock.class : itemClass, name, modID);
}
}
public static class BlockBuildData
{
public Class<? extends BlockMachine> blockClass;
@ -308,7 +244,7 @@ public class ModObjectRegistry
}
/** Adds a tileEntity to be registered when this block is registered
*
*
* @param name - mod name for the tileEntity, should be unique
* @param class1 - new instance of the TileEntity to register */
public BlockBuildData addTileEntity(String name, Class<? extends TileEntity> class1)

View file

@ -0,0 +1,22 @@
package dark.core.registration;
import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.tileentity.TileEntity;
import cpw.mods.fml.common.registry.GameRegistry;
public class RegistryProxy
{
public void registerBlock(Block block, Class<? extends ItemBlock> itemClass, String name, String modID)
{
if (block != null && name != null)
{
GameRegistry.registerBlock(block, itemClass == null ? ItemBlock.class : itemClass, name, modID);
}
}
public void regiserTileEntity(String name, Class<? extends TileEntity> clazz)
{
GameRegistry.registerTileEntityWithAlternatives(clazz, name, "DM" + name);
}
}

View file

@ -0,0 +1,45 @@
package dark.core.registration;
import cpw.mods.fml.client.registry.ClientRegistry;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import dark.core.prefab.IExtraInfo.IExtraTileEntityInfo;
import net.minecraft.block.Block;
import net.minecraft.client.renderer.tileentity.TileEntitySpecialRenderer;
import net.minecraft.item.ItemBlock;
import net.minecraft.tileentity.TileEntity;
@SideOnly(Side.CLIENT)
public class RegistryProxyClient extends RegistryProxy
{
@Override
public void registerBlock(Block block, Class<? extends ItemBlock> itemClass, String name, String modID)
{
super.registerBlock(block, itemClass, name, modID);
}
@Override
public void regiserTileEntity(String name, Class<? extends TileEntity> clazz)
{
super.regiserTileEntity(name, clazz);
try
{
TileEntity entity = clazz.newInstance();
if (entity instanceof IExtraTileEntityInfo)
{
TileEntitySpecialRenderer render = ((IExtraTileEntityInfo) entity).getClientTileEntityRenderer();
if (render != null)
{
ClientRegistry.bindTileEntitySpecialRenderer(clazz, render);
}
}
}
catch (InstantiationException e)
{
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
}