Search in sources :

Example 1 with GeneralizedContext

use of org.spongepowered.common.event.tracking.context.GeneralizedContext in project SpongeCommon by SpongePowered.

the class BlockDecayPhaseState method unwind.

@SuppressWarnings("unchecked")
@Override
public void unwind(GeneralizedContext context) {
    final LocatableBlock locatable = context.getSource(LocatableBlock.class).orElseThrow(TrackingUtil.throwWithContext("Expected to be ticking over at a location!", context));
    final Location<World> worldLocation = locatable.getLocation();
    final IMixinWorldServer mixinWorld = ((IMixinWorldServer) worldLocation.getExtent());
    try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        frame.pushCause(locatable);
        context.addNotifierAndOwnerToCauseStack();
        context.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blocks -> TrackingUtil.processBlockCaptures(blocks, this, context));
        frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.BLOCK_SPAWNING);
        context.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(frame.getCurrentCause(), items);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                for (Entity entity : event.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
        context.getCapturedEntitySupplier().acceptAndClearIfNotEmpty(entities -> {
            final SpawnEntityEvent event = SpongeEventFactory.createSpawnEntityEvent(frame.getCurrentCause(), entities);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                for (Entity entity : event.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
        context.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
            final List<EntityItem> items = drops.stream().map(drop -> drop.create(mixinWorld.asMinecraftWorld())).collect(Collectors.toList());
            final List<Entity> entities = (List<Entity>) (List<?>) items;
            if (!entities.isEmpty()) {
                DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(frame.getCurrentCause(), entities);
                SpongeImpl.postEvent(event);
                if (!event.isCancelled()) {
                    for (Entity droppedItem : event.getEntities()) {
                        mixinWorld.forceSpawnEntity(droppedItem);
                    }
                }
            }
        });
    }
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem) SpongeImpl(org.spongepowered.common.SpongeImpl) Location(org.spongepowered.api.world.Location) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) LocatableBlock(org.spongepowered.api.world.LocatableBlock) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Sponge(org.spongepowered.api.Sponge) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) EntityUtil(org.spongepowered.common.entity.EntityUtil) SpawnTypes(org.spongepowered.api.event.cause.entity.spawn.SpawnTypes) List(java.util.List) TrackingUtil(org.spongepowered.common.event.tracking.TrackingUtil) GeneralizedContext(org.spongepowered.common.event.tracking.context.GeneralizedContext) World(org.spongepowered.api.world.World) Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) World(org.spongepowered.api.world.World) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) LocatableBlock(org.spongepowered.api.world.LocatableBlock) List(java.util.List) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with GeneralizedContext

use of org.spongepowered.common.event.tracking.context.GeneralizedContext in project SpongeCommon by SpongePowered.

the class BlockDropItemsPhaseState method unwind.

