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<String> instead of Map<String>, 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.

* .
This commit is contained in:
AlexanderC authored and GitHub committed 2024-07-30 19:18:21 +07:00
1 parent c2c2f14596
commit 1a97bff6ef
6 files changed
+89 -3

No files matched your search

+4
View File
@@ -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);
@@ -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;
@@ -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;
@@ -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;
@@ -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);
@@ -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<String, Set<String>> 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<String> 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<String> 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);
}
}