Skip to content

Commit

Permalink
fix: 🐛 Fix tool swapper upgrade to work correctly with tools that sha…
Browse files Browse the repository at this point in the history
…re the same base item with something that's not a tool (Tetra hammer)
  • Loading branch information
P3pp3rF1y committed Dec 2, 2023
1 parent bba168c commit 96fd77d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
org.gradle.daemon=false
minecraft_version=1.19.2
forge_version=43.2.13
mod_version=3.19.1
mod_version=3.19.2
jei_mc_version=1.19.2-forge
jei_version=11.6.0.+
curios_version=1.19.2-5.1.1.+
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package net.p3pp3rf1y.sophisticatedbackpacks.upgrades.toolswapper;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Multimap;
import com.google.common.util.concurrent.AtomicDouble;
import net.minecraft.core.BlockPos;
Expand Down Expand Up @@ -50,6 +53,7 @@
import java.util.LinkedList;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
Expand All @@ -60,7 +64,14 @@
public class ToolSwapperUpgradeWrapper extends UpgradeWrapperBase<ToolSwapperUpgradeWrapper, ToolSwapperUpgradeItem>
implements IBlockClickResponseUpgrade, IAttackEntityResponseUpgrade, IBlockToolSwapUpgrade, IEntityToolSwapUpgrade {

private static final Set<Item> notTools = new HashSet<>();
private static final LoadingCache<ItemStack, Boolean> isToolCache = CacheBuilder.newBuilder().expireAfterAccess(1, TimeUnit.MINUTES).build(
new CacheLoader<>() {
@Override
public Boolean load(ItemStack key) {
return canPerformToolAction(key);
}
}
);

private final FilterLogic filterLogic;
@Nullable
Expand Down Expand Up @@ -163,26 +174,16 @@ public boolean onAttackEntity(Player player) {
}

private boolean isNotTool(ItemStack stack) {
if (notTools.contains(stack.getItem())) {
return true;
}

if (canPerformToolAction(stack)) {
return false;
}

notTools.add(stack.getItem());

return true;
return !isToolCache.getUnchecked(stack);
}

private boolean canPerformToolAction(ItemStack stack) {
private static boolean canPerformToolAction(ItemStack stack) {
return canPerformAnyAction(stack, ToolActions.DEFAULT_AXE_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_HOE_ACTIONS)
|| canPerformAnyAction(stack, ToolActions.DEFAULT_PICKAXE_ACTIONS) || canPerformAnyAction(stack, ToolActions.DEFAULT_SHOVEL_ACTIONS)
|| canPerformAnyAction(stack, ToolActions.DEFAULT_SHEARS_ACTIONS);
}

private boolean canPerformAnyAction(ItemStack stack, Set<ToolAction> toolActions) {
private static boolean canPerformAnyAction(ItemStack stack, Set<ToolAction> toolActions) {
for (ToolAction toolAction : toolActions) {
if (stack.canPerformAction(toolAction)) {
return true;
Expand Down

0 comments on commit 96fd77d

Please sign in to comment.