use of org.bukkit.event.entity.EntitySpawnEvent in project Glowstone by GlowstoneMC.
the class GlowWorld method spawn.
public GlowEntity spawn(Location location, Class<? extends GlowEntity> clazz, SpawnReason reason) throws IllegalArgumentException {
GlowEntity entity = null;
if (TNTPrimed.class.isAssignableFrom(clazz)) {
entity = new GlowTNTPrimed(location, null);
}
if (entity == null) {
try {
Constructor<? extends GlowEntity> constructor = clazz.getConstructor(Location.class);
entity = constructor.newInstance(location);
GlowEntity impl = entity;
// function.accept(entity); TODO: work on type mismatches
EntitySpawnEvent spawnEvent;
if (entity instanceof LivingEntity) {
spawnEvent = EventFactory.callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason));
} else {
spawnEvent = EventFactory.callEvent(new EntitySpawnEvent(entity));
}
if (!spawnEvent.isCancelled()) {
List<Message> spawnMessage = entity.createSpawnMessage();
getRawPlayers().stream().filter(player -> player.canSeeEntity(impl)).forEach(player -> player.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
} else {
// TODO: separate spawning and construction for better event cancellation
entity.remove();
}
} catch (NoSuchMethodException e) {
GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e);
} catch (IllegalAccessException | InstantiationException | InvocationTargetException e) {
GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", e);
}
}
if (entity != null) {
return entity;
}
throw new UnsupportedOperationException("Not supported yet.");
}
Aggregations