testserver/Spigot-Server-Patches/0343-Entity-getEntitySpawnR...

101 lines
5.5 KiB
Diff
Raw Normal View History

From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Aikar <aikar@aikar.co>
Date: Sun, 24 Mar 2019 00:24:52 -0400
Subject: [PATCH] Entity#getEntitySpawnReason
Allows you to return the SpawnReason for why an Entity Spawned
Pre existing entities will return NATURAL if it was a non
persistenting Living Entity, SPAWNER for spawners,
or DEFAULT since data was not stored.
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
2020-06-25 14:09:55 +00:00
index 84aa003d90b64c0a37e155f38041c7908860b7f9..8144433d6c309d49b624e88623a3dd376f7c62b3 100644
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
2020-06-25 14:09:55 +00:00
@@ -73,6 +73,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
}
};
List<Entity> entitySlice = null;
+ public org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason spawnReason;
// Paper end
public com.destroystokyo.paper.loottable.PaperLootableInventoryData lootableData; // Paper
2020-06-25 14:09:55 +00:00
@@ -1590,6 +1591,9 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2019-05-06 02:58:04 +00:00
if (this.origin != null) {
nbttagcompound.set("Paper.Origin", this.createList(origin.getX(), origin.getY(), origin.getZ()));
}
+ if (spawnReason != null) {
+ nbttagcompound.setString("Paper.SpawnReason", spawnReason.name());
+ }
// Save entity's from mob spawner status
if (spawnedViaMobSpawner) {
nbttagcompound.setBoolean("Paper.FromMobSpawner", true);
2020-06-25 14:09:55 +00:00
@@ -1718,6 +1722,26 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
}
spawnedViaMobSpawner = nbttagcompound.getBoolean("Paper.FromMobSpawner"); // Restore entity's from mob spawner status
+ if (nbttagcompound.hasKey("Paper.SpawnReason")) {
+ String spawnReasonName = nbttagcompound.getString("Paper.SpawnReason");
+ try {
+ spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.valueOf(spawnReasonName);
+ } catch (Exception ignored) {
+ LogManager.getLogger().error("Unknown SpawnReason " + spawnReasonName + " for " + this);
+ }
+ }
+ if (spawnReason == null) {
+ if (spawnedViaMobSpawner) {
+ spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.SPAWNER;
2019-05-05 17:19:34 +00:00
+ } else if (this instanceof EntityInsentient && (this instanceof EntityAnimal || this instanceof EntityFish) && !((EntityInsentient) this).isTypeNotPersistent(0.0)) {
+ if (!nbttagcompound.getBoolean("PersistenceRequired")) {
+ spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.NATURAL;
+ }
+ }
+ }
+ if (spawnReason == null) {
+ spawnReason = org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.DEFAULT;
+ }
// Paper end
} catch (Throwable throwable) {
diff --git a/src/main/java/net/minecraft/server/PlayerList.java b/src/main/java/net/minecraft/server/PlayerList.java
2020-06-25 14:09:55 +00:00
index e4651eb2a94080bfa431b0b775ae8019fc922538..4b8dd6c1a9c796ded5ee0a63ccecebca9ac2b8ca 100644
--- a/src/main/java/net/minecraft/server/PlayerList.java
+++ b/src/main/java/net/minecraft/server/PlayerList.java
2020-06-25 14:09:55 +00:00
@@ -263,7 +263,7 @@ public abstract class PlayerList {
2019-05-05 17:19:34 +00:00
// CraftBukkit start
2020-06-25 14:09:55 +00:00
WorldServer finalWorldServer = worldserver1;
2019-05-05 17:19:34 +00:00
Entity entity = EntityTypes.a(nbttagcompound1.getCompound("Entity"), finalWorldServer, (entity1) -> {
- return !finalWorldServer.addEntitySerialized(entity1) ? null : entity1;
+ return !finalWorldServer.addEntitySerialized(entity1, org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason.MOUNT) ? null : entity1; // Paper
// CraftBukkit end
});
2019-05-05 17:19:34 +00:00
diff --git a/src/main/java/net/minecraft/server/WorldServer.java b/src/main/java/net/minecraft/server/WorldServer.java
2020-06-25 14:09:55 +00:00
index 14ec04c6503cca58f64fc2a82a9fd715a06dcefb..a72f16cbcbc226854043bfec07e1e57dbe33c62c 100644
2019-05-05 17:19:34 +00:00
--- a/src/main/java/net/minecraft/server/WorldServer.java
+++ b/src/main/java/net/minecraft/server/WorldServer.java
2020-06-25 14:09:55 +00:00
@@ -923,6 +923,7 @@ public class WorldServer extends World implements GeneratorAccessSeed {
2019-05-05 17:19:34 +00:00
// CraftBukkit start
private boolean addEntity0(Entity entity, CreatureSpawnEvent.SpawnReason spawnReason) {
org.spigotmc.AsyncCatcher.catchOp("entity add"); // Spigot
2019-05-05 17:19:34 +00:00
+ if (entity.spawnReason == null) entity.spawnReason = spawnReason; // Paper
// Paper start
if (entity.valid) {
MinecraftServer.LOGGER.error("Attempted Double World add on " + entity, new Throwable());
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
2020-06-25 14:09:55 +00:00
index 51f027e8f98240fec989ea877095031bab087bda..821417610fdb23791bd83e263977026b9d09e31a 100644
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftEntity.java
2020-06-25 14:09:55 +00:00
@@ -1060,5 +1060,10 @@ public abstract class CraftEntity implements org.bukkit.entity.Entity {
public boolean fromMobSpawner() {
return getHandle().spawnedViaMobSpawner;
}
+
2019-05-05 17:19:34 +00:00
+ @Override
+ public org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason getEntitySpawnReason() {
+ return getHandle().spawnReason;
+ }
// Paper end
}