diff --git a/src/main/java/tconstruct/TConstruct.java b/src/main/java/tconstruct/TConstruct.java index 31f41539..51b2c156 100644 --- a/src/main/java/tconstruct/TConstruct.java +++ b/src/main/java/tconstruct/TConstruct.java @@ -1,5 +1,6 @@ package tconstruct; +import java.io.File; import java.util.Map; import java.util.Random; @@ -45,6 +46,7 @@ import tconstruct.library.TConstructCreativeTab; import tconstruct.library.TConstructRegistry; import tconstruct.library.crafting.Detailing; import tconstruct.library.crafting.LiquidCasting; +import tconstruct.library.util.AoEExclusionList; import tconstruct.mechworks.TinkerMechworks; import tconstruct.mechworks.landmine.behavior.Behavior; import tconstruct.mechworks.landmine.behavior.stackCombo.SpecialStackHandler; @@ -183,6 +185,8 @@ public class TConstruct { basinCasting = new LiquidCasting(); chiselDetailing = new Detailing(); + AoEExclusionList.init(new File(event.getModConfigurationDirectory(), "TConstruct_AOEExclusions.cfg")); + playerTracker = new TPlayerHandler(); FMLCommonHandler.instance().bus().register(playerTracker); MinecraftForge.EVENT_BUS.register(playerTracker); diff --git a/src/main/java/tconstruct/items/tools/Excavator.java b/src/main/java/tconstruct/items/tools/Excavator.java index 5aef52db..52a6e58a 100644 --- a/src/main/java/tconstruct/items/tools/Excavator.java +++ b/src/main/java/tconstruct/items/tools/Excavator.java @@ -13,6 +13,11 @@ public class Excavator extends AOEHarvestTool { this.setUnlocalizedName("InfiTool.Excavator"); } + @Override + protected String getAOEToolName() { + return "excavator"; + } + @Override protected Material[] getEffectiveMaterials() { return materials; diff --git a/src/main/java/tconstruct/items/tools/Hammer.java b/src/main/java/tconstruct/items/tools/Hammer.java index ec3fcd2a..2955063a 100644 --- a/src/main/java/tconstruct/items/tools/Hammer.java +++ b/src/main/java/tconstruct/items/tools/Hammer.java @@ -21,6 +21,11 @@ public class Hammer extends AOEHarvestTool { this.setUnlocalizedName("InfiTool.Hammer"); } + @Override + protected String getAOEToolName() { + return "hammer"; + } + @Override public int getPartAmount() { return 4; diff --git a/src/main/java/tconstruct/items/tools/LumberAxe.java b/src/main/java/tconstruct/items/tools/LumberAxe.java index d20141d0..b7d85dbe 100644 --- a/src/main/java/tconstruct/items/tools/LumberAxe.java +++ b/src/main/java/tconstruct/items/tools/LumberAxe.java @@ -35,6 +35,11 @@ public class LumberAxe extends AOEHarvestTool { this.setUnlocalizedName("InfiTool.LumberAxe"); } + @Override + protected String getAOEToolName() { + return "lumberaxe"; + } + @Override protected Material[] getEffectiveMaterials() { return materials; diff --git a/src/main/java/tconstruct/library/tools/AOEHarvestTool.java b/src/main/java/tconstruct/library/tools/AOEHarvestTool.java index cbeccbb2..a73fa708 100644 --- a/src/main/java/tconstruct/library/tools/AOEHarvestTool.java +++ b/src/main/java/tconstruct/library/tools/AOEHarvestTool.java @@ -6,6 +6,8 @@ import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.util.MovingObjectPosition; +import tconstruct.library.util.AoEExclusionList; + public abstract class AOEHarvestTool extends HarvestTool { public int breakRadius; @@ -18,9 +20,14 @@ public abstract class AOEHarvestTool extends HarvestTool { this.breakDepth = breakDepth; } + protected String getAOEToolName() { + return "tool." + getToolName().toLowerCase(); + } + @Override public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPlayer player) { - // only effective materials matter. We don't want to aoe when beraking dirt with a hammer. + // only effective materials matter. We don't want to aoe when breaking dirt with a hammer. + String toolName = "tool." + getAOEToolName().toLowerCase(); Block block = player.worldObj.getBlock(x, y, z); int meta = player.worldObj.getBlockMetadata(x, y, z); if (block == null || !isEffective(block, meta) || !stack.hasTagCompound()) @@ -64,8 +71,13 @@ public abstract class AOEHarvestTool extends HarvestTool { // don't break the originally already broken block, duh if (xPos == x && yPos == y && zPos == z) continue; - if (!super.onBlockStartBreak(stack, xPos, yPos, zPos, player)) - breakExtraBlock(player.worldObj, xPos, yPos, zPos, sideHit, player, x, y, z); + Block targetBlock = player.worldObj.getBlock(xPos, yPos, zPos); + int targetMeta = player.worldObj.getBlockMetadata(xPos, yPos, zPos); + + if (!AoEExclusionList.isBlockExcluded(toolName, targetBlock, targetMeta)) { + if (!super.onBlockStartBreak(stack, xPos, yPos, zPos, player)) + breakExtraBlock(player.worldObj, xPos, yPos, zPos, sideHit, player, x, y, z); + } } return super.onBlockStartBreak(stack, x, y, z, player); diff --git a/src/main/java/tconstruct/library/util/AoEExclusionList.java b/src/main/java/tconstruct/library/util/AoEExclusionList.java new file mode 100644 index 00000000..89a31e3c --- /dev/null +++ b/src/main/java/tconstruct/library/util/AoEExclusionList.java @@ -0,0 +1,55 @@ +package tconstruct.library.util; + +import java.io.File; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + +import net.minecraft.block.Block; +import net.minecraftforge.common.config.Configuration; + +public class AoEExclusionList { + + private static final Map> toolExclusionLists = new HashMap<>(); + private static Configuration config; + + public static void init(File configFile) { + config = new Configuration(configFile); + loadConfig(); + } + + private static void loadConfig() { + config.load(); + + String[] tools = { "tool.hammer", "tool.excavator", "tool.lumberaxe" }; + for (String tool : tools) { + String[] exclusionArray = config.getStringList( + tool + "Exclusions", + "AOE_Exclusions", + new String[] { "examplemod:exampleblock", "examplemod:exampleblock:1" }, + "Block IDs (with optional metadata) that should not be broken by " + tool + "'s AOE effect"); + Set exclusionSet = new HashSet<>(Arrays.asList(exclusionArray)); + toolExclusionLists.put(tool, exclusionSet); + } + + if (config.hasChanged()) { + config.save(); + } + } + + public static boolean isBlockExcluded(String tool, Block block, int metadata) { + Set exclusions = toolExclusionLists.get(tool); + if (exclusions == null) { + exclusions = toolExclusionLists.get("tool." + tool); + } + + if (exclusions == null || exclusions.isEmpty()) { + return false; + } + + String blockId = Block.blockRegistry.getNameForObject(block); + return exclusions.contains(blockId) || exclusions.contains(blockId + ":" + metadata); + } +}