Added playerdata
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package com.willfp.talismans.config;
|
||||
|
||||
import com.willfp.eco.util.config.updating.annotations.ConfigUpdater;
|
||||
import com.willfp.talismans.config.configs.Data;
|
||||
import com.willfp.talismans.config.configs.TalismanConfig;
|
||||
import lombok.Getter;
|
||||
import lombok.experimental.UtilityClass;
|
||||
@@ -11,6 +12,11 @@ import java.util.Set;
|
||||
|
||||
@UtilityClass
|
||||
public class TalismansConfigs {
|
||||
/**
|
||||
* data.yml.
|
||||
*/
|
||||
public static final Data DATA = new Data();
|
||||
|
||||
/**
|
||||
* All talisman-specific configs.
|
||||
*/
|
||||
@@ -22,6 +28,7 @@ public class TalismansConfigs {
|
||||
*/
|
||||
@ConfigUpdater
|
||||
public void updateConfigs() {
|
||||
DATA.update();
|
||||
TALISMAN_CONFIGS.forEach(TalismanYamlConfig::update);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.willfp.talismans.config.configs;
|
||||
|
||||
import com.willfp.eco.util.config.BaseConfig;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Data extends BaseConfig {
|
||||
/**
|
||||
* data.yml.
|
||||
*/
|
||||
public Data() {
|
||||
super("data.yml", false);
|
||||
}
|
||||
|
||||
public File getFile() {
|
||||
return super.configFile;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package com.willfp.talismans.data;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.willfp.talismans.config.TalismansConfigs;
|
||||
import lombok.Getter;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
|
||||
public final class PlayerData {
|
||||
/**
|
||||
* All currently loaded data.
|
||||
*/
|
||||
private static final BiMap<UUID, PlayerData> PLAYER_DATA = HashBiMap.create();
|
||||
|
||||
/**
|
||||
* The UUID of the player that owns this data.
|
||||
*/
|
||||
@Getter
|
||||
private final UUID uuid;
|
||||
|
||||
/**
|
||||
* The player's talisman inventory.
|
||||
*/
|
||||
@Getter
|
||||
private final Map<Integer, ItemStack> inventory = new HashMap<>();
|
||||
|
||||
private PlayerData(@NotNull final UUID uuid) {
|
||||
this.uuid = uuid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save all player data to data.yml.
|
||||
*/
|
||||
public static void save() {
|
||||
PLAYER_DATA.forEach((uuid, playerData) -> {
|
||||
TalismansConfigs.DATA.getConfig().set(uuid.toString() + ".inventory", null);
|
||||
playerData.inventory.forEach((integer, itemStack) -> {
|
||||
TalismansConfigs.DATA.getConfig().set(uuid.toString() + ".inventory." + integer, itemStack);
|
||||
});
|
||||
});
|
||||
|
||||
try {
|
||||
TalismansConfigs.DATA.getConfig().save(TalismansConfigs.DATA.getFile());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a player's data.
|
||||
*
|
||||
* @param player The player to get data for.
|
||||
* @return The data.
|
||||
*/
|
||||
public static PlayerData get(@NotNull final OfflinePlayer player) {
|
||||
if (PLAYER_DATA.containsKey(player.getUniqueId())) {
|
||||
return PLAYER_DATA.get(player.getUniqueId());
|
||||
}
|
||||
|
||||
PlayerData data = new PlayerData(player.getUniqueId());
|
||||
|
||||
if (TalismansConfigs.DATA.getConfig().contains(player.getUniqueId().toString())) {
|
||||
TalismansConfigs.DATA.getConfig().getConfigurationSection(player.getUniqueId().toString() + ".inventory").getKeys(false).forEach(s -> {
|
||||
ItemStack inventoryStack = TalismansConfigs.DATA.getConfig().getItemStack(player.getUniqueId().toString() + ".inventory." + s);
|
||||
|
||||
data.inventory.put(Integer.parseInt(s), inventoryStack);
|
||||
});
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
return new PlayerData(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
0
eco-core/core-plugin/src/main/resources/data.yml
Normal file
0
eco-core/core-plugin/src/main/resources/data.yml
Normal file
Reference in New Issue
Block a user