Search in sources :

Example 16 with StackFrame

use of org.spongepowered.api.event.CauseStackManager.StackFrame 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)

Example 17 with StackFrame

use of org.spongepowered.api.event.CauseStackManager.StackFrame 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 18 with StackFrame

use of org.spongepowered.api.event.CauseStackManager.StackFrame 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 19 with StackFrame

use of org.spongepowered.api.event.CauseStackManager.StackFrame in project SpongeCommon by SpongePowered.

the class MixinEntityOcelot method onTame.

@Redirect(method = "processInteract", at = @At(value = "INVOKE", target = "Ljava/util/Random;nextInt(I)I", ordinal = 0, remap = false))
public int onTame(Random rand, int bound, EntityPlayer player, EnumHand hand) {
    ItemStack stack = player.getHeldItem(hand);
    int random = rand.nextInt(bound);
    if (random == 0) {
        stack.setCount(stack.getCount() + 1);
        try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            Sponge.getCauseStackManager().pushCause(player);
            if (!SpongeImpl.postEvent(SpongeEventFactory.createTameEntityEvent(Sponge.getCauseStackManager().getCurrentCause(), this))) {
                stack.setCount(stack.getCount() - 1);
                return random;
            }
        }
    }
    return 1;
}
Also used : StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ItemStack(net.minecraft.item.ItemStack) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 20 with StackFrame

use of org.spongepowered.api.event.CauseStackManager.StackFrame in project SpongeCommon by SpongePowered.

the class MixinEntityPlayer method onOnContainerClosed.

@Redirect(method = "setDead", at = @At(value = "INVOKE", ordinal = 1, target = "Lnet/minecraft/inventory/Container;onContainerClosed(Lnet/minecraft/entity/player/EntityPlayer;)V"))
private void onOnContainerClosed(Container container, EntityPlayer player) {
    if (player instanceof EntityPlayerMP) {
        final EntityPlayerMP serverPlayer = (EntityPlayerMP) player;
        try (PhaseContext<?> ctx = PacketPhase.General.CLOSE_WINDOW.createPhaseContext().source(serverPlayer).packetPlayer(serverPlayer).openContainer(container).buildAndSwitch();
            StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(serverPlayer);
            final ItemStackSnapshot cursor = ItemStackUtil.snapshotOf(this.inventory.getItemStack());
            container.onContainerClosed(player);
            SpongeCommonEventFactory.callInteractInventoryCloseEvent(this.openContainer, serverPlayer, cursor, ItemStackSnapshot.NONE, false);
        }
    } else {
        // Proceed as normal with client code
        container.onContainerClosed(player);
    }
}
Also used : StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ItemStackSnapshot(org.spongepowered.api.item.inventory.ItemStackSnapshot) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)36 Entity (org.spongepowered.api.entity.Entity)18 ArrayList (java.util.ArrayList)15 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)13 User (org.spongepowered.api.entity.living.player.User)12 EntityItem (net.minecraft.entity.item.EntityItem)11 EntityPlayer (net.minecraft.entity.player.EntityPlayer)8 TileEntity (org.spongepowered.api.block.tileentity.TileEntity)8 IMixinEntity (org.spongepowered.common.interfaces.entity.IMixinEntity)6 ItemStack (net.minecraft.item.ItemStack)5 BlockPos (net.minecraft.util.math.BlockPos)5 WorldServer (net.minecraft.world.WorldServer)5 LocatableBlock (org.spongepowered.api.world.LocatableBlock)5 IMixinTileEntity (org.spongepowered.common.interfaces.block.tile.IMixinTileEntity)5 List (java.util.List)4 UUID (java.util.UUID)4 Collectors (java.util.stream.Collectors)4 EntityLivingBase (net.minecraft.entity.EntityLivingBase)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 Sponge (org.spongepowered.api.Sponge)4