Improved sorting logic to handle both high and low values. (#103)

This commit is contained in:
AlexDev_ 2023-10-13 11:08:30 +02:00 committed by GitHub
parent 9090631677
commit a79404a530
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 32 additions and 24 deletions

View File

@ -188,8 +188,7 @@ public class Velocitab {
@NotNull
public TabPlayer getTabPlayer(@NotNull Player player) {
return new TabPlayer(player,
getLuckPermsHook().map(hook -> hook.getPlayerRole(player)).orElse(Role.DEFAULT_ROLE),
getLuckPermsHook().map(LuckPermsHook::getHighestWeight).orElse(0)
getLuckPermsHook().map(hook -> hook.getPlayerRole(player)).orElse(Role.DEFAULT_ROLE)
);
}

View File

@ -89,8 +89,7 @@ public class LuckPermsHook extends Hook {
.buildTask(plugin, () -> {
final TabPlayer updatedPlayer = new TabPlayer(
player,
getRoleFromMetadata(event.getData().getMetaData()),
getHighestWeight()
getRoleFromMetadata(event.getData().getMetaData())
);
tabList.replacePlayer(updatedPlayer);
tabList.updatePlayer(updatedPlayer);
@ -113,18 +112,6 @@ public class LuckPermsHook extends Hook {
return group.getWeight().orElse(Role.DEFAULT_WEIGHT);
}
public int getHighestWeight() {
if (highestWeight == Role.DEFAULT_WEIGHT) {
api.getGroupManager().getLoadedGroups().forEach(group -> {
final OptionalInt weight = group.getWeight();
if (weight.isPresent() && weight.getAsInt() > highestWeight) {
highestWeight = weight.getAsInt();
}
});
}
return highestWeight;
}
private User getUser(@NotNull UUID uuid) {
return api.getUserManager().getUser(uuid);
}

View File

@ -20,11 +20,13 @@
package net.william278.velocitab.player;
import lombok.Getter;
import net.william278.velocitab.config.Placeholder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
public class Role implements Comparable<Role> {
public static final int DEFAULT_WEIGHT = 0;
@ -50,7 +52,7 @@ public class Role implements Comparable<Role> {
@Override
public int compareTo(@NotNull Role o) {
return weight - o.weight;
return Double.compare(weight, o.weight);
}
public Optional<String> getName() {
@ -70,8 +72,30 @@ public class Role implements Comparable<Role> {
}
@NotNull
protected String getWeightString(int highestWeight) {
return Placeholder.formatSortableInt(weight, highestWeight);
protected String getWeightString() {
return compressNumber(Integer.MAX_VALUE / 4d - weight);
}
public String compressNumber(double number) {
int wholePart = (int) number;
final char decimalChar = (char) ((number - wholePart) * Character.MAX_VALUE);
final List<Character> charList = new ArrayList<>();
while (wholePart > 0) {
char digit = (char) (wholePart % Character.MAX_VALUE);
charList.add(0, digit);
wholePart /= Character.MAX_VALUE;
}
if (charList.isEmpty()) {
charList.add((char) 0);
}
return charList.stream().map(String::valueOf).collect(Collectors.joining()) + decimalChar;
}
}

View File

@ -34,7 +34,6 @@ import java.util.concurrent.CompletableFuture;
public final class TabPlayer implements Comparable<TabPlayer> {
private final Player player;
private final Role role;
private final int highestWeight;
@Getter
private int headerIndex = 0;
@Getter
@ -43,10 +42,9 @@ public final class TabPlayer implements Comparable<TabPlayer> {
private Component lastDisplayname;
private String teamName;
public TabPlayer(@NotNull Player player, @NotNull Role role, int highestWeight) {
public TabPlayer(@NotNull Player player, @NotNull Role role) {
this.player = player;
this.role = role;
this.highestWeight = highestWeight;
}
@NotNull
@ -61,7 +59,7 @@ public final class TabPlayer implements Comparable<TabPlayer> {
@NotNull
public String getRoleWeightString() {
return getRole().getWeightString(highestWeight);
return getRole().getWeightString();
}
/**