use of org.bukkit.event.entity.CreatureSpawnEvent 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 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 in project StackMob-2 by Nathat23.
the class SpawnEvent method onCreatureSpawn.
@EventHandler
public void onCreatureSpawn(CreatureSpawnEvent e) {
final Entity newEntity = e.getEntity();
final CreatureSpawnEvent.SpawnReason sr = e.getSpawnReason();
// EntityTools before running task
if (newEntity instanceof ArmorStand) {
return;
}
if (sm.config.getCustomConfig().getStringList("no-stack-reasons").contains(sr.toString())) {
return;
}
if (sm.config.getCustomConfig().getStringList("no-stack-types").contains(newEntity.getType().toString())) {
return;
}
if (sm.config.getCustomConfig().getStringList("no-stack-worlds").contains(newEntity.getWorld().getName())) {
return;
}
// BukkitRunnable to delay this, so the needed metadata can be set before attempting to merge.
new BukkitRunnable() {
@Override
public void run() {
// EntityTools before attempting to merge with other entities
if (newEntity.hasMetadata(GlobalValues.NO_SPAWN_STACK) && newEntity.getMetadata(GlobalValues.NO_SPAWN_STACK).get(0).asBoolean()) {
newEntity.removeMetadata(GlobalValues.NO_SPAWN_STACK, sm);
return;
}
if (sm.pluginSupport.isMiniPet(newEntity)) {
return;
}
// Check for nearby entities, and merge if compatible.
double xLoc = sm.config.getCustomConfig().getDouble("check-area.x");
double yLoc = sm.config.getCustomConfig().getDouble("check-area.y");
double zLoc = sm.config.getCustomConfig().getDouble("check-area.z");
boolean noMatch = true;
for (Entity nearby : newEntity.getNearbyEntities(xLoc, yLoc, zLoc)) {
// EntityTools on both entities
if (newEntity.getType() != nearby.getType()) {
continue;
}
if (!nearby.hasMetadata(GlobalValues.METATAG)) {
continue;
}
if (sm.tools.notMatching(newEntity, nearby)) {
continue;
} else {
noMatch = false;
}
if (sm.config.getCustomConfig().isInt("custom." + nearby.getType() + ".stack-max")) {
if (nearby.getMetadata(GlobalValues.METATAG).get(0).asInt() + 1 > sm.config.getCustomConfig().getInt("custom." + nearby.getType() + ".stack-max")) {
continue;
}
} else {
if (nearby.getMetadata(GlobalValues.METATAG).get(0).asInt() + 1 > sm.config.getCustomConfig().getInt("stack-max")) {
continue;
}
}
// Continue to stack, if match is found
newEntity.remove();
int oldSize = nearby.getMetadata(GlobalValues.METATAG).get(0).asInt();
nearby.setMetadata(GlobalValues.METATAG, new FixedMetadataValue(sm, oldSize + 1));
return;
}
if (sm.config.getCustomConfig().getInt("dont-stack-until") > 0 && noMatch) {
if (sm.tools.notEnoughNearby(newEntity)) {
newEntity.setMetadata(GlobalValues.NOT_ENOUGH_NEAR, new FixedMetadataValue(sm, true));
}
} else {
// No match was found
newEntity.setMetadata(GlobalValues.METATAG, new FixedMetadataValue(sm, 1));
}
// Set mcMMO stuff
sm.pluginSupport.setMcmmoMetadata(newEntity);
}
}.runTaskLater(sm, 1);
}
use of org.bukkit.event.entity.CreatureSpawnEvent in project Denizen-For-Bukkit by DenizenScript.
the class EntitySpawnScriptEvent method onEntitySpawn.
@EventHandler
public void onEntitySpawn(EntitySpawnEvent event) {
Entity entity = event.getEntity();
this.entity = new EntityTag(entity);
location = new LocationTag(event.getLocation());
if (event instanceof CreatureSpawnEvent) {
CreatureSpawnEvent.SpawnReason creatureReason = ((CreatureSpawnEvent) event).getSpawnReason();
if (creatureReason == CreatureSpawnEvent.SpawnReason.SPAWNER) {
// Let the SpawnerSpawnEvent happen and handle it instead
return;
}
reason = new ElementTag(creatureReason.name());
} else if (event instanceof SpawnerSpawnEvent) {
reason = new ElementTag("SPAWNER");
} else {
reason = new ElementTag("ENTITY_SPAWN");
}
this.event = event;
EntityTag.rememberEntity(entity);
fire(event);
EntityTag.forgetEntity(entity);
}
Aggregations