diff --git a/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java b/bungee/src/main/java/codecrafter47/bungeetablistplus/handler/AbstractTabOverlayHandler.java index b16e9044..426145b0 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; @@ -329,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()) { @@ -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; + + //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. + TEAM_SET_COLOR_OPTIONAL = initSetColorMethod(); + TEAM_GET_COLOR_OPTIONAL = initGetColorMethod(); + } + + 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); + } + + } + + @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; + } + } +}