fix: 搜索不加载区块;背包嵌套深度有界
Build / build (push) Successful in 14m16s
Build / publish (push) Successful in 9m41s

Fixes #2 #3

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
layue13andCursor committed 2026-11-16 00:39:58 +08:00
1 parent 652a0f42e1
commit 45e4b9599f
2 files changed
+125

No files matched your search

View File
Whitespace-only changes.
@@ -0,0 +1,125 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Stratum <stratum@localhost>
Date: Mon, 16 Nov 2026 00:39:58 +0800
Subject: [PATCH] fix: do not force-load chunks; bound nested inventory search
Search is QoL: skip missing chunks, hard-cap radius at 64, and limit backpack nesting depth.
Co-authored-by: Cursor <[email protected]>
---
.../java/com/gtnh/findit/FindItConfig.java | 7 +++--
.../service/itemfinder/FindItemRequest.java | 29 +++++++++++++++----
.../java/com/gtnh/findit/util/WorldUtils.java | 16 +++++++++-
3 files changed, 43 insertions(+), 9 deletions(-)
diff --git a/src/main/java/com/gtnh/findit/FindItConfig.java b/src/main/java/com/gtnh/findit/FindItConfig.java
index 9527ab7..a9bb107 100644
--- a/src/main/java/com/gtnh/findit/FindItConfig.java
+++ b/src/main/java/com/gtnh/findit/FindItConfig.java
@@ -5,12 +5,15 @@ import com.gtnewhorizon.gtnhlib.config.Config;
@Config(modid = "findit")
public class FindItConfig {
- @Config.Comment("Radius to search within")
+ @Config.Comment("Radius to search within. Hard-capped so search cannot force-load distant chunks.")
@Config.DefaultInt(16)
- @Config.RangeInt(min = 1)
+ @Config.RangeInt(min = 1, max = 64)
@Config.Name("SearchRadius")
public static int SEARCH_RADIUS;
+ /** Absolute ceiling even if the config file is hand-edited past {@link #SEARCH_RADIUS}'s RangeInt. */
+ public static final int SEARCH_RADIUS_HARD_MAX = 64;
+
@Config.Comment("Search cooldown in ticks")
@Config.DefaultInt(10)
@Config.RangeInt(min = 0)
diff --git a/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java b/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java
index 882c826..c3d25dc 100644
--- a/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java
+++ b/src/main/java/com/gtnh/findit/service/itemfinder/FindItemRequest.java
@@ -23,6 +23,10 @@ import io.netty.buffer.ByteBuf;
public class FindItemRequest implements IMessage {
+ /** Nested backpack / cover inventories: bound depth so a cycle cannot StackOverflow. */
+ private static final int MAX_NESTED_INVENTORY_DEPTH = 8;
+ private static final ThreadLocal<Integer> NEST_DEPTH = new ThreadLocal<>();
+
private ItemStack targetStack;
private FluidStack targetFluidStack;
private boolean highlightingEmptyItemStacks;
@@ -75,14 +79,27 @@ public class FindItemRequest implements IMessage {
public boolean isStackSatisfies(EntityPlayer player, ItemStack stack) {
if (stack == null || !this.highlightingEmptyItemStacks && stack.stackSize == 0) return false;
- for (IStackFilterProvider provider : FindIt.INSTANCE.pluginsList) {
- IStackFilter filter = provider.getFilter(player, stack);
- if (filter != null && filter.matches(this)) {
- return true;
+ Integer depthBox = NEST_DEPTH.get();
+ int depth = depthBox == null ? 0 : depthBox;
+ if (depth >= MAX_NESTED_INVENTORY_DEPTH) {
+ return false;
+ }
+ NEST_DEPTH.set(depth + 1);
+ try {
+ for (IStackFilterProvider provider : FindIt.INSTANCE.pluginsList) {
+ IStackFilter filter = provider.getFilter(player, stack);
+ if (filter != null && filter.matches(this)) {
+ return true;
+ }
+ }
+ return false;
+ } finally {
+ if (depth == 0) {
+ NEST_DEPTH.remove();
+ } else {
+ NEST_DEPTH.set(depth);
}
}
-
- return false;
}
public boolean isTileSatisfies(EntityPlayer player, TileEntity tileEntity) {
diff --git a/src/main/java/com/gtnh/findit/util/WorldUtils.java b/src/main/java/com/gtnh/findit/util/WorldUtils.java
index 0be8bd8..c48f905 100644
--- a/src/main/java/com/gtnh/findit/util/WorldUtils.java
+++ b/src/main/java/com/gtnh/findit/util/WorldUtils.java
@@ -10,13 +10,23 @@ import net.minecraft.world.ChunkPosition;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
+import com.gtnh.findit.FindItConfig;
+
public class WorldUtils {
public static List<TileEntity> getTileEntitiesAround(Entity entity, int r) {
+ int radius = Math.min(Math.max(r, 1), FindItConfig.SEARCH_RADIUS_HARD_MAX);
int x = (int) Math.floor(entity.posX);
int y = (int) entity.posY;
int z = (int) Math.floor(entity.posZ);
- return getTileEntitiesInRegion(entity.worldObj, x - r, y - r, z - r, x + r + 1, y + r + 1, z + r + 1);
+ return getTileEntitiesInRegion(
+ entity.worldObj,
+ x - radius,
+ y - radius,
+ z - radius,
+ x + radius + 1,
+ y + radius + 1,
+ z + radius + 1);
}
/**
@@ -31,6 +41,10 @@ public class WorldUtils {
for (int cx = x1 >> 4; cx <= cx1; cx++) {
for (int cz = z1 >> 4; cz <= cz1; cz++) {
+ // QoL search must not provideChunk / force-load.
+ if (!world.getChunkProvider().chunkExists(cx, cz)) {
+ continue;
+ }
Chunk chunk = world.getChunkFromChunkCoords(cx, cz);
if (chunk != null) {
@SuppressWarnings("unchecked")