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);
}
}
}
});
}
}
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);
}
}
});
}
}
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;
}
}
}
}
Aggregations