Skip to content

Commit

Permalink
Fix saving of player data in a few cases
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev committed Dec 20, 2023
1 parent b13392a commit fdbb8d1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ public boolean hasUnlockedEverything() {
*/
public void addWaypoint(@Nonnull Waypoint waypoint) {
this.data.addWaypoint(waypoint);
markDirty();
}

/**
Expand All @@ -225,6 +226,7 @@ public void addWaypoint(@Nonnull Waypoint waypoint) {
*/
public void removeWaypoint(@Nonnull Waypoint waypoint) {
this.data.removeWaypoint(waypoint);
markDirty();
}

/**
Expand All @@ -249,6 +251,8 @@ public final void markDirty() {
PlayerBackpack backpack = PlayerBackpack.newBackpack(this.ownerId, id, size);
this.data.addBackpack(backpack);

markDirty();

return backpack;
}

Expand All @@ -260,6 +264,7 @@ public final void markDirty() {
PlayerBackpack backpack = data.getBackpack(id);

if (backpack != null) {
markDirty();
return Optional.of(backpack);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ public PlayerData loadPlayerData(@Nonnull UUID uuid) {
return new PlayerData(researches, backpacks, waypoints);
}

// The current design of saving all at once isn't great, this will be refined.
@Override
public void savePlayerData(@Nonnull UUID uuid, @Nonnull PlayerData data) {
Config playerFile = new Config("data-storage/Slimefun/Players/" + uuid + ".yml");
// Not too sure why this is its own file
Config waypointsFile = new Config("data-storage/Slimefun/waypoints/" + uuid + ".yml");

// Save research
playerFile.setValue("rearches", null);
for (Research research : data.getResearches()) {
// Legacy data uses IDs
playerFile.setValue("researches." + research.getID(), true);
Expand All @@ -96,11 +98,16 @@ public void savePlayerData(@Nonnull UUID uuid, @Nonnull PlayerData data) {
ItemStack item = backpack.getInventory().getItem(i);
if (item != null) {
playerFile.setValue("backpacks." + backpack.getId() + ".contents." + i, item);

// Remove the item if it's no longer in the inventory
} else if (playerFile.contains("backpacks." + backpack.getId() + ".contents." + i)) {
playerFile.setValue("backpacks." + backpack.getId() + ".contents." + i, null);
}
}
}

// Save waypoints
waypointsFile.clear();
for (Waypoint waypoint : data.getWaypoints()) {
// Legacy data uses IDs
waypointsFile.setValue(waypoint.getId(), waypoint.getLocation());
Expand Down

0 comments on commit fdbb8d1

Please sign in to comment.