forked from Upstream/Velocitab
Fix weight sort order, team color, CME when updating players, players not updating via LP
This commit is contained in:
parent
54928f597a
commit
f616a5e08a
@ -92,7 +92,6 @@ public class Velocitab {
|
|||||||
// If LuckPerms is present, load the hook
|
// If LuckPerms is present, load the hook
|
||||||
try {
|
try {
|
||||||
luckPerms = new LuckPermsHook(this);
|
luckPerms = new LuckPermsHook(this);
|
||||||
server.getEventManager().register(this, luckPerms);
|
|
||||||
logger.info("Successfully hooked into LuckPerms");
|
logger.info("Successfully hooked into LuckPerms");
|
||||||
} catch (IllegalArgumentException e) {
|
} catch (IllegalArgumentException e) {
|
||||||
logger.warn("LuckPerms was not loaded: " + e.getMessage(), e);
|
logger.warn("LuckPerms was not loaded: " + e.getMessage(), e);
|
||||||
@ -116,7 +115,7 @@ public class Velocitab {
|
|||||||
|
|
||||||
private void prepareTabList() {
|
private void prepareTabList() {
|
||||||
this.tabList = new PlayerTabList(this);
|
this.tabList = new PlayerTabList(this);
|
||||||
server.getEventManager().register(this, new PlayerTabList(this));
|
server.getEventManager().register(this, tabList);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -126,6 +125,8 @@ public class Velocitab {
|
|||||||
getLuckPerms().map(hook -> hook.getPlayerRole(player))
|
getLuckPerms().map(hook -> hook.getPlayerRole(player))
|
||||||
.orElse(Role.DEFAULT_ROLE),
|
.orElse(Role.DEFAULT_ROLE),
|
||||||
getLuckPerms().map(LuckPermsHook::getHighestWeight)
|
getLuckPerms().map(LuckPermsHook::getHighestWeight)
|
||||||
|
.orElse(0),
|
||||||
|
getLuckPerms().map(LuckPermsHook::getLowestWeight)
|
||||||
.orElse(0));
|
.orElse(0));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
package net.william278.velocitab.luckperms;
|
package net.william278.velocitab.luckperms;
|
||||||
|
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
|
||||||
import com.velocitypowered.api.proxy.Player;
|
import com.velocitypowered.api.proxy.Player;
|
||||||
import net.luckperms.api.LuckPerms;
|
import net.luckperms.api.LuckPerms;
|
||||||
import net.luckperms.api.LuckPermsProvider;
|
import net.luckperms.api.LuckPermsProvider;
|
||||||
@ -10,6 +9,7 @@ import net.luckperms.api.model.group.Group;
|
|||||||
import net.luckperms.api.model.user.User;
|
import net.luckperms.api.model.user.User;
|
||||||
import net.william278.velocitab.Velocitab;
|
import net.william278.velocitab.Velocitab;
|
||||||
import net.william278.velocitab.player.Role;
|
import net.william278.velocitab.player.Role;
|
||||||
|
import net.william278.velocitab.player.TabPlayer;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jetbrains.annotations.Nullable;
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@ -19,17 +19,23 @@ import java.util.UUID;
|
|||||||
public class LuckPermsHook {
|
public class LuckPermsHook {
|
||||||
|
|
||||||
private int highestWeight = Role.DEFAULT_WEIGHT;
|
private int highestWeight = Role.DEFAULT_WEIGHT;
|
||||||
|
private int lowestWeight = Role.DEFAULT_WEIGHT;
|
||||||
private final Velocitab plugin;
|
private final Velocitab plugin;
|
||||||
private final LuckPerms api;
|
private final LuckPerms api;
|
||||||
|
|
||||||
public LuckPermsHook(@NotNull Velocitab plugin) throws IllegalStateException {
|
public LuckPermsHook(@NotNull Velocitab plugin) throws IllegalStateException {
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.api = LuckPermsProvider.get();
|
this.api = LuckPermsProvider.get();
|
||||||
|
api.getEventBus().subscribe(plugin, UserDataRecalculateEvent.class, this::onLuckPermsGroupUpdate);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public Role getPlayerRole(@NotNull Player player) {
|
public Role getPlayerRole(@NotNull Player player) {
|
||||||
final CachedMetaData metaData = getUser(player.getUniqueId()).getCachedData().getMetaData();
|
return getRoleFromMetadata(getUser(player.getUniqueId()).getCachedData().getMetaData());
|
||||||
|
}
|
||||||
|
|
||||||
|
@NotNull
|
||||||
|
private Role getRoleFromMetadata(@NotNull CachedMetaData metaData) {
|
||||||
if (metaData.getPrimaryGroup() == null) {
|
if (metaData.getPrimaryGroup() == null) {
|
||||||
return Role.DEFAULT_ROLE;
|
return Role.DEFAULT_ROLE;
|
||||||
}
|
}
|
||||||
@ -41,10 +47,14 @@ public class LuckPermsHook {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Subscribe
|
|
||||||
public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) {
|
public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) {
|
||||||
plugin.getServer().getPlayer(event.getUser().getUniqueId())
|
plugin.getServer().getPlayer(event.getUser().getUniqueId())
|
||||||
.ifPresent(player -> plugin.getTabList().updatePlayer(plugin.getTabPlayer(player)));
|
.ifPresent(player -> plugin.getTabList().updatePlayer(new TabPlayer(
|
||||||
|
player,
|
||||||
|
getRoleFromMetadata(event.getData().getMetaData()),
|
||||||
|
getHighestWeight(),
|
||||||
|
getLowestWeight()
|
||||||
|
)));
|
||||||
}
|
}
|
||||||
|
|
||||||
private OptionalInt getWeight(@Nullable String groupName) {
|
private OptionalInt getWeight(@Nullable String groupName) {
|
||||||
@ -67,9 +77,22 @@ public class LuckPermsHook {
|
|||||||
return highestWeight;
|
return highestWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int getLowestWeight() {
|
||||||
|
if (lowestWeight == Role.DEFAULT_WEIGHT) {
|
||||||
|
api.getGroupManager().getLoadedGroups().forEach(group -> {
|
||||||
|
final OptionalInt weight = group.getWeight();
|
||||||
|
if (weight.isPresent() && weight.getAsInt() < lowestWeight) {
|
||||||
|
lowestWeight = weight.getAsInt();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return lowestWeight;
|
||||||
|
}
|
||||||
|
|
||||||
private User getUser(@NotNull UUID uuid) {
|
private User getUser(@NotNull UUID uuid) {
|
||||||
return api.getUserManager().getUser(uuid);
|
return api.getUserManager().getUser(uuid);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ public class UpdateTeamsPacket extends AbstractPacket {
|
|||||||
updateTeamsPacket.friendlyFlags(List.of(FriendlyFlag.CAN_HURT_FRIENDLY));
|
updateTeamsPacket.friendlyFlags(List.of(FriendlyFlag.CAN_HURT_FRIENDLY));
|
||||||
updateTeamsPacket.nameTagVisibility(NameTagVisibility.ALWAYS);
|
updateTeamsPacket.nameTagVisibility(NameTagVisibility.ALWAYS);
|
||||||
updateTeamsPacket.collisionRule(CollisionRule.ALWAYS);
|
updateTeamsPacket.collisionRule(CollisionRule.ALWAYS);
|
||||||
updateTeamsPacket.color(0);
|
updateTeamsPacket.color(15);
|
||||||
updateTeamsPacket.prefix(getChatString(""));
|
updateTeamsPacket.prefix(getChatString(""));
|
||||||
updateTeamsPacket.suffix(getChatString(""));
|
updateTeamsPacket.suffix(getChatString(""));
|
||||||
updateTeamsPacket.entities(List.of(member));
|
updateTeamsPacket.entities(List.of(member));
|
||||||
|
@ -40,9 +40,25 @@ public class Role implements Comparable<Role> {
|
|||||||
return Optional.ofNullable(name);
|
return Optional.ofNullable(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
public String getStringComparableWeight(int maximumPossibleWeight, int lowestPossibleWeight) {
|
||||||
public String getStringComparableWeight(int highestWeight) {
|
// Calculate the weight range and the ratio of the input weight to the weight range
|
||||||
return String.format("%0" + (highestWeight + "").length() + "d", weight);
|
int weightRange = maximumPossibleWeight - lowestPossibleWeight;
|
||||||
//return String.format("%0" + (highestWeight + "").length() + "d", highestWeight - weight);
|
double weightRatio = (double) (maximumPossibleWeight - weight) / weightRange;
|
||||||
|
|
||||||
|
// Convert the weight ratio to a string with 3 decimal places and remove the decimal point
|
||||||
|
String weightString = String.format("%.3f", weightRatio).replace(".", "");
|
||||||
|
|
||||||
|
// Pad the weight string with leading zeros to a length of 6 characters
|
||||||
|
weightString = String.format("%6s", weightString).replace(' ', '0');
|
||||||
|
|
||||||
|
// Prepend a minus sign for negative weights
|
||||||
|
if (weight < 0) {
|
||||||
|
weightString = "-" + weightString.substring(1);
|
||||||
|
} else {
|
||||||
|
// Reverse the weight string for non-negative weights
|
||||||
|
weightString = new StringBuilder(weightString).reverse().toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return weightString;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -12,11 +12,13 @@ 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;
|
private final int highestWeight;
|
||||||
|
private final int lowestWeight;
|
||||||
|
|
||||||
public TabPlayer(@NotNull Player player, @NotNull Role role, int highestWeight) {
|
public TabPlayer(@NotNull Player player, @NotNull Role role, int highestWeight, int lowestWeight) {
|
||||||
this.player = player;
|
this.player = player;
|
||||||
this.role = role;
|
this.role = role;
|
||||||
this.highestWeight = highestWeight;
|
this.highestWeight = highestWeight;
|
||||||
|
this.lowestWeight = lowestWeight;
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -43,7 +45,7 @@ public final class TabPlayer implements Comparable<TabPlayer> {
|
|||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
public String getTeamName() {
|
public String getTeamName() {
|
||||||
return role.getStringComparableWeight(highestWeight) + "-" + getServerName() + "-" + player.getUsername();
|
return role.getStringComparableWeight(highestWeight, lowestWeight) + "-" + getServerName() + "-" + player.getUsername();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void sendHeaderAndFooter(@NotNull PlayerTabList tabList) {
|
public void sendHeaderAndFooter(@NotNull PlayerTabList tabList) {
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.william278.velocitab.tab;
|
package net.william278.velocitab.tab;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
import com.velocitypowered.api.event.Subscribe;
|
import com.velocitypowered.api.event.Subscribe;
|
||||||
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
import com.velocitypowered.api.event.connection.DisconnectEvent;
|
||||||
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
|
import com.velocitypowered.api.event.player.ServerPostConnectEvent;
|
||||||
@ -46,36 +47,39 @@ public class PlayerTabList {
|
|||||||
players.removeIf(player -> player.getPlayer().getUniqueId().equals(event.getPlayer().getUniqueId()));
|
players.removeIf(player -> player.getPlayer().getUniqueId().equals(event.getPlayer().getUniqueId()));
|
||||||
|
|
||||||
// Remove the player from the tab list of all other players
|
// Remove the player from the tab list of all other players
|
||||||
plugin.getScoreboardManager().removeTeam(event.getPlayer());
|
plugin.getServer().getAllPlayers().forEach(player -> player.getTabList().removeEntry(event.getPlayer().getUniqueId()));
|
||||||
plugin.getServer().getAllPlayers().forEach(player -> {
|
|
||||||
if (player.getTabList().containsEntry(event.getPlayer().getUniqueId())) {
|
|
||||||
player.getTabList().removeEntry(event.getPlayer().getUniqueId());
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Update the tab list of all players
|
// Update the tab list of all players
|
||||||
plugin.getServer().getScheduler().buildTask(plugin, this::updateList)
|
plugin.getServer().getScheduler().buildTask(plugin, () -> {
|
||||||
|
plugin.getScoreboardManager().removeTeam(event.getPlayer());
|
||||||
|
updateList();
|
||||||
|
})
|
||||||
.delay(500, TimeUnit.MILLISECONDS)
|
.delay(500, TimeUnit.MILLISECONDS)
|
||||||
.schedule();
|
.schedule();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updatePlayer(@NotNull TabPlayer tabPlayer) {
|
public void updatePlayer(@NotNull TabPlayer tabPlayer) {
|
||||||
// Remove the existing player from the tracking list
|
plugin.getServer().getScheduler()
|
||||||
players.removeIf(player -> player.getPlayer().getUniqueId().equals(tabPlayer.getPlayer().getUniqueId()));
|
.buildTask(plugin, () -> {
|
||||||
|
// Remove the existing player from the tracking list
|
||||||
|
players.replaceAll(player -> {
|
||||||
|
if (player.getPlayer().getUniqueId().equals(tabPlayer.getPlayer().getUniqueId())) {
|
||||||
|
return tabPlayer;
|
||||||
|
}
|
||||||
|
return player;
|
||||||
|
});
|
||||||
|
|
||||||
// Add the player to the tracking list
|
// Update the player's team sorting
|
||||||
players.add(tabPlayer);
|
plugin.getScoreboardManager().setPlayerTeam(tabPlayer);
|
||||||
|
|
||||||
// Update the player's team sorting
|
updateList();
|
||||||
plugin.getScoreboardManager().removeTeam(tabPlayer.getPlayer());
|
})
|
||||||
|
|
||||||
// Update the tab list of all players
|
|
||||||
plugin.getServer().getScheduler().buildTask(plugin, this::updateList)
|
|
||||||
.delay(500, TimeUnit.MILLISECONDS)
|
.delay(500, TimeUnit.MILLISECONDS)
|
||||||
.schedule();
|
.schedule();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void updateList() {
|
private void updateList() {
|
||||||
|
final ImmutableList<TabPlayer> players = ImmutableList.copyOf(this.players);
|
||||||
players.forEach(player -> {
|
players.forEach(player -> {
|
||||||
player.sendHeaderAndFooter(this);
|
player.sendHeaderAndFooter(this);
|
||||||
player.getPlayer().getTabList().getEntries()
|
player.getPlayer().getTabList().getEntries()
|
||||||
|
Loading…
Reference in New Issue
Block a user