Archiviert
13
0
Dieses Repository wurde am 2024-12-25 archiviert. Du kannst Dateien ansehen und es klonen, aber nicht pushen oder Issues/Pull-Requests öffnen.
Paper-Old/patches/api/0082-PreCreatureSpawnEvent.patch

132 Zeilen
4.5 KiB
Diff

2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 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 0000000000000000000000000000000000000000..633ef4dcc701916f2dbfefbbebd5994f93ffc2a4
2021-06-11 14:02:28 +02:00
--- /dev/null
+++ b/src/main/java/com/destroystokyo/paper/event/entity/PreCreatureSpawnEvent.java
2024-02-01 10:15:57 +01:00
@@ -0,0 +1,109 @@
2021-06-11 14:02:28 +02:00
+package com.destroystokyo.paper.event.entity;
+
+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;
2024-02-01 10:15:57 +01:00
+import org.jetbrains.annotations.ApiStatus;
2021-06-11 14:02:28 +02:00
+import org.jetbrains.annotations.NotNull;
+
+/**
2024-02-01 10:15:57 +01:00
+ * WARNING: This event only fires for a limited number of cases, and not for every case that {@link CreatureSpawnEvent} does.
+ * <p>
+ * You should still listen to {@link CreatureSpawnEvent} as a backup, and only use this event as an "enhancement".
2021-06-11 14:02:28 +02:00
+ * The intent of this event is to improve server performance, so it fires even if the spawning might fail later, for
+ * example when the entity would be unable to spawn due to limited space or lighting.
2024-02-01 10:15:57 +01:00
+ * <p>
+ * Currently: NATURAL and SPAWNER based reasons. <!-- Please submit a Pull Request for future additions. -->
2021-06-11 14:02:28 +02:00
+ * Also, Plugins that replace Entity Registrations with their own custom entities might not fire this event.
+ */
+public class PreCreatureSpawnEvent extends Event implements Cancellable {
2024-02-01 10:15:57 +01:00
+
+ private static final HandlerList HANDLER_LIST = new HandlerList();
+
2021-06-11 14:02:28 +02:00
+ @NotNull private final Location location;
+ @NotNull private final EntityType type;
+ @NotNull private final CreatureSpawnEvent.SpawnReason reason;
+ private boolean shouldAbortSpawn;
+
2024-02-01 10:15:57 +01:00
+ private boolean cancelled;
+
+ @ApiStatus.Internal
2021-06-11 14:02:28 +02:00
+ public PreCreatureSpawnEvent(@NotNull Location location, @NotNull EntityType type, @NotNull CreatureSpawnEvent.SpawnReason reason) {
2024-02-01 10:15:57 +01:00
+ this.location = location;
+ this.type = type;
+ this.reason = reason;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return The location this creature is being spawned at
+ */
+ @NotNull
+ public Location getSpawnLocation() {
+ return this.location.clone();
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return The type of creature being spawned
+ */
+ @NotNull
+ public EntityType getType() {
2024-02-01 10:15:57 +01:00
+ return this.type;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return Reason this creature is spawning (ie, NATURAL vs SPAWNER)
+ */
+ @NotNull
+ public CreatureSpawnEvent.SpawnReason getReason() {
2024-02-01 10:15:57 +01:00
+ return this.reason;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * @return If the spawn process should be aborted vs trying more attempts
+ */
+ public boolean shouldAbortSpawn() {
2024-02-01 10:15:57 +01:00
+ return this.shouldAbortSpawn;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
+ * 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;
+ }
+
+ /**
+ * @return If the spawn of this creature is cancelled or not
+ */
+ @Override
+ public boolean isCancelled() {
2024-02-01 10:15:57 +01:00
+ return this.cancelled;
2021-06-11 14:02:28 +02:00
+ }
+
+ /**
2024-02-01 10:15:57 +01:00
+ * Cancelling this event is more efficient than cancelling {@link CreatureSpawnEvent}
+ *
+ * @param cancel {@code true} if you wish to cancel this event, and abort the spawn of this creature
2021-06-11 14:02:28 +02:00
+ */
+ @Override
+ public void setCancelled(boolean cancel) {
2024-02-01 10:15:57 +01:00
+ this.cancelled = cancel;
+ }
+
+ @NotNull
+ @Override
+ public HandlerList getHandlers() {
+ return HANDLER_LIST;
+ }
+
+ @NotNull
+ public static HandlerList getHandlerList() {
+ return HANDLER_LIST;
2021-06-11 14:02:28 +02:00
+ }
+}