diff --git a/eco-api/src/main/java/com/willfp/eco/core/data/PlayerData.java b/eco-api/src/main/java/com/willfp/eco/core/data/PlayerData.java new file mode 100644 index 00000000..129582a8 --- /dev/null +++ b/eco-api/src/main/java/com/willfp/eco/core/data/PlayerData.java @@ -0,0 +1,147 @@ +package com.willfp.eco.core.data; + +import com.willfp.eco.core.config.BaseConfig; +import com.willfp.eco.core.config.Config; +import com.willfp.eco.internal.config.ConfigSection; +import com.willfp.eco.internal.config.ConfigWrapper; +import lombok.experimental.UtilityClass; +import org.bukkit.NamespacedKey; +import org.bukkit.OfflinePlayer; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.YamlConfiguration; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.NotNull; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +@SuppressWarnings("unchecked") +@UtilityClass +public class PlayerData { + /** + * Instance of eco data.yml. + */ + private static BaseConfig dataYml = null; + + /** + * All cached player data. + */ + private static final Map PLAYER_DATA = new HashMap<>(); + + /** + * Write an integer to a player's data. + * + * @param player The player. + * @param key The key. + * @param data The data. + */ + public void writeInt(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key, + final int data) { + Config config = getPlayerConfig(player); + + config.set("player-data." + player.getUniqueId() + "." + key.toString(), data); + } + + /** + * Write a string to a player's data. + * + * @param player The player. + * @param key The key. + * @param data The data. + */ + public void writeString(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key, + @NotNull final String data) { + Config config = getPlayerConfig(player); + + config.set("player-data." + player.getUniqueId() + "." + key.toString(), data); + } + + /** + * Write a double to a player's data. + * + * @param player The player. + * @param key The key. + * @param data The data. + */ + public void writeDouble(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key, + final double data) { + Config config = getPlayerConfig(player); + + config.set("player-data." + player.getUniqueId() + "." + key.toString(), data); + } + + /** + * Read an integer from a player's data. + * + * @param player The player. + * @param key The key. + */ + public int readInt(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key) { + return getPlayerConfig(player).getInt("player-data." + player.getUniqueId() + "." + key.toString()); + } + + /** + * Read a string from a player's data. + * + * @param player The player. + * @param key The key. + */ + public String readString(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key) { + return getPlayerConfig(player).getString("player-data." + player.getUniqueId() + "." + key.toString()); + } + + /** + * Read a double from a player's data. + * + * @param player The player. + * @param key The key. + */ + public double readDouble(@NotNull final OfflinePlayer player, + @NotNull final NamespacedKey key) { + return getPlayerConfig(player).getDouble("player-data." + player.getUniqueId() + "." + key.toString()); + } + + /** + * Initialize the player data with an instance of data.yml. + * + * @param config data.yml. + */ + @ApiStatus.Internal + public void init(@NotNull final BaseConfig config) { + dataYml = config; + } + + /** + * Save to data.yml. + * + * @param config Instance of data.yml. + * @throws IOException Error during saving. + */ + @ApiStatus.Internal + public void save(@NotNull final BaseConfig config) throws IOException { + PLAYER_DATA.forEach((uuid, section) -> config.set("player-data." + uuid.toString(), ((ConfigWrapper) section).getHandle())); + config.save(); + } + + private Config getPlayerConfig(@NotNull final OfflinePlayer player) { + Config config = PLAYER_DATA.get(player.getUniqueId()); + + if (config == null) { + config = dataYml.getSubsectionOrNull("player-data." + player.getUniqueId()); + if (config == null) { + config = new ConfigSection(new YamlConfiguration()); + } + PLAYER_DATA.put(player.getUniqueId(), config); + return getPlayerConfig(player); + } + + return config; + } +} diff --git a/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigSection.java b/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigSection.java index 57f81e63..efd23646 100644 --- a/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigSection.java +++ b/eco-api/src/main/java/com/willfp/eco/internal/config/ConfigSection.java @@ -9,7 +9,7 @@ public class ConfigSection extends ConfigWrapper { * * @param section The section. */ - protected ConfigSection(@NotNull final ConfigurationSection section) { + public ConfigSection(@NotNull final ConfigurationSection section) { this.init(section); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java index 382ae9da..3493dfa4 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java @@ -3,6 +3,7 @@ package com.willfp.eco.spigot; import com.willfp.eco.core.AbstractPacketAdapter; import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.command.AbstractCommand; +import com.willfp.eco.core.data.PlayerData; import com.willfp.eco.core.display.Display; import com.willfp.eco.core.display.DisplayModule; import com.willfp.eco.core.integrations.IntegrationLoader; @@ -12,6 +13,7 @@ import com.willfp.eco.core.integrations.mcmmo.McmmoManager; import com.willfp.eco.proxy.proxies.BlockBreakProxy; import com.willfp.eco.proxy.proxies.SkullProxy; import com.willfp.eco.proxy.proxies.TridentStackProxy; +import com.willfp.eco.spigot.config.DataYml; import com.willfp.eco.spigot.display.PacketAutoRecipe; import com.willfp.eco.spigot.display.PacketChat; import com.willfp.eco.spigot.display.PacketOpenWindowMerchant; @@ -42,6 +44,7 @@ import lombok.Getter; import org.bukkit.event.Listener; import org.jetbrains.annotations.Nullable; +import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -53,6 +56,11 @@ public class EcoSpigotPlugin extends EcoPlugin { @Getter private static EcoSpigotPlugin instance; + /** + * data.yml. + */ + private final DataYml dataYml; + /** * Create a new instance of eco. */ @@ -69,6 +77,8 @@ public class EcoSpigotPlugin extends EcoPlugin { TridentStackProxy tridentStackProxy = InternalProxyUtils.getProxy(TridentStackProxy.class); TridentUtils.initialize(tridentStackProxy::getTridentStack); + + this.dataYml = new DataYml(this); } @Override @@ -79,11 +89,19 @@ public class EcoSpigotPlugin extends EcoPlugin { this.getEventManager().registerListener(new DispenserArmorListener()); this.getEventManager().registerListener(new EntityDeathByEntityListeners(this)); this.getEventManager().registerListener(new ShapedRecipeListener()); + + PlayerData.init(this.dataYml); } @Override public void disable() { - + this.getScheduler().run(() -> { + try { + PlayerData.save(this.dataYml); + } catch (IOException e) { + e.printStackTrace(); + } + }); } @Override diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/config/DataYml.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/config/DataYml.java new file mode 100644 index 00000000..b128b62d --- /dev/null +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/config/DataYml.java @@ -0,0 +1,16 @@ +package com.willfp.eco.spigot.config; + +import com.willfp.eco.core.config.BaseConfig; +import com.willfp.eco.spigot.EcoSpigotPlugin; +import org.jetbrains.annotations.NotNull; + +public class DataYml extends BaseConfig { + /** + * Init data.yml. + * + * @param plugin EcoSpigotPlugin. + */ + public DataYml(@NotNull final EcoSpigotPlugin plugin) { + super("data", false, plugin); + } +} diff --git a/eco-core/core-plugin/src/main/resources/data.yml b/eco-core/core-plugin/src/main/resources/data.yml new file mode 100644 index 00000000..e69de29b