Add sorting by server group (#88)

* Add sorting by group order and group name

* Fix sorting by server group

* Use order of groups instead of config option

* Remove redundant getServerGroup in SERVER_GROUP

* Update Sorting in docs
This commit is contained in:
Nikita Obrekht 2023-09-15 18:54:25 +02:00 committed by GitHub
parent c1682aeda9
commit 90a26f15eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 11 deletions

View File

@ -17,11 +17,13 @@ sort_players_by:
### List of elements
The following sorting elements are supported:
| Sorting element | Description |
|:---------------:|----------------------------------------------------|
| `ROLE_WEIGHT` | The weight of the player's primary LuckPerms group |
| `ROLE_NAME` | The name of the player's primary LuckPerms group |
| `SERVER_NAME` | The name of the server the player is connected to |
| Sorting element | Description |
|:-------------------:|---------------------------------------------------------|
| `ROLE_WEIGHT` | The weight of the player's primary LuckPerms group |
| `ROLE_NAME` | The name of the player's primary LuckPerms group |
| `SERVER_NAME` | The name of the server the player is connected to |
| `SERVER_GROUP` | The order in which the groups are designated |
| `SERVER_GROUP_NAME` | The name of the server group the player is connected to |
## Technical details
In Minecraft, the TAB list is sorted by the client; the server does not handle the actual display order of names in the list. Players are sorted first by the name of their scoreboard team, then by their name. This is why having a proxy TAB plugin sort players is a surprisingly complex feature request!

View File

@ -28,6 +28,7 @@ import net.william278.velocitab.player.TabPlayer;
import org.apache.commons.text.StringEscapeUtils;
import org.jetbrains.annotations.NotNull;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@ -66,8 +67,8 @@ public class Settings {
@Getter
@YamlKey("server_groups")
@YamlComment("The servers in each group of servers")
private Map<String, List<String>> serverGroups = Map.of("default", List.of("lobby1", "lobby2", "lobby3"));
@YamlComment("The servers in each group of servers. The order of groups is important when sorting by SERVER_GROUP.")
private LinkedHashMap<String, List<String>> serverGroups = new LinkedHashMap<>(Map.of("default", List.of("lobby1", "lobby2", "lobby3")));
@Getter
@YamlKey("fallback_enabled")
@ -113,7 +114,7 @@ public class Settings {
@YamlKey("sort_players_by")
@YamlComment("Ordered list of elements by which players should be sorted. " +
"(ROLE_WEIGHT, ROLE_NAME and SERVER_NAME are supported)")
"(ROLE_WEIGHT, ROLE_NAME, SERVER_NAME, SERVER_GROUP and SERVER_GROUP_NAME are supported)")
private List<String> sortPlayersBy = List.of(
TabPlayer.SortableElement.ROLE_WEIGHT.name(),
TabPlayer.SortableElement.ROLE_NAME.name()
@ -127,9 +128,9 @@ public class Settings {
private int updateRate = 0;
public Settings(@NotNull Velocitab plugin) {
this.serverGroups = Map.of("default",
this.serverGroups = new LinkedHashMap<>(Map.of("default",
plugin.getServer().getAllServers().stream().map(server -> server.getServerInfo().getName()).toList()
);
));
}
@SuppressWarnings("unused")
@ -175,6 +176,16 @@ public class Settings {
return serverDisplayNames.getOrDefault(serverName, serverName);
}
/**
* Get the ordinal position of the server group
*
* @param serverGroupName The server group name
* @return The ordinal position of the server group
*/
public int getServerGroupPosition(@NotNull String serverGroupName) {
return List.copyOf(serverGroups.keySet()).indexOf(serverGroupName);
}
/**
* Get the server group that a server is in
*

View File

@ -70,6 +70,7 @@ public final class TabPlayer implements Comparable<TabPlayer> {
/**
* Get the TAB server group this player is connected to
*
* @param plugin instance of the {@link Velocitab} plugin
* @return the name of the server group the player is on
*/
@ -78,6 +79,16 @@ public final class TabPlayer implements Comparable<TabPlayer> {
return plugin.getSettings().getServerGroup(this.getServerName());
}
/**
* Get the ordinal position of the TAB server group this player is connected to
*
* @param plugin instance of the {@link Velocitab} plugin
* @return The ordinal position of the server group
*/
public int getServerGroupPosition(@NotNull Velocitab plugin) {
return plugin.getSettings().getServerGroupPosition(getServerGroup(plugin));
}
/**
* Get the display name of the server the player is currently on.
* Affected by server aliases defined in the config.
@ -155,7 +166,15 @@ public final class TabPlayer implements Comparable<TabPlayer> {
ROLE_NAME((player, plugin) -> player.getRole().getName()
.map(name -> name.length() > 3 ? name.substring(0, 3) : name)
.orElse("")),
SERVER_NAME((player, plugin) -> player.getServerName());
SERVER_NAME((player, plugin) -> player.getServerName()),
SERVER_GROUP((player, plugin) -> {
int orderSize = plugin.getSettings().getServerGroups().size();
int position = player.getServerGroupPosition(plugin);
return position >= 0
? String.format("%0" + Integer.toString(orderSize).length() + "d", position)
: String.valueOf(orderSize);
}),
SERVER_GROUP_NAME((player, plugin) -> player.getServerGroup(plugin));
private final BiFunction<TabPlayer, Velocitab, String> elementResolver;