Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
import me.xginko.aef.utils.models.ChunkUID;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntitySpawnEvent;

import java.time.Duration;
import java.util.Objects;

public class FallingBlockLimit extends AEFModule implements Listener {

private static final EntityType FALLING_BLOCK_TYPE = Objects.requireNonNull(XEntityType.FALLING_BLOCK.get());

private final long chunkCheckDelay;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;
Expand Down Expand Up @@ -50,65 +55,46 @@ public void disable() {
HandlerList.unregisterAll(this);
if (checkedChunks != null) {
checkedChunks.clear();
checkedChunks.cleanUp();
checkedChunks = null;
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
if (ChunkUtil.isRetrievalUnsafe(chunk)) return;

final ChunkUID chunkUID = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkUID)) return;

int count = 0;
boolean removed_falling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType() == XEntityType.FALLING_BLOCK.get()) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
}
}

checkedChunks.add(chunkUID);

if (logIsEnabled && removed_falling) info("Removed falling block(s) at " +
LocationUtil.toString(event.getSourceBlock().getLocation()) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
private void onEntitySpawn(EntitySpawnEvent event) {
if (event.getEntityType() != FALLING_BLOCK_TYPE) return;
tryEnforceLimit(event.getEntity().getChunk(), event.getEntity().getLocation(), 1);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntityType() != XEntityType.FALLING_BLOCK.get()) return;
Chunk chunk = event.getBlock().getChunk();
if (ChunkUtil.isRetrievalUnsafe(chunk)) return;
if (event.getEntityType() != FALLING_BLOCK_TYPE) return;
tryEnforceLimit(event.getBlock().getChunk(), event.getBlock().getLocation(), 0);
}

private void tryEnforceLimit(Chunk chunk, Location logLocation, int initialCount) {
if (ChunkUtil.isRetrievalUnsafe(chunk) || !ChunkUtil.isEntitiesLoaded(chunk)) return;

ChunkUID chunkKey = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkKey)) return;

final ChunkUID chunkUID = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkUID)) return;
checkedChunks.add(chunkKey);

int count = 0;
boolean removed_falling = false;
int count = initialCount;
boolean removedFalling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType() == XEntityType.FALLING_BLOCK.get()) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
if (entity.getType() != FALLING_BLOCK_TYPE) continue;

count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removedFalling = true;
}
}

checkedChunks.add(chunkUID);

if (logIsEnabled && removed_falling) info("Removed falling block(s) at " +
LocationUtil.toString(event.getBlock().getLocation()) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
if (logIsEnabled && removedFalling) {
info("Removed falling block(s) at " + LocationUtil.toString(logLocation) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,23 @@
import me.xginko.aef.utils.models.ChunkUID;
import me.xginko.aef.utils.models.ExpiringSet;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockPhysicsEvent;
import org.bukkit.event.entity.EntityChangeBlockEvent;
import org.bukkit.event.entity.EntitySpawnEvent;

import java.time.Duration;
import java.util.Objects;

public class FallingBlockLimit extends AEFModule implements Listener {

private static final EntityType FALLING_BLOCK_TYPE = Objects.requireNonNull(XEntityType.FALLING_BLOCK.get());

private final long chunkCheckDelay;
private final int maxFallingGravityBlockPerChunk;
private final boolean logIsEnabled;
Expand Down Expand Up @@ -50,65 +55,46 @@ public void disable() {
HandlerList.unregisterAll(this);
if (checkedChunks != null) {
checkedChunks.clear();
checkedChunks.cleanUp();
checkedChunks = null;
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onBlockPhysics(BlockPhysicsEvent event) {
Chunk chunk = event.getBlock().getChunk();
if (ChunkUtil.isRetrievalUnsafe(chunk)) return;

final ChunkUID chunkUID = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkUID)) return;

int count = 0;
boolean removed_falling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType() == XEntityType.FALLING_BLOCK.get()) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
}
}

checkedChunks.add(chunkUID);

if (logIsEnabled && removed_falling) info("Removed falling block(s) at " +
LocationUtil.toString(event.getSourceBlock().getLocation()) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
private void onEntitySpawn(EntitySpawnEvent event) {
if (event.getEntityType() != FALLING_BLOCK_TYPE) return;
tryEnforceLimit(event.getEntity().getChunk(), event.getEntity().getLocation(), 1);
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChangeBlock(EntityChangeBlockEvent event) {
if (event.getEntityType() != XEntityType.FALLING_BLOCK.get()) return;
Chunk chunk = event.getBlock().getChunk();
if (ChunkUtil.isRetrievalUnsafe(chunk)) return;
if (event.getEntityType() != FALLING_BLOCK_TYPE) return;
tryEnforceLimit(event.getBlock().getChunk(), event.getBlock().getLocation(), 0);
}

private void tryEnforceLimit(Chunk chunk, Location logLocation, int initialCount) {
if (ChunkUtil.isRetrievalUnsafe(chunk) || !ChunkUtil.isEntitiesLoaded(chunk)) return;

ChunkUID chunkKey = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkKey)) return;

final ChunkUID chunkUID = ChunkUID.of(chunk);
if (checkedChunks.contains(chunkUID)) return;
checkedChunks.add(chunkKey);

int count = 0;
boolean removed_falling = false;
int count = initialCount;
boolean removedFalling = false;

for (Entity entity : chunk.getEntities()) {
if (entity.getType() == XEntityType.FALLING_BLOCK.get()) {
count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removed_falling = true;
}
if (entity.getType() != FALLING_BLOCK_TYPE) continue;

count++;
if (count > maxFallingGravityBlockPerChunk) {
entity.remove();
removedFalling = true;
}
}

checkedChunks.add(chunkUID);

if (logIsEnabled && removed_falling) info("Removed falling block(s) at " +
LocationUtil.toString(event.getBlock().getLocation()) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
if (logIsEnabled && removedFalling) {
info("Removed falling block(s) at " + LocationUtil.toString(logLocation) + " because reached limit of " +
maxFallingGravityBlockPerChunk + " falling gravity blocks per chunk");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.bukkit.World;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;

Expand Down Expand Up @@ -48,7 +47,7 @@ public String toString() {

@Override
public int hashCode() {
return Objects.hash(this.worldUID, this.x, this.z);
return 31 * (31 * this.worldUID.hashCode() + this.x) + this.z;
}

@Override
Expand Down