Skip to content
Closed
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
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-api</artifactId>
<version>5.1.1</version>
<version>5.2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.kyori</groupId>
<artifactId>adventure-text-serializer-legacy</artifactId>
<version>5.1.1</version>
<version>5.2.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
61 changes: 60 additions & 1 deletion src/main/java/fr/mrmicky/fastboard/FastBoardBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import java.lang.reflect.Modifier;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static fr.mrmicky.fastboard.FastReflection.optionalClass;

/**
* Lightweight packet-based scoreboard API for Bukkit plugins.
* It can be safely used asynchronously because everything is handled at the packet level.
Expand Down Expand Up @@ -81,6 +84,17 @@ public abstract class FastBoardBase<T> {
private static final Object ENUM_SB_ACTION_REMOVE;
private static final Object DUMMY_SCOREBOARD_CRITERIA;

// handles for methods that set the raw component fields of a scoreboard team. canvas only
private static MethodHandle SET_PLAYER_SUFFIX_RAW = null;
private static MethodHandle SET_PLAYER_PREFIX_RAW = null;
private static MethodHandle SET_DISPLAY_NAME_RAW = null;

// method to check if we are on a canvas server
private static final boolean CANVAS_PRESENT = optionalClass("io.canvasmc.canvas.util.LockedReference").isPresent();
private static boolean isCanvas() {
Comment thread
byPixelTV marked this conversation as resolved.
return CANVAS_PRESENT;
}

static {
try {
MethodHandles.Lookup lookup = MethodHandles.lookup();
Expand Down Expand Up @@ -131,6 +145,32 @@ public abstract class FastBoardBase<T> {
Class<?> objectiveCriteriaClass = FastReflection.nmsClass("world.scores.criteria", "IScoreboardCriteria", "ObjectiveCriteria");
PLAYER_TEAM = lookup.unreflectConstructor(playerTeamClass.getConstructor(scoreboardClass, String.class));

if (isCanvas()) {
// Canvas has changed the way scoreboard teams work, so we need to use reflection to find the methods that set the raw component fields
List<Method> teamMethods = Stream.concat(
Arrays.stream(playerTeamClass.getSuperclass().getDeclaredMethods()),
Arrays.stream(playerTeamClass.getDeclaredMethods())
).collect(Collectors.toList());

Method setDisplayNameRaw = teamMethods.stream()
.filter(m -> m.getName().equals("setDisplayNameRaw") && m.getParameterCount() == 1 && m.getParameterTypes()[0] == CHAT_COMPONENT_CLASS)
.findFirst().orElseThrow(NoSuchMethodException::new);
setDisplayNameRaw.setAccessible(true);
SET_DISPLAY_NAME_RAW = lookup.unreflect(setDisplayNameRaw);

Method setPlayerPrefixRaw = teamMethods.stream()
.filter(m -> m.getName().equals("setPlayerPrefixRaw") && m.getParameterCount() == 1 && m.getParameterTypes()[0] == CHAT_COMPONENT_CLASS)
.findFirst().orElseThrow(NoSuchMethodException::new);
setPlayerPrefixRaw.setAccessible(true);
SET_PLAYER_PREFIX_RAW = lookup.unreflect(setPlayerPrefixRaw);

Method setPlayerSuffixRaw = teamMethods.stream()
.filter(m -> m.getName().equals("setPlayerSuffixRaw") && m.getParameterCount() == 1 && m.getParameterTypes()[0] == CHAT_COMPONENT_CLASS)
.findFirst().orElseThrow(NoSuchMethodException::new);
setPlayerSuffixRaw.setAccessible(true);
SET_PLAYER_SUFFIX_RAW = lookup.unreflect(setPlayerSuffixRaw);
}

Class<?> objectiveRenderTypeClass = FastReflection.nmsOptionalClass("world.scores.criteria", "IScoreboardCriteria$EnumScoreboardHealthDisplay", "ObjectiveCriteria$RenderType").orElse(null);

Optional<Class<?>> numberFormat = FastReflection.nmsOptionalClass("network.chat.numbers", "NumberFormat");
Expand Down Expand Up @@ -840,16 +880,35 @@ private void setField(Object packet, Class<?> fieldType, Object value, int count
}

private void setComponentField(Object packet, T value, int count) throws Throwable {
Class<?> packetClass = packet.getClass();
String className = packetClass.getSimpleName();

if (isCanvas() && (className.equals("PlayerTeam") || className.equals("ScoreboardTeam"))) {
Object component = toMinecraftComponent(value);
if (count == 1) {
SET_DISPLAY_NAME_RAW.invoke(packet, component);
return;
} else if (count == 2) {
SET_PLAYER_PREFIX_RAW.invoke(packet, component);
return;
} else if (count == 3) {
SET_PLAYER_SUFFIX_RAW.invoke(packet, component);
return;
}
}

if (!VersionType.V1_13.isHigherOrEqual()) {
String line = value != null ? serializeLine(value) : "";
setField(packet, String.class, line, count);
return;
}

int i = 0;
for (Field field : PACKETS.get(packet.getClass())) {
Field[] fields = PACKETS.get(packetClass);
for (Field field : fields) {
if ((field.getType() == String.class || field.getType() == CHAT_COMPONENT_CLASS) && count == i++) {
field.set(packet, toMinecraftComponent(value));
break;
}
}
}
Expand Down