Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
uses: actions/checkout@v7

- name: Setup java
uses: actions/setup-java@v5
uses: actions/setup-java@v6
with:
java-version: 25
check-latest: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,18 @@

public enum EntityAnimation {

SWING_MAIN_ARM(0),
TAKE_DAMAGE(1),
LEAVE_BED(2),
SWING_OFF_HAND(3),
CRITICAL_EFFECT(4),
MAGIC_CRITICAL_EFFECT(5);

private final int id;

EntityAnimation(int id) {
this.id = id;
}
SWING_MAIN_ARM,
TAKE_DAMAGE,
LEAVE_BED,
SWING_OFF_HAND,
CRITICAL_EFFECT,
MAGIC_CRITICAL_EFFECT;

/**
* @deprecated ids are different starting with 26.3, don't use them.
*/
@Deprecated
public int id() {
return this.id;
return this.ordinal();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.github.retrooper.packetevents.event.simple.PacketPlayReceiveEvent;
import com.github.retrooper.packetevents.manager.player.PlayerManager;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.component.builtin.item.ItemSwingAnimation;
import com.github.retrooper.packetevents.protocol.entity.data.EntityData;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataType;
import com.github.retrooper.packetevents.protocol.entity.data.EntityDataTypes;
Expand Down Expand Up @@ -79,6 +80,7 @@
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerPluginMessage;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnEntity;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSpawnPlayer;
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSwingAnimation;
import com.google.common.collect.ImmutableMap;
import io.github.retrooper.packetevents.factory.spigot.SpigotPacketEventsBuilder;
import io.github.retrooper.packetevents.util.SpigotReflectionUtil;
Expand All @@ -104,7 +106,6 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl

static final PacketEventsPacketAdapter INSTANCE = new PacketEventsPacketAdapter();

@SuppressWarnings("UnstableApiUsage") // fine for now
private static final PacketEventsSettings PACKET_EVENTS_SETTINGS = new PacketEventsSettings()
.debug(false)
.checkForUpdates(false)
Expand All @@ -115,7 +116,6 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl
Optional.class,
net.kyori.adventure.text.Component.class);


// lazy initialized, then never null again
private ServerVersion serverVersion;
private PlayerManager packetPlayerManager;
Expand Down Expand Up @@ -274,12 +274,28 @@ final class PacketEventsPacketAdapter implements PlatformPacketAdapter<World, Pl
@NotNull EntityAnimation animation
) {
return (player, npc) -> {
// EntityAnimation (https://wiki.vg/Protocol#Entity_Animation_.28clientbound.29)
if (this.serverVersion.isNewerThanOrEquals(ServerVersion.V_26_3)
&& (animation == EntityAnimation.SWING_MAIN_ARM || animation == EntityAnimation.SWING_OFF_HAND)) {
// main-/offhand swing is a separate packet
InteractionHand hand =
animation == EntityAnimation.SWING_MAIN_ARM ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
PacketWrapper<?> wrapper = new WrapperPlayServerSwingAnimation(
npc.entityId(),
hand,
Lazy.DEFAULT_SWING_ANIMATION);
this.packetPlayerManager.sendPacketSilently(player, wrapper);
return;
}

// remap TAKE_DAMAGE to CRITICAL_EFFECT as it was removed in 1.19.3
EntityAnimation finalAnimation = animation;
if (this.serverVersion.isNewerThanOrEquals(ServerVersion.V_1_19_3) && animation == EntityAnimation.TAKE_DAMAGE) {
finalAnimation = EntityAnimation.CRITICAL_EFFECT;
}

WrapperPlayServerEntityAnimation.EntityAnimationType animationType
= Lazy.ENTITY_ANIMATION_CONVERTER.get(animation);
= Lazy.ENTITY_ANIMATION_CONVERTER.get(finalAnimation);
PacketWrapper<?> wrapper = new WrapperPlayServerEntityAnimation(npc.entityId(), animationType);

// send the packet without notifying any listeners
this.packetPlayerManager.sendPacketSilently(player, wrapper);
};
}
Expand Down Expand Up @@ -450,6 +466,10 @@ private static final class Lazy {
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_GAME_MODE,
WrapperPlayServerPlayerInfoUpdate.Action.UPDATE_DISPLAY_NAME);