@SuppressWarnings("unchecked")
@Override
public void unwind(GeneralizedContext phaseContext) {
    final BlockSnapshot blockSnapshot = phaseContext.getSource(BlockSnapshot.class).orElseThrow(TrackingUtil.throwWithContext("Could not find a block dropping items!", phaseContext));
    try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
        Sponge.getCauseStackManager().pushCause(blockSnapshot);
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, InternalSpawnTypes.DROPPED_ITEM);
        if (phaseContext.getNotifier().isPresent()) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.NOTIFIER, phaseContext.getNotifier().get());
        }
        if (phaseContext.getOwner().isPresent()) {
            Sponge.getCauseStackManager().addContext(EventContextKeys.OWNER, phaseContext.getOwner().get());
        }
        phaseContext.getCapturedItemsSupplier().acceptAndClearIfNotEmpty(items -> {
            final ArrayList<Entity> entities = new ArrayList<>();
            for (EntityItem item : items) {
                entities.add(EntityUtil.fromNative(item));
            }
            final DropItemEvent.Destruct event = SpongeEventFactory.createDropItemEventDestruct(Sponge.getCauseStackManager().getCurrentCause(), entities);
            SpongeImpl.postEvent(event);
            if (!event.isCancelled()) {
                for (Entity entity : event.getEntities()) {
                    EntityUtil.getMixinWorld(entity).forceSpawnEntity(entity);
                }
            }
        });
        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);
                }
            }
            entities.clear();
        });
        final Location<World> worldLocation = blockSnapshot.getLocation().get();
        final IMixinWorldServer mixinWorld = ((IMixinWorldServer) worldLocation.getExtent());
        Sponge.getCauseStackManager().addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.BLOCK_SPAWNING);
        phaseContext.getCapturedBlockSupplier().acceptAndClearIfNotEmpty(blocks -> TrackingUtil.processBlockCaptures(blocks, this, phaseContext));
        phaseContext.getCapturedItemStackSupplier().acceptAndClearIfNotEmpty(drops -> {
            final List<EntityItem> items = drops.stream().map(drop -> drop.create(mixinWorld.asMinecraftWorld())).collect(Collectors.toList());
            final List<Entity> entities = (List<Entity>) (List<?>) items;
            if (!entities.isEmpty()) {
                DropItemEvent.Custom event = SpongeEventFactory.createDropItemEventCustom(Sponge.getCauseStackManager().getCurrentCause(), entities);
                SpongeImpl.postEvent(event);
                if (!event.isCancelled()) {
                    for (Entity droppedItem : event.getEntities()) {
                        mixinWorld.forceSpawnEntity(droppedItem);
                    }
                }
            }
            drops.clear();
        });
        phaseContext.getBlockDropSupplier().acceptAndClearIfNotEmpty(drops -> {
            for (BlockPos key : drops.asMap().keySet()) {
                final List<ItemDropData> values = drops.get(key);
                if (!values.isEmpty()) {
                    TrackingUtil.spawnItemDataForBlockDrops(values, blockSnapshot, phaseContext, this);
                }
            }
        });
    }
}
Also used : EntityItem(net.minecraft.entity.item.EntityItem) SpongeImpl(org.spongepowered.common.SpongeImpl) Location(org.spongepowered.api.world.Location) EventContextKeys(org.spongepowered.api.event.cause.EventContextKeys) SpongeEventFactory(org.spongepowered.api.event.SpongeEventFactory) Sponge(org.spongepowered.api.Sponge) BlockPos(net.minecraft.util.math.BlockPos) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) InternalSpawnTypes(org.spongepowered.common.registry.type.event.InternalSpawnTypes) Entity(org.spongepowered.api.entity.Entity) Collectors(java.util.stream.Collectors) EntityUtil(org.spongepowered.common.entity.EntityUtil) ArrayList(java.util.ArrayList) SpawnTypes(org.spongepowered.api.event.cause.entity.spawn.SpawnTypes) List(java.util.List) TrackingUtil(org.spongepowered.common.event.tracking.TrackingUtil) GeneralizedContext(org.spongepowered.common.event.tracking.context.GeneralizedContext) World(org.spongepowered.api.world.World) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) CauseStackManager(org.spongepowered.api.event.CauseStackManager) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) Entity(org.spongepowered.api.entity.Entity) DropItemEvent(org.spongepowered.api.event.item.inventory.DropItemEvent) BlockSnapshot(org.spongepowered.api.block.BlockSnapshot) ArrayList(java.util.ArrayList) IMixinWorldServer(org.spongepowered.common.interfaces.world.IMixinWorldServer) World(org.spongepowered.api.world.World) SpawnEntityEvent(org.spongepowered.api.event.entity.SpawnEntityEvent) CauseStackManager(org.spongepowered.api.event.CauseStackManager) ArrayList(java.util.ArrayList) List(java.util.List) BlockPos(net.minecraft.util.math.BlockPos) ItemDropData(org.spongepowered.common.event.tracking.context.ItemDropData) EntityItem(net.minecraft.entity.item.EntityItem)

Example 3 with GeneralizedContext

use of org.spongepowered.common.event.tracking.context.GeneralizedContext in project SpongeCommon by SpongePowered.

the class MixinDragonFightManager method tick.

/**
 * @author gabizou - January 22nd, 2017
 * @reason Injects Sponge necessary phase state switches
 */
