Skip to content

Commit

Permalink
[Bukkit] Add 1.21.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
NEZNAMY committed Dec 3, 2024
1 parent 8616769 commit 9405e4e
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class BukkitReflection {
@Getter
private static final boolean is1_20_3Plus = ReflectionUtils.classExists("net.minecraft.network.protocol.game.ClientboundResetScorePacket");

/** Flag determining whether the server version is at least 1.21.4 or not */
@Getter
private static final boolean is1_21_4Plus = serverVersion.minorVersion >= 21 &&
ReflectionUtils.getFields(getClass("network.chat.Style", "network.chat.ChatModifier"), Integer.class).size() == 1;

@NotNull
@SneakyThrows
private static Method getHandle() {
Expand Down Expand Up @@ -78,7 +83,7 @@ public static int getMinorVersion() {
* if class does not exist
*/
@SneakyThrows
public static Class<?> getClass(@NotNull String... names) throws ClassNotFoundException {
public static Class<?> getClass(@NotNull String... names) {
for (String name : names) {
try {
return serverVersion.getClass.apply(name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,15 @@ public ReflectionComponentConverter() throws ReflectiveOperationException {
Class<?> chatHoverable = BukkitReflection.getClass("network.chat.HoverEvent", "network.chat.ChatHoverable", "ChatHoverable");
ResourceLocation_tryParse = ReflectionUtils.getMethod(ResourceLocation, new String[]{"tryParse", "m_135820_", "a"}, String.class);
ChatHexColor_fromRGB = ReflectionUtils.getMethods(chatHexColor, chatHexColor, int.class).get(0); // There should only be 1, but some mods add more
newChatModifier = ReflectionUtils.setAccessible(ChatModifier.getDeclaredConstructor(chatHexColor, Boolean.class, Boolean.class, Boolean.class,
Boolean.class, Boolean.class, chatClickable, chatHoverable, String.class, ResourceLocation));
if (BukkitReflection.is1_21_4Plus()) {
// 1.21.4+
newChatModifier = ReflectionUtils.setAccessible(ChatModifier.getDeclaredConstructor(chatHexColor, Integer.class, Boolean.class, Boolean.class,
Boolean.class, Boolean.class, Boolean.class, chatClickable, chatHoverable, String.class, ResourceLocation));
} else {
// 1.21.3-
newChatModifier = ReflectionUtils.setAccessible(ChatModifier.getDeclaredConstructor(chatHexColor, Boolean.class, Boolean.class, Boolean.class,
Boolean.class, Boolean.class, chatClickable, chatHoverable, String.class, ResourceLocation));
}
convertModifier = this::createModifierModern;
} else {
newChatModifier = ChatModifier.getConstructor();
Expand Down Expand Up @@ -103,18 +110,34 @@ private Object createModifierModern(@NotNull ChatModifier modifier, boolean mode
color = ChatHexColor_fromRGB.invoke(null, modifier.getColor().getLegacyColor().getRgb());
}
}
return newChatModifier.newInstance(
color,
modifier.isBold(),
modifier.isItalic(),
modifier.isUnderlined(),
modifier.isStrikethrough(),
modifier.isObfuscated(),
null,
null,
null,
modifier.getFont() == null ? null : ResourceLocation_tryParse.invoke(null, modifier.getFont())
);
if (BukkitReflection.is1_21_4Plus()) {
return newChatModifier.newInstance(
color,
0,
modifier.isBold(),
modifier.isItalic(),
modifier.isUnderlined(),
modifier.isStrikethrough(),
modifier.isObfuscated(),
null,
null,
null,
modifier.getFont() == null ? null : ResourceLocation_tryParse.invoke(null, modifier.getFont())
);
} else {
return newChatModifier.newInstance(
color,
modifier.isBold(),
modifier.isItalic(),
modifier.isUnderlined(),
modifier.isStrikethrough(),
modifier.isObfuscated(),
null,
null,
null,
modifier.getFont() == null ? null : ResourceLocation_tryParse.invoke(null, modifier.getFont())
);
}
}

@SneakyThrows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ public class PacketTabList1193 extends PacketTabList18 {
private static final Map<Action, EnumSet<?>> actionToEnumSet = new EnumMap<>(Action.class);

private static boolean v1_21_2Plus;
private static boolean v1_21_4Plus;

private static Enum actionAddPlayer;
private static Enum actionUpdateDisplayName;
Expand Down Expand Up @@ -95,14 +94,13 @@ public static void loadNew() throws ReflectiveOperationException {
actionToEnumSet.put(Action.UPDATE_LIST_ORDER, EnumSet.of(Enum.valueOf(ActionClass, Action.UPDATE_LIST_ORDER.name())));
PlayerInfoData_ListOrder = ReflectionUtils.getFields(playerInfoDataClass, int.class).get(1);
v1_21_2Plus = true;
try {
if (BukkitReflection.is1_21_4Plus()) {
// 1.21.4+
actionToEnumSet.put(Action.UPDATE_HAT, EnumSet.of(Enum.valueOf(ActionClass, Action.UPDATE_HAT.name())));
PlayerInfoData_ShowHat = ReflectionUtils.getFields(playerInfoDataClass, boolean.class).get(1);
newPlayerInfoData = playerInfoDataClass.getConstructor(UUID.class, GameProfile.class, boolean.class, int.class,
EnumGamemodeClass, IChatBaseComponent, boolean.class, int.class, RemoteChatSession$Data);
v1_21_4Plus = true;
} catch (Exception ignored) {
} else {
// 1.21.2 - 1.21.3
newPlayerInfoData = playerInfoDataClass.getConstructor(UUID.class, GameProfile.class, boolean.class, int.class,
EnumGamemodeClass, IChatBaseComponent, int.class, RemoteChatSession$Data);
Expand Down Expand Up @@ -176,7 +174,7 @@ public void onPacketSend(@NonNull Object packet) {
Object displayName = PlayerInfoData_DisplayName.get(nmsData);
int latency = PlayerInfoData_Latency.getInt(nmsData);
int listOrder = v1_21_2Plus ? PlayerInfoData_ListOrder.getInt(nmsData) : 0;
boolean showHat = v1_21_4Plus && PlayerInfoData_ShowHat.getBoolean(nmsData);
boolean showHat = BukkitReflection.is1_21_4Plus() && PlayerInfoData_ShowHat.getBoolean(nmsData);
if (actions.contains(actionUpdateDisplayName)) {
Object expectedName = getExpectedDisplayNames().get(id);
if (expectedName != null && expectedName != displayName) {
Expand Down Expand Up @@ -213,7 +211,7 @@ public void onPacketSend(@NonNull Object packet) {
@SneakyThrows
private static Object newPlayerInfoData(@NotNull UUID id, @Nullable GameProfile profile, boolean listed, int latency,
@Nullable Object gameMode, @Nullable Object displayName, boolean showHat, int listOrder, @Nullable Object chatSession) {
if (v1_21_4Plus) {
if (BukkitReflection.is1_21_4Plus()) {
return newPlayerInfoData.newInstance(id, profile, listed, latency, gameMode, displayName, showHat, listOrder, chatSession);
} else if (v1_21_2Plus) {
return newPlayerInfoData.newInstance(id, profile, listed, latency, gameMode, displayName, listOrder, chatSession);
Expand Down

0 comments on commit 9405e4e

Please sign in to comment.