From 2a2f6d6c2a7a481e3ab71e8fb0b61cc82861a4c6 Mon Sep 17 00:00:00 2001 From: Pablo Herrera Date: Mon, 21 Aug 2023 23:21:46 +0200 Subject: [PATCH] Workarround adventure breaking translatable comments --- core/src/main/java/tc/oc/pgm/PGMPlugin.java | 3 +++ .../java/tc/oc/pgm/util/bukkit/ViaUtils.java | 20 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/core/src/main/java/tc/oc/pgm/PGMPlugin.java b/core/src/main/java/tc/oc/pgm/PGMPlugin.java index 5475d3698b..fdcf625656 100644 --- a/core/src/main/java/tc/oc/pgm/PGMPlugin.java +++ b/core/src/main/java/tc/oc/pgm/PGMPlugin.java @@ -67,6 +67,7 @@ import tc.oc.pgm.tablist.MatchTabManager; import tc.oc.pgm.util.FileUtils; import tc.oc.pgm.util.bukkit.BukkitUtils; +import tc.oc.pgm.util.bukkit.ViaUtils; import tc.oc.pgm.util.chunk.NullChunkGenerator; import tc.oc.pgm.util.compatability.SportPaperListener; import tc.oc.pgm.util.concurrent.BukkitExecutorService; @@ -118,6 +119,8 @@ public void onEnable() { // Sanity test PGM is running on a supported version before doing any work NMSHacks.allocateEntityId(); + // Fix before any audiences have the chance of creating + ViaUtils.removeViaChatFacet(); Permissions.registerAll(); diff --git a/util/src/main/java/tc/oc/pgm/util/bukkit/ViaUtils.java b/util/src/main/java/tc/oc/pgm/util/bukkit/ViaUtils.java index a2b80dfe66..0be81e4318 100644 --- a/util/src/main/java/tc/oc/pgm/util/bukkit/ViaUtils.java +++ b/util/src/main/java/tc/oc/pgm/util/bukkit/ViaUtils.java @@ -1,6 +1,8 @@ package tc.oc.pgm.util.bukkit; import com.viaversion.viaversion.api.Via; +import java.lang.reflect.Field; +import java.util.List; import org.bukkit.entity.Player; public class ViaUtils { @@ -41,4 +43,22 @@ public static int getProtocolVersion(Player player) { public static boolean isReady(Player player) { return !enabled() || Via.getAPI().isInjected(player.getUniqueId()); } + + /** + * Adventure has a ViaFacet$Chat class, which sends text using 1.16 format for support for hex + * codes. The issue is by doing that, it skips all translation layers from 1.8 to 1.16, including + * a needed rename for translated items to work. Removing this means hex colors would be + * restricted to the 16 colors even for 1.16 clients (pgm doesn't use them) but translations will + * be correct. + */ + public static void removeViaChatFacet() { + try { + Class bukkitAudience = Class.forName("net.kyori.adventure.platform.bukkit.BukkitAudience"); + Field f = bukkitAudience.getDeclaredField("CHAT"); + f.setAccessible(true); + List list = (List) f.get(null); + list.removeIf(el -> el.getClass().getName().endsWith("ViaFacet$Chat")); + } catch (ReflectiveOperationException ignored) { + } + } }