use of org.spongepowered.api.event.entity.living.AnimateHandEvent in project SpongeCommon by SpongePowered.
the class ServerGamePacketListenerImplMixin method impl$throwAnimationAndInteractEvents.
@SuppressWarnings("ConstantConditions")
@Inject(method = "handleAnimate", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;resetLastActionTime()V"), cancellable = true)
private void impl$throwAnimationAndInteractEvents(final ServerboundSwingPacket packetIn, final CallbackInfo ci) {
if (PhaseTracker.getInstance().getPhaseContext().isEmpty()) {
return;
}
final InteractionHand hand = packetIn.getHand();
if (!((ServerPlayerGameModeAccessor) this.player.gameMode).accessor$isDestroyingBlock()) {
if (this.impl$ignorePackets > 0) {
this.impl$ignorePackets--;
} else {
if (ShouldFire.INTERACT_ITEM_EVENT_PRIMARY) {
final Vec3 startPos = this.player.getEyePosition(1);
// TODO hook for blockReachDistance?
final Vec3 endPos = startPos.add(this.player.getLookAngle().scale(5d));
HitResult result = this.player.getLevel().clip(new ClipContext(startPos, endPos, ClipContext.Block.OUTLINE, ClipContext.Fluid.NONE, this.player));
if (result.getType() == HitResult.Type.MISS) {
final ItemStack heldItem = this.player.getItemInHand(hand);
SpongeCommonEventFactory.callInteractItemEventPrimary(this.player, heldItem, hand);
}
}
}
}
if (ShouldFire.ANIMATE_HAND_EVENT) {
final HandType handType = (HandType) (Object) hand;
final ItemStack heldItem = this.player.getItemInHand(hand);
try (final CauseStackManager.StackFrame frame = PhaseTracker.getCauseStackManager().pushCauseFrame()) {
frame.addContext(EventContextKeys.USED_ITEM, ItemStackUtil.snapshotOf(heldItem));
frame.addContext(EventContextKeys.USED_HAND, handType);
final AnimateHandEvent event = SpongeEventFactory.createAnimateHandEvent(frame.currentCause(), handType, (Humanoid) this.player);
if (SpongeCommon.post(event)) {
ci.cancel();
}
}
}
}
Aggregations