refactor: improve how the persistent data in PlayerEntry are stored which fixes the authenticated trust level being wiped when unvanish
This commit is contained in:
@@ -1 +1 @@
|
||||
3378
|
||||
3381
|
||||
@@ -39,20 +39,20 @@ public class AuthCommand extends Command {
|
||||
if (allowSettingOthers && !targetString.isEmpty()) target = bot.players.getEntry(targetString);
|
||||
if (target == null) target = context.sender;
|
||||
|
||||
target.authenticatedTrustLevel = trustLevel;
|
||||
target.persistingData.authenticatedTrustLevel = trustLevel;
|
||||
|
||||
if (target.equals(context.sender)) {
|
||||
return Component.translatable(
|
||||
"commands.auth.self",
|
||||
bot.colorPalette.defaultColor,
|
||||
target.authenticatedTrustLevel.component
|
||||
target.persistingData.authenticatedTrustLevel.component
|
||||
);
|
||||
} else {
|
||||
return Component.translatable(
|
||||
"commands.auth.others",
|
||||
bot.colorPalette.defaultColor,
|
||||
Component.text(target.profile.getName(), bot.colorPalette.username),
|
||||
target.authenticatedTrustLevel.component
|
||||
target.persistingData.authenticatedTrustLevel.component
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,8 +54,9 @@ public class FindAltsCommand extends Command {
|
||||
|
||||
final String ipFromUsername;
|
||||
|
||||
if (playerInTheServer == null || playerInTheServer.ip == null) ipFromUsername = bot.playersDatabase.getPlayerIP(player);
|
||||
else ipFromUsername = playerInTheServer.ip;
|
||||
if (playerInTheServer == null || playerInTheServer.persistingData.ip == null)
|
||||
ipFromUsername = bot.playersDatabase.getPlayerIP(player);
|
||||
else ipFromUsername = playerInTheServer.persistingData.ip;
|
||||
|
||||
if (ipFromUsername == null) {
|
||||
context.sendOutput(handle(bot, player, player, allServer));
|
||||
|
||||
@@ -56,7 +56,7 @@ public class ListCommand extends Command {
|
||||
.append(Component.newline())
|
||||
.append(Component.newline())
|
||||
.append(
|
||||
entry.usernames.isEmpty()
|
||||
entry.persistingData.usernames.isEmpty()
|
||||
? Component.translatable(
|
||||
"commands.list.no_other_usernames",
|
||||
NamedTextColor.GRAY
|
||||
@@ -67,7 +67,7 @@ public class ListCommand extends Command {
|
||||
Component
|
||||
.join(
|
||||
JoinConfiguration.commas(true),
|
||||
entry.usernames
|
||||
entry.persistingData.usernames
|
||||
.stream()
|
||||
.map(Component::text)
|
||||
.toList()
|
||||
@@ -80,7 +80,7 @@ public class ListCommand extends Command {
|
||||
Component.translatable(
|
||||
"commands.list.vanished",
|
||||
bot.colorPalette.secondary,
|
||||
Component.text(!entry.listed, bot.colorPalette.string)
|
||||
Component.text(!entry.persistingData.listed, bot.colorPalette.string)
|
||||
)
|
||||
)
|
||||
.append(Component.newline())
|
||||
@@ -106,7 +106,12 @@ public class ListCommand extends Command {
|
||||
Component.translatable(
|
||||
"commands.list.ip_address",
|
||||
bot.colorPalette.secondary,
|
||||
Component.text(entry.ip == null ? "N/A" : entry.ip, bot.colorPalette.string)
|
||||
Component.text(
|
||||
entry.persistingData.ip == null
|
||||
? "N/A"
|
||||
: entry.persistingData.ip,
|
||||
bot.colorPalette.string
|
||||
)
|
||||
)
|
||||
)
|
||||
.append(Component.newline())
|
||||
|
||||
@@ -36,7 +36,7 @@ public class ValidateCommand extends Command {
|
||||
else if (context instanceof ConsoleCommandContext) return Component
|
||||
.translatable("commands.validate.console", NamedTextColor.GREEN);
|
||||
else return Component.translatable(
|
||||
context.sender.authenticatedTrustLevel != TrustLevel.PUBLIC
|
||||
context.sender.persistingData.authenticatedTrustLevel != TrustLevel.PUBLIC
|
||||
? "commands.validate.player_authenticated"
|
||||
: "commands.validate.player",
|
||||
NamedTextColor.GREEN,
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package me.chayapak1.chomens_bot.data.player;
|
||||
|
||||
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
|
||||
import me.chayapak1.chomens_bot.command.TrustLevel;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.geysermc.mcprotocollib.auth.GameProfile;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.PlayerListEntry;
|
||||
import org.geysermc.mcprotocollib.protocol.data.game.entity.player.GameMode;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.security.PublicKey;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class PlayerEntry {
|
||||
@@ -15,13 +16,10 @@ public class PlayerEntry {
|
||||
public GameMode gamemode;
|
||||
public int latency;
|
||||
public Component displayName;
|
||||
public final List<String> usernames = new ArrayList<>();
|
||||
public final long expiresAt;
|
||||
public PublicKey publicKey;
|
||||
public final byte[] keySignature;
|
||||
public boolean listed;
|
||||
public String ip;
|
||||
public TrustLevel authenticatedTrustLevel = TrustLevel.PUBLIC;
|
||||
public PersistingData persistingData;
|
||||
|
||||
public PlayerEntry (
|
||||
final GameProfile profile,
|
||||
@@ -40,7 +38,7 @@ public class PlayerEntry {
|
||||
this.expiresAt = expiresAt;
|
||||
this.publicKey = publicKey;
|
||||
this.keySignature = keySignature;
|
||||
this.listed = listed;
|
||||
this.persistingData = new PersistingData(listed);
|
||||
}
|
||||
|
||||
public PlayerEntry (final PlayerListEntry entry) {
|
||||
@@ -50,10 +48,48 @@ public class PlayerEntry {
|
||||
@Override
|
||||
public String toString () {
|
||||
return "PlayerEntry{" +
|
||||
"gamemode=" + gamemode +
|
||||
"profile=" + profile +
|
||||
", gamemode=" + gamemode +
|
||||
", latency=" + latency +
|
||||
", listed=" + listed +
|
||||
", displayName=" + displayName +
|
||||
", persistingData=" + persistingData +
|
||||
'}';
|
||||
}
|
||||
|
||||
public static final class PersistingData {
|
||||
public final List<String> usernames = new ObjectArrayList<>();
|
||||
public boolean listed;
|
||||
public String ip = null;
|
||||
public TrustLevel authenticatedTrustLevel = TrustLevel.PUBLIC;
|
||||
|
||||
public PersistingData (final boolean listed) {
|
||||
this.listed = listed;
|
||||
}
|
||||
|
||||
public PersistingData (final PersistingData friend) {
|
||||
this.usernames.addAll(friend.usernames);
|
||||
this.listed = friend.listed;
|
||||
this.ip = friend.ip;
|
||||
this.authenticatedTrustLevel = friend.authenticatedTrustLevel;
|
||||
}
|
||||
|
||||
public PersistingData (final PlayerEntry oldEntry) {
|
||||
final PersistingData friend = oldEntry.persistingData;
|
||||
|
||||
this.usernames.addAll(oldEntry.persistingData.usernames);
|
||||
this.usernames.addLast(oldEntry.profile.getName());
|
||||
this.listed = friend.listed;
|
||||
this.ip = friend.ip;
|
||||
this.authenticatedTrustLevel = friend.authenticatedTrustLevel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String toString () {
|
||||
return "PersistingData{" +
|
||||
"usernames=" + usernames +
|
||||
", listed=" + listed +
|
||||
", ip='" + ip + '\'' +
|
||||
", authenticatedTrustLevel=" + authenticatedTrustLevel +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -43,7 +43,7 @@ public class AuthPlugin implements Listener {
|
||||
|
||||
cleanup();
|
||||
|
||||
target.authenticatedTrustLevel = TrustLevel.MAX;
|
||||
target.persistingData.authenticatedTrustLevel = TrustLevel.MAX;
|
||||
|
||||
bot.logger.log(
|
||||
LogType.AUTH,
|
||||
|
||||
@@ -129,7 +129,7 @@ public class CommandHandlerPlugin implements Listener {
|
||||
|
||||
if (!bypass && !isCommandAllowed(context, command)) return;
|
||||
|
||||
final TrustLevel authenticatedTrustLevel = context.sender.authenticatedTrustLevel;
|
||||
final TrustLevel authenticatedTrustLevel = context.sender.persistingData.authenticatedTrustLevel;
|
||||
final boolean authenticated = isAuthenticated(context, authenticatedTrustLevel);
|
||||
final boolean needsHash = needsHash(command, inGame, authenticated, splitInput.length);
|
||||
|
||||
@@ -211,7 +211,7 @@ public class CommandHandlerPlugin implements Listener {
|
||||
}
|
||||
|
||||
if (authenticated) {
|
||||
final TrustLevel authLevel = context.sender.authenticatedTrustLevel;
|
||||
final TrustLevel authLevel = context.sender.persistingData.authenticatedTrustLevel;
|
||||
if (authLevel.level < requiredLevel.level) {
|
||||
context.sendOutput(Component.translatable("command_handler.not_enough_roles", NamedTextColor.RED));
|
||||
return false;
|
||||
|
||||
@@ -63,7 +63,7 @@ public class IPFilterPlugin implements Listener {
|
||||
private void check (final PlayerEntry target) {
|
||||
if (bot.options.useCorePlaceBlock) return; // it will spam the place block core so i ignored this
|
||||
|
||||
final String ip = target.ip;
|
||||
final String ip = target.persistingData.ip;
|
||||
|
||||
if (ip == null) return;
|
||||
|
||||
|
||||
@@ -78,12 +78,12 @@ public class PlayersPlugin implements Listener {
|
||||
}
|
||||
|
||||
private void queryPlayersIP (final PlayerEntry target) {
|
||||
if (target.ip != null) return;
|
||||
if (target.persistingData.ip != null) return;
|
||||
|
||||
final CompletableFuture<String> future = getPlayerIP(target, true);
|
||||
|
||||
future.thenApply(ip -> {
|
||||
target.ip = ip;
|
||||
target.persistingData.ip = ip;
|
||||
|
||||
bot.listener.dispatch(listener -> listener.onQueriedPlayerIP(target, ip));
|
||||
|
||||
@@ -162,7 +162,7 @@ public class PlayersPlugin implements Listener {
|
||||
(
|
||||
candidate.profile.getName().equals(username)
|
||||
|| (checkUUID && candidate.profile.getIdAsString().equals(username))
|
||||
|| (checkPastUsernames && candidate.usernames.contains(username))
|
||||
|| (checkPastUsernames && candidate.persistingData.usernames.contains(username))
|
||||
)
|
||||
) {
|
||||
return candidate;
|
||||
@@ -228,7 +228,7 @@ public class PlayersPlugin implements Listener {
|
||||
final PlayerEntry target = getEntry(newEntry);
|
||||
if (target == null) return;
|
||||
|
||||
target.listed = newEntry.isListed();
|
||||
target.persistingData.listed = newEntry.isListed();
|
||||
}
|
||||
|
||||
private void addPlayer (final PlayerListEntry newEntry) {
|
||||
@@ -241,10 +241,8 @@ public class PlayersPlugin implements Listener {
|
||||
if (duplicate != null) {
|
||||
list.removeIf(entry -> entry.equals(duplicate));
|
||||
|
||||
target.listed = true;
|
||||
|
||||
target.usernames.addAll(duplicate.usernames);
|
||||
target.ip = duplicate.ip;
|
||||
target.persistingData = new PlayerEntry.PersistingData(duplicate.persistingData);
|
||||
target.persistingData.listed = true;
|
||||
|
||||
list.add(target);
|
||||
|
||||
@@ -330,13 +328,10 @@ public class PlayersPlugin implements Listener {
|
||||
target.expiresAt,
|
||||
target.publicKey,
|
||||
target.keySignature,
|
||||
target.listed
|
||||
target.persistingData.listed
|
||||
);
|
||||
|
||||
newTarget.usernames.addAll(target.usernames);
|
||||
newTarget.usernames.addLast(target.profile.getName());
|
||||
|
||||
newTarget.ip = target.ip;
|
||||
newTarget.persistingData = new PlayerEntry.PersistingData(target);
|
||||
|
||||
list.removeIf(entry -> entry.profile.getId().equals(target.profile.getId()));
|
||||
|
||||
@@ -349,7 +344,7 @@ public class PlayersPlugin implements Listener {
|
||||
// we already passed all the left and username check,
|
||||
// so the only one left is vanish
|
||||
|
||||
target.listed = false;
|
||||
target.persistingData.listed = false;
|
||||
|
||||
bot.listener.dispatch(listener -> listener.onPlayerVanished(target));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user