Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

EntityData - check if entity can spawn #6484

Merged
merged 6 commits into from
Mar 9, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/main/java/ch/njol/skript/entity/EntityData.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import org.bukkit.RegionAccessor;
import org.bukkit.World;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.eclipse.jdt.annotation.Nullable;

Expand Down Expand Up @@ -94,6 +95,7 @@ public abstract class EntityData<E extends Entity> implements SyntaxElement, Ygg
} catch (NoSuchMethodException | SecurityException ignored) { /* We already checked if the method exists */ }
}

private static final boolean HAS_ENABLED_BY_FEATURE = Skript.methodExists(org.bukkit.entity.EntityType.class, "isEnabledByFeature", World.class);
APickledWalrus marked this conversation as resolved.
Show resolved Hide resolved
public final static String LANGUAGE_NODE = "entities";

public final static Message m_age_pattern = new Message(LANGUAGE_NODE + ".age pattern");
Expand Down Expand Up @@ -463,6 +465,23 @@ private E apply(E entity) {
return entity;
}

/**
* Check if this entity type can spawn.
* <p>Some entity types may be restricted by experimental datapacks.</p>
*
* @param world World to check if entity can spawn in
* @return True if entity can spawn else false
*/
public boolean canSpawn(World world) {
if (HAS_ENABLED_BY_FEATURE) {
// Check if the entity can actually be spawned
// Some entity types may be restricted by experimental datapacks
EntityType bukkitEntityType = EntityUtils.toBukkitEntityType(this);
return bukkitEntityType.isEnabledByFeature(world);
}
return true;
}

/**
* Spawn this entity data at a location.
*
Expand All @@ -476,7 +495,7 @@ public final E spawn(Location location) {

/**
* Spawn this entity data at a location.
* The consumer allows for modiciation to the entity before it actually gets spawned.
* The consumer allows for modification to the entity before it actually gets spawned.
* <p>
* Bukkit's own {@link org.bukkit.util.Consumer} is deprecated.
* Use {@link #spawn(Location, Consumer)}
Expand All @@ -494,7 +513,7 @@ public E spawn(Location location, org.bukkit.util.@Nullable Consumer<E> consumer

/**
* Spawn this entity data at a location.
* The consumer allows for modiciation to the entity before it actually gets spawned.
* The consumer allows for modification to the entity before it actually gets spawned.
*
* @param location The {@link Location} to spawn the entity at.
* @param consumer A {@link Consumer} to apply the entity changes to.
Expand All @@ -503,10 +522,13 @@ public E spawn(Location location, org.bukkit.util.@Nullable Consumer<E> consumer
@Nullable
public E spawn(Location location, @Nullable Consumer<E> consumer) {
assert location != null;
World world = location.getWorld();
if (world == null || !canSpawn(world))
ShaneBeee marked this conversation as resolved.
Show resolved Hide resolved
return null;
if (consumer != null) {
return EntityData.spawn(location, getType(), e -> consumer.accept(this.apply(e)));
} else {
return apply(location.getWorld().spawn(location, getType()));
return apply(world.spawn(location, getType()));
}
}

Expand Down
Loading