Skip to content

Commit

Permalink
Add support for Minecraft 1.20.2 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
syldium authored Sep 28, 2023
1 parent 4d473fb commit 56a6518
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions src/main/java/fr/mrmicky/fastboard/FastBoardBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Stream;

/**
* Lightweight packet-based scoreboard API for Bukkit plugins.
Expand Down Expand Up @@ -73,8 +74,10 @@ public abstract class FastBoardBase<T> {
private static final FastReflection.PacketConstructor PACKET_SB_TEAM;
private static final FastReflection.PacketConstructor PACKET_SB_SERIALIZABLE_TEAM;
// Scoreboard enums
private static final Class<?> DISPLAY_SLOT_TYPE;
private static final Class<?> ENUM_SB_HEALTH_DISPLAY;
private static final Class<?> ENUM_SB_ACTION;
private static final Object SIDEBAR_DISPLAY_SLOT;
private static final Object ENUM_SB_HEALTH_DISPLAY_INTEGER;
private static final Object ENUM_SB_ACTION_CHANGE;
private static final Object ENUM_SB_ACTION_REMOVE;
Expand Down Expand Up @@ -107,13 +110,19 @@ public abstract class FastBoardBase<T> {
Field playerConnectionField = Arrays.stream(entityPlayerClass.getFields())
.filter(field -> field.getType().isAssignableFrom(playerConnectionClass))
.findFirst().orElseThrow(NoSuchFieldException::new);
Method sendPacketMethod = Arrays.stream(playerConnectionClass.getMethods())
Method sendPacketMethod = Stream.concat(
Arrays.stream(playerConnectionClass.getSuperclass().getMethods()),
Arrays.stream(playerConnectionClass.getMethods())
)
.filter(m -> m.getParameterCount() == 1 && m.getParameterTypes()[0] == packetClass)
.findFirst().orElseThrow(NoSuchMethodException::new);

Optional<Class<?>> displaySlotEnum = FastReflection.nmsOptionalClass("world.scores", "DisplaySlot");
CHAT_COMPONENT_CLASS = FastReflection.nmsClass("network.chat", "IChatBaseComponent");
CHAT_FORMAT_ENUM = FastReflection.nmsClass(null, "EnumChatFormat");
DISPLAY_SLOT_TYPE = displaySlotEnum.orElse(int.class);
RESET_FORMATTING = FastReflection.enumValueOf(CHAT_FORMAT_ENUM, "RESET", 21);
SIDEBAR_DISPLAY_SLOT = displaySlotEnum.isPresent() ? FastReflection.enumValueOf(DISPLAY_SLOT_TYPE, "SIDEBAR", 1) : 1;
PLAYER_GET_HANDLE = lookup.findVirtual(craftPlayerClass, "getHandle", MethodType.methodType(entityPlayerClass));
PLAYER_CONNECTION = lookup.unreflectGetter(playerConnectionField);
SEND_PACKET = lookup.unreflect(sendPacketMethod);
Expand Down Expand Up @@ -452,7 +461,7 @@ protected void sendObjectivePacket(ObjectiveMode mode) throws Throwable {
protected void sendDisplayObjectivePacket() throws Throwable {
Object packet = PACKET_SB_DISPLAY_OBJ.invoke();

setField(packet, int.class, 1); // Position (1: sidebar)
setField(packet, DISPLAY_SLOT_TYPE, SIDEBAR_DISPLAY_SLOT); // Position
setField(packet, String.class, this.id); // Score Name

sendPacket(packet);
Expand Down

0 comments on commit 56a6518

Please sign in to comment.