use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class ClientContainer method tryProcessBehavior.
protected void tryProcessBehavior(Consumer<ContainerInteractionBehavior> behavior) {
checkNotNull(this.player);
if (this.interactionBehavior != null) {
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(this.player);
frame.pushCause(this);
// Also add the player as context
frame.addContext(EventContextKeys.PLAYER, this.player);
try {
behavior.accept(this.interactionBehavior);
} catch (Throwable t) {
Lantern.getLogger().error("Failed to process the inventory interaction behavior", t);
}
}
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class PlayerContainerSession method handleWindowClose.
public void handleWindowClose(MessagePlayInOutCloseWindow message) {
if (this.openContainer == null || message.getWindow() != getContainerId()) {
return;
}
final CauseStack causeStack = CauseStack.current();
causeStack.pushCause(this.player);
setRawOpenContainer(causeStack, null, false, true);
causeStack.popCause();
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class PlayerContainerSession method handleItemDrop.
public void handleItemDrop(MessagePlayInDropHeldItem message) {
final AbstractSlot slot = this.player.getInventory().getHotbar().getSelectedSlot();
final Optional<ItemStack> itemStack = message.isFullStack() ? slot.peek() : slot.peek(1);
if (itemStack.isPresent()) {
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(this.player);
frame.pushCause(slot);
frame.addContext(EventContextKeys.PLAYER, this.player);
frame.addContext(EventContextKeys.SPAWN_TYPE, SpawnTypes.DROPPED_ITEM);
final List<Entity> entities = new ArrayList<>();
LanternEventHelper.handlePreDroppedItemSpawning(this.player.getTransform(), itemStack.get().createSnapshot()).ifPresent(entities::add);
final SpawnEntityEvent event = SpongeEventFactory.createDropItemEventDispense(causeStack.getCurrentCause(), entities);
Sponge.getEventManager().post(event);
if (!event.isCancelled()) {
if (message.isFullStack()) {
slot.poll();
} else {
slot.poll(1);
}
LanternWorld.finishSpawnEntityEvent(event);
}
}
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method placeBlock.
@Override
public boolean placeBlock(int x, int y, int z, BlockState block, Direction side, @Nullable GameProfile profile) {
final BehaviorPipeline<Behavior> pipeline = ((LanternBlockType) block.getType()).getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.USED_BLOCK_STATE, block);
frame.addContext(ContextKeys.INTERACTION_FACE, side);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, block.getType());
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(PlaceBlockBehavior.class), (ctx, behavior) -> behavior.tryPlace(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
use of org.lanternpowered.server.event.CauseStack in project LanternServer by LanternPowered.
the class LanternWorld method digBlock.
private boolean digBlock(int x, int y, int z, @Nullable GameProfile profile, @Nullable ItemStack itemStack) {
final LanternBlockType blockType = ((LanternBlockType) getBlockType(x, y, z));
final BehaviorPipeline<Behavior> pipeline = blockType.getPipeline();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(this, x, y, z));
frame.addContext(ContextKeys.BLOCK_TYPE, blockType);
if (profile != null) {
frame.addContext(EventContextKeys.PLAYER_SIMULATED, profile);
}
if (itemStack != null) {
frame.addContext(ContextKeys.USED_ITEM_STACK, itemStack);
}
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
// Just pass an object trough to make sure that a value is present when successful
if (context.process(pipeline.pipeline(BreakBlockBehavior.class), (ctx, behavior) -> behavior.tryBreak(pipeline, ctx)).isSuccess()) {
context.accept();
return true;
}
context.revert();
return false;
}
}
Aggregations