use of org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason 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.");
}
use of org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason in project Glowstone by GlowstoneMC.
the class GlowWorld method spawnGlowEntity.
/**
* Spawns an entity.
*
* @param location the {@link Location} to spawn the entity at
* @param clazz the class of the {@link Entity} to spawn
* @param reason the reason for the spawning of the entity
* @return an instance of the spawned {@link Entity}
* @throws IllegalArgumentException TODO: document the reason this can happen
*/
public <T extends GlowEntity, E extends Entity> GlowEntity spawnGlowEntity(Location location, Class<T> clazz, SpawnReason reason, @Nullable Consumer<E> function) throws IllegalArgumentException {
checkNotNull(location);
checkNotNull(clazz);
GlowEntity entity = null;
try {
if (EntityRegistry.getEntity(clazz) != null) {
entity = EntityStorage.create(clazz, location);
}
if (entity != null && function != null) {
function.accept((E) entity);
}
EntitySpawnEvent spawnEvent = null;
if (entity instanceof LivingEntity) {
spawnEvent = EventFactory.getInstance().callEvent(new CreatureSpawnEvent((LivingEntity) entity, reason));
} else if (!(entity instanceof Item)) {
// ItemSpawnEvent is called elsewhere
spawnEvent = EventFactory.getInstance().callEvent(new EntitySpawnEvent(entity));
}
if (spawnEvent != null && spawnEvent.isCancelled()) {
// TODO: separate spawning and construction for better event cancellation
entity.remove();
} else {
List<Message> spawnMessage = entity.createSpawnMessage();
final GlowEntity finalEntity = entity;
getRawPlayers().stream().filter(player -> player.canSeeEntity(finalEntity)).forEach(player -> player.getSession().sendAll(spawnMessage.toArray(new Message[spawnMessage.size()])));
}
} catch (NoSuchMethodError | IllegalAccessError e) {
GlowServer.logger.log(Level.WARNING, "Invalid entity spawn: ", e);
} catch (Throwable t) {
GlowServer.logger.log(Level.SEVERE, "Unable to spawn entity: ", t);
}
if (entity != null) {
return entity;
}
throw new UnsupportedOperationException("Not supported yet.");
}
use of org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason in project InfernalMobs by NyaaCat.
the class ConfigReader method getEnabledSpawnReasons.
public static List<SpawnReason> getEnabledSpawnReasons() {
List<SpawnReason> ret = new ArrayList<>();
for (String str : cfg().getStringList("enabledSpawnReasons")) {
try {
SpawnReason reason = SpawnReason.valueOf(str);
ret.add(reason);
} catch (Exception ex) {
InfernalMobs.instance.getLogger().warning(str + " is not a valid spawn reason");
}
}
return ret;
}
Aggregations