From 4e682b5c4fa3c6c6c11276c9a052eaeaa891f113 Mon Sep 17 00:00:00 2001 From: APickledWalrus Date: Mon, 3 Jun 2024 16:03:25 -0400 Subject: [PATCH] SimpleEntityData: canSpawn override support --- .../njol/skript/entity/SimpleEntityData.java | 36 ++++++++++++++----- .../regressions/6762-cant spawn a firework.sk | 4 +++ 2 files changed, 31 insertions(+), 9 deletions(-) create mode 100644 src/test/skript/tests/regressions/6762-cant spawn a firework.sk diff --git a/src/main/java/ch/njol/skript/entity/SimpleEntityData.java b/src/main/java/ch/njol/skript/entity/SimpleEntityData.java index 6cad5ec1a2c..504c4449dc7 100644 --- a/src/main/java/ch/njol/skript/entity/SimpleEntityData.java +++ b/src/main/java/ch/njol/skript/entity/SimpleEntityData.java @@ -23,6 +23,8 @@ import java.util.ArrayList; import java.util.List; +import ch.njol.util.Kleenean; +import org.bukkit.World; import org.bukkit.entity.AbstractHorse; import org.bukkit.entity.Allay; import org.bukkit.entity.Animals; @@ -155,15 +157,13 @@ public final static class SimpleEntityDataInfo { final String codeName; final Class c; final boolean isSupertype; + final Kleenean allowSpawning; - SimpleEntityDataInfo(final String codeName, final Class c) { - this(codeName, c, false); - } - - SimpleEntityDataInfo(final String codeName, final Class c, final boolean isSupertype) { + SimpleEntityDataInfo(String codeName, Class c, boolean isSupertype, Kleenean allowSpawning) { this.codeName = codeName; this.c = c; this.isSupertype = isSupertype; + this.allowSpawning = allowSpawning; } @Override @@ -191,11 +191,18 @@ public boolean equals(final @Nullable Object obj) { private final static List types = new ArrayList<>(); private static void addSimpleEntity(String codeName, Class entityClass) { - types.add(new SimpleEntityDataInfo(codeName, entityClass)); + addSimpleEntity(codeName, entityClass, Kleenean.UNKNOWN); + } + + /** + * @param allowSpawning Whether to override the default {@link #canSpawn(World)} behavior and allow this entity to be spawned. + */ + private static void addSimpleEntity(String codeName, Class entityClass, Kleenean allowSpawning) { + types.add(new SimpleEntityDataInfo(codeName, entityClass, false, allowSpawning)); } private static void addSuperEntity(String codeName, Class entityClass) { - types.add(new SimpleEntityDataInfo(codeName, entityClass, true)); + types.add(new SimpleEntityDataInfo(codeName, entityClass, true, Kleenean.UNKNOWN)); } static { // Simple Entities @@ -238,7 +245,9 @@ private static void addSuperEntity(String codeName, Class enti addSimpleEntity("witch", Witch.class); addSimpleEntity("wither", Wither.class); addSimpleEntity("wither skull", WitherSkull.class); - addSimpleEntity("firework", Firework.class); + // bukkit marks fireworks as not spawnable + // see https://hub.spigotmc.org/jira/browse/SPIGOT-7677 + addSimpleEntity("firework", Firework.class, Kleenean.TRUE); addSimpleEntity("endermite", Endermite.class); addSimpleEntity("armor stand", ArmorStand.class); addSimpleEntity("shulker", Shulker.class); @@ -449,7 +458,16 @@ protected boolean equals_i(final EntityData obj) { final SimpleEntityData other = (SimpleEntityData) obj; return info.equals(other.info); } - + + @Override + public boolean canSpawn(@Nullable World world) { + if (info.allowSpawning.isUnknown()) // unspecified, refer to default behavior + return super.canSpawn(world); + if (world == null) + return false; + return info.allowSpawning.isTrue(); + } + @Override public Fields serialize() throws NotSerializableException { final Fields f = super.serialize(); diff --git a/src/test/skript/tests/regressions/6762-cant spawn a firework.sk b/src/test/skript/tests/regressions/6762-cant spawn a firework.sk new file mode 100644 index 00000000000..f272e0bc8e0 --- /dev/null +++ b/src/test/skript/tests/regressions/6762-cant spawn a firework.sk @@ -0,0 +1,4 @@ +test "can't spawn a firework": + + spawn a firework at spawn of world "world" + assert last spawned firework is set with "firework did not spawn"