Skip to content

Commit

Permalink
speed up checking if over the limit
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Dec 31, 2024
1 parent 6d60016 commit 510ddd5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ public class BlockLimit extends AEFModule implements Listener {
private final Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
private final long materialCountCacheMillis;

private Cache<Chunk, Cache<Material, Integer>> chunkMaterialCache;
private Cache<Chunk, Cache<Material, Boolean>> chunkMaterialCache;

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

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

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

Cache<Material, Integer> materialCountCache = chunkMaterialCache.getIfPresent(chunk);
if (materialCountCache == null) {
materialCountCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
Cache<Material, Boolean> exceededCache = chunkMaterialCache.getIfPresent(chunk);
if (exceededCache == null) {
exceededCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
}

Integer materialCount = materialCountCache.get(blockType, material -> {
Boolean exceeded = exceededCache.get(blockType, material -> {
int count = 0;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = minY; y < maxY; y++) {
if (chunk.getBlock(x, y, z).getType() == material) {
count++;
if (count > limit) {
return true;
}
}
}
}
}
return count;
return false;
});

chunkMaterialCache.put(chunk, materialCountCache);
return materialCount > blockLimits.get(blockType);
chunkMaterialCache.put(chunk, exceededCache);
return exceeded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,12 @@ public class BlockLimit extends AEFModule implements Listener {
private final Map<Material, Integer> blockLimits = new EnumMap<>(Material.class);
private final long materialCountCacheMillis;

private Cache<Chunk, Cache<Material, Integer>> chunkMaterialCache;
private Cache<Chunk, Cache<Material, Boolean>> chunkMaterialCache;

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

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

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

Cache<Material, Integer> materialCountCache = chunkMaterialCache.getIfPresent(chunk);
if (materialCountCache == null) {
materialCountCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
Cache<Material, Boolean> exceededCache = chunkMaterialCache.getIfPresent(chunk);
if (exceededCache == null) {
exceededCache = Caffeine.newBuilder().expireAfterWrite(Duration.ofMillis(materialCountCacheMillis)).build();
}

Integer materialCount = materialCountCache.get(blockType, material -> {
Boolean exceeded = exceededCache.get(blockType, material -> {
int count = 0;
for (int x = 0; x < 16; x++) {
for (int z = 0; z < 16; z++) {
for (int y = minY; y < maxY; y++) {
if (chunk.getBlock(x, y, z).getType() == material) {
count++;
if (count > limit) {
return true;
}
}
}
}
}
return count;
return false;
});

chunkMaterialCache.put(chunk, materialCountCache);
return materialCount > blockLimits.get(blockType);
chunkMaterialCache.put(chunk, exceededCache);
return exceeded;
}
}

0 comments on commit 510ddd5

Please sign in to comment.