// default item swing animation (since 26.3)
private static final ItemSwingAnimation DEFAULT_SWING_ANIMATION =
new ItemSwingAnimation(ItemSwingAnimation.Type.WHACK, 6);

static {
// associate item slots actions with their respective packet events enum
ITEM_SLOT_CONVERTER = new EnumMap<>(ItemSlot.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import com.comphenix.protocol.PacketType;
import com.comphenix.protocol.ProtocolLibrary;
import com.comphenix.protocol.ProtocolManager;
import com.comphenix.protocol.events.InternalStructure;
import com.comphenix.protocol.events.PacketAdapter;
import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.events.PacketEvent;
import com.comphenix.protocol.reflect.StructureModifier;
import com.comphenix.protocol.utility.MinecraftReflection;
import com.comphenix.protocol.utility.MinecraftVersion;
import com.comphenix.protocol.wrappers.EnumWrappers;
Expand Down Expand Up @@ -77,6 +79,7 @@
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.util.Vector;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -92,6 +95,7 @@
private static final MinecraftVersion SERVER_VERSION = MinecraftVersion.fromServerVersion(Bukkit.getVersion());

private static final EnumMap<EntityPose, Object> ENTITY_POSE_CONVERTER;
private static final EnumMap<EntityAnimation, Integer> ENTITY_ANIMATION_ID_LOOKUP;
private static final EnumMap<ItemSlot, EnumWrappers.ItemSlot> ITEM_SLOT_CONVERTER;
private static final EnumMap<EnumWrappers.Hand, InteractNpcEvent.Hand> HAND_CONVERTER;

Expand All @@ -106,6 +110,9 @@
EnumWrappers.PlayerInfoAction.UPDATE_GAME_MODE,
EnumWrappers.PlayerInfoAction.UPDATE_DISPLAY_NAME);

// net.minecraft.world.item.SwingAnimationType.WHACK (since 26.3)
private static final Enum<?> WHACK_ANIMATION_TYPE;

static {
// associate item slots with their respective protocol lib enum
ITEM_SLOT_CONVERTER = new EnumMap<>(ItemSlot.class);
Expand Down Expand Up @@ -193,6 +200,33 @@
}
})
.build();

// associate the entity animation with their respective id
ENTITY_ANIMATION_ID_LOOKUP = new EnumMap<>(EntityAnimation.class);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.SWING_MAIN_ARM, 0);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.TAKE_DAMAGE, 1);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.LEAVE_BED, 2);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.SWING_OFF_HAND, 3);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.CRITICAL_EFFECT, 4);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.MAGIC_CRITICAL_EFFECT, 5);
if (MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove()) {
// 1.19.3+: TAKE_DAMAGE was removed, use CRITICAL_EFFECT instead
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.TAKE_DAMAGE, 4);
}
if (MinecraftVersion.v26_3.atOrAbove()) {
// 26.3+: SWING_MAIN_HAND/SWING_OFF_HAND were removed, all ids were shifted
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.LEAVE_BED, 0);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.TAKE_DAMAGE, 1);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.CRITICAL_EFFECT, 1);
ENTITY_ANIMATION_ID_LOOKUP.put(EntityAnimation.MAGIC_CRITICAL_EFFECT, 2);
ENTITY_ANIMATION_ID_LOOKUP.remove(EntityAnimation.SWING_MAIN_ARM);
ENTITY_ANIMATION_ID_LOOKUP.remove(EntityAnimation.SWING_OFF_HAND);
}

// WHACK animation type
Class<?> swingAnimType = MinecraftReflection.getNullableNMS("world.item.SwingAnimationType");
//noinspection unchecked,rawtypes
WHACK_ANIMATION_TYPE = swingAnimType == null ? null : Enum.valueOf((Class<? extends Enum>) swingAnimType, "WHACK");

Check warning on line 229 in bukkit/src/main/java/com/github/juliarn/npclib/bukkit/protocol/ProtocolLibPacketAdapter.java

View workflow job for this annotation

GitHub Actions / build

[rawtypes] found raw type: Enum

Check warning on line 229 in bukkit/src/main/java/com/github/juliarn/npclib/bukkit/protocol/ProtocolLibPacketAdapter.java

View workflow job for this annotation

GitHub Actions / build

[rawtypes] found raw type: Enum
}

