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 @NotNull
public TabPlayer getTabPlayer(@NotNull Player player) { public TabPlayer getTabPlayer(@NotNull Player player) {
return new TabPlayer(player, return new TabPlayer(player,
getLuckPermsHook().map(hook -> hook.getPlayerRole(player)).orElse(Role.DEFAULT_ROLE), getLuckPermsHook().map(hook -> hook.getPlayerRole(player)).orElse(Role.DEFAULT_ROLE)
getLuckPermsHook().map(LuckPermsHook::getHighestWeight).orElse(0)
); );
} }

View File

@ -89,8 +89,7 @@ public class LuckPermsHook extends Hook {
.buildTask(plugin, () -> { .buildTask(plugin, () -> {
final TabPlayer updatedPlayer = new TabPlayer( final TabPlayer updatedPlayer = new TabPlayer(
player, player,
getRoleFromMetadata(event.getData().getMetaData()), getRoleFromMetadata(event.getData().getMetaData())
getHighestWeight()
); );
tabList.replacePlayer(updatedPlayer); tabList.replacePlayer(updatedPlayer);
tabList.updatePlayer(updatedPlayer); tabList.updatePlayer(updatedPlayer);
@ -113,18 +112,6 @@ public class LuckPermsHook extends Hook {
return group.getWeight().orElse(Role.DEFAULT_WEIGHT); 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) { private User getUser(@NotNull UUID uuid) {
return api.getUserManager().getUser(uuid); return api.getUserManager().getUser(uuid);
} }

View File

@ -20,11 +20,13 @@
package net.william278.velocitab.player; package net.william278.velocitab.player;
import lombok.Getter; import lombok.Getter;
import net.william278.velocitab.config.Placeholder;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.stream.Collectors;
public class Role implements Comparable<Role> { public class Role implements Comparable<Role> {
public static final int DEFAULT_WEIGHT = 0; public static final int DEFAULT_WEIGHT = 0;
@ -50,7 +52,7 @@ public class Role implements Comparable<Role> {
@Override @Override
public int compareTo(@NotNull Role o) { public int compareTo(@NotNull Role o) {
return weight - o.weight; return Double.compare(weight, o.weight);
} }
public Optional<String> getName() { public Optional<String> getName() {
@ -70,8 +72,30 @@ public class Role implements Comparable<Role> {
} }
@NotNull @NotNull
protected String getWeightString(int highestWeight) { protected String getWeightString() {
return Placeholder.formatSortableInt(weight, highestWeight); 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> { public final class TabPlayer implements Comparable<TabPlayer> {
private final Player player; private final Player player;
private final Role role; private final Role role;
private final int highestWeight;
@Getter @Getter
private int headerIndex = 0; private int headerIndex = 0;
@Getter @Getter
@ -43,10 +42,9 @@ public final class TabPlayer implements Comparable<TabPlayer> {
private Component lastDisplayname; private Component lastDisplayname;
private String teamName; 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.player = player;
this.role = role; this.role = role;
this.highestWeight = highestWeight;
} }
@NotNull @NotNull
@ -61,7 +59,7 @@ public final class TabPlayer implements Comparable<TabPlayer> {
@NotNull @NotNull
public String getRoleWeightString() { public String getRoleWeightString() {
return getRole().getWeightString(highestWeight); return getRole().getWeightString();
} }
/** /**