diff --git a/java/squeek/wailaharvestlevels/ModWailaHarvestLevels.java b/java/squeek/wailaharvestlevels/ModWailaHarvestLevels.java index 31c252c..a8a2cf4 100644 --- a/java/squeek/wailaharvestlevels/ModWailaHarvestLevels.java +++ b/java/squeek/wailaharvestlevels/ModWailaHarvestLevels.java @@ -1,5 +1,6 @@ package squeek.wailaharvestlevels; +import squeek.wailaharvestlevels.helpers.ToolHelper; import squeek.wailaharvestlevels.proxy.ProxyIguanaTweaks; import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.Mod; @@ -34,5 +35,7 @@ public class ModWailaHarvestLevels hasIguanaTweaks = true; ProxyIguanaTweaks.init(); } + + ToolHelper.init(); } } diff --git a/java/squeek/wailaharvestlevels/WailaHandler.java b/java/squeek/wailaharvestlevels/WailaHandler.java index 31b88fe..13e04dd 100644 --- a/java/squeek/wailaharvestlevels/WailaHandler.java +++ b/java/squeek/wailaharvestlevels/WailaHandler.java @@ -2,7 +2,10 @@ package squeek.wailaharvestlevels; import java.util.ArrayList; import java.util.List; +import squeek.wailaharvestlevels.helpers.BlockHelper; +import squeek.wailaharvestlevels.helpers.ColorHelper; import squeek.wailaharvestlevels.helpers.StringHelper; +import squeek.wailaharvestlevels.helpers.ToolHelper; import mcp.mobius.waila.api.IWailaConfigHandler; import mcp.mobius.waila.api.IWailaDataAccessor; import mcp.mobius.waila.api.IWailaDataProvider; @@ -10,8 +13,7 @@ import mcp.mobius.waila.api.IWailaRegistrar; import net.minecraft.block.Block; import net.minecraft.item.ItemStack; import net.minecraft.util.EnumChatFormatting; -import net.minecraftforge.common.ForgeHooks; -import net.minecraftforge.common.MinecraftForge; +import net.minecraft.util.StatCollector; import net.minecraftforge.oredict.OreDictionary; public class WailaHandler implements IWailaDataProvider @@ -31,95 +33,90 @@ public class WailaHandler implements IWailaDataProvider @Override public List getWailaBody(ItemStack itemStack, List toolTip, IWailaDataAccessor accessor, IWailaConfigHandler config) { - if (config.getConfig("mining.harvestlevel") || config.getConfig("mining.effectivetool")) + if (config.getConfig("harvestlevels.sneakingonly", false) && !accessor.getPlayer().isSneaking()) + return toolTip; + + if (config.getConfig("harvestlevels.toolrequiredonly") && accessor.getBlock().blockMaterial.isToolNotRequired()) + return toolTip; + + boolean showHarvestLevel = config.getConfig("harvestlevels.harvestlevel"); + boolean showEffectiveTool = config.getConfig("harvestlevels.effectivetool"); + boolean showCurrentlyHarvestable = config.getConfig("harvestlevels.currentlyharvestable"); + boolean showOresOnly = config.getConfig("harvestlevels.oresonly", false); + boolean minimalLayout = config.getConfig("harvestlevels.minimal", false); + boolean hideWhileHarvestable = config.getConfig("harvestlevels.unharvestableonly", false); + + if (showHarvestLevel || showEffectiveTool || showCurrentlyHarvestable) { int oreID = -1; - if (config.getConfig("mining.oresonly") && ((oreID = OreDictionary.getOreID(itemStack)) == -1 || !OreDictionary.getOreName(oreID).startsWith("ore"))) + if (showOresOnly && ((oreID = OreDictionary.getOreID(itemStack)) == -1 || !OreDictionary.getOreName(oreID).startsWith("ore"))) { return toolTip; } - EnumChatFormatting effectiveColor = EnumChatFormatting.DARK_RED; - EnumChatFormatting harvestColor = EnumChatFormatting.DARK_RED; - ItemStack itemHeld = accessor.getPlayer().getHeldItem(); - List harvestTypes = new ArrayList(); - int tinkersHarvestLevel = -1; - if (itemHeld != null) + String toolClasses[] = new String[] { "pickaxe", "shovel", "axe" }; + int harvestLevels[] = new int[toolClasses.length]; + boolean blockHasEffectiveTools = BlockHelper.getHarvestLevelsOf(accessor.getBlock(), accessor.getMetadata(), toolClasses, harvestLevels); + + if (!blockHasEffectiveTools) + return toolTip; + + int harvestLevel = -1; + String effectiveTool = ""; + int i = 0; + for (String toolClass : toolClasses) { - /* - if (itemHeld.getItem() instanceof HarvestTool) + if (harvestLevels[i] >= 0) { - try - { - Method getHarvestType = HarvestTool.class.getDeclaredMethod("getHarvestType"); - getHarvestType.setAccessible(true); - harvestTypes.add((String) getHarvestType.invoke(itemHeld.getItem())); - } - catch (Exception e) - { - e.printStackTrace(); - } - - if (itemHeld.getItem() instanceof DualHarvestTool) - { - try - { - Method getSecondHarvestType = DualHarvestTool.class.getDeclaredMethod("getSecondHarvestType"); - getSecondHarvestType.setAccessible(true); - harvestTypes.add((String) getSecondHarvestType.invoke(itemHeld.getItem())); - } - catch (Exception e) - { - e.printStackTrace(); - } - } - tinkersHarvestLevel = Math.max(ToolHelper.getPrimaryHarvestLevel(ToolHelper.getToolTag(itemHeld)), ToolHelper.getSecondaryHarvestLevel(ToolHelper.getToolTag(itemHeld))); - } - else*/ if (ForgeHooks.isToolEffective(itemHeld, accessor.getBlock(), accessor.getMetadata())) - { - effectiveColor = EnumChatFormatting.DARK_GREEN; - - if (ForgeHooks.canToolHarvestBlock(accessor.getBlock(), accessor.getMetadata(), itemHeld)) - { - harvestColor = EnumChatFormatting.DARK_GREEN; - } + harvestLevel = harvestLevels[i]; + effectiveTool = toolClass; + break; } + i++; } - int hlvl = MinecraftForge.getBlockHarvestLevel(accessor.getBlock(), accessor.getMetadata(), "pickaxe"); - String effectiveTool = "Pickaxe"; - int hlvlMin = 0; + boolean canHarvest = false; + boolean isEffective = false; + boolean isAboveMinHarvestLevel = false; + + ItemStack itemHeld = accessor.getPlayer().getHeldItem(); + if (itemHeld != null) + { + canHarvest = ToolHelper.canToolHarvestBlock(itemHeld, accessor.getBlock(), accessor.getMetadata()); + isAboveMinHarvestLevel = (showCurrentlyHarvestable || showHarvestLevel) && ToolHelper.canToolHarvestLevel(itemHeld, accessor.getBlock(), accessor.getMetadata(), harvestLevel); + isEffective = showEffectiveTool && ToolHelper.isToolEffectiveAgainst(itemHeld, accessor.getBlock(), accessor.getMetadata(), effectiveTool); + } + + boolean isCurrentlyHarvestable = canHarvest && isAboveMinHarvestLevel; + + if (hideWhileHarvestable && isCurrentlyHarvestable) + return toolTip; - if (hlvl == -1) - { - hlvlMin = 1; + if (!minimalLayout) + { + if (showCurrentlyHarvestable) + toolTip.add(ColorHelper.getBooleanColor(isCurrentlyHarvestable) + (isCurrentlyHarvestable ? "\u2714" : "\u2718") + EnumChatFormatting.RESET + " Currently Harvestable"); + if (harvestLevel != -1 && showEffectiveTool) + toolTip.add("Effective Tool : " + ColorHelper.getBooleanColor(isEffective && canHarvest, isEffective && !canHarvest) + StatCollector.translateToLocal("harvestlevels.toolclass." + effectiveTool)); + if (harvestLevel >= 1 && showHarvestLevel) + toolTip.add("Harvest Level : " + ColorHelper.getBooleanColor(isAboveMinHarvestLevel && canHarvest) + StringHelper.getHarvestLevelName(harvestLevel)); + } + else + { + List stringParts = new ArrayList(); + + if (showCurrentlyHarvestable) + stringParts.add(ColorHelper.getBooleanColor(isCurrentlyHarvestable) + (isCurrentlyHarvestable ? "\u2714" : "\u2718")); + if (harvestLevel != -1 && showEffectiveTool) + stringParts.add(ColorHelper.getBooleanColor(isEffective && canHarvest, isEffective && !canHarvest) + StatCollector.translateToLocal("harvestlevels.toolclass." + effectiveTool)); + if (harvestLevel >= 1 && showHarvestLevel) + stringParts.add(ColorHelper.getBooleanColor(isAboveMinHarvestLevel && canHarvest) + StringHelper.getHarvestLevelName(harvestLevel)); - hlvl = MinecraftForge.getBlockHarvestLevel(accessor.getBlock(), accessor.getMetadata(), "shovel"); - effectiveTool = "Shovel"; - - if (hlvl == -1) + if (!stringParts.isEmpty()) { - hlvl = MinecraftForge.getBlockHarvestLevel(accessor.getBlock(), accessor.getMetadata(), "axe"); - effectiveTool = "Axe"; + toolTip.add(StringHelper.concatenateStringList(stringParts, EnumChatFormatting.RESET + " : ")); } - } - - if (harvestTypes.contains(effectiveTool.toLowerCase())) - { - boolean isEffectiveButCantHarvest = itemHeld != null && !itemHeld.canHarvestBlock(accessor.getBlock()); - if (isEffectiveButCantHarvest) - effectiveColor = EnumChatFormatting.RED; - else - effectiveColor = EnumChatFormatting.DARK_GREEN; - } - if (tinkersHarvestLevel >= hlvl) - harvestColor = EnumChatFormatting.DARK_GREEN; - - if (hlvl >= hlvlMin && config.getConfig("mining.harvestlevel")) - toolTip.add("Harvest Level : "+harvestColor+StringHelper.getHarvestLevelName(hlvl)); - if (hlvl != -1 && config.getConfig("mining.effectivetool")) - toolTip.add("Effective Tool : "+effectiveColor+effectiveTool); - toolTip.addAll(harvestTypes); + } } return toolTip; @@ -134,9 +131,14 @@ public class WailaHandler implements IWailaDataProvider public static void callbackRegister(IWailaRegistrar registrar) { WailaHandler instance = new WailaHandler(); - registrar.addConfig("Mining", "mining.harvestlevel", "Show harvest level"); - registrar.addConfig("Mining", "mining.effectivetool", "Show effective tool"); - registrar.addConfig("Mining", "mining.oresonly", "Only show on ore blocks"); + registrar.addConfig("HarvestLevels", "harvestlevels.harvestlevel", "option.harvestlevels.harvestlevel"); + registrar.addConfig("HarvestLevels", "harvestlevels.effectivetool", "option.harvestlevels.effectivetool"); + registrar.addConfig("HarvestLevels", "harvestlevels.currentlyharvestable", "option.harvestlevels.currentlyharvestable"); + registrar.addConfig("HarvestLevels", "harvestlevels.oresonly", "option.harvestlevels.oresonly"); + registrar.addConfig("HarvestLevels", "harvestlevels.sneakingonly", "option.harvestlevels.sneakingonly"); + registrar.addConfig("HarvestLevels", "harvestlevels.minimal", "option.harvestlevels.minimal"); + registrar.addConfig("HarvestLevels", "harvestlevels.unharvestableonly", "option.harvestlevels.unharvestableonly"); + registrar.addConfig("HarvestLevels", "harvestlevels.toolrequiredonly", "option.harvestlevels.toolrequiredonly"); registrar.registerBodyProvider(instance, Block.class); } } \ No newline at end of file diff --git a/java/squeek/wailaharvestlevels/helpers/BlockHelper.java b/java/squeek/wailaharvestlevels/helpers/BlockHelper.java new file mode 100644 index 0000000..2505423 --- /dev/null +++ b/java/squeek/wailaharvestlevels/helpers/BlockHelper.java @@ -0,0 +1,25 @@ +package squeek.wailaharvestlevels.helpers; + +import net.minecraft.block.Block; +import net.minecraftforge.common.MinecraftForge; + +public class BlockHelper +{ + + public static boolean getHarvestLevelsOf(Block block, int metadata, String[] toolClasses, int[] harvestLevels) + { + int i = 0; + boolean hasEffectiveTools = false; + for (String toolClass : toolClasses) + { + harvestLevels[i] = MinecraftForge.getBlockHarvestLevel(block, metadata, toolClass); + + if (harvestLevels[i] != -1) + hasEffectiveTools = true; + + i++; + } + return hasEffectiveTools; + } + +} diff --git a/java/squeek/wailaharvestlevels/helpers/ColorHelper.java b/java/squeek/wailaharvestlevels/helpers/ColorHelper.java index b3f446c..3c0b912 100644 --- a/java/squeek/wailaharvestlevels/helpers/ColorHelper.java +++ b/java/squeek/wailaharvestlevels/helpers/ColorHelper.java @@ -13,6 +13,13 @@ public class ColorHelper EnumChatFormatting.GREEN, EnumChatFormatting.AQUA }; + + private static final EnumChatFormatting booleanColorRange[] = { + EnumChatFormatting.DARK_RED, + EnumChatFormatting.RED, + EnumChatFormatting.DARK_GREEN, + EnumChatFormatting.GREEN + }; public static String getRelativeColor(double val, double min, double max) { @@ -26,4 +33,14 @@ public class ColorHelper int index = (int) (((val - min) / (max - min)) * (colorRange.length - 1)); return colorRange[Math.max(0, Math.min(colorRange.length - 1, index))].toString(); } + + public static String getBooleanColor(boolean val) + { + return getBooleanColor(val, false); + } + + public static String getBooleanColor(boolean val, boolean modified) + { + return booleanColorRange[(val ? 2 : 0) + (modified ? 1 : 0)].toString(); + } } \ No newline at end of file diff --git a/java/squeek/wailaharvestlevels/helpers/StringHelper.java b/java/squeek/wailaharvestlevels/helpers/StringHelper.java index 9b17b01..0d4fe73 100644 --- a/java/squeek/wailaharvestlevels/helpers/StringHelper.java +++ b/java/squeek/wailaharvestlevels/helpers/StringHelper.java @@ -1,6 +1,7 @@ package squeek.wailaharvestlevels.helpers; import java.util.HashMap; +import java.util.List; import squeek.wailaharvestlevels.ModWailaHarvestLevels; import squeek.wailaharvestlevels.proxy.ProxyIguanaTweaks; import net.minecraft.util.StatCollector; @@ -54,4 +55,14 @@ public class StringHelper } return localized; } + + public static String concatenateStringList(List strings, String separator) { + StringBuilder sb = new StringBuilder(); + String sep = ""; + for(String s: strings) { + sb.append(sep).append(s); + sep = separator; + } + return sb.toString(); + } } diff --git a/java/squeek/wailaharvestlevels/helpers/ToolHelper.java b/java/squeek/wailaharvestlevels/helpers/ToolHelper.java index bf0d40c..d610eff 100644 --- a/java/squeek/wailaharvestlevels/helpers/ToolHelper.java +++ b/java/squeek/wailaharvestlevels/helpers/ToolHelper.java @@ -1,14 +1,46 @@ package squeek.wailaharvestlevels.helpers; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import cpw.mods.fml.common.Loader; +import net.minecraft.block.Block; +import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; +import net.minecraftforge.common.ForgeHooks; public class ToolHelper { + private static Class HarvestTool = null; + private static Class DualHarvestTool = null; + private static Method getHarvestType = null; + private static Method getSecondHarvestType = null; + public static boolean tinkersConstructLoaded = false; public static void init() { - + if (Loader.isModLoaded("TConstruct")) + { + try + { + HarvestTool = Class.forName("tconstruct.library.tools.HarvestTool"); + DualHarvestTool = Class.forName("tconstruct.library.tools.DualHarvestTool"); + getHarvestType = HarvestTool.getDeclaredMethod("getHarvestType"); + getSecondHarvestType = DualHarvestTool.getDeclaredMethod("getSecondHarvestType"); + getHarvestType.setAccessible(true); + getSecondHarvestType.setAccessible(true); + tinkersConstructLoaded = true; + } + catch (ClassNotFoundException e) + { + return; + } + catch (NoSuchMethodException e) + { + return; + } + } } public static boolean hasToolTag(ItemStack itemStack) @@ -18,8 +50,9 @@ public class ToolHelper public static NBTTagCompound getToolTag(ItemStack tool) { - NBTTagCompound tag; - tag = tool.getTagCompound().getCompoundTag("InfiTool"); + NBTTagCompound tag = null; + if (tool.hasTagCompound()) + tag = tool.getTagCompound().getCompoundTag("InfiTool"); return tag; } @@ -32,5 +65,58 @@ public class ToolHelper { return toolTag.getInteger("HarvestLevel2"); } + + public static boolean isToolEffectiveAgainst(ItemStack tool, Block block, int metadata, String effectiveToolClass) + { + if (tinkersConstructLoaded && HarvestTool.isInstance(tool.getItem())) + { + Item item = tool.getItem(); + List harvestTypes = new ArrayList(); + try + { + harvestTypes.add((String) getHarvestType.invoke(item)); + } + catch (Exception e) + { + e.printStackTrace(); + tinkersConstructLoaded = false; + } + + if (DualHarvestTool.isInstance(item)) + { + try + { + harvestTypes.add((String) getSecondHarvestType.invoke(item)); + } + catch (Exception e) + { + e.printStackTrace(); + tinkersConstructLoaded = false; + } + } + + return harvestTypes.contains(effectiveToolClass); + } + return ForgeHooks.isToolEffective(tool, block, metadata); + } + + public static boolean canToolHarvestLevel(ItemStack tool, Block block, int metadata, int harvestLevel) + { + boolean canTinkersToolHarvestBlock = false; + + NBTTagCompound toolTag = ToolHelper.getToolTag(tool); + if (toolTag != null) + { + int toolHarvestLevel = Math.max(ToolHelper.getPrimaryHarvestLevel(toolTag), ToolHelper.getSecondaryHarvestLevel(toolTag)); + canTinkersToolHarvestBlock = toolHarvestLevel >= harvestLevel; + } + + return canTinkersToolHarvestBlock || ForgeHooks.canToolHarvestBlock(block, metadata, tool); + } + + public static boolean canToolHarvestBlock(ItemStack tool, Block block, int metadata) + { + return tool.canHarvestBlock(block); + } } diff --git a/resources/assets/wailaharvestlevels/lang/en_US.lang b/resources/assets/wailaharvestlevels/lang/en_US.lang new file mode 100644 index 0000000..4463c39 --- /dev/null +++ b/resources/assets/wailaharvestlevels/lang/en_US.lang @@ -0,0 +1,14 @@ +# tools +harvestlevels.toolclass.pickaxe=Pickaxe +harvestlevels.toolclass.shovel=Shovel +harvestlevels.toolclass.axe=Axe + +# options +option.harvestlevels.harvestlevel=Harvest level +option.harvestlevels.effectivetool=Effective tool +option.harvestlevels.currentlyharvestable=Currently harvestable +option.harvestlevels.oresonly=Only ores +option.harvestlevels.sneakingonly=Only while sneaking +option.harvestlevels.minimal=Minimal layout +option.harvestlevels.unharvestableonly=Unharvestable only +option.harvestlevels.toolrequiredonly=Tool required only \ No newline at end of file