@@ -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