I’d like to implement a feature where when a command is run, the plugin
(1) Checks if the ender dragon is still alive in the end
(2) If dead, then spawns a new one
I’m not sure how to do the checking, so help would be appreciated there.
For the spawning, is it as simple as simply summoning an ender dragon in that location, and can I be sure that it’ll behave like a regular ender dragon (e.g. not fly away from the island)? Thank you!
Unless I’m mistaken, you can mainly only check for LOADED entities in any world (also the end), which is quite simple to do, however means it can skip over the enderdragon, when for instance no one is in the end.
As for (2), you can call a certain function to return a “default” entity, which should be the best way to create a normal functioning enderdragon. You can then spawn this one at any position you give it.
Below is my example code extract.
Note: This can likely be done better, but this should give you an idea
//no idea how to get the end world specifically, this might not work like this
Optional<World> endWorldopt = Sponge.getServer().getWorld("the_end");
if(endWorldopt.isPresent())
{
World endWorld = endWorldopt.get();
//below we look through all the LOADED entities in the world and filter everything out that isn't a enderdragon
if(endWorld.getEntities(EnderDragon.class::isInstance).size() > 1)
{
//if the size of the list we get (which is only going to contain enderdragons) is greater 1 (meaning there is a enderdragon loaded)
//we spawn a new one
Vector3d position = new Vector3d(); //get the position from command context or whatever
Entity edragon = endWorld.createEntityNaturally(EntityTypes.ENDER_DRAGON, position); //creates a default entity of that type
endWorld.spawnEntity(edragon); //actually spawns it in the world
}
}