|
| 1 | +/* |
| 2 | + * This file is part of npc-lib, licensed under the MIT License (MIT). |
| 3 | + * |
| 4 | + * Copyright (c) 2022-2025 Julian M., Pasqual K. and contributors |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package com.github.juliarn.npclib.fabric.mixins; |
| 26 | + |
| 27 | +import com.github.juliarn.npclib.fabric.controller.FabricActionControllerEvents; |
| 28 | +import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket; |
| 29 | +import net.minecraft.network.protocol.game.ServerboundMoveVehiclePacket; |
| 30 | +import net.minecraft.network.protocol.game.ServerboundPlayerCommandPacket; |
| 31 | +import net.minecraft.network.protocol.game.ServerboundSwingPacket; |
| 32 | +import net.minecraft.server.level.ServerPlayer; |
| 33 | +import net.minecraft.server.network.ServerGamePacketListenerImpl; |
| 34 | +import net.minecraft.world.phys.Vec2; |
| 35 | +import net.minecraft.world.phys.Vec3; |
| 36 | +import org.spongepowered.asm.mixin.Mixin; |
| 37 | +import org.spongepowered.asm.mixin.Shadow; |
| 38 | +import org.spongepowered.asm.mixin.injection.At; |
| 39 | +import org.spongepowered.asm.mixin.injection.Inject; |
| 40 | +import org.spongepowered.asm.mixin.injection.Slice; |
| 41 | +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; |
| 42 | + |
| 43 | +@Mixin(ServerGamePacketListenerImpl.class) |
| 44 | +public abstract class ServerGamePacketListenerImplMixin { |
| 45 | + |
| 46 | + @Shadow |
| 47 | + public abstract ServerPlayer getPlayer(); |
| 48 | + |
| 49 | + @Inject(method = "handlePlayerCommand", at = @At("TAIL")) |
| 50 | + public void npc_lib$handlePlayerCommand(ServerboundPlayerCommandPacket packet, CallbackInfo ci) { |
| 51 | + var player = this.getPlayer(); |
| 52 | + var command = packet.getAction(); |
| 53 | + if (command == ServerboundPlayerCommandPacket.Action.PRESS_SHIFT_KEY |
| 54 | + || command == ServerboundPlayerCommandPacket.Action.RELEASE_SHIFT_KEY) { |
| 55 | + var invoker = FabricActionControllerEvents.SERVER_PLAYER_TOGGLE_SNEAK.invoker(); |
| 56 | + var sprintStarted = command == ServerboundPlayerCommandPacket.Action.PRESS_SHIFT_KEY; |
| 57 | + invoker.toggleSneak(player, sprintStarted); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + @Inject(method = "handleAnimate", at = @At("TAIL")) |
| 62 | + public void npc_lib$handleAnimate(ServerboundSwingPacket packet, CallbackInfo ci) { |
| 63 | + var player = this.getPlayer(); |
| 64 | + var invoker = FabricActionControllerEvents.SERVER_PLAYER_HAND_SWING.invoker(); |
| 65 | + invoker.swingHand(player); |
| 66 | + } |
| 67 | + |
| 68 | + @Inject( |
| 69 | + method = "handleMovePlayer", |
| 70 | + at = @At( |
| 71 | + value = "INVOKE", |
| 72 | + target = "Lnet/minecraft/network/protocol/game/ServerboundMovePlayerPacket;getYRot(F)F" |
| 73 | + ), |
| 74 | + slice = @Slice( |
| 75 | + from = @At(value = "INVOKE", target = "Lnet/minecraft/server/network/ServerGamePacketListenerImpl;updateAwaitingTeleport()Z"), |
| 76 | + to = @At(value = "INVOKE", target = "Lnet/minecraft/server/level/ServerPlayer;isPassenger()Z") |
| 77 | + ) |
| 78 | + ) |
| 79 | + public void npc_lib$handleMovePlayer(ServerboundMovePlayerPacket packet, CallbackInfo ci) { |
| 80 | + // ensure that the packet contains at least position and rotation |
| 81 | + var hasPos = packet.hasPosition(); |
| 82 | + var hasRot = packet.hasRotation(); |
| 83 | + if (!hasPos && !hasRot) { |
| 84 | + return; |
| 85 | + } |
| 86 | + |
| 87 | + // get the new target position, null in case it's not provided by the packet |
| 88 | + var player = this.getPlayer(); |
| 89 | + var posToX = packet.getX(player.getX()); |
| 90 | + var posToY = packet.getY(player.getY()); |
| 91 | + var posToZ = packet.getZ(player.getZ()); |
| 92 | + var posTo = hasPos ? new Vec3(posToX, posToY, posToZ) : null; |
| 93 | + |
| 94 | + // get the new target rotation, null in case it's not provided by the packet |
| 95 | + var rotToX = packet.getXRot(player.getXRot()); |
| 96 | + var rotToY = packet.getYRot(player.getYRot()); |
| 97 | + var rotTo = hasRot ? new Vec2(rotToX, rotToY) : null; |
| 98 | + |
| 99 | + // invoke the player move event |
| 100 | + var invoker = FabricActionControllerEvents.SERVER_PLAYER_MOVE.invoker(); |
| 101 | + invoker.move(player, posTo, rotTo); |
| 102 | + } |
| 103 | + |
| 104 | + @Inject( |
| 105 | + method = "handleMoveVehicle", |
| 106 | + at = @At( |
| 107 | + value = "INVOKE", |
| 108 | + target = "Lnet/minecraft/world/entity/Entity;getControllingPassenger()Lnet/minecraft/world/entity/LivingEntity;" |
| 109 | + ) |
| 110 | + ) |
| 111 | + public void npc_lib$handleMoveVehicle(ServerboundMoveVehiclePacket packet, CallbackInfo ci) { |
| 112 | + var player = this.getPlayer(); |
| 113 | + var rootVehicle = player.getRootVehicle(); |
| 114 | + |
| 115 | + // get the target position, set to null in case it didn't change |
| 116 | + var posFrom = rootVehicle.position(); |
| 117 | + var posTo = packet.position(); |
| 118 | + if (posTo.equals(posFrom)) { |
| 119 | + posTo = null; |
| 120 | + } |
| 121 | + |
| 122 | + // get the target rotation, set to null in case it didn't change |
| 123 | + var rotFrom = rootVehicle.getRotationVector(); |
| 124 | + var rotTo = new Vec2(packet.xRot(), packet.yRot()); |
| 125 | + if (rotTo.equals(rotFrom)) { |
| 126 | + rotTo = null; |
| 127 | + } |
| 128 | + |
| 129 | + // fire player move event in case position or rotation changed |
| 130 | + if (posTo != null || rotTo != null) { |
| 131 | + var invoker = FabricActionControllerEvents.SERVER_PLAYER_MOVE.invoker(); |
| 132 | + invoker.move(player, posTo, rotTo); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments