48 lines
1.6 KiB
Java
48 lines
1.6 KiB
Java
package games.dmg.spigottyrant;
|
|
|
|
import org.bukkit.Location;
|
|
import org.bukkit.Server;
|
|
import org.bukkit.entity.EntitySnapshot;
|
|
import org.bukkit.entity.Player;
|
|
|
|
public final class BukkitCapturedMobSpawner implements CapturedMobSpawner {
|
|
private final Server server;
|
|
private final PluginSettings settings;
|
|
|
|
public BukkitCapturedMobSpawner(Server server, PluginSettings settings) {
|
|
this.server = server;
|
|
this.settings = settings;
|
|
}
|
|
|
|
@Override
|
|
public boolean spawn(Player player, CapturedMob mob) {
|
|
Location location = player.getLocation();
|
|
if (!safe(location)
|
|
|| "ENDER_DRAGON".equals(mob.entityType())
|
|
|| settings.deniedMobTypes().contains(mob.entityType())) {
|
|
return false;
|
|
}
|
|
String serialized = mob.data().get("snapshot");
|
|
if (serialized == null || serialized.isBlank()) {
|
|
return false;
|
|
}
|
|
try {
|
|
EntitySnapshot snapshot = server.getEntityFactory().createEntitySnapshot(serialized);
|
|
if (!snapshot.getEntityType().name().equals(mob.entityType())) {
|
|
return false;
|
|
}
|
|
snapshot.createEntity(location);
|
|
return true;
|
|
} catch (IllegalArgumentException | IllegalStateException exception) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
private static boolean safe(Location location) {
|
|
return location.getWorld() != null
|
|
&& location.getBlock().isPassable()
|
|
&& location.clone().add(0.0, 1.0, 0.0).getBlock().isPassable()
|
|
&& location.getWorld().getWorldBorder().isInside(location);
|
|
}
|
|
}
|