diff --git a/build.gradle b/build.gradle index 71a9301..5a97892 100644 --- a/build.gradle +++ b/build.gradle @@ -1,6 +1,7 @@ plugins { id 'java' id "com.github.johnrengelman.shadow" version "8.1.1" + id "io.freefair.lombok" version "6.6.1" } group = 'net.riblab' @@ -32,6 +33,8 @@ dependencies { compileOnly "com.github.MilkBowl:VaultAPI:1.7" implementation 'dev.jorel:commandapi-bukkit-shade:9.0.3' implementation 'de.tr7zw:item-nbt-api-plugin:2.11.3' + implementation 'com.github.Exlll.ConfigLib:configlib-yaml:v4.2.0' + compileOnly "org.projectlombok:lombok:1.18.24" } def targetJavaVersion = 17 @@ -62,6 +65,7 @@ processResources { shadowJar { relocate 'de.tr7zw.nbtapi', 'net.riblab.tradecore.nbtapi' relocate 'dev.jorel.commandapi', 'net.riblab.tradecore.commandapi' + relocate 'de.exlll.configlib', 'net.riblab.tradecore.configlib' } //Disable jar and replace with shadowJar diff --git a/src/main/java/net/riblab/tradecore/ConfigManager.java b/src/main/java/net/riblab/tradecore/ConfigManager.java new file mode 100644 index 0000000..41b454e --- /dev/null +++ b/src/main/java/net/riblab/tradecore/ConfigManager.java @@ -0,0 +1,37 @@ +package net.riblab.tradecore; + +import java.io.File; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; +import java.util.Map; +import java.util.UUID; + +import de.exlll.configlib.Comment; +import de.exlll.configlib.Configuration; +import de.exlll.configlib.YamlConfigurations; +import lombok.Getter; + +public class ConfigManager { + + @Configuration + public static class CurrencyData { + @Comment({"所持金"}) + public Map playerBank = new HashMap<>(); + @Comment({"所持投票チケット数"}) + public Map playerTickets = new HashMap<>(); + } + + @Getter + private CurrencyData currencyData; + private static final Path currencyConfigFile = new File(TradeCore.getInstance().getDataFolder(), "currency.yml").toPath(); + + public void save(){ + YamlConfigurations.save(currencyConfigFile, CurrencyData.class, currencyData); + } + + public void load(){ + // Load a new instance from the configuration file + currencyData = YamlConfigurations.update(currencyConfigFile, CurrencyData.class); + } +} \ No newline at end of file diff --git a/src/main/java/net/riblab/tradecore/EconomyImplementer.java b/src/main/java/net/riblab/tradecore/EconomyImplementer.java index 992b7d4..f97e0f9 100644 --- a/src/main/java/net/riblab/tradecore/EconomyImplementer.java +++ b/src/main/java/net/riblab/tradecore/EconomyImplementer.java @@ -12,8 +12,7 @@ import java.util.UUID; public class EconomyImplementer implements Economy { - - private TradeCore plugin = TradeCore.getInstance(); + private final ConfigManager.CurrencyData data = TradeCore.getInstance().getConfigManager().getCurrencyData(); @Override public boolean isEnabled() { @@ -57,7 +56,8 @@ public boolean hasAccount(String s) { @Override public boolean hasAccount(OfflinePlayer offlinePlayer) { - return plugin.playerBank.get(offlinePlayer) != null; + UUID uuid = offlinePlayer.getUniqueId(); + return data.playerBank.get(uuid) != null; } @Override @@ -74,26 +74,26 @@ public boolean hasAccount(OfflinePlayer offlinePlayer, String s) { public double getBalance(String s) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - return plugin.playerBank.get(uuid); + return data.playerBank.get(uuid); } @Override public double getBalance(OfflinePlayer offlinePlayer) { UUID uuid = offlinePlayer.getUniqueId(); - return plugin.playerBank.get(uuid); + return data.playerBank.get(uuid); } @Override public double getBalance(String s, String s1) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - return plugin.playerBank.get(uuid); + return data.playerBank.get(uuid); } @Override public double getBalance(OfflinePlayer offlinePlayer, String s) { UUID uuid = offlinePlayer.getUniqueId(); - return plugin.playerBank.get(uuid); + return data.playerBank.get(uuid); } @Override @@ -120,16 +120,16 @@ public boolean has(OfflinePlayer offlinePlayer, String s, double v) { public EconomyResponse withdrawPlayer(String s, double v) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance - v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance - v); return null; } @Override public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) { UUID uuid = offlinePlayer.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance - v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance - v); return null; } @@ -137,16 +137,16 @@ public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, double v) { public EconomyResponse withdrawPlayer(String s, String s1, double v) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance - v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance - v); return null; } @Override public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, String s, double v) { UUID uuid = offlinePlayer.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance - v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance - v); return null; } @@ -154,16 +154,16 @@ public EconomyResponse withdrawPlayer(OfflinePlayer offlinePlayer, String s, dou public EconomyResponse depositPlayer(String s, double v) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance + v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance + v); return null; } @Override public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double v) { UUID uuid = offlinePlayer.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance + v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance + v); return null; } @@ -171,16 +171,16 @@ public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, double v) { public EconomyResponse depositPlayer(String s, String s1, double v) { Player player = Bukkit.getPlayer(s); UUID uuid = player.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance + v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance + v); return null; } @Override public EconomyResponse depositPlayer(OfflinePlayer offlinePlayer, String s, double v) { UUID uuid = offlinePlayer.getUniqueId(); - double oldBalance = plugin.playerBank.get(uuid); - plugin.playerBank.put(uuid, oldBalance + v); + double oldBalance = data.playerBank.get(uuid); + data.playerBank.put(uuid, oldBalance + v); return null; } @@ -255,7 +255,7 @@ public boolean createPlayerAccount(OfflinePlayer offlinePlayer) { return false; UUID uuid = offlinePlayer.getUniqueId(); - plugin.playerBank.put(offlinePlayer.getUniqueId(), 0d); + data.playerBank.put(offlinePlayer.getUniqueId(), 0d); return true; } diff --git a/src/main/java/net/riblab/tradecore/TradeCore.java b/src/main/java/net/riblab/tradecore/TradeCore.java index 06c76e2..34cfe38 100644 --- a/src/main/java/net/riblab/tradecore/TradeCore.java +++ b/src/main/java/net/riblab/tradecore/TradeCore.java @@ -6,6 +6,7 @@ import dev.jorel.commandapi.CommandPermission; import dev.jorel.commandapi.arguments.DoubleArgument; import dev.jorel.commandapi.arguments.PlayerArgument; +import lombok.Getter; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.TextComponent; import org.bukkit.Bukkit; @@ -23,11 +24,11 @@ public final class TradeCore extends JavaPlugin implements Listener { private static TradeCore instance; - public EconomyImplementer economy; + @Getter + private EconomyImplementer economy; private VaultHook vaultHook; - - public final HashMap playerBank = new HashMap<>(); - + @Getter + private ConfigManager configManager; public TradeCore() { instance = this; } @@ -56,6 +57,9 @@ public void onLoad(){ @Override public void onEnable() { + configManager = new ConfigManager(); + configManager.load(); + economy = new EconomyImplementer(); vaultHook = new VaultHook(); vaultHook.hook(); @@ -80,18 +84,27 @@ public void run() { Component text = Component.text(""); text = text.append(Component.text(negativeSpace + negativeSpace + negativeSpace + negativeSpace + TCResourcePackData.IconsFont.COIN.get_char()).font(TCResourcePackData.iconsFontName)); text = text.append(Component.text(" " + balance).font(TCResourcePackData.yPlus12FontName)); - text = text.append(Component.text(" " + TCResourcePackData.IconsFont.VOTE_TICKET.get_char()).font(TCResourcePackData.iconsFontName)); + text = text.append(Component.text(" " + TCResourcePackData.IconsFont.VOTE_TICKET.get_char()).font(TCResourcePackData.iconsFontName)); text = text.append(Component.text(" 0").font(TCResourcePackData.yPlus12FontName)); player.sendActionBar(text); }); } }.runTaskTimer(this, 0, 20); + + //定期的にコンフィグを保存 + new BukkitRunnable(){ + @Override + public void run() { + configManager.save(); + } + }.runTaskTimer(this, 0, 3600); } @Override public void onDisable() { vaultHook.unhook(); CommandAPI.onDisable(); + configManager.save(); } @EventHandler diff --git a/src/main/java/net/riblab/tradecore/VaultHook.java b/src/main/java/net/riblab/tradecore/VaultHook.java index 70f72da..ce8b25a 100644 --- a/src/main/java/net/riblab/tradecore/VaultHook.java +++ b/src/main/java/net/riblab/tradecore/VaultHook.java @@ -12,7 +12,7 @@ public class VaultHook { private Economy provider; public void hook() { - provider = plugin.economy; + provider = plugin.getEconomy(); Bukkit.getServicesManager().register(Economy.class, this.provider, this.plugin, ServicePriority.Normal); Bukkit.getConsoleSender().sendMessage(ChatColor.GREEN + "VaultAPI hooked into " + ChatColor.AQUA + plugin.getName()); }