PreCreatureSpawnEvent - Closes #917

Adds an event to fire before an Entity is created, so that plugins that need to cancel
CreatureSpawnEvent can do so from this event instead.

Cancelling CreatureSpawnEvent rapidly causes a lot of garbage collection and CPU waste
as it's done after the Entity object has been fully created.

Mob Limiting plugins and blanket "ban this type of monster" plugins should use this event
instead and save a lot of server resources.

See: https://github.com/PaperMC/Paper/issues/917
This commit is contained in:
Aikar 2018-01-14 17:02:38 -05:00
parent 7c60dbe814
commit 6a5948b316
No known key found for this signature in database
GPG Key ID: 401ADFC9891FAAFE
4 changed files with 249 additions and 9 deletions

View File

@ -0,0 +1,122 @@
From 9ac5fb79f031b9eb090079fc3937dbf5650b5464 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 14 Jan 2018 16:59:43 -0500
Subject: [PATCH] PreCreatureSpawnEvent
Adds an event to fire before an Entity is created, so that plugins that need to cancel
CreatureSpawnEvent can do so from this event instead.
Cancelling CreatureSpawnEvent rapidly causes a lot of garbage collection and CPU waste
as it's done after the Entity object has been fully created.
Mob Limiting plugins and blanket "ban this type of monster" plugins should use this event
instead and save a lot of server resources.
See: https://github.com/PaperMC/Paper/issues/917
diff --git a/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java
new file mode 100644
index 00000000..c4751b61
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java
@@ -0,0 +1,97 @@
+package com.destroystokyo.paper.event.entity;
+
+import com.google.common.base.Preconditions;
+import org.bukkit.Location;
+import org.bukkit.entity.EntityType;
+import org.bukkit.event.Cancellable;
+import org.bukkit.event.Event;
+import org.bukkit.event.HandlerList;
+import org.bukkit.event.entity.CreatureSpawnEvent;
+
+/**
+ * WARNING: This event only fires for a limited number of cases, and not for every case that CreatureSpawnEvent does.
+ *
+ * You should still listen to CreatureSpawnEvent as a backup, and only use this event as an "enhancement".
+ * The intent of this event is to improve server performance, so limited use cases.
+ *
+ * Currently: NATURAL and SPAWNER based reasons. Please submit a Pull Request for future additions.
+ */
+public class PreCreatureSpawnEvent extends Event implements Cancellable {
+ private final Location location;
+ private final EntityType type;
+ private final CreatureSpawnEvent.SpawnReason reason;
+ private boolean shouldAbortSpawn;
+
+ public PreCreatureSpawnEvent(Location location, EntityType type, CreatureSpawnEvent.SpawnReason reason) {
+ this.location = Preconditions.checkNotNull(location, "Location may not be null").clone();
+ this.type = Preconditions.checkNotNull(type, "Type may not be null");
+ this.reason = Preconditions.checkNotNull(reason, "Reason may not be null");
+ }
+
+ /**
+ * @return The location this creature is being spawned at
+ */
+ public Location getSpawnLocation() {
+ return location;
+ }
+
+ /**
+ * @return The type of creature being spawned
+ */
+ public EntityType getType() {
+ return type;
+ }
+
+ /**
+ * @return Reason this creature is spawning (ie, NATURAL vs SPAWNER)
+ */
+ public CreatureSpawnEvent.SpawnReason getReason() {
+ return reason;
+ }
+
+ /**
+ * @return If the spawn process should be aborted vs trying more attempts
+ */
+ public boolean shouldAbortSpawn() {
+ return shouldAbortSpawn;
+ }
+
+ /**
+ * Set this if you are more blanket blocking all types of these spawns, and wish to abort the spawn process from
+ * trying more attempts after this cancellation.
+ *
+ * @param shouldAbortSpawn Set if the spawn process should be aborted vs trying more attempts
+ */
+ public void setShouldAbortSpawn(boolean shouldAbortSpawn) {
+ this.shouldAbortSpawn = shouldAbortSpawn;
+ }
+
+ private static final HandlerList handlers = new HandlerList();
+
+ public HandlerList getHandlers() {
+ return handlers;
+ }
+
+ public static HandlerList getHandlerList() {
+ return handlers;
+ }
+
+ private boolean cancelled = false;
+
+ /**
+ * @return If the spawn of this creature is cancelled or not
+ */
+ @Override
+ public boolean isCancelled() {
+ return cancelled;
+ }
+
+ /**
+ * Cancelling this event is more effecient than cancelling CreatureSpawnEvent
+ * @param cancel true if you wish to cancel this event, and abort the spawn of this creature
+ */
+ @Override
+ public void setCancelled(boolean cancel) {
+ cancelled = cancel;
+ }
+}
--
2.15.1

