Search in sources :

Example 36 with SpawnEntityEvent

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

the class UseItemPacketState method unwind.

@Override
public void unwind(BasicPacketContext context) {
    final EntityPlayerMP player = context.getPacketPlayer();
    final ItemStack itemStack = context.getItemUsed();
    final ItemStackSnapshot snapshot = ItemStackUtil.snapshotOf(itemStack);
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(player);
        Sponge.getCauseStackManager().pushCause(snapshot);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, itemStack.getType() == ItemTypes.SPAWN_EGG ? InternalSpawnTypes.SPAWN_EGG : InternalSpawnTypes.PLACEMENT);
        context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(spawnEntityEvent);
            if (!spawnEntityEvent.isCancelled()) {
                processSpawnedEntities(player, spawnEntityEvent);
            }
        });
    }
    context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(originalBlocks -> {
        boolean success = TrackingUtil.processBlockCaptures(originalBlocks, this, context);
        if (!success && snapshot != ItemTypeRegistryModule.NONE_SNAPSHOT) {
            Sponge.getCauseStackManager().pushCause(player);
            EnumHand hand = ((CPacketPlayerTryUseItem) context.getPacket()).getHand();
            PacketPhaseUtil.handlePlayerSlotRestore(player, (net.minecraft.item.ItemStack) itemStack, hand);
        }
    });
}
Also used : CPacketPlayerTryUseItem(net.minecraft.network.play.client.CPacketPlayerTryUseItem) CauseStackManager(org.spongepowered.api.event.CauseStackManager) EnumHand(net.minecraft.util.EnumHand) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) ItemStack(org.spongepowered.api.item.inventory.ItemStack) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 37 with SpawnEntityEvent

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

the class BlockEventTickPhaseState method spawnEntityOrCapture.

@Override
public boolean spawnEntityOrCapture(BlockEventTickContext context, Entity entity, int chunkX, int chunkZ) {
    final Optional<User> notifier = context.getNotifier();
    final Optional<User> owner = context.getOwner();
    final User entityCreator = notifier.orElseGet(() -> owner.orElse(null));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
        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()) {
                if (entityCreator != null) {
                    EntityUtil.toMixin(anEntity).setCreator(entityCreator.getUniqueId());
                }
                EntityUtil.getMixinWorld(anEntity).forceSpawnEntity(anEntity);
            }
            return true;
        }
    }
    return false;
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) 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) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent)

Example 38 with SpawnEntityEvent

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

the class BlockEventTickPhaseState method unwind.

@Override
public void unwind(BlockEventTickContext 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()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.CUSTOM);
        phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> TrackingUtil.processBlockCaptures(blockSnapshots, this, phaseContext));
        phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
            final ArrayList<Entity> capturedEntities = new ArrayList<>();
            for (EntityItem entity : items) {
                capturedEntities.add(EntityUtil.fromNative(entity));
            }
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
            SpongeImpl.postEvent(spawnEntityEvent);
            for (Entity entity : spawnEntityEvent.getEntities()) {
                if (entityCreator != null) {
                    EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
                }
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
            }
        });
    }
}
Also used : TileEntity(org.spongepowered.api.block.tileentity.TileEntity) Entity(org.spongepowered.api.entity.Entity) 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 39 with SpawnEntityEvent

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

the class BlockTickPhaseState method unwind.

@Override
public void unwind(BlockTickContext context) {
    final LocatableBlock locatableBlock = context.requireSource(LocatableBlock.class);
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(locatableBlock);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        final User entityCreator = context.getNotifier().orElseGet(() -> context.getOwner().orElse(null));
        context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blockSnapshots -> TrackingUtil.processBlockCaptures(blockSnapshots, this, context));
        context.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
            final ArrayList<Entity> capturedEntities = new ArrayList<>();
            for (EntityItem entity : items) {
                capturedEntities.add(EntityUtil.fromNative(entity));
            }
            final SpawnEntityEvent spawnEntityEvent = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), capturedEntities);
            SpongeImpl.postEvent(spawnEntityEvent);
            for (Entity entity : spawnEntityEvent.getEntities()) {
                if (entityCreator != null) {
                    EntityUtil.toMixin(entity).setCreator(entityCreator.getUniqueId());
                }
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
            }
        });
    }
}
Also used : Entity(org.spongepowered.api.entity.Entity) User(org.spongepowered.api.entity.living.player.User) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ArrayList(java.util.ArrayList) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) EntityItem(net.minecraft.entity.item.EntityItem)

Example 40 with SpawnEntityEvent

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

the class DimensionTickPhaseState method spawnEntityOrCapture.

/*
    @author - gabizou
    non-javadoc
    This is a stopgap to get dragon respawns working. Since there's 4 classes that interweave themselves
    between various states including but not withstanding: respawning endercrystals, respawning the dragon,
    locating the crystals, etc. it's best to not capture the spawns and simply spawn them in directly.
    This is a todo until the dragon phases are completely configured and correctly managed (should be able to at some point restore
    traditional ai logic to the dragon without the necessity for the dragon being summoned the manual way).

     */
@Override
public boolean spawnEntityOrCapture(DimensionContext context, Entity entity, int chunkX, int chunkZ) {
    final User user = context.getNotifier().orElseGet(() -> context.getOwner().orElse(null));
    if (user != null) {
        entity.setCreator(user.getUniqueId());
    }
    final ArrayList<Entity> entities = new ArrayList<>(1);
    entities.add(entity);
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.PLACEMENT);
        final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), entities);
        SpongeImpl.postEvent(event);
        if (!event.isCancelled() && event.getEntities().size() > 0) {
            for (Entity item : event.getEntities()) {
                EntityUtil.getMixinWorld(entity).forceSpawnEntity(item);
            }
            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) 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