From 1a97bff6ef9e4318405bf91ec448bd83d658062a Mon Sep 17 00:00:00 2001 From: AlexanderC <133077173+AlexanderChenhall@users.noreply.github.com> Date: Tue, 30 Jul 2024 22:18:21 +1000 Subject: [PATCH] Implementation of AoE Blacklist for Tinker's Construct Tools (#121) * Implement AoE exclusion list for Tinkers' Construct tools - Created AoEExclusionList class in tconstruct.library.util package - Added config file loading for tool-specific block exclusions - Modified AOEHarvestTool class to check exclusions before breaking blocks - Updated Hammer class to use new exclusion system - Added debug logging for troubleshooting This change allows users to specify blocks that should not be broken by the AoE effect of tools like the Hammer, while still allowing the targeted block to be broken. * Implement AoE exclusion list for Tinkers' Construct tools - Created AoEExclusionList class in tconstruct.library.util package - Added config file loading for tool-specific block exclusions - Modified AOEHarvestTool class to check exclusions before breaking blocks - Updated Hammer class to use new exclusion system - Added debug logging for troubleshooting This change allows users to specify blocks that should not be broken by the AoE effect of tools like the Hammer, while still allowing the targeted block to be broken. * Update README.md * Removed/Commented debug. * Gradle Clean * Removed missed debug message Added example formatting for config file. Inlined Variable * Added the license back, oops * Removed redundant variable * Brought back toolName, used proper getAOEToolName variable, intended for caching the name instead of calling the method later on. Changed function call to simply toolName in relation to this. * Removed now redundant if statements. * Spotless Apply * Changed to use Set instead of Map, and other appropriate changes made so other parts still work properly & SpotlesApply * Reinstated original README * Changed to include metadata (optionally) in the blacklist. * Changed to include metadata (optionally) in the blacklist. * Removed bad import. * . --- src/main/java/tconstruct/TConstruct.java | 4 ++ .../tconstruct/items/tools/Excavator.java | 5 ++ .../java/tconstruct/items/tools/Hammer.java | 5 ++ .../tconstruct/items/tools/LumberAxe.java | 5 ++ .../library/tools/AOEHarvestTool.java | 18 +++++- .../library/util/AoEExclusionList.java | 55 +++++++++++++++++++ 6 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 src/main/java/tconstruct/library/util/AoEExclusionList.java 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); + } +}