use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class EntityTickPhaseState method unwind.
@SuppressWarnings("unchecked")
@Override
public void unwind(EntityTickContext phaseContext) {
final Entity tickingEntity = phaseContext.getSource(Entity.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on an Entity!", phaseContext));
final Optional<User> creator = phaseContext.getOwner();
final Optional<User> notifier = phaseContext.getNotifier();
final User entityCreator = notifier.orElseGet(() -> creator.orElse(null));
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(tickingEntity);
phaseContext.addNotifierAndOwnerToCauseStack();
phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
final List<Entity> experience = new ArrayList<Entity>(entities.size());
final List<Entity> nonExp = new ArrayList<Entity>(entities.size());
final List<Entity> breeding = new ArrayList<Entity>(entities.size());
final List<Entity> projectile = new ArrayList<Entity>(entities.size());
for (Entity entity : entities) {
if (entity instanceof EntityXPOrb) {
experience.add(entity);
} else if (tickingEntity instanceof Ageable && tickingEntity.getClass() == entity.getClass()) {
breeding.add(entity);
} else if (entity instanceof Projectile) {
projectile.add(entity);
} else {
nonExp.add(entity);
}
}
if (!experience.isEmpty()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.EXPERIENCE);
if (EntityUtil.isEntityDead(tickingEntity)) {
if (tickingEntity instanceof EntityLivingBase) {
CombatEntry entry = ((EntityLivingBase) tickingEntity).getCombatTracker().getBestCombatEntry();
if (entry != null) {
if (entry.damageSrc != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.LAST_DAMAGE_SOURCE, (DamageSource) entry.damageSrc);
}
}
}
}
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), experience);
if (!SpongeImpl.postEvent(event)) {
for (Entity entity : event.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
Sponge.getCauseStackManager().removeContext(EventContextKeys.LAST_DAMAGE_SOURCE);
}
if (!breeding.isEmpty()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.BREEDING);
if (tickingEntity instanceof EntityAnimal) {
final EntityPlayer playerInLove = ((EntityAnimal) tickingEntity).getLoveCause();
if (playerInLove != null) {
Sponge.getCauseStackManager().addContext(EventContextKeys.PLAYER, (Player) playerInLove);
}
}
SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), breeding);
if (!SpongeImpl.postEvent(event)) {
for (Entity entity : event.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
Sponge.getCauseStackManager().removeContext(EventContextKeys.PLAYER);
}
if (!projectile.isEmpty()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PROJECTILE);
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), projectile);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity entity : event.getEntities()) {
if (entityCreator != null) {
entity.setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
}
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PASSIVE);
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), nonExp);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity entity : event.getEntities()) {
if (entityCreator != null) {
entity.setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
});
phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(entities -> {
final ArrayList<Entity> capturedEntities = new ArrayList<>();
for (EntityItem entity : entities) {
capturedEntities.add(EntityUtil.fromNative(entity));
}
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
final DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity entity : event.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
});
phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext));
phaseContext.getBlockItemDropSupplier().acceptIfNotEmpty(map -> {
final List<BlockSnapshot> capturedBlocks = phaseContext.getCapturedBlocks();
for (BlockSnapshot snapshot : capturedBlocks) {
final BlockPos blockPos = ((IMixinLocation) (Object) snapshot.getLocation().get()).getBlockPos();
final Collection<EntityItem> entityItems = map.get(blockPos);
if (!entityItems.isEmpty()) {
Sponge.getCauseStackManager().pushCause(snapshot);
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
final List<Entity> items = entityItems.stream().map(EntityUtil::fromNative).collect(Collectors.toList());
final DropItemEvent.Destruct event = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), items);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity entity : event.getEntities()) {
creator.ifPresent(user -> entity.setCreator(user.getUniqueId()));
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
Sponge.getCauseStackManager().popCause();
}
}
});
phaseContext.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
final List<EntityItem> items = drops.stream().map(drop -> drop.create(EntityUtil.getMinecraftWorld(tickingEntity))).collect(Collectors.toList());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
final List<Entity> entities = (List<Entity>) (List<?>) items;
if (!entities.isEmpty()) {
DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity droppedItem : event.getEntities()) {
EntityUtil.getMixinWorld(droppedItem).forceSpawnEntity(droppedItem);
}
}
}
});
this.fireMovementEvents(EntityUtil.toNative(tickingEntity));
}
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class WeatherTickPhaseState method spawnEntityOrCapture.
@Override
public boolean spawnEntityOrCapture(TickContext.General context, Entity entity, int chunkX, int chunkZ) {
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.WEATHER);
final ArrayList<Entity> capturedEntities = new ArrayList<>();
capturedEntities.add(entity);
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
for (Entity anEntity : spawnEntityEvent.getEntities()) {
EntityUtil.getMixinWorld(anEntity).forceSpawnEntity(anEntity);
}
return true;
}
}
return false;
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project LanternServer by LanternPowered.
the class PlayerContainerSession method handleItemDrop.
public void handleItemDrop(MessagePlayInDropHeldItem message) {
final AbstractSlot slot = this.player.getInventory().getHotbar().getSelectedSlot();
final Optional<ItemStack> itemStack = message.isFullStack() ? slot.peek() : slot.peek(1);
if (itemStack.isPresent()) {
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(this.player);
frame.pushCause(slot);
frame.addContext(EventContextKeys.PLAYER, this.player);
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final List<Entity> entities = new ArrayList<>();
LanternEventHelper.handlePreDroppedItemSpawning(this.player.getTransform(), itemStack.get().createSnapshot()).ifPresent(entities::add);
final SpawnEntityEvent event = SpongeEventFactory.createDropItemEventDispense(causeStack.getCurrentCause(), entities);
Sponge.getEventManager().post(event);
if (!event.isCancelled()) {
if (message.isFullStack()) {
slot.poll();
} else {
slot.poll(1);
}
LanternWorld.finishSpawnEntityEvent(event);
}
}
}
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project LanternServer by LanternPowered.
the class LanternWorld method handleEntitySpawning.
public static void handleEntitySpawning(Iterable<EntitySpawningEntry> entries, BiFunction<Cause, List<Entity>, SpawnEntityEvent> spawnEventConstructor) {
final CauseStack causeStack = CauseStack.current();
final List<Entity> entities = handlePreEntitySpawning(causeStack, entries);
if (entities.isEmpty()) {
return;
}
final SpawnEntityEvent spawnEvent = spawnEventConstructor.apply(causeStack.getCurrentCause(), entities);
// Post the spawn event
Sponge.getEventManager().post(spawnEvent);
// Spawn the entities in the world
finishSpawnEntityEvent(spawnEvent);
}
Aggregations