Claim protection from a local index: revision-polled panel endpoint, no per-block network checks

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01DjMbLQujBHunCCu5GpsHaT
This commit is contained in:
Claude committed 2026-09-30 17:51:05 +00:00
1 parent 78da10364f
commit 5defda1128
10 files changed
+442 -127

No files matched your search

@@ -8,19 +8,63 @@ import java.time.Duration;
public final class PanelClient {
private volatile Settings settings;
private record ChunkKey(String dimension, int x, int z, java.util.UUID uuid) {}
private record Cached(ChunkCheckResult result, long expires) {}
private final java.util.Map<ChunkKey, Cached> claims = new java.util.concurrent.ConcurrentHashMap<>();
private final java.util.Set<ChunkKey> pendingClaims = java.util.concurrent.ConcurrentHashMap.newKeySet();
private final java.util.Set<ChunkKey> pendingSnapshots = java.util.concurrent.ConcurrentHashMap.newKeySet();
private final java.util.concurrent.ThreadPoolExecutor claimWorker = new java.util.concurrent.ThreadPoolExecutor(
2, 2, 0, java.util.concurrent.TimeUnit.SECONDS, new java.util.concurrent.ArrayBlockingQueue<>(64),
task -> { Thread t = new Thread(task, "scopenet-claims"); t.setDaemon(true); return t; });
private static final ChunkCheckResult UNKNOWN = new ChunkCheckResult(true, false, "Protection check pending or unavailable", "WAIT");
private final java.util.concurrent.atomic.AtomicLong claimGeneration = new java.util.concurrent.atomic.AtomicLong();
public void invalidateClaims() { claimGeneration.incrementAndGet(); claims.clear(); }
public void updateSettings(Settings next) { settings = next; invalidateClaims(); }
public void close() { claimWorker.shutdownNow(); }
private final ClaimIndex claimIndex = new ClaimIndex();
private final java.util.concurrent.ScheduledExecutorService claimSync = java.util.concurrent.Executors.newSingleThreadScheduledExecutor(task -> {
Thread t = new Thread(task, "scopenet-claims"); t.setDaemon(true); return t;
});
private final java.util.concurrent.atomic.AtomicBoolean claimRefreshQueued = new java.util.concurrent.atomic.AtomicBoolean();
private volatile boolean claimSyncStarted;
private volatile long claimFailureLogged;
private volatile java.util.function.Consumer<String> claimLog = m -> {};
public void updateSettings(Settings next) { settings = next; requestClaimRefresh(); }
public void close() { claimSync.shutdownNow(); }
public ClaimIndex claims() { return claimIndex; }
/**
* Keep the local claim index fresh: load it now (so protection is on from
* the first tick) and then check the panel's revision every few seconds.
*/
public void startClaimSync(java.util.function.Consumer<String> log) {
if (claimSyncStarted) return;
claimSyncStarted = true;
claimLog = log;
claimSync.scheduleWithFixedDelay(this::refreshClaimsQuietly, 0, 5, java.util.concurrent.TimeUnit.SECONDS);
}
/** Pull a fresh index soon, e.g. right after this server changed a claim. */
public void requestClaimRefresh() {
if (!claimSyncStarted || !claimRefreshQueued.compareAndSet(false, true)) return;
try { claimSync.execute(() -> { claimRefreshQueued.set(false); refreshClaimsQuietly(); }); }
catch (java.util.concurrent.RejectedExecutionException e) { claimRefreshQueued.set(false); }
}
/** One synchronous refresh. Returns true when the index changed. */
public boolean refreshClaims() throws IOException, InterruptedException {
JsonObject body = new JsonObject();
String revision = claimIndex.revision();
if (revision != null) body.addProperty("revision", revision);
return claimIndex.apply(post("guilds/claim-index", body));
}
private void refreshClaimsQuietly() {
if (!settings.guildsEnabled() || !settings.landClaimingEnabled()) return;
try {
boolean first = !claimIndex.loaded();
boolean changed = refreshClaims();
if (first && changed) claimLog.accept("SCOPENET claim protection loaded (" + claimIndex.claimCount() + " claimed chunks)");
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
} catch (Exception e) {
// Keep enforcing the last copy we have; just don't spam the log.
long now = System.nanoTime();
if (now - claimFailureLogged > java.util.concurrent.TimeUnit.MINUTES.toNanos(5)) {
claimFailureLogged = now;
claimLog.accept("SCOPENET couldn't refresh claims from the panel (" + e.getMessage() + "); using the last known copy");
}
}
}
public static class HttpFailure extends IOException {
public final int status;
public HttpFailure(int status, String message) { super(message); this.status = status; }
@@ -104,80 +148,14 @@ public final class PanelClient {
? message.getAsString() : "You do not have access to this server.";
}
/** Local lookup; never touches the network. */
public ChunkCheckResult checkChunk(String dimension, int chunkX, int chunkZ, java.util.UUID uuid) {
if (!settings.guildsEnabled() || !settings.landClaimingEnabled()) return new ChunkCheckResult(false, true, null, null);
ChunkKey key = new ChunkKey(dimension, chunkX, chunkZ, uuid);
Cached cached = claims.get(key);
if (cached != null && cached.expires() > System.nanoTime()) return cached.result();
if (pendingClaims.add(key)) {
long generation = claimGeneration.get();
try { claimWorker.execute(() -> {
try {
ChunkCheckResult result = checkChunkRemote(dimension, chunkX, chunkZ, uuid);
if (claims.size() > 4096) claims.clear();
if (generation == claimGeneration.get()) claims.put(key, new Cached(result, System.nanoTime() + java.util.concurrent.TimeUnit.SECONDS.toNanos(2)));
} finally { pendingClaims.remove(key); }
}); } catch (java.util.concurrent.RejectedExecutionException e) { pendingClaims.remove(key); }
}
return UNKNOWN;
return claimIndex.check(dimension, chunkX, chunkZ, uuid);
}
/** Warm a 5x5 area when a player changes chunks, including environmental protection. */
public void prefetchClaims(String dimension, int chunkX, int chunkZ, java.util.UUID uuid) {
if (!settings.guildsEnabled() || !settings.landClaimingEnabled()) return;
ChunkKey center = new ChunkKey(dimension, chunkX, chunkZ, uuid);
Cached cached = claims.get(center);
if (cached != null && cached.expires() > System.nanoTime()) return;
if (!pendingSnapshots.add(center)) return;
long generation = claimGeneration.get();
try { claimWorker.execute(() -> {
try {
JsonObject req = new JsonObject();
req.addProperty("uuid", uuid.toString());
req.addProperty("dimension", dimension);
req.addProperty("chunk_x", chunkX);
req.addProperty("chunk_z", chunkZ);
JsonObject snapshot = post("guilds/claim-snapshot", req);
java.util.Map<String, ChunkCheckResult> found = new java.util.HashMap<>();
for (JsonElement el : snapshot.getAsJsonArray("claims")) {
JsonObject c = el.getAsJsonObject();
String coord = c.get("chunk_x").getAsInt() + ":" + c.get("chunk_z").getAsInt();
found.put(coord, new ChunkCheckResult(true, c.get("allowed").getAsBoolean(),
c.get("guild_name").getAsString(), c.get("guild_tag").getAsString()));
}
long expiry = System.nanoTime() + java.util.concurrent.TimeUnit.SECONDS.toNanos(15);
if (generation != claimGeneration.get()) return;
if (claims.size() > 4096) claims.clear();
for (int x = chunkX - 2; x <= chunkX + 2; x++) for (int z = chunkZ - 2; z <= chunkZ + 2; z++) {
ChunkCheckResult value = found.getOrDefault(x + ":" + z, new ChunkCheckResult(false, true, null, null));
claims.put(new ChunkKey(dimension, x, z, uuid), new Cached(value, expiry));
claims.put(new ChunkKey(dimension, x, z, new java.util.UUID(0, 0)),
new Cached(new ChunkCheckResult(value.claimed(), !value.claimed(), value.guildName(), value.guildTag()), expiry));
}
} catch (Exception ignored) { /* cold checks remain fail closed */ }
finally { pendingSnapshots.remove(center); }
}); } catch (java.util.concurrent.RejectedExecutionException e) { pendingSnapshots.remove(center); }
}
private ChunkCheckResult checkChunkRemote(String dimension, int chunkX, int chunkZ, java.util.UUID uuid) {
if (!settings.guildsEnabled() || !settings.landClaimingEnabled()) {
return new ChunkCheckResult(false, true, null, null);
}
JsonObject req = new JsonObject();
req.addProperty("dimension", dimension);
req.addProperty("chunk_x", chunkX);
req.addProperty("chunk_z", chunkZ);
req.addProperty("uuid", uuid.toString());
try {
JsonObject res = post("guilds/check-chunk", req);
boolean claimed = res.has("claimed") && res.get("claimed").getAsBoolean();
boolean allowed = res.has("allowed") && res.get("allowed").getAsBoolean();
String guildName = res.has("guild_name") && !res.get("guild_name").isJsonNull() ? res.get("guild_name").getAsString() : null;
String guildTag = res.has("guild_tag") && !res.get("guild_tag").isJsonNull() ? res.get("guild_tag").getAsString() : null;
return new ChunkCheckResult(claimed, allowed, guildName, guildTag);
} catch (Exception e) {
return UNKNOWN;
}
public boolean isClaimed(String dimension, int chunkX, int chunkZ) {
return settings.guildsEnabled() && settings.landClaimingEnabled() && claimIndex.isClaimed(dimension, chunkX, chunkZ);
}
public JsonObject claimChunk(String dimension, int chunkX, int chunkZ, java.util.UUID uuid) throws IOException, InterruptedException {
@@ -187,7 +165,7 @@ public final class PanelClient {
req.addProperty("chunk_x", chunkX);
req.addProperty("chunk_z", chunkZ);
JsonObject result = post("guilds/claim", req);
invalidateClaims();
requestClaimRefresh();
return result;
}
@@ -198,7 +176,7 @@ public final class PanelClient {
req.addProperty("chunk_x", chunkX);
req.addProperty("chunk_z", chunkZ);
JsonObject result = post("guilds/unclaim", req);
invalidateClaims();
requestClaimRefresh();
return result;
}
@@ -214,13 +192,17 @@ public final class PanelClient {
req.addProperty("username", username);
req.addProperty("name", name);
req.addProperty("tag", tag);
return post("guilds/create", req);
JsonObject result = post("guilds/create", req);
requestClaimRefresh();
return result;
}
public JsonObject leaveGuild(java.util.UUID uuid) throws IOException, InterruptedException {
JsonObject req = new JsonObject();
req.addProperty("uuid", uuid.toString());
return post("guilds/leave", req);
JsonObject result = post("guilds/leave", req);
requestClaimRefresh();
return result;
}
public double getBalance(java.util.UUID uuid, String username) throws IOException, InterruptedException {