From c589dd66de822f90b79c621fd5c684343e41480f Mon Sep 17 00:00:00 2001 From: LemADEC Date: Sun, 6 Dec 2015 23:13:33 +0100 Subject: [PATCH] Refactored decorative blocks --- .../block/passive/BlockDecorative.java | 61 ++++++++----------- .../block/passive/ItemBlockDecorative.java | 13 ++-- .../cr0s/warpdrive/data/DecorativeType.java | 30 +++++++++ 3 files changed, 64 insertions(+), 40 deletions(-) create mode 100644 src/main/java/cr0s/warpdrive/data/DecorativeType.java diff --git a/src/main/java/cr0s/warpdrive/block/passive/BlockDecorative.java b/src/main/java/cr0s/warpdrive/block/passive/BlockDecorative.java index 320ca636..2ca720ae 100644 --- a/src/main/java/cr0s/warpdrive/block/passive/BlockDecorative.java +++ b/src/main/java/cr0s/warpdrive/block/passive/BlockDecorative.java @@ -10,47 +10,44 @@ import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.util.IIcon; import cr0s.warpdrive.WarpDrive; +import cr0s.warpdrive.data.ComponentType; +import cr0s.warpdrive.data.DecorativeType; public class BlockDecorative extends Block { - public static enum decorativeTypes { - Plain, Energized, Network - }; - - private static ItemStack[] isCache = new ItemStack[decorativeTypes.values().length]; - private static IIcon[] iconBuffer = new IIcon[decorativeTypes.values().length]; + private static IIcon[] icons; + private static ItemStack[] itemStackCache; public BlockDecorative() { super(Material.iron); setHardness(0.5f); setStepSound(Block.soundTypeMetal); - setCreativeTab(WarpDrive.creativeTabWarpDrive); setBlockName("warpdrive.passive.Plain"); - } - - private static boolean isValidDamage(final int damage) { - return damage >= 0 && damage < decorativeTypes.values().length; + setCreativeTab(WarpDrive.creativeTabWarpDrive); + + icons = new IIcon[DecorativeType.length]; + itemStackCache = new ItemStack[DecorativeType.length]; } @Override - public void getSubBlocks(Item par1, CreativeTabs par2CreativeTabs, List par3List) { - for (decorativeTypes val : decorativeTypes.values()) { - par3List.add(new ItemStack(par1, 1, val.ordinal())); + public void getSubBlocks(Item item, CreativeTabs creativeTabs, List list) { + for (DecorativeType decorativeType : DecorativeType.values()) { + list.add(new ItemStack(item, 1, decorativeType.ordinal())); } } @Override - public void registerBlockIcons(IIconRegister ir) { - for (decorativeTypes val : decorativeTypes.values()) { - iconBuffer[val.ordinal()] = ir.registerIcon("warpdrive:passive/decorative" + val.toString()); + public void registerBlockIcons(IIconRegister iconRegister) { + for (DecorativeType decorativeType : DecorativeType.values()) { + icons[decorativeType.ordinal()] = iconRegister.registerIcon("warpdrive:passive/decorative" + decorativeType.unlocalizedName); } } @Override public IIcon getIcon(int side, int damage) { - if (isValidDamage(damage)) { - return iconBuffer[damage]; + if (damage >= 0 && damage < ComponentType.length) { + return icons[damage]; } - return iconBuffer[0]; + return icons[0]; } @Override @@ -58,22 +55,18 @@ public class BlockDecorative extends Block { return damage; } - public static ItemStack getItemStack(int damage) { - if (!isValidDamage(damage)) { - return null; + public static ItemStack getItemStack(DecorativeType decorativeType) { + if (decorativeType != null) { + int damage = decorativeType.ordinal(); + if (itemStackCache[damage] == null) { + itemStackCache[damage] = new ItemStack(WarpDrive.blockDecorative, 1, damage); + } + return itemStackCache[damage]; } - - if (isCache[damage] == null) { - isCache[damage] = getItemStackNoCache(damage, 1); - } - return isCache[damage]; + return null; } - public static ItemStack getItemStackNoCache(int damage, int amount) { - if (!isValidDamage(damage)) { - return null; - } - - return new ItemStack(WarpDrive.blockDecorative, amount, damage); + public static ItemStack getItemStackNoCache(DecorativeType decorativeType, int amount) { + return new ItemStack(WarpDrive.blockDecorative, amount, decorativeType.ordinal()); } } diff --git a/src/main/java/cr0s/warpdrive/block/passive/ItemBlockDecorative.java b/src/main/java/cr0s/warpdrive/block/passive/ItemBlockDecorative.java index 416fa230..7a114fed 100644 --- a/src/main/java/cr0s/warpdrive/block/passive/ItemBlockDecorative.java +++ b/src/main/java/cr0s/warpdrive/block/passive/ItemBlockDecorative.java @@ -2,6 +2,7 @@ package cr0s.warpdrive.block.passive; import java.util.List; +import cr0s.warpdrive.data.DecorativeType; import net.minecraft.block.Block; import net.minecraft.creativetab.CreativeTabs; import net.minecraft.item.Item; @@ -22,17 +23,17 @@ public class ItemBlockDecorative extends ItemBlock { } @Override - public void getSubItems(Item par1, CreativeTabs par2CreativeTabs, List par3List) { - for (int i = 0; i < BlockDecorative.decorativeTypes.values().length; i++) { - par3List.add(new ItemStack(par1, 1, i)); + public void getSubItems(Item item, CreativeTabs creativeTabs, List list) { + for (DecorativeType decorativeType : DecorativeType.values()) { + list.add(new ItemStack(item, 1, decorativeType.ordinal())); } } @Override public String getUnlocalizedName(ItemStack itemstack) { - if (itemstack == null) + if (itemstack == null) { return getUnlocalizedName(); - return "tile.warpdrive.passive." + BlockDecorative.decorativeTypes.values()[itemstack.getItemDamage()].toString(); + } + return "tile.warpdrive.passive." + DecorativeType.get(itemstack.getItemDamage()).unlocalizedName; } - } diff --git a/src/main/java/cr0s/warpdrive/data/DecorativeType.java b/src/main/java/cr0s/warpdrive/data/DecorativeType.java new file mode 100644 index 00000000..c01f6c36 --- /dev/null +++ b/src/main/java/cr0s/warpdrive/data/DecorativeType.java @@ -0,0 +1,30 @@ +package cr0s.warpdrive.data; + +import java.util.HashMap; + +public enum DecorativeType { + PLAIN ("Plain"), + ENERGIZED ("Energized"), + NETWORK ("Network"); + + public final String unlocalizedName; + + // cached values + public static final int length; + private static final HashMap ID_MAP = new HashMap(); + + static { + length = DecorativeType.values().length; + for (DecorativeType componentType : values()) { + ID_MAP.put(componentType.ordinal(), componentType); + } + } + + private DecorativeType(String unlocalizedName) { + this.unlocalizedName = unlocalizedName; + } + + public static DecorativeType get(final int damage) { + return ID_MAP.get(damage); + } +}