// MonotonicNonNull, lazily initialized
Expand Down Expand Up @@ -295,6 +329,13 @@
container.getDataWatcherModifier().write(0, new WrappedDataWatcher());
}

// 1.21.9+: write the movement vector, prevents a rare
// ProtocolLib bug where the vector becomes null depending
// on the order the init constructors are selected
if (MinecraftVersion.v1_21_9.atOrAbove()) {
container.getVectors().write(0, new Vector());
}

// send the packet without notifying any bound packet listeners
PROTOCOL_MANAGER.sendServerPacket(player, container, false);
};
Expand Down Expand Up @@ -354,10 +395,8 @@
PacketContainer container = new PacketContainer(PacketType.Play.Server.PLAYER_INFO);

// action
int playerInfoDataIndex = 0;
if (MinecraftVersion.FEATURE_PREVIEW_UPDATE.atOrAbove()) {
// mc 1.19.3: multiple actions to update everything related to the player
playerInfoDataIndex = 1; // there are now 2 list types in the packets
container.getPlayerInfoActions().write(0, ADD_ACTIONS);
} else {
// mc 1.8: one action which automatically updated everything
Expand All @@ -372,7 +411,7 @@
EnumWrappers.NativeGameMode.CREATIVE,
wrappedGameProfile,
null);
container.getPlayerInfoDataLists().write(playerInfoDataIndex, Lists.newArrayList(playerInfoData));
container.getPlayerInfoDataLists().write(0, Lists.newArrayList(playerInfoData));

// send the packet without notifying any bound packet listeners
PROTOCOL_MANAGER.sendServerPacket(player, container, false);
Expand Down Expand Up @@ -427,15 +466,29 @@
@NotNull EntityAnimation animation
) {
return (player, npc) -> {
// EntityAnimation (https://wiki.vg/Protocol#Entity_Animation_.28clientbound.29)
PacketContainer container = new PacketContainer(PacketType.Play.Server.ANIMATION);

// entity id & animation id
container.getIntegers()
.write(0, npc.entityId())
.write(1, animation.id());
if (MinecraftVersion.v26_3.atOrAbove()
&& (animation == EntityAnimation.SWING_MAIN_ARM || animation == EntityAnimation.SWING_OFF_HAND)) {
// main-/offhand swing is a separate packet
EnumWrappers.Hand hand = animation == EntityAnimation.SWING_MAIN_ARM
? EnumWrappers.Hand.MAIN_HAND
: EnumWrappers.Hand.OFF_HAND;
PacketContainer container = new PacketContainer(PacketType.Play.Server.SWING_ANIMATION);
container.getIntegers().write(0, npc.entityId());
container.getHands().write(0, hand);

// modify the animation (default WHACK animation)
InternalStructure animationStructure = container.getStructures().read(1);
StructureModifier<Enum<?>> animationTypeModifier = animationStructure.getModifier().withType(Enum.class);
animationStructure.getIntegers().write(0, 6); // duration
animationTypeModifier.write(0, WHACK_ANIMATION_TYPE);

PROTOCOL_MANAGER.sendServerPacket(player, container, false);
return;
}

// send the packet without notifying any bound packet listeners
int animationId = ENTITY_ANIMATION_ID_LOOKUP.get(animation);
PacketContainer container = new PacketContainer(PacketType.Play.Server.ANIMATION);
container.getIntegers().write(0, npc.entityId()).write(1, animationId);
PROTOCOL_MANAGER.sendServerPacket(player, container, false);
};
}
Expand Down
2 changes: 1 addition & 1 deletion fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ tasks.shadowJar {
}

tasks.compileJava {
options.release = 21
options.release = 25
}

