Testing an idea on how to handle block registry

This is both to help me do modding better as well to test how i want to
handle block creation in my game.
This commit is contained in:
DarkGuardsman 2013-08-28 16:54:44 -04:00
parent 75eeae764b
commit a66aa8e053
6 changed files with 208 additions and 26 deletions

View file

@ -1,6 +1,8 @@
package dark.common.transmit;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
@ -16,9 +18,11 @@ import universalelectricity.core.block.IConductor;
import universalelectricity.core.vector.Vector3;
import universalelectricity.prefab.tile.TileEntityConductor;
import dark.core.DarkMain;
import dark.core.IExtraObjectInfo;
import dark.core.helpers.Pair;
import dark.prefab.BlockMachine;
public class BlockWire extends BlockMachine
public class BlockWire extends BlockMachine implements IExtraObjectInfo
{
public boolean isWireCollision = true;
@ -66,7 +70,6 @@ public class BlockWire extends BlockMachine
return new TileEntityWire();
}
@Override
public void onBlockAdded(World world, int x, int y, int z)
{
@ -80,7 +83,6 @@ public class BlockWire extends BlockMachine
}
}
@Override
public void onNeighborBlockChange(World world, int x, int y, int z, int blockID)
{
@ -92,7 +94,6 @@ public class BlockWire extends BlockMachine
}
}
@Override
public AxisAlignedBB getCollisionBoundingBoxFromPool(World world, int x, int y, int z)
{
@ -222,4 +223,37 @@ public class BlockWire extends BlockMachine
super.addCollisionBoxesToList(world, x, y, z, axisalignedbb, list, entity);
}
}
@Override
public void loadRecipes()
{
// TODO Auto-generated method stub
}
@Override
public void getTileEntities(int blockID, Set<Pair<String, TileEntity>> list)
{
list.add(new Pair<String, TileEntity>("DMWireTile", new TileEntityWire()));
}
@Override
public boolean hasExtraConfigs()
{
return false;
}
@Override
public void loadExtraConfigs(Configuration config)
{
//TODO add config options for max amps, and resistance
}
@Override
public void loadOreNames()
{
// TODO Auto-generated method stub
}
}

View file

