Skip to content

Commit 510ddd5

Browse files
committed
speed up checking if over the limit
1 parent 6d60016 commit 510ddd5

File tree

2 files changed

+30
-20
lines changed
  • AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits
  • AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits

2 files changed

+30
-20
lines changed

AnarchyExploitFixesFolia/src/main/java/me/xginko/aef/modules/chunklimits/BlockLimit.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,12 @@ public class BlockLimit extends AEFModule implements Listener {
2424
private final Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
2525
private final long materialCountCacheMillis;
2626

27-
private Cache<Chunk, Cache<Material, Integer>> chunkMaterialCache;
27+
private Cache<Chunk, Cache<Material, Boolean>> chunkMaterialCache;
2828

2929
public BlockLimit() {
3030
super("chunk-limits.block-limit", false);
31-
this.materialCountCacheMillis = config.getLong(configPath + ".material-count-cache-millis", 1000);
31+
this.materialCountCacheMillis = config.getLong(configPath + ".material-count-cache-millis", 1000,
32+
"Recommended to not go higher than 5000ms.");
3233

3334
Map<XMaterial, Integer> universal = new EnumMap<>(XMaterial.class);
3435
universal.put(XMaterial.ENCHANTING_TABLE, 16);
@@ -159,7 +160,7 @@ public void enable() {
159160
public void disable() {
160161
HandlerList.unregisterAll(this);
161162
if (chunkMaterialCache != null) {
162-
for (Map.Entry<Chunk, Cache<Material, Integer>> entry : chunkMaterialCache.asMap().entrySet()) {
163+
for (Map.Entry<Chunk, Cache<Material, Boolean>> entry : chunkMaterialCache.asMap().entrySet()) {
163164
entry.getValue().invalidateAll();
164165
entry.getValue().cleanUp();
165166
}
@@ -188,29 +189,33 @@ && exceedsPerChunkLimit(event.getMaterial(), event.getPlayer().getChunk())) {
188189
}
189190

190191
private boolean exceedsPerChunkLimit(Material blockType, Chunk chunk) {
192+
final int limit = blockLimits.get(blockType);
191193
final int minY = chunk.getWorld().getMinHeight();
192194
final int maxY = chunk.getWorld().getMaxHeight();
193195

194-
Cache<Material, Integer> materialCountCache = chunkMaterialCache.getIfPresent(chunk);
195-
if (materialCountCache == null) {
196-
materialCountCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
196+
Cache<Material, Boolean> exceededCache = chunkMaterialCache.getIfPresent(chunk);
197+
if (exceededCache == null) {
198+
exceededCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
197199
}
198200

199-
Integer materialCount = materialCountCache.get(blockType, material -> {
201+
Boolean exceeded = exceededCache.get(blockType, material -> {
200202
int count = 0;
201203
for (int x = 0; x < 16; x++) {
202204
for (int z = 0; z < 16; z++) {
203205
for (int y = minY; y < maxY; y++) {
204206
if (chunk.getBlock(x, y, z).getType() == material) {
205207
count++;
208+
if (count > limit) {
209+
return true;
210+
}
206211
}
207212
}
208213
}
209214
}
210-
return count;
215+
return false;
211216
});
212217

213-
chunkMaterialCache.put(chunk, materialCountCache);
214-
return materialCount > blockLimits.get(blockType);
218+
chunkMaterialCache.put(chunk, exceededCache);
219+
return exceeded;
215220
}
216221
}

AnarchyExploitFixesLegacy/src/main/java/me/xginko/aef/modules/chunklimits/BlockLimit.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ public class BlockLimit extends AEFModule implements Listener {
2626
private final Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
2727
private final long materialCountCacheMillis;
2828

29-
private Cache<Chunk, Cache<Material, Integer>> chunkMaterialCache;
29+
private Cache<Chunk, Cache<Material, Boolean>> chunkMaterialCache;
3030

3131
public BlockLimit() {
3232
super("chunk-limits.block-limit", false);
33-
this.materialCountCacheMillis = config.getLong(configPath + ".material-count-cache-millis", 1000);
33+
this.materialCountCacheMillis = config.getLong(configPath + ".material-count-cache-millis", 1000,
34+
"Recommended to not go higher than 5000ms.");
3435

3536
Map<XMaterial, Integer> universal = new EnumMap<>(XMaterial.class);
3637
universal.put(XMaterial.ENCHANTING_TABLE, 16);
@@ -161,7 +162,7 @@ public void enable() {
161162
public void disable() {
162163
HandlerList.unregisterAll(this);
163164
if (chunkMaterialCache != null) {
164-
for (Map.Entry<Chunk, Cache<Material, Integer>> entry : chunkMaterialCache.asMap().entrySet()) {
165+
for (Map.Entry<Chunk, Cache<Material, Boolean>> entry : chunkMaterialCache.asMap().entrySet()) {
165166
entry.getValue().invalidateAll();
166167
entry.getValue().cleanUp();
167168
}
@@ -190,29 +191,33 @@ && exceedsPerChunkLimit(event.getMaterial(), event.getPlayer().getChunk())) {
190191
}
191192

192193
private boolean exceedsPerChunkLimit(Material blockType, Chunk chunk) {
194+
final int limit = blockLimits.get(blockType);
193195
final int minY = WorldUtil.getMinWorldHeight(chunk.getWorld());
194196
final int maxY = chunk.getWorld().getMaxHeight();
195197

196-
Cache<Material, Integer> materialCountCache = chunkMaterialCache.getIfPresent(chunk);
197-
if (materialCountCache == null) {
198-
materialCountCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
198+
Cache<Material, Boolean> exceededCache = chunkMaterialCache.getIfPresent(chunk);
199+
if (exceededCache == null) {
200+
exceededCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
199201
}
200202

201-
Integer materialCount = materialCountCache.get(blockType, material -> {
203+
Boolean exceeded = exceededCache.get(blockType, material -> {
202204
int count = 0;
203205
for (int x = 0; x < 16; x++) {
204206
for (int z = 0; z < 16; z++) {
205207
for (int y = minY; y < maxY; y++) {
206208
if (chunk.getBlock(x, y, z).getType() == material) {
207209
count++;
210+
if (count > limit) {
211+
return true;
212+
}
208213
}
209214
}
210215
}
211216
}
212-
return count;
217+
return false;
213218
});
214219

215-
chunkMaterialCache.put(chunk, materialCountCache);
216-
return materialCount > blockLimits.get(blockType);
220+
chunkMaterialCache.put(chunk, exceededCache);
221+
return exceeded;
217222
}
218223
}

0 commit comments

Comments
 (0)