use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class TickPhaseState method postProcessSpawns.
@Override
public void postProcessSpawns(C phaseContext, ArrayList<Entity> entities) {
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity entity : event.getEntities()) {
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class TileEntityTickPhaseState method unwind.
@Override
public void unwind(TileEntityTickContext phaseContext) {
final TileEntity tickingTile = phaseContext.getSource(TileEntity.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on a TileEntity!", phaseContext));
final Optional<User> notifier = phaseContext.getNotifier();
final Optional<User> owner = phaseContext.getOwner();
final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> {
TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext);
});
Sponge.getCauseStackManager().pushCause(tickingTile.getLocatableBlock());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.BLOCK_SPAWNING);
phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(entities -> {
final ArrayList<Entity> capturedEntities = new ArrayList<>();
for (EntityItem entity : entities) {
capturedEntities.add(EntityUtil.fromNative(entity));
}
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
for (Entity entity : spawnEntityEvent.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
}
});
} catch (Exception e) {
throw new RuntimeException(String.format("Exception occured while processing tile entity %s at %s", tickingTile, tickingTile.getLocation()), e);
}
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class TileEntityTickPhaseState method spawnEntityOrCapture.
@Override
public boolean spawnEntityOrCapture(TileEntityTickContext context, Entity entity, int chunkX, int chunkZ) {
final TileEntity tickingTile = context.getSource(TileEntity.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on a TileEntity!", context));
final Optional<User> notifier = context.getNotifier();
final Optional<User> owner = context.getOwner();
final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
final IMixinTileEntity mixinTileEntity = (IMixinTileEntity) tickingTile;
// Separate experience from other entities
if (entity instanceof EntityXPOrb) {
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(tickingTile.getLocatableBlock());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.EXPERIENCE);
context.addNotifierAndOwnerToCauseStack();
final ArrayList<Entity> exp = new ArrayList<>();
exp.add(entity);
final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), exp);
SpongeImpl.postEvent(event);
if (!event.isCancelled()) {
for (Entity anEntity : event.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
}
return true;
}
}
return false;
}
final List<Entity> nonExpEntities = new ArrayList<>(1);
nonExpEntities.add(entity);
try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().pushCause(tickingTile.getLocatableBlock());
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, mixinTileEntity.getTickedSpawnType());
context.addNotifierAndOwnerToCauseStack();
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), nonExpEntities);
SpongeImpl.postEvent(spawnEntityEvent);
if (!spawnEntityEvent.isCancelled()) {
for (Entity anEntity : spawnEntityEvent.getEntities()) {
if (entityCreator != null) {
EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
}
EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
}
return true;
}
}
return false;
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.
the class WeatherTickPhaseState method unwind.
@Override
public void unwind(TickContext.General phaseContext) {
try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.WEATHER);
phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
SpongeImpl.postEvent(spawnEntityEvent);
for (Entity entity : spawnEntityEvent.getEntities()) {
EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
}
});
phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> {
TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext);
});
}
}
use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeForge by SpongePowered.
the class SpongeForgeEventFactory method callEntityJoinWorldEvent.
public static SpawnEntityEvent callEntityJoinWorldEvent(Event event) {
SpawnEntityEvent spongeEvent = (SpawnEntityEvent) event;
ListIterator<org.spongepowered.api.entity.Entity> iterator = spongeEvent.getEntities().listIterator();
if (spongeEvent.getEntities().size() == 0) {
return spongeEvent;
}
// used to avoid player item restores when set to dead
boolean canCancelEvent = true;
while (iterator.hasNext()) {
org.spongepowered.api.entity.Entity entity = iterator.next();
EntityJoinWorldEvent forgeEvent = new EntityJoinWorldEvent((Entity) entity, (net.minecraft.world.World) entity.getLocation().getExtent());
boolean prev = StaticMixinForgeHelper.preventInternalForgeEntityListener;
StaticMixinForgeHelper.preventInternalForgeEntityListener = true;
((IMixinEventBus) MinecraftForge.EVENT_BUS).post(forgeEvent, true);
StaticMixinForgeHelper.preventInternalForgeEntityListener = prev;
Entity mcEntity = (Entity) entity;
if (mcEntity.isDead) {
// Don't restore packet item if a mod wants it dead
// Mods such as Flux-Networks kills the entity item to spawn a custom one
canCancelEvent = false;
}
if (forgeEvent.isCanceled()) {
iterator.remove();
}
}
if (spongeEvent.getEntities().size() == 0 && canCancelEvent) {
spongeEvent.setCancelled(true);
}
return spongeEvent;
}
Aggregations