@ -1,26 +1,100 @@
package dark.core;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map.Entry;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.item.ItemBlock;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.Configuration;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.registry.GameRegistry;
import dark.core.helpers.Pair;
/** Handler to make registering all parts of a block a bit easier
*
* @author DarkGuardsman */
public class BlockRegistry
{
private static Configuration masterBlockConfig = new Configuration(new File(Loader.instance().getConfigDir(), "Dark/EnabledBlocks.cfg"));
private static HashMap<Block, BlockData> blockMap = new HashMap<Block, BlockData>();
/** Adds a block to the mapping to be registered next call */
public static void addBlockToRegister(BlockData data)
{
if (data != null && data.block != null && !blockMap.containsKey(data.block))
synchronized (blockMap)
{
blockMap.put(data.block, data);
if (data != null && data.block != null && !blockMap.containsKey(data.block))
{
blockMap.put(data.block, data);
}
}
}
public static void addBlocks(List<BlockData> data)
{
for (BlockData entry : data)
{
BlockRegistry.addBlockToRegister(entry);
}
}
/** Runs threw the list of entries and then registers all parts */
public static void registerAllBlocks()
{
masterBlockConfig.load();
synchronized (blockMap)
{
HashMap<String, TileEntity> tileList = new HashMap<String, TileEntity>();
for (Entry<Block, BlockData> entry : blockMap.entrySet())
{
BlockData blockData = entry.getValue();
Block block = blockData.block;
if (!blockData.allowDisable || blockData.allowDisable && masterBlockConfig.get("EnabledList", "Enable_" + blockData.block.getUnlocalizedName(), true).getBoolean(true))
{
if (blockData.itemBlock != null)
{
GameRegistry.registerBlock(blockData.block, blockData.itemBlock, blockData.modBlockID);
}
else
{
GameRegistry.registerBlock(blockData.block, blockData.modBlockID);
}
if (blockData.block instanceof IExtraObjectInfo)
{
if (((IExtraObjectInfo) block).hasExtraConfigs())
{
Configuration extraBlockConfig = new Configuration(new File(Loader.instance().getConfigDir(), "Dark/blocks/" + block.getUnlocalizedName() + ".cfg"));
extraBlockConfig.load();
((IExtraObjectInfo) block).loadExtraConfigs(extraBlockConfig);
extraBlockConfig.save();
}
((IExtraObjectInfo) block).loadOreNames();
Set<Pair<String, TileEntity>> tileListNew = new HashSet<Pair<String, TileEntity>>();
((IExtraObjectInfo) block).getTileEntities(block.blockID, tileListNew);
for (Pair<String, TileEntity> par : tileListNew)
{
if (!tileList.containsKey(par.getKey()) && !tileList.containsValue(par.getValue()))
{
tileList.put(par.getKey(), par.getValue());
}
else if(par.getValue() != null)
{
System.out.println("BlockRegistry tried to list a tile or tileName that was already listed");
System.out.println("Tile>" + par.getValue().toString() + " | Name>" + par.getKey());
}
}
}
}
}
}
masterBlockConfig.save();
}
/** Used to store info on the block that will later be used to register all parts of the block */
public static class BlockData
{
@ -28,6 +102,7 @@ public class BlockRegistry
public Class<? extends ItemBlock> itemBlock;
public String modBlockID;
public HashMap<String, TileEntity> tiles = new HashMap<String, TileEntity>();
public boolean allowDisable = true;
public BlockData(Block block, String name)
{

View file

@ -1,7 +1,9 @@
package dark.core;
import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.Configuration;
@ -29,6 +31,7 @@ import dark.common.debug.BlockDebug;
import dark.common.debug.BlockDebug.debugBlocks;
import dark.common.transmit.BlockWire;
import dark.common.transmit.TileEntityWire;
import dark.core.BlockRegistry.BlockData;
import dark.core.blocks.BlockOre;
import dark.core.helpers.FluidRestrictionHandler;
import dark.core.helpers.SaveManager;
@ -112,9 +115,6 @@ public class DarkMain extends ModPrefab
if (CoreRecipeLoader.blockOre != null)
{
GameRegistry.registerBlock(CoreRecipeLoader.blockOre, ItemBlockOre.class, "DMOre");
BlockOre.regiserOreNames();
for (int i = 0; i < EnumMeterials.values().length; i++)
{
if (EnumMeterials.values()[i].doWorldGen)
@ -174,8 +174,9 @@ public class DarkMain extends ModPrefab
}
@Override
public void loadConfig()
public List<BlockData> getBlocks()
{
List<BlockData> dataList = new ArrayList<BlockData>();
if (recipeLoader == null)
{
recipeLoader = new CoreRecipeLoader();
@ -188,6 +189,7 @@ public class DarkMain extends ModPrefab
if (CONFIGURATION.get("general", "LoadOre", true, "Disabling this also disabled the items that go with the ores").getBoolean(true))
{
CoreRecipeLoader.blockOre = new BlockOre(getNextID(), CONFIGURATION);
dataList.add(new BlockData(CoreRecipeLoader.blockOre, ItemBlockOre.class, "DMOre"));
}
if (CONFIGURATION.get("general", "EnableWires", true).getBoolean(true))
{
@ -218,7 +220,7 @@ public class DarkMain extends ModPrefab
CONFIGURATION.save();
/* CONFIG END */
return dataList;
}
@Override

View file

@ -0,0 +1,34 @@
package dark.core;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;
import dark.core.helpers.Pair;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.common.Configuration;
/** 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 IExtraObjectInfo
{
/** List of all tileEntities this block needs */
public void getTileEntities(int blockID, Set<Pair<String, TileEntity>> list);
/** True will cause a config file to be generated for this block */
public boolean hasExtraConfigs();
/** Loads the config file for this block */
public void loadExtraConfigs(Configuration config);
/** Optional way to handle recipes based out of the block/item class */
public void loadRecipes();
/** Loads the names used to reference this item in a recipe */
public void loadOreNames();
}

View file

@ -1,19 +1,24 @@
package dark.core.blocks;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.Icon;
import net.minecraftforge.common.Configuration;
import net.minecraftforge.oredict.OreDictionary;
import dark.core.DarkMain;
import dark.core.IExtraObjectInfo;
import dark.core.helpers.Pair;
import dark.core.items.EnumMeterials;
public class BlockOre extends Block
public class BlockOre extends Block implements IExtraObjectInfo
{
Icon[] icons = new Icon[EnumMeterials.values().length];
@ -24,17 +29,6 @@ public class BlockOre extends Block
this.setUnlocalizedName(DarkMain.getInstance().PREFIX + "Ore");
}
public static void regiserOreNames()
{
for (int i = 0; i < EnumMeterials.values().length; i++)
{
if (EnumMeterials.values()[i].doWorldGen)
{
OreDictionary.registerOre(EnumMeterials.values()[i].name + "Ore", new ItemStack(DarkMain.recipeLoader.blockOre.blockID, 1, i));
}
}
}
@Override
public void getSubBlocks(int par1, CreativeTabs par2CreativeTabs, List par3List)
{
@ -68,4 +62,40 @@ public class BlockOre extends Block
}
return Block.stone.getIcon(side, metadata);
}
@Override
public void loadRecipes()
{
}
@Override
public void getTileEntities(int blockID, Set<Pair<String, TileEntity>> list)
{
}
@Override
public boolean hasExtraConfigs()
{
return false;
}
@Override
public void loadExtraConfigs(Configuration config)
{
// TODO Auto-generated method stub
}
@Override
public void loadOreNames()
{
for (int i = 0; i < EnumMeterials.values().length; i++)
{
if (EnumMeterials.values()[i].doWorldGen)
{
OreDictionary.registerOre(EnumMeterials.values()[i].name + "Ore", new ItemStack(DarkMain.recipeLoader.blockOre.blockID, 1, i));
}
}
}
}

View file

@ -1,5 +1,7 @@
package dark.prefab;
import java.util.List;
import net.minecraft.block.Block;
import org.modstats.Modstats;
@ -10,6 +12,8 @@ import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.relauncher.Side;
import dark.core.BlockRegistry;
import dark.core.BlockRegistry.BlockData;
public abstract class ModPrefab
{
@ -58,13 +62,13 @@ public abstract class ModPrefab
{
this.loadModMeta();
Modstats.instance().getReporter().registerMod(this);
this.loadConfig();
BlockRegistry.addBlocks(this.getBlocks());
}
@EventHandler
public void init(FMLInitializationEvent event)
{
BlockRegistry.registerAllBlocks();
}
@EventHandler
@ -79,7 +83,10 @@ public abstract class ModPrefab
System.out.println(" " + data);
}
public abstract void loadConfig();
/** Grabs a list of all the mods block Data used to register the block, tileEntities, and extra
* configs */
public abstract List<BlockData> getBlocks();
/** Loads the settings that tell what this mod is named, about, and other info to the user */
public abstract void loadModMeta();
}