View File

@ -1,4 +1,4 @@
From f12d521a85a8487c53f0c0545a7c1aa6fd2953ed Mon Sep 17 00:00:00 2001 From c44f974b3550e4eadae976624a6184c3fd8d78a3 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co> From: Aikar <aikar@aikar.co>
Date: Mon, 28 Mar 2016 20:55:47 -0400 Date: Mon, 28 Mar 2016 20:55:47 -0400
Subject: [PATCH] MC Utils Subject: [PATCH] MC Utils
@ -51,12 +51,36 @@ index 1f2fe87b6..2cb462b8e 100644
protected DataBits b; protected DataBits b;
protected DataPalette c; protected DataPalette c;
private int e; private int e;
diff --git a/src/main/java/net/minecraft/server/EntityTypes.java b/src/main/java/net/minecraft/server/EntityTypes.java
index ba461ad48..66c270493 100644
--- a/src/main/java/net/minecraft/server/EntityTypes.java
+++ b/src/main/java/net/minecraft/server/EntityTypes.java
@@ -237,6 +237,10 @@ public class EntityTypes {
a("zombie_villager", 5651507, 7969893);
EntityTypes.d.add(EntityTypes.a);
}
+ // Paper start
+ public static Map<Class<? extends Entity>, MinecraftKey> clsToKeyMap = new java.util.HashMap<>();
+ public static Map<Class<? extends Entity>, org.bukkit.entity.EntityType> clsToTypeMap = new java.util.HashMap<>();
+ // Paper end
private static void a(int i, String s, Class<? extends Entity> oclass, String s1) {
try {
@@ -252,6 +256,8 @@ public class EntityTypes {
EntityTypes.b.a(i, minecraftkey, oclass);
EntityTypes.d.add(minecraftkey);
+ clsToKeyMap.put(oclass, minecraftkey); // Paper
+ clsToTypeMap.put(oclass, org.bukkit.entity.EntityType.fromName(s)); // Paper
while (EntityTypes.g.size() <= i) {
EntityTypes.g.add(null); // Paper - Decompile fix
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
new file mode 100644 new file mode 100644
index 000000000..1159eea1a index 000000000..a4b0901cf
--- /dev/null --- /dev/null
+++ b/src/main/java/net/minecraft/server/MCUtil.java +++ b/src/main/java/net/minecraft/server/MCUtil.java
@@ -0,0 +1,189 @@ @@ -0,0 +1,201 @@
+package net.minecraft.server; +package net.minecraft.server;
+ +
+import com.google.common.util.concurrent.ThreadFactoryBuilder; +import com.google.common.util.concurrent.ThreadFactoryBuilder;
@ -176,6 +200,18 @@ index 000000000..1159eea1a
+ /** + /**
+ * Converts a NMS World/BlockPosition to Bukkit Location + * Converts a NMS World/BlockPosition to Bukkit Location
+ * @param world + * @param world
+ * @param x
+ * @param y
+ * @param z
+ * @return
+ */
+ public static Location toLocation(World world, double x, double y, double z) {
+ return new Location(world.getWorld(), x, y, z);
+ }
+
+ /**
+ * Converts a NMS World/BlockPosition to Bukkit Location
+ * @param world
+ * @param pos + * @param pos
+ * @return + * @return
+ */ + */
@ -312,5 +348,5 @@ index 8cede938a..cd2d58bfb 100644
return System.nanoTime() / 1000000L; return System.nanoTime() / 1000000L;
} }
-- --
2.15.0 2.15.1

View File

@ -1,11 +1,11 @@
From 83e3feb7b9c8a8e51732e592149a8e48fa926ed9 Mon Sep 17 00:00:00 2001 From d30873880e717097e6a7661b180f377b26818f23 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co> From: Aikar <aikar@aikar.co>
Date: Tue, 27 Dec 2016 15:02:42 -0500 Date: Tue, 27 Dec 2016 15:02:42 -0500
Subject: [PATCH] String based Action Bar API Subject: [PATCH] String based Action Bar API
diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java diff --git a/src/main/java/net/minecraft/server/MCUtil.java b/src/main/java/net/minecraft/server/MCUtil.java
index 1159eea1..38359517 100644 index a4b0901cf..02940d697 100644
--- a/src/main/java/net/minecraft/server/MCUtil.java --- a/src/main/java/net/minecraft/server/MCUtil.java
+++ b/src/main/java/net/minecraft/server/MCUtil.java +++ b/src/main/java/net/minecraft/server/MCUtil.java
@@ -1,10 +1,13 @@ @@ -1,10 +1,13 @@
@ -47,7 +47,7 @@ index 1159eea1..38359517 100644
/** /**
* Ensures the target code is running on the main thread * Ensures the target code is running on the main thread
@@ -186,4 +207,13 @@ public final class MCUtil { @@ -198,4 +219,13 @@ public final class MCUtil {
} }
return null; return null;
} }
@ -62,7 +62,7 @@ index 1159eea1..38359517 100644
+ } + }
} }
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
index ef873c17..b17f2c06 100644 index ef873c17d..b17f2c067 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java --- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java +++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
@@ -178,6 +178,18 @@ public class CraftPlayer extends CraftHumanEntity implements Player { @@ -178,6 +178,18 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
@ -85,5 +85,5 @@ index ef873c17..b17f2c06 100644
public void setPlayerListHeaderFooter(BaseComponent[] header, BaseComponent[] footer) { public void setPlayerListHeaderFooter(BaseComponent[] header, BaseComponent[] footer) {
PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter(); PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
-- --
2.14.3 2.15.1

View File

@ -0,0 +1,82 @@
From b27bbefc11af081a7e8e7e5212b964c8fc814777 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 14 Jan 2018 17:01:31 -0500
Subject: [PATCH] PreCreatureSpawnEvent
Adds an event to fire before an Entity is created, so that plugins that need to cancel
CreatureSpawnEvent can do so from this event instead.
Cancelling CreatureSpawnEvent rapidly causes a lot of garbage collection and CPU waste
as it's done after the Entity object has been fully created.
Mob Limiting plugins and blanket "ban this type of monster" plugins should use this event
instead and save a lot of server resources.
See: https://github.com/PaperMC/Paper/issues/917
diff --git a/src/main/java/net/minecraft/server/MobSpawnerAbstract.java b/src/main/java/net/minecraft/server/MobSpawnerAbstract.java
index c29df55fa..d6c73a5a0 100644
--- a/src/main/java/net/minecraft/server/MobSpawnerAbstract.java
+++ b/src/main/java/net/minecraft/server/MobSpawnerAbstract.java
@@ -1,6 +1,7 @@
package net.minecraft.server;
import com.google.common.collect.Lists;
+
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nullable;
@@ -87,6 +88,23 @@ public abstract class MobSpawnerAbstract {
double d3 = j >= 1 ? nbttaglist.f(0) : (double) blockposition.getX() + (world.random.nextDouble() - world.random.nextDouble()) * (double) this.spawnRange + 0.5D;
double d4 = j >= 2 ? nbttaglist.f(1) : (double) (blockposition.getY() + world.random.nextInt(3) - 1);
double d5 = j >= 3 ? nbttaglist.f(2) : (double) blockposition.getZ() + (world.random.nextDouble() - world.random.nextDouble()) * (double) this.spawnRange + 0.5D;
+ // Paper start
+ if (this.getMobName() == null) {
+ return;
+ }
+ com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent event;
+ event = new com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent(
+ MCUtil.toLocation(world, d3, d4, d5),
+ org.bukkit.entity.EntityType.fromName(this.getMobName().getKey()),
+ org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.SPAWNER
+ );
+ if (!event.callEvent()) {
+ if (event.shouldAbortSpawn()) {
+ break;
+ }
+ continue;
+ }
+ // Paper end
Entity entity = ChunkRegionLoader.a(nbttagcompound, world, d3, d4, d5, false);
if (entity == null) {
diff --git a/src/main/java/net/minecraft/server/SpawnerCreature.java b/src/main/java/net/minecraft/server/SpawnerCreature.java
index 2cd063829..1137dda86 100644
--- a/src/main/java/net/minecraft/server/SpawnerCreature.java
+++ b/src/main/java/net/minecraft/server/SpawnerCreature.java
@@ -184,6 +184,22 @@ public final class SpawnerCreature {
}
if (worldserver.a(enumcreaturetype, biomebase_biomemeta, (BlockPosition) blockposition_mutableblockposition) && a(EntityPositionTypes.a(biomebase_biomemeta.b), worldserver, blockposition_mutableblockposition)) {
+ // Paper start
+ com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent event;
+ event = new com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent(
+ MCUtil.toLocation(worldserver, blockposition_mutableblockposition),
+ EntityTypes.clsToTypeMap.get(biomebase_biomemeta.b),
+ SpawnReason.NATURAL
+ );
+ if (!event.callEvent()) {
+ if (event.shouldAbortSpawn()) {
+ continue label120;
+ }
+ j1 += l2;
+ ++j4;
+ continue;
+ }
+ // Paper end
EntityInsentient entityinsentient;
try {
--
2.15.1