@Overwrite
public void tick() {
    this.bossInfo.setVisible(!this.dragonKilled);
    if (++this.ticksSinceLastPlayerScan >= 20) {
        this.updateplayers();
        this.ticksSinceLastPlayerScan = 0;
    }
    if (!this.bossInfo.getPlayers().isEmpty()) {
        if (this.scanForLegacyFight) {
            LOGGER.info("Scanning for legacy world dragon fight...");
            this.loadChunks();
            this.scanForLegacyFight = false;
            boolean flag = this.hasDragonBeenKilled();
            if (flag) {
                LOGGER.info("Found that the dragon has been killed in this world already.");
                this.previouslyKilled = true;
            } else {
                LOGGER.info("Found that the dragon has not yet been killed in this world.");
                this.previouslyKilled = false;
                this.generatePortal(false);
            }
            List<EntityDragon> list = this.world.getEntities(EntityDragon.class, EntitySelectors.IS_ALIVE);
            if (list.isEmpty()) {
                this.dragonKilled = true;
            } else {
                EntityDragon entitydragon = list.get(0);
                this.dragonUniqueId = entitydragon.getUniqueID();
                LOGGER.info("Found that there\'s a dragon still alive ({})", entitydragon);
                this.dragonKilled = false;
                if (!flag) {
                    LOGGER.info("But we didn\'t have a portal, let\'s remove it.");
                    entitydragon.setDead();
                    this.dragonUniqueId = null;
                }
            }
            if (!this.previouslyKilled && this.dragonKilled) {
                this.dragonKilled = false;
            }
        }
        if (this.respawnState != null) {
            if (this.crystals == null) {
                this.respawnState = null;
                this.respawnDragon();
            }
            // Sponge Start - Cause tracker - todo: do more logistical configuration of how this all works.
            try (GeneralizedContext context = DragonPhase.State.RESPAWN_DRAGON.createPhaseContext().buildAndSwitch()) {
                // Sponge End
                this.respawnState.process(this.world, (DragonFightManager) (Object) this, this.crystals, this.respawnStateTicks++, this.exitPortalLocation);
            }
        // Sponge - Complete cause tracker
        }
        if (!this.dragonKilled) {
            if (this.dragonUniqueId == null || ++this.ticksSinceDragonSeen >= 1200) {
                this.loadChunks();
                List<EntityDragon> list1 = this.world.getEntities(EntityDragon.class, EntitySelectors.IS_ALIVE);
                if (list1.isEmpty()) {
                    LOGGER.debug("Haven\'t seen the dragon, respawning it");
                    this.createNewDragon();
                } else {
                    LOGGER.debug("Haven\'t seen our dragon, but found another one to use.");
                    this.dragonUniqueId = list1.get(0).getUniqueID();
                }
                this.ticksSinceDragonSeen = 0;
            }
            if (++this.ticksSinceCrystalsScanned >= 100) {
                this.findAliveCrystals();
                this.ticksSinceCrystalsScanned = 0;
            }
        }
    }
}
Also used : EntityDragon(net.minecraft.entity.boss.EntityDragon) GeneralizedContext(org.spongepowered.common.event.tracking.context.GeneralizedContext) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Aggregations

GeneralizedContext (org.spongepowered.common.event.tracking.context.GeneralizedContext)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 EntityItem (net.minecraft.entity.item.EntityItem)2 Sponge (org.spongepowered.api.Sponge)2 Entity (org.spongepowered.api.entity.Entity)2 SpongeEventFactory (org.spongepowered.api.event.SpongeEventFactory)2 EventContextKeys (org.spongepowered.api.event.cause.EventContextKeys)2 SpawnTypes (org.spongepowered.api.event.cause.entity.spawn.SpawnTypes)2 SpawnEntityEvent (org.spongepowered.api.event.entity.SpawnEntityEvent)2 DropItemEvent (org.spongepowered.api.event.item.inventory.DropItemEvent)2 Location (org.spongepowered.api.world.Location)2 World (org.spongepowered.api.world.World)2 SpongeImpl (org.spongepowered.common.SpongeImpl)2 EntityUtil (org.spongepowered.common.entity.EntityUtil)2 TrackingUtil (org.spongepowered.common.event.tracking.TrackingUtil)2 IMixinWorldServer (org.spongepowered.common.interfaces.world.IMixinWorldServer)2 ArrayList (java.util.ArrayList)1 EntityDragon (net.minecraft.entity.boss.EntityDragon)1 BlockPos (net.minecraft.util.math.BlockPos)1