Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()) {
Expand Down Expand Up @@ -2494,7 +2495,7 @@ private static Team createPacketTeamCreate(String name, Either<String, BaseCompo
team.setSuffix(suffix);
team.setNameTagVisibility(nameTagVisibility);
team.setCollisionRule(collisionRule);
team.setColor(color);
TeamUtil.setColor(team, color);
team.setFriendlyFire(friendlyFire);
team.setPlayers(players);
return team;
Expand All @@ -2516,7 +2517,7 @@ private static Team createPacketTeamUpdate(String name, Either<String, BaseCompo
team.setSuffix(suffix);
team.setNameTagVisibility(nameTagVisibility);
team.setCollisionRule(collisionRule);
team.setColor(color);
TeamUtil.setColor(team, color);
team.setFriendlyFire(friendlyFire);
return team;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,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;
Expand Down Expand Up @@ -80,11 +81,6 @@ public class NewTabOverlayHandler implements PacketHandler, TabOverlayHandler {
private static final Set<String> CUSTOM_SLOT_USERNAMES;
private static final String[] CUSTOM_SLOT_TEAMNAME;

// The latest BungeeCord uses Optional<Integer> 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<String, Team.NameTagVisibility> nameTagVisibilityAlways;
private final Either<String, Team.CollisionRule> collisionRuleAlways;

Expand Down Expand Up @@ -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<Integer>.
Method setColorOptional;
try {
setColorOptional = Team.class.getMethod("setColor", Optional.class);
} catch (NoSuchMethodException e) {
setColorOptional = null;
}
TEAM_SET_COLOR_OPTIONAL = setColorOptional;
}

private final Logger logger;
Expand Down Expand Up @@ -1287,15 +1275,7 @@ private static Team createPacketTeamCreate(String name, Either<String, BaseCompo
team.setSuffix(suffix);
team.setNameTagVisibility(nameTagVisibility);
team.setCollisionRule(collisionRule);
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);
}
TeamUtil.setColor(team, color);
team.setFriendlyFire(friendlyFire);
team.setPlayers(players);
return team;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package codecrafter47.bungeetablistplus.util;

import net.md_5.bungee.protocol.packet.Team;

import javax.annotation.Nullable;
import java.lang.reflect.Method;
import java.util.Optional;

public class TeamUtil {

// The latest BungeeCord uses Optional<Integer> 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<Integer>.
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<Integer> colorOpt = (Optional<Integer>) 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;
}
}
}