Search in sources :

Example 16 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class BlockTickPhaseState method spawnEntityOrCapture.

@Override
public boolean spawnEntityOrCapture(BlockTickContext context, Entity entity, int chunkX, int chunkZ) {
    final LocatableBlock locatableBlock = getLocatableBlockSourceFromContext(context);
    final Optional<User> owner = context.getOwner();
    final Optional<User> notifier = context.getNotifier();
    final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(locatableBlock);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.EXPERIENCE);
        if (notifier.isPresent()) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, notifier.get());
        }
        if (owner.isPresent()) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, owner.get());
        }
        if (entity instanceof EntityXPOrb) {
            final ArrayList<Entity> entities = new ArrayList<>(1);
            entities.add(entity);
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            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);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.BLOCK_SPAWNING);
        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 : Entity(org.spongepowered.api.entity.Entity) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ArrayList(java.util.ArrayList) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityXPOrb(net.minecraft.entity.item.EntityXPOrb)

Example 17 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class DimensionTickPhaseState method unwind.

@Override
public void unwind(DimensionContext phaseContext) {
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
        phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> {
            TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext);
        });
        phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(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);
                }
            }
        });
        phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(entities -> {
            final ArrayList<Entity> capturedEntities = new ArrayList<>();
            for (EntityItem entity : entities) {
                capturedEntities.add(EntityUtil.fromNative(entity));
            }
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                for (Entity entity : event.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) 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 18 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class EntityTickPhaseState method spawnEntityOrCapture.

@Override
public boolean spawnEntityOrCapture(EntityTickContext context, Entity entity, int chunkX, int chunkZ) {
    final Entity tickingEntity = context.getSource(Entity.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on an Entity!", context));
    final Optional<User> creator = context.getOwner();
    final Optional<User> notifier = context.getNotifier();
    final User entityCreator = notifier.orElseGet(() -> creator.orElse(null));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        context.addNotifierAndOwnerToCauseStack();
        Sponge.getCauseStackManager().pushCause(tickingEntity);
        if (entity instanceof EntityXPOrb) {
            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 List<Entity> experience = new ArrayList<Entity>(1);
            experience.add(entity);
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), experience);
            if (!SpongeImpl.postEvent(event)) {
                for (Entity anEntity : event.getEntities()) {
                    if (entityCreator != null) {
                        EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
                    }
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
                }
                return true;
            }
            return false;
        } else if (tickingEntity instanceof Ageable && tickingEntity.getClass() == entity.getClass()) {
            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);
                }
            }
            final List<Entity> breeding = new ArrayList<Entity>(1);
            breeding.add(entity);
            SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), breeding);
            if (!SpongeImpl.postEvent(event)) {
                for (Entity anEntity : event.getEntities()) {
                    if (entityCreator != null) {
                        EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
                    }
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
                }
                return true;
            }
            return false;
        } else if (entity instanceof Projectile) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PROJECTILE);
            final List<Entity> projectile = new ArrayList<Entity>(1);
            projectile.add(entity);
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), projectile);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                for (Entity anEntity : event.getEntities()) {
                    if (entityCreator != null) {
                        anEntity.setCreator(entityCreator.getUniqueId());
                    }
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
                }
                return true;
            }
            return false;
        }
        final List<Entity> nonExp = new ArrayList<Entity>(1);
        nonExp.add(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 anEntity : event.getEntities()) {
                if (entityCreator != null) {
                    anEntity.setCreator(entityCreator.getUniqueId());
                }
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
            }
            return true;
        }
    }
    return false;
}
Also used : Entity(org.spongepowered.api.entity.Entity) EntityPlayer(net.minecraft.entity.player.EntityPlayer) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) ArrayList(java.util.ArrayList) CombatEntry(net.minecraft.util.CombatEntry) Ageable(org.spongepowered.api.entity.living.Ageable) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) IProjectile(net.minecraft.entity.IProjectile) Projectile(org.spongepowered.api.entity.projectile.Projectile) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EntityLivingBase(net.minecraft.entity.EntityLivingBase) EntityPlayer(net.minecraft.entity.player.EntityPlayer) ArrayList(java.util.ArrayList) List(java.util.List) EntityAnimal(net.minecraft.entity.passive.EntityAnimal) EntityXPOrb(net.minecraft.entity.item.EntityXPOrb)

Example 19 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class PlayerTickPhaseState method unwind.

@Override
public void unwind(PlayerTickContext phaseContext) {
    final Player player = phaseContext.getSource(Player.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on a Player!", phaseContext));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(player);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PASSIVE);
        phaseContext.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            for (Entity entity : spawnEntityEvent.getEntities()) {
                EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
            }
        });
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        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);
            for (Entity entity : spawnEntityEvent.getEntities()) {
                EntityUtil.toMixin(entity).setCreator(player.getUniqueId());
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
            }
        });
        phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> {
            TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext);
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ArrayList(java.util.ArrayList) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityItem(net.minecraft.entity.item.EntityItem)

Example 20 with SpawnEntityEvent

use of org.spongepowered.api.event.entity.SpawnEntityEvent in project SpongeCommon by SpongePowered.

the class PlayerTickPhaseState method spawnEntityOrCapture.

@Override
public boolean spawnEntityOrCapture(PlayerTickContext context, Entity entity, int chunkX, int chunkZ) {
    final Player player = context.getSource(Player.class).orElseThrow(TrackingUtil.throwWithContext("Not ticking on a Player!", context));
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(player);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.PASSIVE);
        final List<Entity> entities = new ArrayList<>(1);
        entities.add(entity);
        final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
        SpongeImpl.postEvent(spawnEntityEvent);
        if (!spawnEntityEvent.isCancelled()) {
            for (Entity anEntity : spawnEntityEvent.getEntities()) {
                EntityUtil.toMixin(anEntity).setCreator(player.getUniqueId());
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(anEntity);
            }
            return true;
        }
    }
    return false;
}
Also used : Entity(org.spongepowered.api.entity.Entity) Player(org.spongepowered.api.entity.living.player.Player) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

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