Search in sources :

Example 21 with SpawnEntityEvent

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);
        }
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 22 with SpawnEntityEvent

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);
    }
}
Also used : IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityItem(net.minecraft.entity.item.EntityItem)

Example 23 with SpawnEntityEvent

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;
}
Also used : IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) Entity(org.spongepowered.api.entity.Entity) TileEntity(org.spongepowered.api.block.tileentity.TileEntity) User(org.spongepowered.api.entity.living.player.User) IMixinTileEntity(org.spongepowered.common.interfaces.block.tile.IMixinTileEntity) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityXPOrb(net.minecraft.entity.item.EntityXPOrb)

Example 24 with SpawnEntityEvent

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);
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) CauseStackManager(org.spongepowered.api.event.CauseStackManager) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 25 with SpawnEntityEvent

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;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(net.minecraft.entity.Entity) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityJoinWorldEvent(net.minecraftforge.event.entity.EntityJoinWorldEvent) IMixinEventBus(org.spongepowered.mod.interfaces.IMixinEventBus)

Aggregations

SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)44 Entity (org.spongepowered.api.entity.Entity)41 ArrayList (java.util.ArrayList)30 CauseStackManager (org.spongepowered.api.event.CauseStackManager)23 User (org.spongepowered.api.entity.living.player.User)18 EntityItem (net.minecraft.entity.item.EntityItem)17 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)15 List (java.util.List)11 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)11 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)11 Sponge (org.spongepowered.api.Sponge)10 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)10 SpongeImpl (org.spongepowered.common.SpongeImpl)10 EntityUtil (org.spongepowered.common.entity.EntityUtil)10 World (org.spongepowered.api.world.World)9 Collection (java.util.Collection)8 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)8 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)8 ItemStack (org.spongepowered.api.item.inventory.ItemStack)8 ItemDropData (org.spongepowered.common.event.tracking.context.ItemDropData)8