Skip to content

Commit

Permalink
fix: various bugs, improve non-VT user handling (#170)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexdev03 authored Mar 1, 2024
1 parent 3064aad commit c0abf48
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 13 deletions.
14 changes: 13 additions & 1 deletion src/main/java/net/william278/velocitab/hook/LuckPermsHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public class LuckPermsHook extends Hook {
private final LuckPerms api;
private final EventSubscription<UserDataRecalculateEvent> event;
private final Map<UUID, Long> lastUpdate;
private boolean enabled;

public LuckPermsHook(@NotNull Velocitab plugin) throws IllegalStateException {
super(plugin);
Expand All @@ -53,10 +54,12 @@ public LuckPermsHook(@NotNull Velocitab plugin) throws IllegalStateException {
this.event = api.getEventBus().subscribe(
plugin, UserDataRecalculateEvent.class, this::onLuckPermsGroupUpdate
);
this.enabled = true;
}

public void closeEvent() {
event.close();
this.enabled = false;
}

@NotNull
Expand Down Expand Up @@ -92,6 +95,10 @@ public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) {
}
lastUpdate.put(event.getUser().getUniqueId(), System.currentTimeMillis());

if (!enabled) {
return;
}

final PlayerTabList tabList = plugin.getTabList();
plugin.getServer().getPlayer(event.getUser().getUniqueId())
.ifPresent(player -> plugin.getServer().getScheduler()
Expand All @@ -103,7 +110,12 @@ public void onLuckPermsGroupUpdate(@NotNull UserDataRecalculateEvent event) {

final TabPlayer tabPlayer = tabPlayerOptional.get();
final Role oldRole = tabPlayer.getRole();
tabPlayer.setRole(getRoleFromMetadata(event.getData().getMetaData()));
final Role newRole = getRoleFromMetadata(event.getUser().getCachedData().getMetaData());
if (oldRole.equals(newRole)) {
return;
}

tabPlayer.setRole(newRole);
tabList.updatePlayerDisplayName(tabPlayer);
tabList.getVanishTabList().recalculateVanishForPlayer(tabPlayer);
checkRoleUpdate(tabPlayer, oldRole);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import io.netty.channel.ChannelPromise;
import lombok.RequiredArgsConstructor;
import net.william278.velocitab.Velocitab;
import net.william278.velocitab.player.TabPlayer;
import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.Optional;


@RequiredArgsConstructor
Expand All @@ -44,6 +46,11 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
return;
}

final Optional<TabPlayer> tabPlayer = plugin.getTabList().getTabPlayer(player);
if (tabPlayer.isEmpty()) {
super.write(ctx, msg, promise);
return;
}

if (plugin.getSettings().isRemoveSpectatorEffect() && minecraftPacket.containsAction(UpsertPlayerInfoPacket.Action.UPDATE_GAME_MODE)) {
forceGameMode(minecraftPacket.getEntries());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,9 @@ public void close() {
public void resetCache(@NotNull Player player) {
final String team = createdTeams.remove(player.getUniqueId());
if (team != null) {
final TabPlayer tabPlayer = plugin.getTabList().getTabPlayer(player).orElseThrow();
dispatchGroupPacket(UpdateTeamsPacket.removeTeam(plugin, team), tabPlayer);
plugin.getTabList().getTabPlayer(player).ifPresent(tabPlayer ->
dispatchGroupPacket(UpdateTeamsPacket.removeTeam(plugin, team), tabPlayer)
);
}
}

Expand Down
23 changes: 15 additions & 8 deletions src/main/java/net/william278/velocitab/player/Role.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@
package net.william278.velocitab.player;

import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;
import java.util.Optional;

@RequiredArgsConstructor
public class Role implements Comparable<Role> {

public static final int DEFAULT_WEIGHT = 0;
public static final Role DEFAULT_ROLE = new Role(DEFAULT_WEIGHT, null, null, null, null);
@Getter
Expand All @@ -39,14 +43,6 @@ public class Role implements Comparable<Role> {
@Nullable
private final String suffix;

public Role(int weight, @Nullable String name, @Nullable String displayName, @Nullable String prefix, @Nullable String suffix) {
this.weight = weight;
this.name = name;
this.displayName = displayName;
this.prefix = prefix;
this.suffix = suffix;
}

@Override
public int compareTo(@NotNull Role o) {
return Double.compare(weight, o.weight);
Expand All @@ -73,4 +69,15 @@ protected String getWeightString() {
return Integer.toString(weight);
}

@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
final Role role = (Role) obj;
return weight == role.weight &&
Objects.equals(name, role.name) &&
Objects.equals(displayName, role.displayName) &&
Objects.equals(prefix, role.prefix) &&
Objects.equals(suffix, role.suffix);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package net.william278.velocitab.tab;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.ServerConnection;
Expand Down Expand Up @@ -128,7 +129,7 @@ public void close() {
return;
}

final List<RegisteredServer> serversInGroup = new ArrayList<>(tabPlayer.getGroup().registeredServers(plugin));
final List<RegisteredServer> serversInGroup = Lists.newArrayList(tabPlayer.getGroup().registeredServers(plugin));
if (serversInGroup.isEmpty()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ public void onPlayerJoin(@NotNull ServerPostConnectEvent event) {

// If the server is not in a group, use fallback.
// If fallback is disabled, permit the player to switch excluded servers without a header or footer override
if (isDefault && !plugin.getSettings().isFallbackEnabled() && event.getPreviousServer() != null) {
if (isDefault && !plugin.getSettings().isFallbackEnabled()) {
final Optional<TabPlayer> tabPlayer = tabList.getTabPlayer(joined);
if (tabPlayer.isEmpty()) {
return;
}

if (event.getPreviousServer() == null) {
return;
}

final Component header = tabPlayer.get().getLastHeader();
final Component footer = tabPlayer.get().getLastFooter();
final Component displayName = tabPlayer.get().getLastDisplayName();
Expand Down

0 comments on commit c0abf48

Please sign in to comment.