2020-05-01 22:03:47 +00:00
|
|
|
From b70880b6efa57cdc1bc10a32ff65d2bd8d2a9294 Mon Sep 17 00:00:00 2001
|
2018-01-14 22:02:38 +00:00
|
|
|
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
|
2020-04-02 21:07:06 +00:00
|
|
|
index 000000000..d5edde9cd
|
2018-01-14 22:02:38 +00:00
|
|
|
--- /dev/null
|
|
|
|
+++ b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java
|
2019-03-20 00:28:15 +00:00
|
|
|
@@ -0,0 +1,104 @@
|
2018-01-14 22:02:38 +00:00
|
|
|
+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;
|
2019-03-20 00:28:15 +00:00
|
|
|
+import org.jetbrains.annotations.NotNull;
|
2018-01-14 22:02:38 +00:00
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * 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.
|
2018-01-29 03:50:35 +00:00
|
|
|
+ * Also, Plugins that replace Entity Registrations with their own custom entities might not fire this event.
|
2018-01-14 22:02:38 +00:00
|
|
|
+ */
|
|
|
|
+public class PreCreatureSpawnEvent extends Event implements Cancellable {
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull private final Location location;
|
|
|
|
+ @NotNull private final EntityType type;
|
|
|
|
+ @NotNull private final CreatureSpawnEvent.SpawnReason reason;
|
2018-01-14 22:02:38 +00:00
|
|
|
+ private boolean shouldAbortSpawn;
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+ public PreCreatureSpawnEvent(@NotNull Location location, @NotNull EntityType type, @NotNull CreatureSpawnEvent.SpawnReason reason) {
|
2018-01-14 22:02:38 +00:00
|
|
|
+ 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
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-01-14 22:02:38 +00:00
|
|
|
+ public Location getSpawnLocation() {
|
|
|
|
+ return location;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return The type of creature being spawned
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-01-14 22:02:38 +00:00
|
|
|
+ public EntityType getType() {
|
|
|
|
+ return type;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * @return Reason this creature is spawning (ie, NATURAL vs SPAWNER)
|
|
|
|
+ */
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-01-14 22:02:38 +00:00
|
|
|
+ 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();
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-01-14 22:02:38 +00:00
|
|
|
+ public HandlerList getHandlers() {
|
|
|
|
+ return handlers;
|
|
|
|
+ }
|
|
|
|
+
|
2019-03-20 00:28:15 +00:00
|
|
|
+ @NotNull
|
2018-01-14 22:02:38 +00:00
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
--
|
2020-05-01 22:03:47 +00:00
|
|
|
2.26.2
|
2018-01-14 22:02:38 +00:00
|
|
|
|