Skip to content

Commit

Permalink
make dropped-item-limit check on chunkload
Browse files Browse the repository at this point in the history
  • Loading branch information
xGinko committed Jan 21, 2024
1 parent 780eaf9 commit cbdf4fe
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.world.ChunkLoadEvent;

import java.util.Arrays;
import java.util.HashSet;
Expand Down Expand Up @@ -104,6 +105,26 @@ private void onItemDrop(ItemSpawnEvent event) {
}
}

@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true)
private void onChunkLoad(ChunkLoadEvent event) {
if (event.isNewChunk()) return;

int droppedItemCount = 0;

for (Entity entity : event.getChunk().getEntities()) {
if (entity.getType() != EntityType.DROPPED_ITEM) continue;

droppedItemCount++;
if (droppedItemCount <= maxDroppedItemsPerChunk) continue;
if (usingWhitelist && whitelistedItems.contains(((Item) entity).getItemStack().getType())) continue;

entity.remove();
if (logIsEnabled) LogUtil.moduleLog(Level.INFO, name(), "Removed dropped item at"
+ " x:" + entity.getLocation().getX() + " y:" + entity.getLocation().getY() + " z:" + entity.getLocation().getZ()
+ " in world " + entity.getWorld().getName() + ", because reached limit of " + maxDroppedItemsPerChunk);
}
}

@Override
public void run() {
for (World world : plugin.getServer().getWorlds()) {
Expand Down

0 comments on commit cbdf4fe

Please sign in to comment.