use of org.bukkit.event.entity.ItemSpawnEvent in project Glowstone by GlowstoneMC.
the class GlowWorld method dropItem.
/**
* Spawn an item at the given {@link Location} without shooting effect.
*
* @param location the {@link Location} to spawn the item at
* @param item the {@link ItemStack} the item should have
*/
@Override
public GlowItem dropItem(Location location, ItemStack item) {
checkNotNull(location);
GlowItem entity = new GlowItem(location, item);
ItemSpawnEvent event = EventFactory.getInstance().callEvent(new ItemSpawnEvent(entity));
if (event.isCancelled()) {
entity.remove();
}
return entity;
}
use of org.bukkit.event.entity.ItemSpawnEvent 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.");
}
Aggregations