Skip to content

Commit

Permalink
Implementation of AoE Blacklist for Tinker's Construct Tools (#121)
Browse files Browse the repository at this point in the history
* 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.

* .
  • Loading branch information
AlexanderChenhall authored Jul 30, 2024
1 parent c2c2f14 commit 1a97bff
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/main/java/tconstruct/TConstruct.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package tconstruct;

import java.io.File;
import java.util.Map;
import java.util.Random;

Expand Down Expand Up @@ -45,6 +46,7 @@
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;
Expand Down Expand Up @@ -183,6 +185,8 @@ public void preInit(FMLPreInitializationEvent event) {
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);
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tconstruct/items/tools/Excavator.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public Excavator() {
this.setUnlocalizedName("InfiTool.Excavator");
}

@Override
protected String getAOEToolName() {
return "excavator";
}

@Override
protected Material[] getEffectiveMaterials() {
return materials;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tconstruct/items/tools/Hammer.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public Hammer() {
this.setUnlocalizedName("InfiTool.Hammer");
}

@Override
protected String getAOEToolName() {
return "hammer";
}

@Override
public int getPartAmount() {
return 4;
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/tconstruct/items/tools/LumberAxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public LumberAxe() {
this.setUnlocalizedName("InfiTool.LumberAxe");
}

@Override
protected String getAOEToolName() {
return "lumberaxe";
}

@Override
protected Material[] getEffectiveMaterials() {
return materials;
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/tconstruct/library/tools/AOEHarvestTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.MovingObjectPosition;

import tconstruct.library.util.AoEExclusionList;

public abstract class AOEHarvestTool extends HarvestTool {

public int breakRadius;
Expand All @@ -18,9 +20,14 @@ public AOEHarvestTool(int baseDamage, int breakRadius, int breakDepth) {
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())
Expand Down Expand Up @@ -64,8 +71,13 @@ public boolean onBlockStartBreak(ItemStack stack, int x, int y, int z, EntityPla
// 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);
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/tconstruct/library/util/AoEExclusionList.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 1a97bff

Please sign in to comment.