From bc3224beda1292d6dad0e3d97e14a77d63e942f0 Mon Sep 17 00:00:00 2001 From: Trcx Date: Thu, 19 Mar 2015 00:27:34 -0500 Subject: [PATCH] improved the tool detection algorithm (closes #4) 1.0.3 jar build --- .../com/trcx/swapper/Common/Item/Swapper.java | 34 ++++++++++++------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/trcx/swapper/Common/Item/Swapper.java b/src/main/java/com/trcx/swapper/Common/Item/Swapper.java index 953722e..3276602 100644 --- a/src/main/java/com/trcx/swapper/Common/Item/Swapper.java +++ b/src/main/java/com/trcx/swapper/Common/Item/Swapper.java @@ -29,7 +29,9 @@ import net.minecraftforge.event.entity.player.EntityItemPickupEvent; import net.minecraftforge.oredict.ShapedOreRecipe; +import java.util.HashMap; import java.util.List; +import java.util.Map; import java.util.Set; /** @@ -54,6 +56,14 @@ public class Swapper extends Item implements IEnergyContainerItem{ public static final int swapperSlots = 5; + private static final HashMap testTools = new HashMap(); + static + { + testTools.put("pickaxe", new ItemStack(Items.wooden_pickaxe)); + testTools.put("shovel", new ItemStack(Items.wooden_shovel)); + testTools.put("axe", new ItemStack(Items.wooden_axe)); + } + public Swapper(){ super(); setCreativeTab(CreativeTabs.tabTools); @@ -124,6 +134,17 @@ public boolean onBlockStartBreak(ItemStack swapper, int X, int Y, int Z, EntityP public float getDigSpeed(ItemStack swapper, Block block, int metadata) { String effTool = block.getHarvestTool(metadata); ItemStack is; + if (effTool == null){ + for (Map.Entry testToolEntry : testTools.entrySet()) + { + ItemStack testTool = testToolEntry.getValue(); + if (testTool != null && testTool.getItem() instanceof ItemTool && testTool.func_150997_a(block) >= ((ItemTool) testTool.getItem()).func_150913_i().getEfficiencyOnProperMaterial()) + { + effTool = testToolEntry.getKey(); + break; + } + } + } if (effTool != null) { if (effTool.equals("shovel")) { is = getStack(slotSHOVEL, swapper); @@ -137,8 +158,6 @@ public float getDigSpeed(ItemStack swapper, Block block, int metadata) { } else { // have to handle vanilla not properly registering some blocks... if (block == Blocks.web) { is = getStack(slotSword, swapper); - } else if (block == Blocks.wooden_door || block == Blocks.trapdoor || block == Blocks.ladder || block == Blocks.fence || block == Blocks.fence_gate) { - is = getStack(slotAXE, swapper); } else { is = getStack(slotPICK, swapper); } @@ -153,16 +172,7 @@ public float getDigSpeed(ItemStack swapper, Block block, int metadata) { @Override public int getHarvestLevel(ItemStack swapper, String toolClass) { - ItemStack is; - if (toolClass.equals("shovel")){ - is = getStack(slotSHOVEL,swapper); - } else if (toolClass.equals("axe")) { - is = getStack(slotAXE, swapper); - } else if (toolClass.equals("sword")) { - is = getStack(slotSword, swapper); - } else { - is = getStack(slotPICK, swapper); - } + ItemStack is = getLastStack(swapper); if (is != null) { int ret = is.getItem().getHarvestLevel(is, toolClass); putLastStack(swapper,is);