2929import ch .njol .yggdrasil .Fields ;
3030import ch .njol .yggdrasil .Fields .FieldContext ;
3131import ch .njol .yggdrasil .YggdrasilSerializable .YggdrasilExtendedSerializable ;
32+ import io .papermc .paper .world .flag .FeatureDependant ;
33+ import io .papermc .paper .world .flag .FeatureFlagSetHolder ;
3234import org .bukkit .Bukkit ;
3335import org .bukkit .Chunk ;
3436import org .bukkit .Location ;
5860@ SuppressWarnings ("rawtypes" )
5961public abstract class EntityData <E extends Entity > implements SyntaxElement , YggdrasilExtendedSerializable {
6062
61- /*
62- * In 1.20.2 Spigot deprecated org.bukkit.util.Consumer.
63- * From the class header: "API methods which use this consumer will be remapped to Java's consumer at runtime, resulting in an error."
64- * But in 1.13-1.16 the only way to use a consumer was World#spawn(Location, Class, org.bukkit.util.Consumer).
65- */
66- protected static @ Nullable Method WORLD_1_17_CONSUMER_METHOD ;
67- protected static boolean WORLD_1_17_CONSUMER ;
63+ // Removed in 1.21.9 in favor of 'FeatureFlagSetHolder'
64+ private static final boolean HAS_ENABLED_BY_FEATURE = Skript .methodExists (EntityType .class , "isEnabledByFeature" , World .class );
65+ private static final @ Nullable Method ENABLED_BY_FEATURE_METHOD ;
66+
67+ // Added in 1.21.9, replaces 'isEnabledByFeature'
68+ private static final @ Nullable Method IS_ENABLED_METHOD ;
6869
6970 static {
70- try {
71- if (WORLD_1_17_CONSUMER = Skript .methodExists (RegionAccessor .class , "spawn" , Location .class , Class .class , org .bukkit .util .Consumer .class ))
72- WORLD_1_17_CONSUMER_METHOD = RegionAccessor .class .getDeclaredMethod ("spawn" , Location .class , Class .class , org .bukkit .util .Consumer .class );
73- } catch (NoSuchMethodException | SecurityException ignored ) { /* We already checked if the method exists */ }
71+ if (HAS_ENABLED_BY_FEATURE ) {
72+ IS_ENABLED_METHOD = null ;
73+ try {
74+ ENABLED_BY_FEATURE_METHOD = EntityType .class .getDeclaredMethod ("isEnabledByFeature" , World .class );
75+ } catch (NoSuchMethodException e ) {
76+ throw new RuntimeException (e );
77+ }
78+ } else {
79+ ENABLED_BY_FEATURE_METHOD = null ;
80+ try {
81+ IS_ENABLED_METHOD = FeatureFlagSetHolder .class .getDeclaredMethod ("isEnabled" , FeatureDependant .class );
82+ } catch (NoSuchMethodException e ) {
83+ throw new RuntimeException (e );
84+ }
85+ }
7486 }
75-
76- private static final boolean HAS_ENABLED_BY_FEATURE = Skript .methodExists (EntityType .class , "isEnabledByFeature" , World .class );
7787 public static final String LANGUAGE_NODE = "entities" ;
7888
7989 public static final Message m_age_pattern = new Message (LANGUAGE_NODE + ".age pattern" );
@@ -638,19 +648,28 @@ private E apply(E entity) {
638648 * @param world The world to check spawning permissions in.
639649 * @return {@code true} if the entity can be spawned in the given world, or in general if world is {@code null}; otherwise {@code false}.
640650 */
641- @ SuppressWarnings ({"removal" })
642651 public boolean canSpawn (@ Nullable World world ) {
643652 if (world == null )
644653 return false ;
645654 EntityType bukkitEntityType = info .entityType != null ? info .entityType : EntityUtils .toBukkitEntityType (this );
646- if (bukkitEntityType == null )
655+ if (bukkitEntityType == null || ! bukkitEntityType . isSpawnable () )
647656 return false ;
648657 if (HAS_ENABLED_BY_FEATURE ) {
649658 // Check if the entity can actually be spawned
650659 // Some entity types may be restricted by experimental datapacks
651- return bukkitEntityType .isEnabledByFeature (world ) && bukkitEntityType .isSpawnable ();
660+ assert ENABLED_BY_FEATURE_METHOD != null ;
661+ try {
662+ return (boolean ) ENABLED_BY_FEATURE_METHOD .invoke (bukkitEntityType , world );
663+ } catch (IllegalAccessException | InvocationTargetException e ) {
664+ return false ;
665+ }
666+ }
667+ assert IS_ENABLED_METHOD != null ;
668+ try {
669+ return (boolean ) IS_ENABLED_METHOD .invoke (world , bukkitEntityType );
670+ } catch (IllegalAccessException | InvocationTargetException e ) {
671+ return false ;
652672 }
653- return bukkitEntityType .isSpawnable ();
654673 }
655674
656675 /**
@@ -663,23 +682,6 @@ public boolean canSpawn(@Nullable World world) {
663682 return spawn (location , (Consumer <E >) null );
664683 }
665684
666- /**
667- * Spawn this entity data at a location.
668- * The consumer allows for modification to the entity before it actually gets spawned.
669- * <p>
670- * Bukkit's own {@link org.bukkit.util.Consumer} is deprecated.
671- * Use {@link #spawn(Location, Consumer)}
672- *
673- * @param location The {@link Location} to spawn the entity at.
674- * @param consumer A {@link Consumer} to apply the entity changes to.
675- * @return The Entity object that is spawned.
676- */
677- @ Deprecated (since = "2.8.0" , forRemoval = true )
678- @ SuppressWarnings ("deprecation" )
679- public @ Nullable E spawn (Location location , org .bukkit .util .@ Nullable Consumer <E > consumer ) {
680- return spawn (location , (Consumer <E >) e -> consumer .accept (e ));
681- }
682-
683685 /**
684686 * Spawn this entity data at a location.
685687 * The consumer allows for modification to the entity before it actually gets spawned.
@@ -887,20 +889,10 @@ public void deserialize(Fields fields) throws StreamCorruptedException, NotSeria
887889 return "entity data" ;
888890 }
889891
890- @ SuppressWarnings ({"unchecked" , "deprecation" })
891892 protected static <E extends Entity > @ Nullable E spawn (Location location , Class <E > type , Consumer <E > consumer ) {
892893 World world = location .getWorld ();
893894 if (world == null )
894895 return null ;
895- if (WORLD_1_17_CONSUMER ) {
896- try {
897- return (@ Nullable E ) WORLD_1_17_CONSUMER_METHOD .invoke (world , location , type , (org .bukkit .util .Consumer <E >) consumer ::accept );
898- } catch (InvocationTargetException | IllegalAccessException e ) {
899- if (Skript .testing ())
900- Skript .exception (e , "Can't spawn " + type .getName ());
901- return null ;
902- }
903- }
904896 return world .spawn (location , type , consumer );
905897 }
906898
0 commit comments