tasks.processResources {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ public abstract class PlayerListMixin {
@Inject(method = "placeNewPlayer", at = @At("TAIL"))
public void npc_lib$placeNewPlayer(
Connection connection,
ServerPlayer serverPlayer,
CommonListenerCookie commonListenerCookie,
ServerPlayer player,
CommonListenerCookie cookie,
CallbackInfo ci
) {
var newLevel = serverPlayer.level();
var newLevel = player.level();
var invoker = FabricActionControllerEvents.SERVER_PLAYER_LEVEL_CHANGE.invoker();
invoker.levelChange(serverPlayer, null, newLevel);
invoker.levelChange(player, null, newLevel);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import net.minecraft.network.protocol.game.ServerboundMovePlayerPacket;
import net.minecraft.network.protocol.game.ServerboundMoveVehiclePacket;
import net.minecraft.network.protocol.game.ServerboundPlayerInputPacket;
import net.minecraft.network.protocol.game.ServerboundSwingPacket;
import net.minecraft.network.protocol.game.ServerboundPunchPacket;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
import net.minecraft.world.InteractionHand;
Expand Down Expand Up @@ -67,8 +67,8 @@ public abstract class ServerGamePacketListenerImplMixin {
}
}

@Inject(method = "handleAnimate", at = @At("TAIL"))
public void npc_lib$handleAnimate(ServerboundSwingPacket packet, CallbackInfo ci) {
@Inject(method = "handlePunch", at = @At("TAIL"))
public void npc_lib$handlePunch(ServerboundPunchPacket packet, CallbackInfo ci) {
var player = this.getPlayer();
var invoker = FabricActionControllerEvents.SERVER_PLAYER_HAND_SWING.invoker();
invoker.swingHand(player);
Expand Down Expand Up @@ -120,17 +120,18 @@ public abstract class ServerGamePacketListenerImplMixin {
public void npc_lib$handleMoveVehicle(ServerboundMoveVehiclePacket packet, CallbackInfo ci) {
var player = this.getPlayer();
var rootVehicle = player.getRootVehicle();
var newPosAndRot = packet.movingTo();

// get the target position, set to null in case it didn't change
var posFrom = rootVehicle.position();
var posTo = packet.position();
var posTo = newPosAndRot.position();
if (posTo.equals(posFrom)) {
posTo = null;
}

// get the target rotation, set to null in case it didn't change
var rotFrom = rootVehicle.getRotationVector();
var rotTo = new Vec2(packet.xRot(), packet.yRot());
var rotTo = new Vec2(newPosAndRot.xRot(), newPosAndRot.yRot());
if (rotTo.equals(rotFrom)) {
rotTo = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import net.minecraft.network.protocol.game.ClientboundRotateHeadPacket;
import net.minecraft.network.protocol.game.ClientboundSetEntityDataPacket;
import net.minecraft.network.protocol.game.ClientboundSetEquipmentPacket;
import net.minecraft.network.protocol.game.ClientboundSwingAnimationPacket;
import net.minecraft.network.syncher.EntityDataSerializers;
import net.minecraft.network.syncher.SynchedEntityData;
import net.minecraft.resources.Identifier;
Expand All @@ -83,6 +84,7 @@
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.Pose;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.component.SwingAnimation;
import net.minecraft.world.level.GameType;
import net.minecraft.world.phys.Vec3;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -322,7 +324,20 @@ private FabricProtocolAdapter() {
@NotNull EntityAnimation animation
) {
return (player, npc) -> {
var packet = new ClientboundAnimatePacket(player, animation.id());
if (animation == EntityAnimation.SWING_MAIN_ARM || animation == EntityAnimation.SWING_OFF_HAND) {
var hand = animation == EntityAnimation.SWING_MAIN_ARM ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND;
var packet = new ClientboundSwingAnimationPacket(npc.entityId(), hand, SwingAnimation.DEFAULT);
player.connection.send(packet);
return;
}

var animationId = switch (animation) {
case LEAVE_BED -> ClientboundAnimatePacket.WAKE_UP;
case MAGIC_CRITICAL_EFFECT -> ClientboundAnimatePacket.MAGIC_CRITICAL_HIT;
case CRITICAL_EFFECT, TAKE_DAMAGE -> ClientboundAnimatePacket.CRITICAL_HIT;
case SWING_MAIN_ARM, SWING_OFF_HAND -> throw new AssertionError("Must not reach here");
};
var packet = new ClientboundAnimatePacket(player, animationId);
packet.id = npc.entityId();
player.connection.send(packet);
};
Expand Down
4 changes: 2 additions & 2 deletions fabric/src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
"accessWidener": "npc_lib.accesswidener",
"depends": {
"fabricloader": ">=0.17.0",
"minecraft": "~26.2",
"java": ">=21",
"minecraft": "~26.3",
"java": ">=25",
"fabric-networking-api-v1": "*"
}
}
Loading
Loading