From a9b5019b87c2c4f4c837ea4c4357ab506db99efd Mon Sep 17 00:00:00 2001 From: MrMicky Date: Sat, 14 Oct 2023 15:57:08 +0200 Subject: [PATCH] Add Adventure support on legacy servers (closes #38) --- README.md | 8 ++++---- pom.xml | 2 +- src/main/java/fr/mrmicky/fastboard/FastBoard.java | 5 +++++ src/main/java/fr/mrmicky/fastboard/FastBoardBase.java | 7 +++++-- .../java/fr/mrmicky/fastboard/adventure/FastBoard.java | 7 ++++++- 5 files changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 1e85bd2..0302c2e 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ # FastBoard [![Java CI](https://github.com/MrMicky-FR/FastBoard/actions/workflows/build.yml/badge.svg)](https://github.com/MrMicky-FR/FastBoard/actions/workflows/build.yml) -[![Maven Central](https://img.shields.io/maven-central/v/fr.mrmicky/fastboard.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:%22fr.mrmicky%22%20AND%20a:%22fastboard%22) +[![Maven Central](https://img.shields.io/maven-central/v/fr.mrmicky/fastboard.svg?label=Maven%20Central)](https://central.sonatype.com/artifact/fr.mrmicky/fastboard) [![Discord](https://img.shields.io/discord/390919659874156560.svg?colorB=5865f2&label=Discord&logo=discord&logoColor=white)](https://discord.gg/q9UwaBT) Lightweight packet-based scoreboard API for Bukkit plugins, with 1.7.10 to 1.20 support. @@ -57,12 +57,12 @@ Lightweight packet-based scoreboard API for Bukkit plugins, with 1.7.10 to 1.20 fr.mrmicky fastboard - 2.0.0 + 2.0.1 ``` -When using Maven, make sure to build directly with Maven and not with your IDE configuration. (on IntelliJ IDEA: in the `Maven` tab on the right, in `Lifecycle`, use `package`). +When using Maven, make sure to build directly with Maven and not with your IDE configuration (on IntelliJ IDEA: in the `Maven` tab on the right, in `Lifecycle`, use `package`). ### Gradle @@ -76,7 +76,7 @@ repositories { } dependencies { - implementation 'fr.mrmicky:fastboard:2.0.0' + implementation 'fr.mrmicky:fastboard:2.0.1' } shadowJar { diff --git a/pom.xml b/pom.xml index d893315..625b3cb 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ fr.mrmicky fastboard - 2.0.0 + 2.0.1 FastBoard Lightweight packet-based scoreboard API for Bukkit plugins. diff --git a/src/main/java/fr/mrmicky/fastboard/FastBoard.java b/src/main/java/fr/mrmicky/fastboard/FastBoard.java index 4328b05..41ade11 100644 --- a/src/main/java/fr/mrmicky/fastboard/FastBoard.java +++ b/src/main/java/fr/mrmicky/fastboard/FastBoard.java @@ -138,6 +138,11 @@ protected Object toMinecraftComponent(String line) throws Throwable { return Array.get(MESSAGE_FROM_STRING.invoke(line), 0); } + @Override + protected String serializeLine(String value) { + return value; + } + @Override protected String emptyLine() { return ""; diff --git a/src/main/java/fr/mrmicky/fastboard/FastBoardBase.java b/src/main/java/fr/mrmicky/fastboard/FastBoardBase.java index ec42e77..b28aa32 100644 --- a/src/main/java/fr/mrmicky/fastboard/FastBoardBase.java +++ b/src/main/java/fr/mrmicky/fastboard/FastBoardBase.java @@ -51,7 +51,7 @@ * The project is on GitHub. * * @author MrMicky - * @version 2.0.0 + * @version 2.0.1 */ public abstract class FastBoardBase { @@ -411,6 +411,8 @@ public void delete() { protected abstract Object toMinecraftComponent(T value) throws Throwable; + protected abstract String serializeLine(T value); + protected abstract T emptyLine(); private void checkLineNumber(int line, boolean checkInRange, boolean checkMax) { @@ -561,7 +563,8 @@ private void setField(Object packet, Class fieldType, Object value, int count private void setComponentField(Object packet, T value, int count) throws Throwable { if (!VersionType.V1_13.isHigherOrEqual()) { - setField(packet, String.class, value != null ? value : "", count); + String line = value != null ? serializeLine(value) : ""; + setField(packet, String.class, line, count); return; } diff --git a/src/main/java/fr/mrmicky/fastboard/adventure/FastBoard.java b/src/main/java/fr/mrmicky/fastboard/adventure/FastBoard.java index b30282c..fb86f48 100644 --- a/src/main/java/fr/mrmicky/fastboard/adventure/FastBoard.java +++ b/src/main/java/fr/mrmicky/fastboard/adventure/FastBoard.java @@ -91,7 +91,7 @@ protected Object toMinecraftComponent(Component component) throws Throwable { // If the server isn't running adventure natively, we convert the component to legacy text // and then to a Minecraft chat component if (!ADVENTURE_SUPPORT) { - String legacy = LegacyComponentSerializer.legacySection().serialize(component); + String legacy = serializeLine(component); return Array.get(COMPONENT_METHOD.invoke(legacy), 0); } @@ -99,6 +99,11 @@ protected Object toMinecraftComponent(Component component) throws Throwable { return COMPONENT_METHOD.invoke(component); } + @Override + protected String serializeLine(Component value) { + return LegacyComponentSerializer.legacySection().serialize(value); + } + @Override protected Component emptyLine() { return Component.empty();