use of org.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.
the class CodecPlayInUseEntity method decode.
@Override
public MessagePlayInUseEntity decode(CodecContext context, ByteBuffer buf) throws CodecException {
final int entityId = buf.readVarInt();
final int action = buf.readVarInt();
if (action == 1) {
return new MessagePlayInUseEntity.Attack(entityId);
} else if (action == 0 || action == 2) {
Vector3d position = null;
if (action == 2) {
final double x = buf.readFloat();
final double y = buf.readFloat();
final double z = buf.readFloat();
position = new Vector3d(x, y, z);
}
final HandType hand = buf.readVarInt() == 0 ? HandTypes.MAIN_HAND : HandTypes.OFF_HAND;
return new MessagePlayInUseEntity.Interact(entityId, hand, position);
} else {
throw new DecoderException("Received a UseEntity message with a unknown action: " + action);
}
}
use of org.spongepowered.api.data.type.HandType 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.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method handleFinishItemInteraction.
public void handleFinishItemInteraction(MessagePlayInOutFinishUsingItem message) {
final Optional<HandType> activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty());
// The player is already interacting
if (!activeHand.isPresent() || this.activeHandStartTime == -1L) {
return;
}
// Try the action of the hotbar item first
final AbstractSlot slot = activeHand.get() == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
final ItemStack rawItemStack = slot.getRawItemStack();
if (rawItemStack == null) {
return;
}
// Require a minimum amount of ticks for the interaction to succeed
final MinimumUseDurationProperty property = rawItemStack.getProperty(MinimumUseDurationProperty.class).orElse(null);
if (property != null) {
final long time = LanternGame.currentTimeTicks();
if (time - this.activeHandStartTime < property.getValue()) {
resetItemUseTime();
return;
}
}
handleFinishItemInteraction0(slot, activeHand.get());
}
use of org.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method handleHandItemInteraction.
private boolean handleHandItemInteraction(BehaviorContextImpl context, HandType handType, AbstractSlot slot, @Nullable BehaviorContext.Snapshot snapshot) {
final Optional<HandType> activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty());
// The player is already interacting
if (activeHand.isPresent()) {
return true;
}
final Optional<ItemStack> handItem = slot.peek();
if (handItem.isPresent()) {
final LanternItemType itemType = (LanternItemType) handItem.get().getType();
context.addContext(ContextKeys.USED_ITEM_STACK, handItem.get());
context.addContext(ContextKeys.USED_SLOT, slot);
context.addContext(ContextKeys.INTERACTION_HAND, handType);
context.addContext(ContextKeys.ITEM_TYPE, itemType);
final BehaviorResult result = context.process(itemType.getPipeline().pipeline(InteractWithItemBehavior.class), (ctx, behavior) -> behavior.tryInteract(itemType.getPipeline(), ctx));
if (result.isSuccess()) {
return true;
}
if (snapshot != null) {
context.popSnapshot(snapshot);
}
}
return false;
}
use of org.spongepowered.api.data.type.HandType in project LanternServer by LanternPowered.
the class PlayerInteractionHandler method pulse.
/**
* Pulses the interaction handler.
*/
void pulse() {
if (this.diggingBlock != null) {
final int breakState = (int) Math.round(((double) Math.max(0, this.diggingEndTime - System.nanoTime()) / (double) this.diggingDuration) * 10.0);
if (this.lastBreakState != breakState) {
sendBreakUpdate(breakState);
this.lastBreakState = breakState;
}
}
final HandType activeHand = this.player.get(LanternKeys.ACTIVE_HAND).orElse(Optional.empty()).orElse(null);
final AbstractSlot slot = activeHand == null ? null : activeHand == HandTypes.MAIN_HAND ? this.player.getInventory().getHotbar().getSelectedSlot() : this.player.getInventory().getOffhand();
// The interaction just started
if (!Objects.equals(activeHand, this.lastActiveHand)) {
this.lastActiveHand = activeHand;
this.lastActiveItemStack = slot == null ? null : slot.getRawItemStack();
} else if (activeHand != null) {
if (this.activeHandStartTime == -1L) {
this.activeHandStartTime = LanternGame.currentTimeTicks();
}
final ItemStack itemStack = slot.getRawItemStack();
if (itemStack == null || this.lastActiveItemStack != itemStack) {
// Stop the interaction
resetItemUseTime();
} else {
final MaximumUseDurationProperty property = itemStack.getProperty(MaximumUseDurationProperty.class).orElse(null);
if (property != null) {
// Check if the interaction reached it's max time
final long time = LanternGame.currentTimeTicks();
if (time - this.activeHandStartTime > property.getValue()) {
handleFinishItemInteraction0(slot, activeHand);
}
}
}
}
}
Aggregations