From a66aa8e05320ab5894e97aa5b0700c29ff0e1099 Mon Sep 17 00:00:00 2001 From: DarkGuardsman Date: Wed, 28 Aug 2013 16:54:44 -0400 Subject: [PATCH] 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. --- src/dark/common/transmit/BlockWire.java | 42 +++++++++++-- src/dark/core/BlockRegistry.java | 79 ++++++++++++++++++++++++- src/dark/core/DarkMain.java | 12 ++-- src/dark/core/IExtraObjectInfo.java | 34 +++++++++++ src/dark/core/blocks/BlockOre.java | 54 +++++++++++++---- src/dark/prefab/ModPrefab.java | 13 +++- 6 files changed, 208 insertions(+), 26 deletions(-) create mode 100644 src/dark/core/IExtraObjectInfo.java diff --git a/src/dark/common/transmit/BlockWire.java b/src/dark/common/transmit/BlockWire.java index 2a7e13225..b8bcb4170 100644 --- a/src/dark/common/transmit/BlockWire.java +++ b/src/dark/common/transmit/BlockWire.java @@ -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> list) + { + list.add(new Pair("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 + + } } diff --git a/src/dark/core/BlockRegistry.java b/src/dark/core/BlockRegistry.java index bd880e5f6..067f7fce1 100644 --- a/src/dark/core/BlockRegistry.java +++ b/src/dark/core/BlockRegistry.java @@ -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 blockMap = new HashMap(); + /** 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 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 tileList = new HashMap(); + for (Entry 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> tileListNew = new HashSet>(); + ((IExtraObjectInfo) block).getTileEntities(block.blockID, tileListNew); + for (Pair 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 itemBlock; public String modBlockID; public HashMap tiles = new HashMap(); + public boolean allowDisable = true; public BlockData(Block block, String name) { diff --git a/src/dark/core/DarkMain.java b/src/dark/core/DarkMain.java index fc895d740..8c4a73815 100644 --- a/src/dark/core/DarkMain.java +++ b/src/dark/core/DarkMain.java @@ -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 getBlocks() { + List dataList = new ArrayList(); 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 diff --git a/src/dark/core/IExtraObjectInfo.java b/src/dark/core/IExtraObjectInfo.java new file mode 100644 index 000000000..66db87150 --- /dev/null +++ b/src/dark/core/IExtraObjectInfo.java @@ -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> 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(); + +} diff --git a/src/dark/core/blocks/BlockOre.java b/src/dark/core/blocks/BlockOre.java index 087b36ce5..2ca5656a1 100644 --- a/src/dark/core/blocks/BlockOre.java +++ b/src/dark/core/blocks/BlockOre.java @@ -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> 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)); + } + } + + } } diff --git a/src/dark/prefab/ModPrefab.java b/src/dark/prefab/ModPrefab.java index cb4e4d65e..daa59e3fe 100644 --- a/src/dark/prefab/ModPrefab.java +++ b/src/dark/prefab/ModPrefab.java @@ -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 getBlocks(); + /** Loads the settings that tell what this mod is named, about, and other info to the user */ public abstract void loadModMeta(); }