From 6789f419403fe7a6c8db2cedfed776f9c7182604 Mon Sep 17 00:00:00 2001 From: Rasmus Date: Fri, 10 Jul 2026 16:39:44 +0200 Subject: [PATCH 1/2] Split setting team color to an utility class to be used multiple places. --- .../handler/AbstractTabOverlayHandler.java | 5 ++- .../handler/NewTabOverlayHandler.java | 24 +---------- .../bungeetablistplus/util/TeamUtil.java | 40 +++++++++++++++++++ 3 files changed, 45 insertions(+), 24 deletions(-) create mode 100644 bungee/src/main/java/codecrafter47/bungeetablistplus/util/TeamUtil.java diff --git a/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java b/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java index b16e9044..342f7c3a 100644 --- a/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java +++ b/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java @@ -22,6 +22,7 @@ import codecrafter47.bungeetablistplus.util.BitSet; import codecrafter47.bungeetablistplus.util.ConcurrentBitSet; import codecrafter47.bungeetablistplus.util.Property119Handler; +import codecrafter47.bungeetablistplus.util.TeamUtil; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; @@ -2494,7 +2495,7 @@ private static Team createPacketTeamCreate(String name, Either CUSTOM_SLOT_USERNAMES; private static final String[] CUSTOM_SLOT_TEAMNAME; - // The latest BungeeCord uses Optional instead of int for the team color. - // If that setter is present we invoke it via reflection, otherwise we fall back to Team.setColor(int). - @Nullable - private static final Method TEAM_SET_COLOR_OPTIONAL; - private final Either nameTagVisibilityAlways; private final Either collisionRuleAlways; @@ -157,14 +153,6 @@ public class NewTabOverlayHandler implements PacketHandler, TabOverlayHandler { CUSTOM_SLOT_TEAMNAME[i] = String.format(" BTLP%08x %02d", unique, i); } - // Newer BungeeCord versions changed Team.setColor to accept Optional. - Method setColorOptional; - try { - setColorOptional = Team.class.getMethod("setColor", Optional.class); - } catch (NoSuchMethodException e) { - setColorOptional = null; - } - TEAM_SET_COLOR_OPTIONAL = setColorOptional; } private final Logger logger; @@ -1287,15 +1275,7 @@ private static Team createPacketTeamCreate(String name, Either instead of int for the team color. + // If that setter is present we invoke it via reflection, otherwise we fall back to Team.setColor(int). + @Nullable + private static final Method TEAM_SET_COLOR_OPTIONAL; + + static { + // Newer BungeeCord versions changed Team.setColor to accept Optional. + Method setColorOptional; + try { + setColorOptional = Team.class.getMethod("setColor", Optional.class); + } catch (NoSuchMethodException e) { + setColorOptional = null; + } + TEAM_SET_COLOR_OPTIONAL = setColorOptional; + } + + public static void setColor(Team team, int color) { + if (TEAM_SET_COLOR_OPTIONAL != null) { + try { + TEAM_SET_COLOR_OPTIONAL.invoke(team, Optional.of(color)); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to set team color", e); + } + } else { + team.setColor(color); + } + + } + +} From 53ebfe60b5faa82a20bd4f999c144cf03854503b Mon Sep 17 00:00:00 2001 From: Rasmus Date: Sat, 11 Jul 2026 06:48:54 +0200 Subject: [PATCH 2/2] getColor was also changed to returning an optional as well --- .../handler/AbstractTabOverlayHandler.java | 2 +- .../bungeetablistplus/util/TeamUtil.java | 42 +++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java b/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java index 342f7c3a..426145b0 100644 --- a/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java +++ b/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java @@ -330,7 +330,7 @@ public PacketListenerResult onTeamPacket(Team packet) { teamEntry.setFriendlyFire(packet.getFriendlyFire()); teamEntry.setNameTagVisibility(packet.getNameTagVisibility()); teamEntry.setCollisionRule(packet.getCollisionRule()); - teamEntry.setColor(packet.getColor()); + teamEntry.setColor(TeamUtil.getColor(packet)); } if (packet.getPlayers() != null) { for (String s : packet.getPlayers()) { diff --git a/bungee/src/main/java/codecrafter47/bungeetablistplus/util/TeamUtil.java b/bungee/src/main/java/codecrafter47/bungeetablistplus/util/TeamUtil.java index 49e2332e..0f45f771 100644 --- a/bungee/src/main/java/codecrafter47/bungeetablistplus/util/TeamUtil.java +++ b/bungee/src/main/java/codecrafter47/bungeetablistplus/util/TeamUtil.java @@ -13,15 +13,14 @@ public class TeamUtil { @Nullable private static final Method TEAM_SET_COLOR_OPTIONAL; + //Same as above but just to fetch the team color, returning an optional instead of an int + @Nullable + private static final Method TEAM_GET_COLOR_OPTIONAL; + static { // Newer BungeeCord versions changed Team.setColor to accept Optional. - Method setColorOptional; - try { - setColorOptional = Team.class.getMethod("setColor", Optional.class); - } catch (NoSuchMethodException e) { - setColorOptional = null; - } - TEAM_SET_COLOR_OPTIONAL = setColorOptional; + TEAM_SET_COLOR_OPTIONAL = initSetColorMethod(); + TEAM_GET_COLOR_OPTIONAL = initGetColorMethod(); } public static void setColor(Team team, int color) { @@ -37,4 +36,33 @@ public static void setColor(Team team, int color) { } + @SuppressWarnings("unchecked") + public static int getColor(Team team) { + if (TEAM_GET_COLOR_OPTIONAL != null) { + try { + Optional colorOpt = (Optional) TEAM_GET_COLOR_OPTIONAL.invoke(team); + return colorOpt.orElse(0); + } catch (ReflectiveOperationException e) { + throw new RuntimeException("Failed to set team color", e); + } + } + return team.getColor(); + } + + private static @Nullable Method initGetColorMethod() { + try { + Method m = Team.class.getMethod("getColor"); + return m.getReturnType() == Optional.class ? m : null; + } catch (NoSuchMethodException e) { + return null; + } + } + + private static @Nullable Method initSetColorMethod() { + try { + return Team.class.getMethod("setColor", Optional.class); + } catch (NoSuchMethodException e) { + return null; + } + } }