use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement in project LanternServer by LanternPowered.
the class CodecPlayInPlayerBlockPlacement method decode.
@Override
public MessagePlayInPlayerBlockPlacement decode(CodecContext context, ByteBuffer buf) throws CodecException {
final Vector3i position = buf.read(Types.VECTOR_3_I);
final Direction face = fromFace(buf.readVarInt());
final HandType hand = buf.readVarInt() == 0 ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
final double ox = buf.readFloat();
final double oy = buf.readFloat();
final double oz = buf.readFloat();
final Vector3d offset = new Vector3d(ox, oy, oz);
return new MessagePlayInPlayerBlockPlacement(position, offset, face, hand);
}
use of org.lanternpowered.server.network.vanilla.message.type.play.MessagePlayInPlayerBlockPlacement in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method handleBlockPlacing.
public void handleBlockPlacing(MessagePlayInPlayerBlockPlacement message) {
final HandType handType = message.getHandType();
// the main hand message.
if (handType == HandTypes.OFF_HAND) {
return;
}
// Try the action of the hotbar item first
final AbstractSlot hotbarSlot = this.player.getInventory().getHotbar().getSelectedSlot();
final AbstractSlot offHandSlot = this.player.getInventory().getOffhand();
// The offset can round up to 1, causing
// an incorrect clicked block location
final Vector3d pos2 = message.getClickOffset();
final double dx = Math.min(pos2.getX(), 0.999);
final double dy = Math.min(pos2.getY(), 0.999);
final double dz = Math.min(pos2.getZ(), 0.999);
final Location<World> clickedLocation = new Location<>(this.player.getWorld(), message.getPosition().toDouble().add(dx, dy, dz));
final Direction face = message.getFace();
final CauseStack causeStack = CauseStack.current();
try (CauseStack.Frame frame = causeStack.pushCauseFrame()) {
frame.pushCause(this.player);
// Add context
frame.addContext(ContextKeys.INTERACTION_FACE, face);
frame.addContext(ContextKeys.INTERACTION_LOCATION, clickedLocation);
frame.addContext(ContextKeys.BLOCK_LOCATION, new Location<>(clickedLocation.getExtent(), message.getPosition()));
frame.addContext(ContextKeys.PLAYER, this.player);
final BehaviorContextImpl context = new BehaviorContextImpl(causeStack);
final BehaviorContext.Snapshot snapshot = context.pushSnapshot();
if (!this.player.get(Keys.IS_SNEAKING).orElse(false)) {
final BlockState blockState = this.player.getWorld().getBlock(message.getPosition());
final LanternBlockType blockType = (LanternBlockType) blockState.getType();
frame.addContext(ContextKeys.BLOCK_TYPE, blockState.getType());
frame.addContext(ContextKeys.USED_BLOCK_STATE, blockState);
BehaviorContext.Snapshot snapshot1 = context.pushSnapshot();
// Try first with the main hand
hotbarSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
frame.addContext(ContextKeys.USED_SLOT, hotbarSlot);
frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.MAIN_HAND);
BehaviorResult result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
if (!result.isSuccess()) {
context.popSnapshot(snapshot1);
snapshot1 = context.pushSnapshot();
// Try again with the off hand
offHandSlot.peek().ifPresent(stack -> frame.addContext(ContextKeys.USED_ITEM_STACK, stack));
frame.addContext(ContextKeys.USED_SLOT, offHandSlot);
frame.addContext(ContextKeys.INTERACTION_HAND, HandTypes.OFF_HAND);
result = context.process(blockType.getPipeline().pipeline(InteractWithBlockBehavior.class), (ctx, behavior) -> behavior.tryInteract(blockType.getPipeline(), ctx));
}
if (result.isSuccess()) {
snapshot1 = context.pushSnapshot();
// We can still continue, doing other operations
if (result == BehaviorResult.CONTINUE) {
handleMainHandItemInteraction(context, snapshot1);
}
context.accept();
return;
}
context.popSnapshot(snapshot1);
snapshot1 = context.pushSnapshot();
if (result.isSuccess()) {
snapshot1 = context.pushSnapshot();
// We can still continue, doing other operations
if (result == BehaviorResult.CONTINUE) {
handleOffHandItemInteraction(context, snapshot1);
}
context.accept();
return;
}
context.popSnapshot(snapshot1);
}
handleItemInteraction(context, snapshot);
}
}
Aggregations