From 08817725055dd21694bff8b7f67c47fbb7591c9e Mon Sep 17 00:00:00 2001 From: B1n_ry Date: Tue, 1 Oct 2024 13:43:04 +0200 Subject: [PATCH] Went back to using GameProfiles as map keys, as comparing them doesn't compare property maps --- CHANGELOG.md | 1 + .../b1n_ry/yigd/data/DeathInfoManager.java | 44 ++++++++++--------- .../com/b1n_ry/yigd/util/YigdCommands.java | 11 ++--- 3 files changed, 30 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 15cbdba..405aeb3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ the air ### Fixes * If dying for the first time in a world with an empty inventory, players will no longer be disconnected. +* Backup data should now recognize players more easily --- diff --git a/src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java b/src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java index b4dddb2..34285db 100644 --- a/src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java +++ b/src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java @@ -5,6 +5,7 @@ import com.b1n_ry.yigd.components.RespawnComponent; import com.b1n_ry.yigd.config.YigdConfig; import com.b1n_ry.yigd.util.GraveCompassHelper; +import com.mojang.authlib.GameProfile; import net.minecraft.core.HolderLookup; import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; @@ -27,12 +28,12 @@ public class DeathInfoManager extends SavedData { public static DeathInfoManager INSTANCE = new DeathInfoManager(); - private final Map respawnEffects = new HashMap<>(); - private final Map> graveBackups = new HashMap<>(); + private final Map respawnEffects = new HashMap<>(); + private final Map> graveBackups = new HashMap<>(); private final Map graveMap = new HashMap<>(); private ListMode graveListMode = ListMode.BLACKLIST; - private final Set affectedPlayers = new HashSet<>(); + private final Set affectedPlayers = new HashSet<>(); public void clear() { this.respawnEffects.clear(); @@ -42,7 +43,7 @@ public void clear() { this.affectedPlayers.clear(); } - public Set getAffectedPlayers() { + public Set getAffectedPlayers() { return affectedPlayers; } @@ -59,7 +60,7 @@ public InteractionResult delete(UUID graveId) { GraveComponent component = this.graveMap.get(graveId); if (component == null) return InteractionResult.FAIL; - ResolvableProfile profile = component.getOwner(); + GameProfile profile = component.getOwner().gameProfile(); this.graveMap.remove(graveId); @@ -73,22 +74,23 @@ public InteractionResult delete(UUID graveId) { } public void addRespawnComponent(ResolvableProfile profile, RespawnComponent component) { - this.respawnEffects.put(profile, component); + this.respawnEffects.put(profile.gameProfile(), component); } public Optional getRespawnComponent(ResolvableProfile profile) { - return Optional.ofNullable(this.respawnEffects.get(profile)); + return Optional.ofNullable(this.respawnEffects.get(profile.gameProfile())); } - public Map> getPlayerGraves() { + public Map> getPlayerGraves() { return this.graveBackups; } public void removeRespawnComponent(ResolvableProfile profile) { - this.respawnEffects.remove(profile); + this.respawnEffects.remove(profile.gameProfile()); } - public void addBackup(ResolvableProfile profile, GraveComponent component) { + public void addBackup(ResolvableProfile p, GraveComponent component) { YigdConfig config = YigdConfig.getConfig(); + GameProfile profile = p.gameProfile(); if (!this.graveBackups.containsKey(profile)) { this.graveBackups.put(profile, new ArrayList<>()); } @@ -108,11 +110,11 @@ public void addBackup(ResolvableProfile profile, GraveComponent component) { if (config.extraFeatures.graveCompass.pointToClosest != YigdConfig.ExtraFeatures.GraveCompassConfig.CompassGraveTarget.DISABLED && component.getStatus() == GraveStatus.UNCLAIMED) { - GraveCompassHelper.addGravePosition(component.getWorldRegistryKey(), component.getPos(), profile.id().orElse(null)); + GraveCompassHelper.addGravePosition(component.getWorldRegistryKey(), component.getPos(), profile.getId()); } } public @NotNull List getBackupData(ResolvableProfile profile) { - return this.graveBackups.computeIfAbsent(profile, k -> new ArrayList<>()); + return this.graveBackups.computeIfAbsent(profile.gameProfile(), k -> new ArrayList<>()); } public Optional getGrave(UUID graveId) { return Optional.ofNullable(this.graveMap.get(graveId)); @@ -125,13 +127,13 @@ public void setGraveListMode(ListMode listMode) { this.graveListMode = listMode; } public void addToList(ResolvableProfile profile) { - this.affectedPlayers.add(profile); + this.affectedPlayers.add(profile.gameProfile()); } public boolean removeFromList(ResolvableProfile profile) { - return this.affectedPlayers.remove(profile); + return this.affectedPlayers.remove(profile.gameProfile()); } public boolean isInList(ResolvableProfile profile) { - return this.affectedPlayers.contains(profile); + return this.affectedPlayers.contains(profile.gameProfile()); } @Override @@ -139,19 +141,19 @@ public boolean isInList(ResolvableProfile profile) { ListTag respawnNbt = new ListTag(); ListTag graveNbt = new ListTag(); CompoundTag graveListNbt = new CompoundTag(); - for (Map.Entry entry : this.respawnEffects.entrySet()) { + for (Map.Entry entry : this.respawnEffects.entrySet()) { CompoundTag respawnCompound = new CompoundTag(); - ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, entry.getKey()).result() + ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, new ResolvableProfile(entry.getKey())).result() .ifPresent(nbtElement -> respawnCompound.put("user", nbtElement)); respawnCompound.put("component", entry.getValue().toNbt(registryLookup)); respawnNbt.add(respawnCompound); } - for (Map.Entry> entry : this.graveBackups.entrySet()) { + for (Map.Entry> entry : this.graveBackups.entrySet()) { CompoundTag graveCompound = new CompoundTag(); - ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, entry.getKey()).result() + ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, new ResolvableProfile(entry.getKey())).result() .ifPresent(nbtElement -> graveCompound.put("user", nbtElement)); ListTag graveNbtList = new ListTag(); @@ -166,8 +168,8 @@ public boolean isInList(ResolvableProfile profile) { graveListNbt.putString("listMode", this.graveListMode.name()); ListTag affectedPlayersNbt = new ListTag(); - for (ResolvableProfile profile : this.affectedPlayers) { - Tag profileNbt = ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, profile).result().orElseThrow(); + for (GameProfile profile : this.affectedPlayers) { + Tag profileNbt = ResolvableProfile.CODEC.encodeStart(NbtOps.INSTANCE, new ResolvableProfile(profile)).result().orElseThrow(); affectedPlayersNbt.add(profileNbt); } graveListNbt.put("affectedPlayers", affectedPlayersNbt); diff --git a/src/main/java/com/b1n_ry/yigd/util/YigdCommands.java b/src/main/java/com/b1n_ry/yigd/util/YigdCommands.java index 7dfeecd..16b9476 100644 --- a/src/main/java/com/b1n_ry/yigd/util/YigdCommands.java +++ b/src/main/java/com/b1n_ry/yigd/util/YigdCommands.java @@ -10,6 +10,7 @@ import com.b1n_ry.yigd.networking.packets.GraveOverviewS2CPacket; import com.b1n_ry.yigd.networking.packets.GraveSelectionS2CPacket; import com.b1n_ry.yigd.networking.packets.PlayerSelectionS2CPacket; +import com.mojang.authlib.GameProfile; import com.mojang.brigadier.context.CommandContext; import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.arguments.EntityArgument; @@ -136,7 +137,7 @@ private static int viewAll(CommandContext context) { if (player == null) return -1; - Map> players = DeathInfoManager.INSTANCE.getPlayerGraves(); + Map> players = DeathInfoManager.INSTANCE.getPlayerGraves(); List lightPlayerData = new ArrayList<>(); players.forEach((profile, components) -> { @@ -148,7 +149,7 @@ private static int viewAll(CommandContext context) { case DESTROYED -> destroyed++; } } - LightPlayerData lightData = new LightPlayerData(components.size(), unclaimed, destroyed, profile); + LightPlayerData lightData = new LightPlayerData(components.size(), unclaimed, destroyed, new ResolvableProfile(profile)); lightPlayerData.add(lightData); }); @@ -258,11 +259,11 @@ private static int toggleListType(CommandContext context) { } private static int showList(CommandContext context) { ListMode listMode = DeathInfoManager.INSTANCE.getGraveListMode(); - Set affectedPlayers = DeathInfoManager.INSTANCE.getAffectedPlayers(); + Set affectedPlayers = DeathInfoManager.INSTANCE.getAffectedPlayers(); StringJoiner joiner = new StringJoiner(", "); - for (ResolvableProfile profile : affectedPlayers) { - joiner.add(profile.name().orElse("PLAYER_NOT_FOUND")); + for (GameProfile profile : affectedPlayers) { + joiner.add(Optional.ofNullable(profile.getName()).orElse("PLAYER_NOT_FOUND")); } context.getSource().sendSystemMessage(Component.literal(listMode.name() + ": %s" + joiner));