diff --git a/src/main/java/com/cricketcraft/chisel/Features.java b/src/main/java/com/cricketcraft/chisel/Features.java index 68d42acf..4a517a8b 100644 --- a/src/main/java/com/cricketcraft/chisel/Features.java +++ b/src/main/java/com/cricketcraft/chisel/Features.java @@ -43,8 +43,8 @@ import com.cricketcraft.chisel.block.BlockLavastone; import com.cricketcraft.chisel.block.BlockLeaf; import com.cricketcraft.chisel.block.BlockMarbleBookshelf; import com.cricketcraft.chisel.block.BlockMarbleCarpet; -import com.cricketcraft.chisel.block.BlockMarbleStairsMaker; -import com.cricketcraft.chisel.block.BlockMarbleStairsMakerCreator; +import com.cricketcraft.chisel.block.CarvableStairsMaker; +import com.cricketcraft.chisel.block.IStairsCreator; import com.cricketcraft.chisel.block.BlockMarbleTexturedOre; import com.cricketcraft.chisel.block.BlockPresent; import com.cricketcraft.chisel.block.BlockRoadLine; @@ -134,14 +134,11 @@ public enum Features { antiBlock.carverHelper.setChiselBlockName("Anti Block"); for (int i = 0; i < 16; i++) { - final String blockName = "antiBlock" + ItemDye.field_150921_b[i]; - String oreName = "antiBlock" + ItemDye.field_150921_b[i]; - antiBlock.carverHelper.addVariation("tile.antiBlock." + ItemDye.field_150921_b[i] + ".desc", i, "antiblock/" + ItemDye.field_150921_b[i] + "-antiBlock"); - Carving.chisel.registerOre(blockName, oreName); } antiBlock.carverHelper.register(antiBlock, "antiBlock"); + Carving.chisel.registerOre("antiBlock", "antiBlock"); } @Override @@ -317,8 +314,7 @@ public enum Features { carpet_block.carverHelper.addVariation("tile.carpet_block.15.desc", 15, "carpet/black"); carpet_block.carverHelper.forbidChiseling = true; carpet_block.carverHelper.register(carpet_block, "carpet_block"); - OreDictionary.registerOre("carpet", carpet_block); - Carving.chisel.registerOre("carpet_block", "carpet"); + OreDictionary.registerOre("blockCarpet", carpet_block); } @Override @@ -361,12 +357,13 @@ public enum Features { for (int i = 0; i < 16; i++) { String group = "carpet." + i; - Carving.needle.addVariation(group, Blocks.carpet, i, 0); - Carving.needle.addVariation(group, carpet, i, 2); - Carving.needle.addVariation(group, carpet_block, i, 1); + // TODO needle stuff +// Carving.needle.addVariation(group, Blocks.carpet, i, 0); +// Carving.needle.addVariation(group, carpet, i, 2); +// Carving.needle.addVariation(group, carpet_block, i, 1); } - Carving.chisel.registerOre("carpet", "carpet"); + OreDictionary.registerOre("carpet", carpet); } @Override @@ -1045,7 +1042,6 @@ public enum Features { ice_pillar.carverHelper.addVariation("tile.icePillar.5.desc", 5, "icepillar/carved"); ice_pillar.carverHelper.addVariation("tile.icePillar.6.desc", 6, "icepillar/ornamental"); ice_pillar.carverHelper.register(ice_pillar, "ice_pillar"); - Carving.chisel.setGroupClass("ice_pillar", "ice"); } @Override @@ -1058,7 +1054,7 @@ public enum Features { @Override void addBlocks() { - BlockMarbleStairsMaker makerIceStairs = new BlockMarbleStairsMaker(Blocks.ice); + CarvableStairsMaker makerIceStairs = new CarvableStairsMaker(Blocks.ice); makerIceStairs.carverHelper.setChiselBlockName("Ice Stairs"); makerIceStairs.carverHelper.addVariation("tile.iceStairs.0.desc", 0, Blocks.ice); makerIceStairs.carverHelper.addVariation("tile.iceStairs.1.desc", 1, "ice/a1-ice-light"); @@ -1076,14 +1072,14 @@ public enum Features { makerIceStairs.carverHelper.addVariation("tile.iceStairs.13.desc", 13, "ice/zelda"); makerIceStairs.carverHelper.addVariation("tile.iceStairs.14.desc", 14, "ice/bismuth"); makerIceStairs.carverHelper.addVariation("tile.iceStairs.15.desc", 15, "ice/poison"); - makerIceStairs.create(new BlockMarbleStairsMakerCreator() { + makerIceStairs.create(new IStairsCreator() { @Override public BlockCarvableStairs create(Block block, int meta, CarvableHelper helper) { return new BlockCarvableIceStairs(block, meta, helper); } }, "ice_stairs", ChiselBlocks.iceStairs); - Carving.chisel.registerOre("iceStairs", "iceStairs"); + Carving.chisel.registerOre("ice_stairs", "iceStairs"); } }, @@ -1311,7 +1307,7 @@ public enum Features { registerSlabTop(limestone_slab, limestone_slab.top); Carving.chisel.registerOre("limestone_slab", "limestone_slab"); - BlockMarbleStairsMaker makerLimestoneStairs = new BlockMarbleStairsMaker(limestone); + CarvableStairsMaker makerLimestoneStairs = new CarvableStairsMaker(limestone); makerLimestoneStairs.carverHelper.setChiselBlockName("Limestone Stairs"); makerLimestoneStairs.carverHelper.addVariation("tile.limestoneStairs.0.desc", 0, "limestone"); makerLimestoneStairs.carverHelper.addVariation("tile.limestoneStairs.1.desc", 1, "limestone/terrain-cobbsmalltilelight"); @@ -1389,7 +1385,7 @@ public enum Features { registerSlabTop(marble_slab, marble_slab.top); Carving.chisel.registerOre("marble_slab", "marble_slab"); - BlockMarbleStairsMaker makerMarbleStairs = new BlockMarbleStairsMaker(marble); + CarvableStairsMaker makerMarbleStairs = new CarvableStairsMaker(marble); makerMarbleStairs.carverHelper.setChiselBlockName("Marble Stairs"); makerMarbleStairs.carverHelper.addVariation("tile.marbleStairs.0.desc", 0, "marble"); makerMarbleStairs.carverHelper.addVariation("tile.marbleStairs.6.desc", 1, "marbleslab/a1-stoneornamental-marblebrick"); @@ -1465,7 +1461,6 @@ public enum Features { marble_pillar.carverHelper.addVariation("tile.marblePillar.15.desc", 15, "marblepillar/ornamental"); } marble_pillar.carverHelper.register(marble_pillar, "marble_pillar"); - Carving.chisel.setGroupClass("marble_pillar", "marble"); BlockCarvableSlab marble_pillar_slab = (BlockCarvableSlab) new BlockCarvableSlab(marble_pillar).setCreativeTab(ChiselTabs.tabStoneChiselBlocks).setHardness(2.0F).setResistance(10F) .setStepSound(Block.soundTypeStone); @@ -1639,7 +1634,6 @@ public enum Features { packedice_pillar.carverHelper.addVariation("tile.packedicePillar.5.desc", 5, "icepillar/carved"); packedice_pillar.carverHelper.addVariation("tile.packedicePillar.6.desc", 6, "icepillar/ornamental"); packedice_pillar.carverHelper.register(packedice_pillar, "packedice_pillar"); - Carving.chisel.setGroupClass("packedice_pillar", "packedice"); } }, @@ -1647,7 +1641,7 @@ public enum Features { @Override void addBlocks() { - BlockMarbleStairsMaker makerPackedIceStairs = new BlockMarbleStairsMaker(Blocks.packed_ice); + CarvableStairsMaker makerPackedIceStairs = new CarvableStairsMaker(Blocks.packed_ice); makerPackedIceStairs.carverHelper.setChiselBlockName("Packed Ice Stairs"); makerPackedIceStairs.carverHelper.addVariation("tile.packediceStairs.0.desc", 0, Blocks.packed_ice); makerPackedIceStairs.carverHelper.addVariation("tile.packediceStairs.1.desc", 1, "ice/a1-ice-light"); @@ -1665,14 +1659,14 @@ public enum Features { makerPackedIceStairs.carverHelper.addVariation("tile.packediceStairs.13.desc", 13, "ice/zelda"); makerPackedIceStairs.carverHelper.addVariation("tile.packediceStairs.14.desc", 14, "ice/bismuth"); makerPackedIceStairs.carverHelper.addVariation("tile.packediceStairs.15.desc", 15, "ice/poison"); - makerPackedIceStairs.create(new BlockMarbleStairsMakerCreator() { + makerPackedIceStairs.create(new IStairsCreator() { @Override public BlockCarvableStairs create(Block block, int meta, CarvableHelper helper) { return new BlockCarvablePackedIceStairs(block, meta, helper); } }, "packedice_stairs", ChiselBlocks.packediceStairs); - Carving.chisel.registerOre("packedIceStairs", "packedIceStairs"); + Carving.chisel.registerOre("packedice_stairs", "packedIceStairs"); } }, @@ -1711,7 +1705,7 @@ public enum Features { GameRegistry.registerBlock(present[x], "chest" + x); Carving.chisel.addVariation("present", present[x], 0, (x + 1)); } - Carving.chisel.registerOre("chest", "chest"); + Carving.chisel.registerOre("present", "chest"); } @Override @@ -1888,8 +1882,8 @@ public enum Features { road_line.carverHelper.addVariation("tile.roadLine.1.desc", 1, "line-marking/double-white-center"); road_line.carverHelper.addVariation("tile.roadLine.2.desc", 2, "line-marking/yellow-center"); road_line.carverHelper.addVariation("tile.roadLine.3.desc", 3, "line-marking/double-yellow-center"); - GameRegistry.registerBlock(road_line, ItemCarvable.class, "road_line"); - Carving.chisel.registerOre("roadLine", "roadLine"); + road_line.carverHelper.register(road_line, "road_line"); + Carving.chisel.registerOre("road_line", "roadLine"); } @Override @@ -1995,7 +1989,7 @@ public enum Features { // 13), "Sandstone snake block body"); Carving.chisel.addVariation("sandstone", sand_snakestone, 1, 16); Carving.chisel.addVariation("sandstone", sand_snakestone, 13, 17); - Carving.chisel.registerOre("sandSnakestone", "sandSnakestone"); +// Carving.chisel.registerOre("sandstone", "sandSnakestone"); } }, @@ -2011,7 +2005,7 @@ public enum Features { // "Stone snake block body"); Carving.chisel.addVariation("stonebrick", stone_snakestone, 1, 16); Carving.chisel.addVariation("stonebrick", stone_snakestone, 13, 17); - Carving.chisel.registerOre("snakestoneStone", "snakestoneStone"); +// Carving.chisel.registerOre("snakestoneStone", "snakestoneStone"); } }, @@ -2024,7 +2018,7 @@ public enum Features { GameRegistry.registerBlock(obsidian_snakestone, ItemCarvable.class, "obsidian_snakestone"); Carving.chisel.addVariation("obsidian", obsidian_snakestone, 1, 16); Carving.chisel.addVariation("obsidian", obsidian_snakestone, 13, 17); - Carving.chisel.registerOre("obsidianSnakestone", "obsidianSnakestone"); +// Carving.chisel.registerOre("obsidianSnakestone", "obsidianSnakestone"); } }, @@ -2414,7 +2408,7 @@ public enum Features { for (int i = 0; i < plank_names.length; i++) { String n = plank_names[i]; String u = plank_ucnames[i]; - final String orename = n.replace('-', '_') + "_planks"; + final String name = n.replace('-', '_') + "_planks"; planks[i] = (BlockCarvable) (new BlockCarvable(Material.wood)).setCreativeTab(ChiselTabs.tabWoodChiselBlocks).setHardness(2.0F).setResistance(5.0F).setStepSound(Block.soundTypeWood); planks[i].carverHelper.setChiselBlockName(u + " Wood Planks"); @@ -2433,11 +2427,11 @@ public enum Features { planks[i].carverHelper.addVariation(u + " wood scaffold", 12, "planks-" + n + "/crateex"); planks[i].carverHelper.addVariation(u + " wood planks in disarray", 14, "planks-" + n + "/chaotic-hor"); planks[i].carverHelper.addVariation("Vertical " + n + " wood planks in disarray", 15, "planks-" + n + "/chaotic"); - planks[i].carverHelper.register(planks[i], orename); - Carving.chisel.addVariation(orename, Blocks.planks, i, 0); + planks[i].carverHelper.register(planks[i], name); + Carving.chisel.addVariation(name, Blocks.planks, i, 0); planks[i].setHarvestLevel("axe", 0); - Carving.chisel.registerOre("wood", "wood"); - Carving.chisel.setVariationSound(orename, Chisel.MOD_ID + ":chisel.wood"); + Carving.chisel.registerOre(name, "wood"); + Carving.chisel.setVariationSound(name, Chisel.MOD_ID + ":chisel.wood"); } } }, @@ -2569,7 +2563,7 @@ public enum Features { boolean needsMetaRecipes() { return false; } - + private static void registerSlabTop(Block bottom, Block top) { String name = Block.blockRegistry.getNameForObject(bottom); name = name.substring(name.indexOf(':') + 1) + "_top"; diff --git a/src/main/java/com/cricketcraft/chisel/api/carving/CarvingUtils.java b/src/main/java/com/cricketcraft/chisel/api/carving/CarvingUtils.java new file mode 100644 index 00000000..f9950593 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/api/carving/CarvingUtils.java @@ -0,0 +1,17 @@ +package com.cricketcraft.chisel.api.carving; + +public class CarvingUtils { + + /** + * The default implementation of {@link ICarvingVariation#compareTo(ICarvingVariation)} + * + * @param v1 + * The first {@link ICarvingVariation variation}. Usually 'this'. + * @param v2 + * The second {@link ICarvingVariation variation}. Usually the parameter to the {@code comapreTo} method. + * @return A positive integer if the first's order is greater, a negative integer if the second's is greater, and 0 if they are equal. + */ + public static int compare(ICarvingVariation v1, ICarvingVariation v2) { + return v1.getOrder() - v2.getOrder(); + } +} diff --git a/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingGroup.java b/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingGroup.java new file mode 100644 index 00000000..287929c8 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingGroup.java @@ -0,0 +1,58 @@ +package com.cricketcraft.chisel.api.carving; + +import java.util.List; + +public interface ICarvingGroup { + + /** + * The name of this group. Used for internal identification. + * + * @return A string name for the group + */ + String getName(); + + /** + * Can return null. If null, fallback sound will be used (the standard chisel sound). + * + * @return The string resource path of the sound to use for chiseling items in this group + */ + String getSound(); + + /** + * Sets the sound of this group + * + * @param sound + * A string resource path for the sound this group makes when chiseled + */ + void setSound(String sound); + + /** + * The oredict name to match to this group. All items with this oredict name will be assumed to be part of this group. + * + * @return An ore dictionary name + */ + String getOreName(); + + /** + * Sets the oredict name for this group. + * + * @param oreName + * The String oredict name to be associated with this group. + */ + void setOreName(String oreName); + + /** + * Gets all carving variations associated with this group. + * + * @return A {@link List} of {@link ICarvingVariation}s + */ + List getVariations(); + + /** + * Adds a variation to this group. + * + * @param variation + * An {@link ICarvingVariation} to add to this group + */ + void addVariation(ICarvingVariation variation); +} diff --git a/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingVariation.java b/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingVariation.java new file mode 100644 index 00000000..6b09c259 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/api/carving/ICarvingVariation.java @@ -0,0 +1,34 @@ +package com.cricketcraft.chisel.api.carving; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; + +/** + * Represents a variation of a chiselable block. + *

+ * Do note that while this interface extends {@link Comparable}, a default implementation of {@link Comparable#compareTo(Object)} is provided in + * {@link CarvingUtils#compare(ICarvingVariation, ICarvingVariation)} + */ +public interface ICarvingVariation extends Comparable { + + Block getBlock(); + + int getBlockMeta(); + + int getItemMeta(); + + /** + * Gets an {@link ItemStack} representing this variation. + * + * @return An {@link ItemStack} to represent this variation + */ + ItemStack getStack(); + + /** + * The "order" of this variation. Represents its position in the list of variations. + * + * @return An integer to represent the position of this variation in the list of all variations in the group + */ + int getOrder(); + +} diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMaker.java b/src/main/java/com/cricketcraft/chisel/block/CarvableStairsMaker.java similarity index 64% rename from src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMaker.java rename to src/main/java/com/cricketcraft/chisel/block/CarvableStairsMaker.java index b75a379f..524a8b9d 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMaker.java +++ b/src/main/java/com/cricketcraft/chisel/block/CarvableStairsMaker.java @@ -2,7 +2,6 @@ package com.cricketcraft.chisel.block; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; -import net.minecraft.item.crafting.CraftingManager; import com.cricketcraft.chisel.carving.CarvableHelper; import com.cricketcraft.chisel.carving.CarvableVariation; @@ -10,14 +9,14 @@ import com.cricketcraft.chisel.item.ItemCarvable; import cpw.mods.fml.common.registry.GameRegistry; -public class BlockMarbleStairsMaker { +public class CarvableStairsMaker { public CarvableHelper carverHelper; // int idStart; Block blockBase; String blockName; - public BlockMarbleStairsMaker(Block base) { + public CarvableStairsMaker(Block base) { carverHelper = new CarvableHelper(); blockBase = base; } @@ -26,7 +25,7 @@ public class BlockMarbleStairsMaker { create(null, name, blocks); } - public void create(BlockMarbleStairsMakerCreator creator, String name, Block[] blocks) { + public void create(IStairsCreator creator, String name, Block[] blocks) { for (int i = 0; i < blocks.length; i++) { String n = name + "." + i; blocks[i] = creator == null ? new BlockCarvableStairs(blockBase, i * 2, carverHelper) : creator.create(blockBase, i * 2, carverHelper); @@ -37,15 +36,10 @@ public class BlockMarbleStairsMaker { for (int meta = 0; meta < 2 && i * 2 + meta < carverHelper.variations.size(); meta++) { CarvableVariation variation = carverHelper.variations.get(i * 2 + meta); - for (int j = 0; j < 8; j++) - carverHelper.registerVariation(name + ".orientation." + j, variation, blocks[i], j + meta * 8); + carverHelper.registerVariation(name, variation, blocks[i], meta * 8); - CraftingManager.getInstance().addRecipe(new ItemStack(blocks[i], 4, meta * 8), new Object[] { "* ", "** ", "***", '*', new ItemStack(blockBase, 1, i * 2 + meta) }); + GameRegistry.addRecipe(new ItemStack(blocks[i], 4, meta * 8), "* ", "** ", "***", '*', new ItemStack(blockBase, 1, i * 2 + meta)); } - - CarvableHelper.chiselBlocks.add(blocks[i]); } - } - } diff --git a/src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMakerCreator.java b/src/main/java/com/cricketcraft/chisel/block/IStairsCreator.java similarity index 81% rename from src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMakerCreator.java rename to src/main/java/com/cricketcraft/chisel/block/IStairsCreator.java index a850081c..c3d91703 100644 --- a/src/main/java/com/cricketcraft/chisel/block/BlockMarbleStairsMakerCreator.java +++ b/src/main/java/com/cricketcraft/chisel/block/IStairsCreator.java @@ -4,7 +4,7 @@ import net.minecraft.block.Block; import com.cricketcraft.chisel.carving.CarvableHelper; -public interface BlockMarbleStairsMakerCreator { +public interface IStairsCreator { public BlockCarvableStairs create(Block block, int meta, CarvableHelper helper); } diff --git a/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java b/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java index b495773b..5f2464bd 100644 --- a/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java +++ b/src/main/java/com/cricketcraft/chisel/carving/CarvableHelper.java @@ -13,6 +13,7 @@ import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; import net.minecraft.world.IBlockAccess; +import net.minecraftforge.oredict.OreDictionary; import com.cricketcraft.chisel.Chisel; import com.cricketcraft.chisel.block.BlockCarvableSlab; @@ -28,8 +29,6 @@ import cpw.mods.fml.common.registry.GameRegistry; public class CarvableHelper { - public static final ArrayList chiselBlocks = new ArrayList(); - public static final int NORMAL = 0; public static final int TOPSIDE = 1; public static final int TOPBOTSIDE = 2; @@ -270,7 +269,6 @@ public class CarvableHelper { void registerBlock(Block block, String name, Class cl) { block.setBlockName(name); GameRegistry.registerBlock(block, cl, name); - chiselBlocks.add(block); } public void register(Block block, String name, Class cl) { @@ -284,7 +282,6 @@ public class CarvableHelper { if (!forbidChiseling) { Carving.chisel.addVariation(name + ".top", slab.top, variation.metadata, 0); - Carving.chisel.setGroupClass(name + ".top", name); } } } @@ -389,7 +386,10 @@ public class CarvableHelper { public static Set getChiselBlockSet() { HashSet tools = new HashSet(); - tools.addAll(CarvableHelper.chiselBlocks); return tools; } + + public void registerOre(String ore) { + OreDictionary.registerOre(ore, variations.get(0).block); + } } diff --git a/src/main/java/com/cricketcraft/chisel/carving/Carving.java b/src/main/java/com/cricketcraft/chisel/carving/Carving.java index 05366412..0a9e501b 100644 --- a/src/main/java/com/cricketcraft/chisel/carving/Carving.java +++ b/src/main/java/com/cricketcraft/chisel/carving/Carving.java @@ -11,15 +11,18 @@ import net.minecraft.item.ItemStack; import net.minecraftforge.oredict.OreDictionary; import com.cricketcraft.chisel.Chisel; +import com.cricketcraft.chisel.api.carving.ICarvingGroup; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; public class Carving { - HashMap carvingGroupsByName = new HashMap(); - HashMap carvingGroupsByOre = new HashMap(); - HashMap carvingGroupsByVariation = new HashMap(); + GroupList groups = new GroupList(); - public static Carving chisel = new Carving(); - public static Carving needle = new Carving(); + public static final Carving chisel = new Carving(); + public static final Carving needle = new Carving(); + + private Carving() { + } String key(Item item, int metadata) { return Item.itemRegistry.getNameForObject(item) + "|" + metadata; @@ -29,34 +32,20 @@ public class Carving { return Block.blockRegistry.getNameForObject(block) + "|" + metadata; } - public boolean isVariationOfSameClass(Block target, int metadata1, Block block2, int metadata2) { - CarvingGroup group1 = carvingGroupsByVariation.get(key(target, metadata1)); - if (group1 == null) - return false; - - CarvingGroup group2 = carvingGroupsByVariation.get(key(block2, metadata2)); - if (group2 == null) - return false; - - return group1 == group2 || group1.className.equals(group2.className) && !group1.className.isEmpty(); - - } - - public CarvingVariation[] getVariations(Block block, int metadata) { - CarvingGroup group = getGroup(block, metadata); + public List getVariations(Block block, int metadata) { + ICarvingGroup group = getGroup(block, metadata); if (group == null) return null; - CarvingVariation[] res = group.variations.toArray(new CarvingVariation[group.variations.size()]); - return res; + return group.getVariations(); } public String getOre(Block block, int metadata) { - CarvingGroup group = getGroup(block, metadata); + ICarvingGroup group = getGroup(block, metadata); if (group == null) return null; - return group.oreName; + return group.getOreName(); } public ArrayList getItems(ItemStack chiseledItem) { @@ -64,26 +53,29 @@ public class Carving { int damage = chiseledItem.getItemDamage(); - CarvingGroup group = getGroup(Block.getBlockFromItem(chiseledItem.getItem()), damage); + ICarvingGroup group = getGroup(Block.getBlockFromItem(chiseledItem.getItem()), damage); if (group == null) return items; HashMap mapping = new HashMap(); - if (group.variations != null) { - for (CarvingVariation v : group.variations) { + List variations = group.getVariations(); - String key = Block.getIdFromBlock(v.block) + "|" + v.damage; + if (!group.getVariations().isEmpty()) { + for (ICarvingVariation v : variations) { + + String key = Block.getIdFromBlock(v.getBlock()) + "|" + v.getItemMeta(); if (mapping.containsKey(key)) continue; mapping.put(key, 1); - items.add(new ItemStack(v.block, 1, v.damage)); + items.add(new ItemStack(v.getBlock(), 1, v.getItemMeta())); } } ArrayList ores; - if (group.oreName != null && ((ores = OreDictionary.getOres(group.oreName)) != null)) { + String oreName = group.getOreName(); + if (oreName != null && ((ores = OreDictionary.getOres(oreName)) != null)) { for (ItemStack stack : ores) { String key = Item.getIdFromItem(stack.getItem()) + "|" + stack.getItemDamage(); @@ -98,71 +90,53 @@ public class Carving { return items; } - public CarvingGroup getGroup(Block block, int metadata) { - // Check name first - CarvingGroup res; - int i = OreDictionary.getOreID(block.getUnlocalizedName()); - if (i < 1) - return null; - - // get All dictionary names for that block via Ids - // the first one matching with a group is chosen - int[] oreDictIds = OreDictionary.getOreIDs(new ItemStack(block, 1, metadata)); - for (int oreId : oreDictIds) { - String oreDictName = OreDictionary.getOreName(oreId); - if (carvingGroupsByOre.containsKey(oreDictName)) - return carvingGroupsByOre.get(oreDictName); + public ICarvingGroup getGroup(Block block, int metadata) { + int[] ids = OreDictionary.getOreIDs(new ItemStack(block, metadata)); + if (ids.length > 0) { + for (int id : ids) { + return groups.getGroupByOre(OreDictionary.getOreName(id)); + } } - if ((res = carvingGroupsByVariation.get(key(block, metadata))) != null) - return res; - - return null; + return groups.getGroup(block, metadata); } - public CarvingGroup getGroup(String name) { - CarvingGroup group = carvingGroupsByName.get(name); - if (group == null) { - group = new CarvingGroup(name); - group.className = name; - carvingGroupsByName.put(name, group); - } - - return group; + public ICarvingGroup getGroup(String name) { + return groups.getGroupByName(name); } public CarvingVariation addVariation(String name, Block block, int metadata, int order) { - CarvingGroup group = getGroup(name); - - CarvingGroup blockGroup = carvingGroupsByVariation.get(key(block, metadata)); - if (blockGroup != null || blockGroup == group) - return null; CarvingVariation variation = new CarvingVariation(block, metadata, order); - group.variations.add(variation); - Collections.sort(group.variations); - carvingGroupsByVariation.put(key(block, metadata), group); + + ICarvingGroup group = groups.getGroupByName(name); + + if (group == null) { + group = new CarvingGroup(name); + groups.add(group); + } + + groups.addVariation(name, variation); return variation; } public void registerOre(String name, String oreName) { - CarvingGroup group = getGroup(name); - - carvingGroupsByOre.put(oreName, group); - - group.oreName = oreName; - } - - public void setGroupClass(String name, String className) { - CarvingGroup group = getGroup(name); - - group.className = className; + ICarvingGroup group = groups.getGroupByName(name); + if (group != null) { + group.setOreName(oreName); + } else { + throw new NullPointerException("Cannot register ore name for group " + name + ", as it does not exist."); + } } public void setVariationSound(String name, String sound) { - CarvingGroup group = getGroup(name); - group.sound = sound; + ICarvingGroup group = groups.getGroupByName(name); + if (group != null) { + group.setSound(sound); + } else { + throw new NullPointerException("Cannot set sound for group " + name + ", as it does not exist."); + } } public String getVariationSound(Block block, int metadata) { @@ -170,18 +144,14 @@ public class Carving { } public String getVariationSound(Item item, int metadata) { - CarvingGroup blockGroup = carvingGroupsByVariation.get(key(item, metadata)); - if (blockGroup == null || blockGroup.sound == null) - return Chisel.MOD_ID + ":chisel.fallback"; - - return blockGroup.sound; + ICarvingGroup group = groups.getGroup(Block.getBlockFromItem(item), metadata); + String sound = group == null ? null : group.getSound(); + return sound == null ? Chisel.MOD_ID + ":chisel.fallback" : sound; } public List getSortedGroupNames() { List names = new ArrayList(); - for (String s : carvingGroupsByName.keySet()) { - names.add(s); - } + names.addAll(groups.getNames()); Collections.sort(names); return names; } diff --git a/src/main/java/com/cricketcraft/chisel/carving/CarvingGroup.java b/src/main/java/com/cricketcraft/chisel/carving/CarvingGroup.java index 09c40808..5f4ecf3f 100644 --- a/src/main/java/com/cricketcraft/chisel/carving/CarvingGroup.java +++ b/src/main/java/com/cricketcraft/chisel/carving/CarvingGroup.java @@ -1,33 +1,57 @@ package com.cricketcraft.chisel.carving; -import java.util.ArrayList; +import java.util.Collections; import java.util.List; +import com.cricketcraft.chisel.api.carving.ICarvingGroup; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; import com.google.common.collect.ImmutableList; +import com.google.common.collect.Lists; -public class CarvingGroup { +public class CarvingGroup implements ICarvingGroup { - public CarvingGroup(String n) { - name = n; + private String name; + private String sound; + private String oreName; + + private List variations = Lists.newArrayList(); + + public CarvingGroup(String name) { + this.name = name; } - String name; - String className; - String sound; - String oreName; - - List variations = new ArrayList(); - - public List getVariations() { + public List getVariations() { return ImmutableList.copyOf(variations); } + @Override + public void addVariation(ICarvingVariation variation) { + variations.add(variation); + Collections.sort(variations); + } + + @Override public String getName() { return name; } + @Override + public String getSound() { + return sound; + } + + @Override + public void setSound(String sound) { + this.sound = sound; + } + + @Override public String getOreName() { return oreName; } + @Override + public void setOreName(String oreName) { + this.oreName = oreName; + } } diff --git a/src/main/java/com/cricketcraft/chisel/carving/CarvingVariation.java b/src/main/java/com/cricketcraft/chisel/carving/CarvingVariation.java index 99da0f9e..42cc6d59 100644 --- a/src/main/java/com/cricketcraft/chisel/carving/CarvingVariation.java +++ b/src/main/java/com/cricketcraft/chisel/carving/CarvingVariation.java @@ -3,7 +3,15 @@ package com.cricketcraft.chisel.carving; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; -public class CarvingVariation implements Comparable { +import com.cricketcraft.chisel.api.carving.CarvingUtils; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; + +public class CarvingVariation implements ICarvingVariation { + + private int order; + private Block block; + private int meta; + private int damage; public CarvingVariation(Block block, int metadata, int ord) { this.order = ord; @@ -13,17 +21,32 @@ public class CarvingVariation implements Comparable { } @Override - public int compareTo(CarvingVariation a) { - return order - a.order; - } - - public int order; - public Block block; - public int meta; - public int damage; - public ItemStack getStack() { return new ItemStack(block, 1, damage); } + @Override + public Block getBlock() { + return block; + } + + @Override + public int getBlockMeta() { + return meta; + } + + @Override + public int getItemMeta() { + return damage; + } + + @Override + public int getOrder() { + return order; + } + + @Override + public int compareTo(ICarvingVariation o) { + return CarvingUtils.compare(this, o); + } } diff --git a/src/main/java/com/cricketcraft/chisel/carving/GroupList.java b/src/main/java/com/cricketcraft/chisel/carving/GroupList.java new file mode 100644 index 00000000..80e0dc88 --- /dev/null +++ b/src/main/java/com/cricketcraft/chisel/carving/GroupList.java @@ -0,0 +1,217 @@ +package com.cricketcraft.chisel.carving; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Set; + +import net.minecraft.block.Block; +import net.minecraft.item.ItemStack; + +import org.apache.commons.lang3.tuple.Pair; + +import com.cricketcraft.chisel.api.carving.ICarvingGroup; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; +import com.google.common.collect.Maps; + +public class GroupList implements Set { + + private class VariationWrapper { + + private ICarvingVariation v; + + private VariationWrapper(ICarvingVariation v) { + this.v = v; + } + + @Override + public boolean equals(Object obj) { + if (obj instanceof ICarvingVariation) { + ICarvingVariation v2 = (ICarvingVariation) obj; + return v.getBlock() == v2.getBlock() && (v.getBlockMeta() == v2.getBlockMeta() || v.getItemMeta() == v2.getItemMeta()); + } else if (obj instanceof VariationWrapper) { + return equals(((VariationWrapper) obj).v); + } + return false; + } + + @Override + public int hashCode() { + return v.getBlock().hashCode(); + } + } + + private class VariationKey implements ICarvingVariation { + + Pair data; + + private VariationKey(Block block, int blockMeta) { + data = Pair.of(block, blockMeta); + } + + @Override + public int compareTo(ICarvingVariation o) { + return 0; + } + + @Override + public Block getBlock() { + return data.getLeft(); + } + + @Override + public int getBlockMeta() { + return data.getRight(); + } + + @Override + public int getItemMeta() { + return data.getRight(); + } + + @Override + public ItemStack getStack() { + return null; + } + + @Override + public int getOrder() { + return 0; + } + + @Override + public String toString() { + return data.toString(); + } + } + + private HashMap groups = Maps.newHashMap(); + private HashMap lookup = Maps.newHashMap(); + + @Override + public int size() { + return groups.size(); + } + + @Override + public boolean isEmpty() { + return groups.isEmpty(); + } + + @Override + public boolean contains(Object o) { + if (o instanceof ICarvingGroup) { + return groups.containsKey(((ICarvingGroup) o).getName()); + } + return false; + } + + @Override + public Iterator iterator() { + return groups.values().iterator(); + } + + @Override + public Object[] toArray() { + return groups.values().toArray(); + } + + @Override + public T[] toArray(T[] arr) { + return groups.values().toArray(arr); + } + + @Override + public boolean add(ICarvingGroup group) { + String key = group.getName(); + if (groups.containsKey(key)) { + return false; + } + groups.put(key, group); + return true; + } + + @Override + public boolean remove(Object o) { + if (o instanceof ICarvingGroup) { + return groups.remove(((ICarvingGroup) o).getName()) != null; + } + return false; + } + + @Override + public boolean containsAll(Collection c) { + for (Object o : c) { + if (!(o instanceof ICarvingGroup)) { + return false; + } + if (!groups.containsKey(((ICarvingGroup) o).getName())) { + return false; + } + } + return true; + } + + @Override + public boolean addAll(Collection c) { + boolean ret = false; + for (ICarvingGroup g : c) { + ret |= add(g); + } + return ret; + } + + @Override + public boolean removeAll(Collection c) { + boolean ret = false; + for (Object o : c) { + ret |= remove(o); + } + return ret; + } + + @Override + public boolean retainAll(Collection c) { + throw new UnsupportedOperationException(); // not using it, not gonna bother + } + + @Override + public void clear() { + groups.clear(); + } + + public ICarvingGroup getGroup(Block block, int metadata) { + return lookup.get(new VariationWrapper(new VariationKey(block, metadata))); + } + + public void addVariation(String name, ICarvingVariation variation) { + ICarvingGroup g = groups.get(name); + if (g == null) { + throw new NullPointerException("No group exists for name " + name); + } + g.addVariation(variation); + lookup.put(new VariationWrapper(variation), g); + } + + public ICarvingGroup getGroupByName(String groupName) { + return groups.get(groupName); + } + + public ICarvingGroup getGroupByOre(String oreName) { + for (ICarvingGroup group : groups.values()) { + if (oreName.equals(group.getOreName())) { + return group; + } + } + return null; + } + + public Collection getNames() { + return groups.keySet(); + } + + @Override + public String toString() { + return groups.toString(); + } +} diff --git a/src/main/java/com/cricketcraft/chisel/compat/Compatibility.java b/src/main/java/com/cricketcraft/chisel/compat/Compatibility.java index ec415d94..b3d2ef4a 100644 --- a/src/main/java/com/cricketcraft/chisel/compat/Compatibility.java +++ b/src/main/java/com/cricketcraft/chisel/compat/Compatibility.java @@ -29,8 +29,10 @@ public class Compatibility { /* Mariculture */ Block block = GameRegistry.findBlock("mariculture", "limestone"); - for (int i = 0; i < 16; i++) { - addSupport("limestone", block, i, 99); + if (block != null) { + for (int i = 0; i < 16; i++) { + addSupport("limestone", block, i, 99); + } } /* Geologica */ diff --git a/src/main/java/com/cricketcraft/chisel/compat/nei/RecipeHandlerChisel.java b/src/main/java/com/cricketcraft/chisel/compat/nei/RecipeHandlerChisel.java index 572bf491..05c541e5 100644 --- a/src/main/java/com/cricketcraft/chisel/compat/nei/RecipeHandlerChisel.java +++ b/src/main/java/com/cricketcraft/chisel/compat/nei/RecipeHandlerChisel.java @@ -15,9 +15,9 @@ import codechicken.nei.NEIServerUtils; import codechicken.nei.PositionedStack; import codechicken.nei.recipe.TemplateRecipeHandler; +import com.cricketcraft.chisel.api.carving.ICarvingGroup; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; import com.cricketcraft.chisel.carving.Carving; -import com.cricketcraft.chisel.carving.CarvingGroup; -import com.cricketcraft.chisel.carving.CarvingVariation; public class RecipeHandlerChisel extends TemplateRecipeHandler { @@ -107,7 +107,7 @@ public class RecipeHandlerChisel extends TemplateRecipeHandler { public void loadCraftingRecipes(String outputId, Object... results) { if (outputId.equals("chisel2.chisel")) { for (String name : Carving.chisel.getSortedGroupNames()) { - CarvingGroup g = Carving.chisel.getGroup(name); + ICarvingGroup g = Carving.chisel.getGroup(name); if (!g.getVariations().isEmpty()) { addCached(getVariationStacks(g)); } @@ -120,7 +120,7 @@ public class RecipeHandlerChisel extends TemplateRecipeHandler { @Override public void loadCraftingRecipes(ItemStack result) { for (String name : Carving.chisel.getSortedGroupNames()) { - CarvingGroup g = Carving.chisel.getGroup(name); + ICarvingGroup g = Carving.chisel.getGroup(name); List variations = getVariationStacks(g); for (ItemStack stack : variations) { if (NEIServerUtils.areStacksSameTypeCrafting(stack, result)) { @@ -133,7 +133,7 @@ public class RecipeHandlerChisel extends TemplateRecipeHandler { @Override public void loadUsageRecipes(ItemStack ingredient) { for (String name : Carving.chisel.getSortedGroupNames()) { - CarvingGroup g = Carving.chisel.getGroup(name); + ICarvingGroup g = Carving.chisel.getGroup(name); List variations = getVariationStacks(g); for (ItemStack stack : variations) { if (NEIServerUtils.areStacksSameTypeCrafting(stack, ingredient)) { @@ -162,9 +162,9 @@ public class RecipeHandlerChisel extends TemplateRecipeHandler { addCached(variations, null, null); } - private static List getVariationStacks(CarvingGroup g) { + private static List getVariationStacks(ICarvingGroup g) { List stacks = new ArrayList(); - for (CarvingVariation v : g.getVariations()) { + for (ICarvingVariation v : g.getVariations()) { stacks.add(v.getStack()); } String oreName = g.getOreName(); diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java index e903fd56..7a8a8485 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ChiselController.java @@ -1,5 +1,7 @@ package com.cricketcraft.chisel.item.chisel; +import java.util.List; + import net.minecraft.block.Block; import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayerMP; @@ -12,6 +14,7 @@ import org.apache.commons.lang3.ArrayUtils; import com.cricketcraft.chisel.Chisel; import com.cricketcraft.chisel.api.IChiselItem; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.carving.CarvingVariation; import com.cricketcraft.chisel.network.PacketHandler; @@ -49,28 +52,34 @@ public final class ChiselController { z = event.z; Block block = event.world.getBlock(x, y, z); int metadata = event.world.getBlockMetadata(x, y, z); - CarvingVariation[] variations = Carving.chisel.getVariations(block, metadata); + List list = Carving.chisel.getVariations(block, metadata); + + if (list == null || list.isEmpty()) { + break; + } + + ICarvingVariation[] variations = list.toArray(new CarvingVariation[] {}); - if (variations != null && chisel.canChiselBlock(event.world, x, y, z, block, metadata)) { + if (chisel.canChiselBlock(event.world, x, y, z, block, metadata)) { ItemStack target = General.getChiselTarget(held); if (target != null) { - for (CarvingVariation v : variations) { - if (v.block == Block.getBlockFromItem(target.getItem()) && v.meta == target.getItemDamage()) { + for (ICarvingVariation v : variations) { + if (v.getBlock() == Block.getBlockFromItem(target.getItem()) && v.getBlockMeta() == target.getItemDamage()) { setVariation(event.entityPlayer, event.world, x, y, z, v, target); } } } else { for (int i = 0; i < variations.length; i++) { - CarvingVariation v = variations[i]; - if (v.block == block && v.meta == metadata) { + ICarvingVariation v = variations[i]; + if (v.getBlock() == block && v.getBlockMeta() == metadata) { variations = ArrayUtils.remove(variations, i--); } } int index = event.world.rand.nextInt(variations.length); - CarvingVariation newVar = variations[index]; - setVariation(event.entityPlayer, event.world, x, y, z, newVar, new ItemStack(newVar.block, 1, newVar.damage)); + ICarvingVariation newVar = variations[index]; + setVariation(event.entityPlayer, event.world, x, y, z, newVar, new ItemStack(newVar.getBlock(), 1, newVar.getItemMeta())); event.entityPlayer.inventory.currentItem = slot; } } @@ -87,10 +96,9 @@ public final class ChiselController { /** * Assumes that the player is holding a chisel */ - private void setVariation(EntityPlayer player, World world, int x, int y, int z, CarvingVariation v, ItemStack target) { + private void setVariation(EntityPlayer player, World world, int x, int y, int z, ICarvingVariation v, ItemStack target) { PacketHandler.INSTANCE.sendTo(new MessageChiselSound(x, y, z, v), (EntityPlayerMP) player); - world.setBlock(x, y, z, v.block); - world.setBlockMetadataWithNotify(x, y, z, v.meta, 3); + world.setBlock(x, y, z, v.getBlock(), v.getBlockMeta(), 3); ((IChiselItem) player.getCurrentEquippedItem().getItem()).onChisel(world, player.inventory, player.inventory.currentItem, player.getCurrentEquippedItem(), target); } } diff --git a/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java b/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java index 16470d97..2eed31dc 100644 --- a/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java +++ b/src/main/java/com/cricketcraft/chisel/item/chisel/ItemChisel.java @@ -17,7 +17,6 @@ import com.cricketcraft.chisel.api.ChiselMode; import com.cricketcraft.chisel.api.IChiselItem; import com.cricketcraft.chisel.carving.Carving; import com.cricketcraft.chisel.config.Configurations; -import com.cricketcraft.chisel.init.ChiselBlocks; import com.cricketcraft.chisel.utils.GeneralClient; public class ItemChisel extends Item implements IChiselItem { @@ -104,10 +103,8 @@ public class ItemChisel extends Item implements IChiselItem { String sound = carving.getVariationSound(target.getItem(), target.getItemDamage()); EntityPlayer player = Chisel.proxy.getClientPlayer(); GeneralClient.playChiselSound(world, MathHelper.floor_double(player.posX), MathHelper.floor_double(player.posY), MathHelper.floor_double(player.posZ), sound); - } else { - if (chisel.getItemDamage() >= chisel.getMaxDamage()) { - inv.decrStackSize(slot, 1); - } + } else if (chisel.getItemDamage() >= chisel.getMaxDamage()) { + inv.decrStackSize(slot, 1); } } diff --git a/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java b/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java index 5df9a879..781e3da6 100644 --- a/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java +++ b/src/main/java/com/cricketcraft/chisel/network/message/MessageChiselSound.java @@ -3,6 +3,7 @@ package com.cricketcraft.chisel.network.message; import io.netty.buffer.ByteBuf; import net.minecraft.block.Block; +import com.cricketcraft.chisel.api.carving.ICarvingVariation; import com.cricketcraft.chisel.carving.CarvingVariation; import com.cricketcraft.chisel.client.GeneralChiselClient; import com.cricketcraft.chisel.item.chisel.ItemChisel; @@ -21,10 +22,10 @@ public class MessageChiselSound extends MessageCoords { private int block; private byte meta; - public MessageChiselSound(int x, int y, int z, CarvingVariation v) { + public MessageChiselSound(int x, int y, int z, ICarvingVariation v) { super(x, y, z); - this.block = Block.getIdFromBlock(v.block); - this.meta = (byte) v.meta; + this.block = Block.getIdFromBlock(v.getBlock()); + this.meta = (byte) v.getBlockMeta(); } @Override