Skip to content

Commit

Permalink
Went back to using GameProfiles as map keys, as comparing them doesn'…
Browse files Browse the repository at this point in the history
…t compare property maps
  • Loading branch information
B1n-ry committed Oct 1, 2024
1 parent f3aba36 commit 0881772
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 26 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

---

Expand Down
44 changes: 23 additions & 21 deletions src/main/java/com/b1n_ry/yigd/data/DeathInfoManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,12 +28,12 @@
public class DeathInfoManager extends SavedData {
public static DeathInfoManager INSTANCE = new DeathInfoManager();

private final Map<ResolvableProfile, RespawnComponent> respawnEffects = new HashMap<>();
private final Map<ResolvableProfile, List<GraveComponent>> graveBackups = new HashMap<>();
private final Map<GameProfile, RespawnComponent> respawnEffects = new HashMap<>();
private final Map<GameProfile, List<GraveComponent>> graveBackups = new HashMap<>();
private final Map<UUID, GraveComponent> graveMap = new HashMap<>();

private ListMode graveListMode = ListMode.BLACKLIST;
private final Set<ResolvableProfile> affectedPlayers = new HashSet<>();
private final Set<GameProfile> affectedPlayers = new HashSet<>();

public void clear() {
this.respawnEffects.clear();
Expand All @@ -42,7 +43,7 @@ public void clear() {
this.affectedPlayers.clear();
}

public Set<ResolvableProfile> getAffectedPlayers() {
public Set<GameProfile> getAffectedPlayers() {
return affectedPlayers;
}

Expand All @@ -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);

Expand All @@ -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<RespawnComponent> getRespawnComponent(ResolvableProfile profile) {
return Optional.ofNullable(this.respawnEffects.get(profile));
return Optional.ofNullable(this.respawnEffects.get(profile.gameProfile()));
}
public Map<ResolvableProfile, List<GraveComponent>> getPlayerGraves() {
public Map<GameProfile, List<GraveComponent>> 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<>());
}
Expand All @@ -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<GraveComponent> getBackupData(ResolvableProfile profile) {
return this.graveBackups.computeIfAbsent(profile, k -> new ArrayList<>());
return this.graveBackups.computeIfAbsent(profile.gameProfile(), k -> new ArrayList<>());
}
public Optional<GraveComponent> getGrave(UUID graveId) {
return Optional.ofNullable(this.graveMap.get(graveId));
Expand All @@ -125,33 +127,33 @@ 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
public @NotNull CompoundTag save(@NotNull CompoundTag nbt, HolderLookup.@NotNull Provider registryLookup) {
ListTag respawnNbt = new ListTag();
ListTag graveNbt = new ListTag();
CompoundTag graveListNbt = new CompoundTag();
for (Map.Entry<ResolvableProfile, RespawnComponent> entry : this.respawnEffects.entrySet()) {
for (Map.Entry<GameProfile, RespawnComponent> 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<ResolvableProfile, List<GraveComponent>> entry : this.graveBackups.entrySet()) {
for (Map.Entry<GameProfile, List<GraveComponent>> 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();
Expand All @@ -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);
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/b1n_ry/yigd/util/YigdCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -136,7 +137,7 @@ private static int viewAll(CommandContext<CommandSourceStack> context) {

if (player == null) return -1;

Map<ResolvableProfile, List<GraveComponent>> players = DeathInfoManager.INSTANCE.getPlayerGraves();
Map<GameProfile, List<GraveComponent>> players = DeathInfoManager.INSTANCE.getPlayerGraves();

List<LightPlayerData> lightPlayerData = new ArrayList<>();
players.forEach((profile, components) -> {
Expand All @@ -148,7 +149,7 @@ private static int viewAll(CommandContext<CommandSourceStack> 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);
});

Expand Down Expand Up @@ -258,11 +259,11 @@ private static int toggleListType(CommandContext<CommandSourceStack> context) {
}
private static int showList(CommandContext<CommandSourceStack> context) {
ListMode listMode = DeathInfoManager.INSTANCE.getGraveListMode();
Set<ResolvableProfile> affectedPlayers = DeathInfoManager.INSTANCE.getAffectedPlayers();
Set<GameProfile> 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));

Expand Down

0 comments on commit 0881772

Please sign in to comment.