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