From c6dc9ee189696ad41f74cd9a0fbf5d419b44215a Mon Sep 17 00:00:00 2001 From: dordsor21 Date: Thu, 30 Apr 2020 17:50:52 +0100 Subject: [PATCH] Add some more listeners - Also use setShouldAbortSpawn so it doesn't just try forever - Add config to enable or disable all paper listeners --- .../com/plotsquared/bukkit/BukkitMain.java | 3 +- .../bukkit/listener/PaperListener.java | 68 ++++++++++++++++++- .../core/configuration/Settings.java | 5 +- 3 files changed, 70 insertions(+), 6 deletions(-) diff --git a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java index 199e0066e..79d27ae33 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/BukkitMain.java @@ -637,8 +637,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain final PlayerEvents main = new PlayerEvents(); getServer().getPluginManager().registerEvents(main, this); getServer().getPluginManager().registerEvents(new EntitySpawnListener(), this); - if (PaperLib.isPaper() && Settings.Paper_Components.ENTITY_PATHING - || Settings.Paper_Components.PRE_SPAWN_LISTENER) { + if (PaperLib.isPaper() && Settings.Paper_Components.PAPER_LISTENERS) { getServer().getPluginManager().registerEvents(new PaperListener(), this); } PlotListener.startRunnable(); 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 22f6890a6..7ecd26436 100644 --- a/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java +++ b/Bukkit/src/main/java/com/plotsquared/bukkit/listener/PaperListener.java @@ -27,20 +27,29 @@ package com.plotsquared.bukkit.listener; import com.destroystokyo.paper.entity.Pathfinder; import com.destroystokyo.paper.event.entity.EntityPathfindEvent; +import com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent; import com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent; +import com.destroystokyo.paper.event.entity.PreSpawnerSpawnEvent; 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.Settings; import com.plotsquared.core.location.Location; +import com.plotsquared.core.player.PlotPlayer; import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.flag.implementations.DoneFlag; import org.bukkit.Chunk; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; +import org.bukkit.entity.Player; +import org.bukkit.entity.Projectile; +import org.bukkit.entity.ThrownPotion; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; +import org.bukkit.projectiles.ProjectileSource; /** * Events specific to Paper. Some toit nups here @@ -132,9 +141,6 @@ public class PaperListener implements Listener { } @EventHandler public void onPreCreatureSpawnEvent(PreCreatureSpawnEvent event) { - if (!Settings.Paper_Components.PRE_SPAWN_LISTENER) { - return; - } Location location = BukkitUtil.getLocation(event.getSpawnLocation()); PlotArea area = location.getPlotArea(); if (!location.isPlotArea()) { @@ -143,6 +149,7 @@ public class PaperListener implements Listener { //If entities are spawning... the chunk should be loaded? Entity[] entities = event.getSpawnLocation().getChunk().getEntities(); if (entities.length > Settings.Chunk_Processor.MAX_ENTITIES) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } @@ -153,6 +160,7 @@ public class PaperListener implements Listener { case OCELOT_BABY: case SPAWNER_EGG: if (!area.isSpawnEggs()) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } @@ -171,11 +179,13 @@ public class PaperListener implements Listener { case BEEHIVE: case CHUNK_GEN: if (!area.isMobSpawning()) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } case BREEDING: if (!area.isSpawnBreeding()) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } @@ -185,12 +195,14 @@ public class PaperListener implements Listener { case BUILD_WITHER: case CUSTOM: if (!area.isSpawnCustom() && event.getType() != EntityType.ARMOR_STAND) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } break; case SPAWNER: if (!area.isMobSpawnerSpawning()) { + event.setShouldAbortSpawn(true); event.setCancelled(true); return; } @@ -203,17 +215,67 @@ public class PaperListener implements Listener { switch (type) { case DROPPED_ITEM: if (Settings.Enabled_Components.KILL_ROAD_ITEMS) { + event.setShouldAbortSpawn(true); event.setCancelled(true); break; } case PLAYER: return; } + event.setShouldAbortSpawn(true); event.setCancelled(true); } return; } if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { + event.setShouldAbortSpawn(true); + event.setCancelled(true); + } + } + + @EventHandler + public void onPlayerNaturallySpawnCreaturesEvent(PlayerNaturallySpawnCreaturesEvent event) { + if (Settings.Paper_Components.CANCEL_CHUNK_SPAWN) { + Location location = BukkitUtil.getLocation(event.getPlayer().getLocation()); + PlotArea area = location.getPlotArea(); + if (area != null && !area.isMobSpawning()) { + event.setCancelled(true); + } + } + } + + @EventHandler public void onPreSpawnerSpawnEvent(PreSpawnerSpawnEvent event) { + Location location = BukkitUtil.getLocation(event.getSpawnerLocation()); + PlotArea area = location.getPlotArea(); + if (area != null && !area.isMobSpawnerSpawning()) { + event.setCancelled(true); + event.setShouldAbortSpawn(true); + } + } + + /** + * Unsure if this will be any performance improvement over the spigot version, + * but here it is anyway :) + * + * @param event Paper's PlayerLaunchProjectileEvent + */ + @EventHandler public void onProjectileLaunch(PlayerLaunchProjectileEvent event) { + Projectile entity = event.getProjectile(); + if (!(entity instanceof ThrownPotion)) { + return; + } + ProjectileSource shooter = entity.getShooter(); + if (!(shooter instanceof Player)) { + return; + } + Location location = BukkitUtil.getLocation(entity); + if (!PlotSquared.get().hasPlotArea(location.getWorld())) { + return; + } + PlotPlayer pp = BukkitUtil.getPlayer((Player) shooter); + Plot plot = location.getOwnedPlot(); + if (plot != null && !plot.isAdded(pp.getUUID())) { + entity.remove(); event.setCancelled(true); } } 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 88c279d54..5d7672445 100644 --- a/Core/src/main/java/com/plotsquared/core/configuration/Settings.java +++ b/Core/src/main/java/com/plotsquared/core/configuration/Settings.java @@ -359,8 +359,11 @@ public class Settings extends Config { @Comment("Enable or disable parts of the plugin specific to using Paper") public static final class Paper_Components { + @Comment("Enable Paper's listeners.") public static boolean PAPER_LISTENERS = true; @Comment("Prevent entities from leaving plots") public static boolean ENTITY_PATHING = true; - @Comment("Use paper's PreCreatureSpawnEvent") public static boolean PRE_SPAWN_LISTENER = true; + @Comment( + "Cancel entity spawns when the chunk is loaded if the PlotArea's mob spawning is off") + public static boolean CANCEL_CHUNK_SPAWN = true; }