Add some more listeners

- Also use setShouldAbortSpawn so it doesn't just try forever
 - Add config to enable or disable all paper listeners
This commit is contained in:
dordsor21 2020-04-30 17:50:52 +01:00
parent 85a75ad868
commit c6dc9ee189
3 changed files with 70 additions and 6 deletions

View File

@ -637,8 +637,7 @@ public final class BukkitMain extends JavaPlugin implements Listener, IPlotMain
final PlayerEvents main = new PlayerEvents(); final PlayerEvents main = new PlayerEvents();
getServer().getPluginManager().registerEvents(main, this); getServer().getPluginManager().registerEvents(main, this);
getServer().getPluginManager().registerEvents(new EntitySpawnListener(), this); getServer().getPluginManager().registerEvents(new EntitySpawnListener(), this);
if (PaperLib.isPaper() && Settings.Paper_Components.ENTITY_PATHING if (PaperLib.isPaper() && Settings.Paper_Components.PAPER_LISTENERS) {
|| Settings.Paper_Components.PRE_SPAWN_LISTENER) {
getServer().getPluginManager().registerEvents(new PaperListener(), this); getServer().getPluginManager().registerEvents(new PaperListener(), this);
} }
PlotListener.startRunnable(); PlotListener.startRunnable();

View File

@ -27,20 +27,29 @@ package com.plotsquared.bukkit.listener;
import com.destroystokyo.paper.entity.Pathfinder; import com.destroystokyo.paper.entity.Pathfinder;
import com.destroystokyo.paper.event.entity.EntityPathfindEvent; 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.PreCreatureSpawnEvent;
import com.destroystokyo.paper.event.entity.PreSpawnerSpawnEvent;
import com.destroystokyo.paper.event.entity.SlimePathfindEvent; import com.destroystokyo.paper.event.entity.SlimePathfindEvent;
import com.destroystokyo.paper.event.player.PlayerLaunchProjectileEvent;
import com.plotsquared.bukkit.util.BukkitUtil; import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.core.PlotSquared;
import com.plotsquared.core.configuration.Settings; import com.plotsquared.core.configuration.Settings;
import com.plotsquared.core.location.Location; import com.plotsquared.core.location.Location;
import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.plot.Plot; import com.plotsquared.core.plot.Plot;
import com.plotsquared.core.plot.PlotArea; import com.plotsquared.core.plot.PlotArea;
import com.plotsquared.core.plot.flag.implementations.DoneFlag; import com.plotsquared.core.plot.flag.implementations.DoneFlag;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; 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.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.projectiles.ProjectileSource;
/** /**
* Events specific to Paper. Some toit nups here * Events specific to Paper. Some toit nups here
@ -132,9 +141,6 @@ public class PaperListener implements Listener {
} }
@EventHandler public void onPreCreatureSpawnEvent(PreCreatureSpawnEvent event) { @EventHandler public void onPreCreatureSpawnEvent(PreCreatureSpawnEvent event) {
if (!Settings.Paper_Components.PRE_SPAWN_LISTENER) {
return;
}
Location location = BukkitUtil.getLocation(event.getSpawnLocation()); Location location = BukkitUtil.getLocation(event.getSpawnLocation());
PlotArea area = location.getPlotArea(); PlotArea area = location.getPlotArea();
if (!location.isPlotArea()) { if (!location.isPlotArea()) {
@ -143,6 +149,7 @@ public class PaperListener implements Listener {
//If entities are spawning... the chunk should be loaded? //If entities are spawning... the chunk should be loaded?
Entity[] entities = event.getSpawnLocation().getChunk().getEntities(); Entity[] entities = event.getSpawnLocation().getChunk().getEntities();
if (entities.length > Settings.Chunk_Processor.MAX_ENTITIES) { if (entities.length > Settings.Chunk_Processor.MAX_ENTITIES) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -153,6 +160,7 @@ public class PaperListener implements Listener {
case OCELOT_BABY: case OCELOT_BABY:
case SPAWNER_EGG: case SPAWNER_EGG:
if (!area.isSpawnEggs()) { if (!area.isSpawnEggs()) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -171,11 +179,13 @@ public class PaperListener implements Listener {
case BEEHIVE: case BEEHIVE:
case CHUNK_GEN: case CHUNK_GEN:
if (!area.isMobSpawning()) { if (!area.isMobSpawning()) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
case BREEDING: case BREEDING:
if (!area.isSpawnBreeding()) { if (!area.isSpawnBreeding()) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -185,12 +195,14 @@ public class PaperListener implements Listener {
case BUILD_WITHER: case BUILD_WITHER:
case CUSTOM: case CUSTOM:
if (!area.isSpawnCustom() && event.getType() != EntityType.ARMOR_STAND) { if (!area.isSpawnCustom() && event.getType() != EntityType.ARMOR_STAND) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
break; break;
case SPAWNER: case SPAWNER:
if (!area.isMobSpawnerSpawning()) { if (!area.isMobSpawnerSpawning()) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
@ -203,17 +215,67 @@ public class PaperListener implements Listener {
switch (type) { switch (type) {
case DROPPED_ITEM: case DROPPED_ITEM:
if (Settings.Enabled_Components.KILL_ROAD_ITEMS) { if (Settings.Enabled_Components.KILL_ROAD_ITEMS) {
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
break; break;
} }
case PLAYER: case PLAYER:
return; return;
} }
event.setShouldAbortSpawn(true);
event.setCancelled(true); event.setCancelled(true);
} }
return; return;
} }
if (Settings.Done.RESTRICT_BUILDING && DoneFlag.isDone(plot)) { 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); event.setCancelled(true);
} }
} }

View File

@ -359,8 +359,11 @@ public class Settings extends Config {
@Comment("Enable or disable parts of the plugin specific to using Paper") @Comment("Enable or disable parts of the plugin specific to using Paper")
public static final class Paper_Components { 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("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;
} }