From 3deff629b0a35743808ab3e10a40b1eabcb86b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20S=C3=B6derberg?= Date: Tue, 12 May 2020 23:01:14 +0200 Subject: [PATCH] Add paper specific listener for limiting the amount of tile entities in a chunk during build, instead of just doing it during chunk unloading --- .../bukkit/listener/PaperListener.java | 25 +++++++++++++++++++ .../core/configuration/Captions.java | 1 + .../core/configuration/Settings.java | 2 ++ 3 files changed, 28 insertions(+) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java index f87050c88..870747c1a 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java @@ -33,6 +33,7 @@ import com.destroystokyo.paper.event.entity.SlimePathfindEvent; import com.destroystokyo.paper.event.player.PlayerLaunchProjectileEvent; import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.core.PlotSquared; +import com.plotsquared.core.configuration.Captions; import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.location.Location; import com.plotsquared.core.player.PlotPlayer; @@ -41,6 +42,7 @@ import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import org.bukkit.Chunk; import org.bukkit.block.Block; +import org.bukkit.block.TileState; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; @@ -48,7 +50,9 @@ import org.bukkit.entity.Projectile; import org.bukkit.entity.Slime; import org.bukkit.entity.ThrownPotion; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.projectiles.ProjectileSource; @@ -252,6 +256,27 @@ public class PaperListener implements Listener { } } + @EventHandler(priority = EventPriority.HIGHEST) public void onBlockPlace(BlockPlaceEvent event) { + if (!Settings.Paper_Components.TILE_ENTITY_CHECK) { + return; + } + if (!(event.getBlock().getState(false) instanceof TileState)) { + return; + } + final Location location = BukkitUtil.getLocation(event.getBlock().getLocation()); + final PlotArea plotArea = location.getPlotArea(); + if (plotArea == null) { + return; + } + final int tileEntityCount = event.getBlock().getChunk().getTileEntities(false).length; + if (tileEntityCount >= Settings.Chunk_Processor.MAX_TILES) { + final PlotPlayer plotPlayer = BukkitUtil.getPlayer(event.getPlayer()); + Captions.TILE_ENTITY_CAP_REACHED.send(plotPlayer, Settings.Chunk_Processor.MAX_TILES); + event.setCancelled(true); + event.setBuild(false); + } + } + /** * Unsure if this will be any performance improvement over the spigot version, * but here it is anyway :) diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java index 5909b119f..17c0d7c4d 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Captions.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Captions.java @@ -446,6 +446,7 @@ public enum Captions implements Caption { NOT_VALID_PLOT_WORLD("$2That is not a valid plot area (case sensitive)", "Errors"), NO_PLOTS("$2You don't have any plots", "Errors"), WAIT_FOR_TIMER("$2A set block timer is bound to either the current plot or you. Please wait for it to finish", "Errors"), + TILE_ENTITY_CAP_REACHED("$2The total number of tile entities in this chunk may not exceed $1%s", "Errors"), // DEBUG_REPORT_CREATED("$1Uploaded a full debug to: $1%url%", "Paste"), PURGE_SUCCESS("$4Successfully purged %s plots", "Purge"), diff --git a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java index 0967b137c..108bff9de 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -484,6 +484,8 @@ public class Settings extends Config { public static boolean SPAWNER_SPAWN = true; @Comment("Cancel entity spawns from tick spawn rates before they happen (performance buff)") public static boolean CREATURE_SPAWN = true; + @Comment("Check the tile entity limit on block placement") + public static boolean TILE_ENTITY_CHECK = false; }