Skip to content

Commit

Permalink
Merge pull request #73 from RedstoneFuture/Fix/PlayerDataStorage
Browse files Browse the repository at this point in the history
Hot-Fix loading of player data storage
  • Loading branch information
daniel-naegele authored Apr 11, 2023
2 parents d9fc3f7 + bd8fcaa commit 922f394
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import de.butzlabben.missilewars.game.stats.StatsFetcher;
import de.butzlabben.missilewars.listener.PlayerListener;
import de.butzlabben.missilewars.listener.SignListener;
import de.butzlabben.missilewars.player.PlayerData;
import de.butzlabben.missilewars.util.ConnectionHolder;
import de.butzlabben.missilewars.util.MoneyUtil;
import de.butzlabben.missilewars.util.SetupUtil;
Expand All @@ -39,6 +40,7 @@
import org.apache.commons.io.FileUtils;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.configuration.serialization.ConfigurationSerialization;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

Expand Down Expand Up @@ -124,6 +126,8 @@ public void onEnable() {
}

checkPlaceholderAPI();

ConfigurationSerialization.registerClass(PlayerData.class);

endTime = System.currentTimeMillis();
Logger.SUCCESS.log("MissileWars was enabled in " + (endTime - startTime) + "ms");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,47 @@
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@ToString
@AllArgsConstructor
public class PlayerData implements ConfigurationSerializable {

private final long time;
private UUID uuid;
private ItemStack[] contents;
private float exp;
private GameMode gameMode;
private double health;
private float exp;
private int expLevel, foodLevel;
private GameMode gameMode;
private final long time;

public PlayerData(Player player) {
uuid = player.getUniqueId();
contents = player.getInventory().getContents();
gameMode = player.getGameMode();
health = player.getHealth();
exp = player.getExp();
expLevel = player.getLevel();
foodLevel = player.getFoodLevel();
health = player.getHealth();
gameMode = player.getGameMode();
uuid = player.getUniqueId();
time = System.currentTimeMillis();
}

/**
* This method is used to load the original player data from the temporary player-data file.
*/
public PlayerData(Map<String, Object> elements) {
uuid = UUID.fromString(elements.get("uuid").toString());
contents = ((List<ItemStack>) elements.get("contents")).toArray(new ItemStack[] {});
gameMode = GameMode.valueOf(elements.get("gamemode").toString());
health = Double.parseDouble(elements.get("health").toString());
exp = Float.parseFloat(elements.get("exp").toString());
expLevel = Integer.parseInt(elements.get("exp-level").toString());
foodLevel = Integer.parseInt(elements.get("food-level").toString());
time = Long.parseLong(elements.get("time").toString());
}

public static PlayerData loadFromFile(File file) {
Preconditions.checkArgument(file.isFile(), file.getAbsolutePath() + " is not a file");

Expand All @@ -68,6 +83,7 @@ public static PlayerData loadFromFile(File file) {
} else {
data = (PlayerData) yamlConfiguration.get("data");
}

return data;
}

Expand All @@ -76,13 +92,13 @@ public void apply(Player player) {
player + " is not the user of this data (data UUID: " + uuid + ")");

player.getInventory().setContents(contents);
player.setGameMode(gameMode);
player.setHealth(Math.min(health, player.getMaxHealth()));
player.setExp(exp);
player.setLevel(expLevel);
player.setHealth(Math.min(health, player.getMaxHealth()));
player.setFoodLevel(foodLevel);
player.setGameMode(gameMode);
}

public void saveToFile(String file) {
YamlConfiguration config = new YamlConfiguration();
config.set("data", this);
Expand All @@ -94,16 +110,19 @@ public void saveToFile(String file) {
}
}

/**
* This method is used to save the original player data in the temporary player-data file.
*/
@Override
public Map<String, Object> serialize() {
Map<String, Object> serialized = new HashMap<>();
serialized.put("uuid", uuid.toString());
serialized.put("contents", contents);
serialized.put("gamemode", gameMode.name());
serialized.put("health", health);
serialized.put("food-level", foodLevel);
serialized.put("exp", exp);
serialized.put("exp-level", expLevel);
serialized.put("contents", contents);
serialized.put("food-level", foodLevel);
serialized.put("time", time);
return serialized;
}
Expand Down

0 comments on commit 922f394

Please sign in to comment.