From 4ab996e6f1c58087df8708f193b8f3de3eaf1875 Mon Sep 17 00:00:00 2001 From: PlayerNguyen Date: Mon, 19 Oct 2020 23:16:25 +0700 Subject: [PATCH] Add the PlayerPoints Convert command to convert data from PlayerPoints to OptEco --- .../java/me/playernguyen/opteco/OptEco.java | 16 ++++ .../opteco/OptEcoImplementation.java | 15 +++ .../account/OptEcoCacheAccountManager.java | 10 ++ .../command/PlayerPointToOptEcoCommand.java | 89 +++++++++++++++++ .../opteco/command/SubCommandSet.java | 2 +- .../opteco/permission/OptEcoPermission.java | 2 + .../playerpoints/PlayerPointsAdapter.java | 96 +++++++++++++++++++ src/main/resources/plugin.yml | 4 + 8 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 src/main/java/me/playernguyen/opteco/command/PlayerPointToOptEcoCommand.java create mode 100644 src/main/java/me/playernguyen/opteco/playerpoints/PlayerPointsAdapter.java diff --git a/src/main/java/me/playernguyen/opteco/OptEco.java b/src/main/java/me/playernguyen/opteco/OptEco.java index 3a49605..249a6a1 100644 --- a/src/main/java/me/playernguyen/opteco/OptEco.java +++ b/src/main/java/me/playernguyen/opteco/OptEco.java @@ -8,6 +8,7 @@ import me.playernguyen.opteco.bossshoppro.OptEcoBossShopPro; import me.playernguyen.opteco.command.CommandManager; import me.playernguyen.opteco.command.OptEcoCommand; +import me.playernguyen.opteco.command.PlayerPointToOptEcoCommand; import me.playernguyen.opteco.configuration.OptEcoConfigurationLoader; import me.playernguyen.opteco.configuration.OptEcoLanguageLoader; import me.playernguyen.opteco.configuration.StorageType; @@ -28,6 +29,9 @@ import org.bukkit.plugin.java.JavaPlugin; import java.util.ArrayList; +import java.util.HashMap; +import java.util.Random; +import java.util.UUID; import java.util.logging.Logger; /** @@ -66,10 +70,21 @@ public void onEnable() { this.setupAccount(); this.hookPlaceHolderAPI(); this.hookBossShopPro(); + this.announcePlayerPoints(); this.setupMetric(); this.waterMarkPrint(); } + private void announcePlayerPoints() { + // Find BossShopPro + Plugin plugin = Bukkit.getPluginManager().getPlugin("PlayerPoints"); + // Whether found PlayerPoints + if (plugin != null) { + Bukkit.getConsoleSender().sendMessage(ChatColor.GOLD + "Detect PlayerPoints, using " + + ChatColor.RED + "/ppto " + ChatColor.GOLD + "to convert the data."); + } + } + private void hookBossShopPro() { // Find BossShopPro Plugin plugin = Bukkit.getPluginManager().getPlugin("BossShopPro"); @@ -271,6 +286,7 @@ private void registerExecutors() { this.commandManager = new CommandManager(); // Append new commands getCommandManager().add(new OptEcoCommand()); + getCommandManager().add(new PlayerPointToOptEcoCommand()); } public Debugger getDebugger() { diff --git a/src/main/java/me/playernguyen/opteco/OptEcoImplementation.java b/src/main/java/me/playernguyen/opteco/OptEcoImplementation.java index 204dbfc..ae55e44 100644 --- a/src/main/java/me/playernguyen/opteco/OptEcoImplementation.java +++ b/src/main/java/me/playernguyen/opteco/OptEcoImplementation.java @@ -8,6 +8,8 @@ import me.playernguyen.opteco.logger.Debugger; import me.playernguyen.opteco.transaction.TransactionManager; +import java.util.logging.Logger; + /** * The implementation class to implement from {@link OptEco} class instance */ @@ -75,8 +77,21 @@ protected TransactionManager getTransactionManager() { return getPlugin().getTransactionManager(); } + /** + * Get language loader class + * @return {@link OptEcoLanguageLoader} class + */ protected OptEcoLanguageLoader getLanguageLoader() { return getPlugin().getLanguageLoader(); } + /** + * + * @return The logger of OptEco + */ + protected Logger getLogger() { + return getPlugin().getLogger(); + } + + } diff --git a/src/main/java/me/playernguyen/opteco/account/OptEcoCacheAccountManager.java b/src/main/java/me/playernguyen/opteco/account/OptEcoCacheAccountManager.java index 113c6db..db1f6e1 100644 --- a/src/main/java/me/playernguyen/opteco/account/OptEcoCacheAccountManager.java +++ b/src/main/java/me/playernguyen/opteco/account/OptEcoCacheAccountManager.java @@ -80,6 +80,16 @@ public void add(UUID uuid) { this.map.put(uuid, OptEcoCacheAccount.loadFromAccount(account)); } + public void pushNewOne(UUID uuid, double balance) { + // Push new account with balance value + Account account = + new Account(uuid, balance); + + // Save account + this.getOptEco().getDebugger().info("Saving " + uuid + " with balance " + balance + "."); + this.getOptEco().getAccountDatabase().save(account); + } + public void remove(UUID uuid) { // notnull check, not found throw exception if (!exist(uuid)) { diff --git a/src/main/java/me/playernguyen/opteco/command/PlayerPointToOptEcoCommand.java b/src/main/java/me/playernguyen/opteco/command/PlayerPointToOptEcoCommand.java new file mode 100644 index 0000000..a3559c2 --- /dev/null +++ b/src/main/java/me/playernguyen/opteco/command/PlayerPointToOptEcoCommand.java @@ -0,0 +1,89 @@ +package me.playernguyen.opteco.command; + +import me.playernguyen.opteco.OptEco; +import me.playernguyen.opteco.permission.OptEcoPermission; +import me.playernguyen.opteco.playerpoints.PlayerPointsAdapter; +import org.bukkit.ChatColor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.RemoteConsoleCommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public class PlayerPointToOptEcoCommand extends OptEcoAbstractCommand{ + + public PlayerPointToOptEcoCommand() { + super("playerpointtoopteco"); + // Add permissions + addPermissions(OptEcoPermission.EVERYTHING); + addPermissions(OptEcoPermission.ADMIN); + addPermissions(OptEcoPermission.PPTO); + } + + @Override + public boolean onPlayerCommand(Player player, Command command, String s, String[] args) { + return exec(player, args); + } + + @Override + public boolean onConsoleCommand(ConsoleCommandSender sender, Command command, String s, String[] args) { + return exec(sender, args); + } + + @Override + public boolean onRemoteConsole(RemoteConsoleCommandSender sender, Command command, String s, String[] args) { + return exec(sender, args); + } + + @Override + public boolean onAny(CommandSender sender, Command command, String s, String[] args) { + return exec(sender, args); + } + + private boolean exec(CommandSender sender, String[] arguments) { + sender.sendMessage(ChatColor.GOLD + "Trying to convert data..."); + PlayerPointsAdapter adapter = new PlayerPointsAdapter(); + // Checking + if (!adapter.isHasPlugin()) { + sender.sendMessage(ChatColor.RED + "The plugin PlayerPoints not found!"); + return true; + } + if (!adapter.getDataFolder().exists()) { + sender.sendMessage(ChatColor.RED + "The folder plugins/PlayerPoints not found!"); + return true; + } + if (!adapter.getStorageFile().exists()) { + sender.sendMessage(ChatColor.RED + "The storage file (storage.yml) of PlayerPoints not found!"); + return true; + } + // Collect data + List collect = adapter.collect(); + // Push data to database + if (collect.size() > 0) { + sender.sendMessage(ChatColor.GRAY + "Pushing data into the database..."); + for (PlayerPointsAdapter.PlayerPointsObject object : collect) { + getLogger().info("---- Import ----"); + getLogger().info("- UUID: " + object.getUUID() + "-"); + getLogger().info("- Points: " + object.getPoint() + "-"); + sender.sendMessage(ChatColor.GRAY + " * " + object.getUUID() + ": " + object.getPoint() + "..."); + getAccountManager().pushNewOne(object.getUUID(), object.getPoint()); + } + } else { + sender.sendMessage(ChatColor.GRAY + " (Nothing changes because the storage is empty)"); + } + sender.sendMessage(ChatColor.GREEN + "Done!"); + sender.sendMessage(ChatColor.GOLD + "Now you can remove the PlayerPoints plugin, reload and use " + + "OptEco instead. Good luck!"); + + return true; + } + + @Override + public @Nullable List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) { + return null; + } +} diff --git a/src/main/java/me/playernguyen/opteco/command/SubCommandSet.java b/src/main/java/me/playernguyen/opteco/command/SubCommandSet.java index a564acc..084ca94 100644 --- a/src/main/java/me/playernguyen/opteco/command/SubCommandSet.java +++ b/src/main/java/me/playernguyen/opteco/command/SubCommandSet.java @@ -77,7 +77,7 @@ private boolean execute(CommandSender sender, ArrayList args) { sender.sendMessage( getMessageFormat() .format(getPlugin().getLanguageLoader().getLanguage(OptEcoLanguage.SET_SUCCESS)) - .replace("%value%", _value) + .replace("%value%", Double.toString(value)) .replace("%who%", target.getName()) .replace("%currency%", getPlugin().getConfigurationLoader().getString(OptEcoConfiguration.CURRENCY_SYMBOL)) ); diff --git a/src/main/java/me/playernguyen/opteco/permission/OptEcoPermission.java b/src/main/java/me/playernguyen/opteco/permission/OptEcoPermission.java index 9a4eaf8..0f4bfd1 100644 --- a/src/main/java/me/playernguyen/opteco/permission/OptEcoPermission.java +++ b/src/main/java/me/playernguyen/opteco/permission/OptEcoPermission.java @@ -17,6 +17,8 @@ public enum OptEcoPermission { CANCEL("opteco.cancel"), TOP("opteco.top"), + PPTO("opteco.ppto") + ; private final String permission; diff --git a/src/main/java/me/playernguyen/opteco/playerpoints/PlayerPointsAdapter.java b/src/main/java/me/playernguyen/opteco/playerpoints/PlayerPointsAdapter.java new file mode 100644 index 0000000..c4c5ec9 --- /dev/null +++ b/src/main/java/me/playernguyen/opteco/playerpoints/PlayerPointsAdapter.java @@ -0,0 +1,96 @@ +package me.playernguyen.opteco.playerpoints; + +import com.google.common.base.Preconditions; +import me.playernguyen.opteco.OptEco; +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.configuration.file.YamlConfiguration; +import org.bukkit.plugin.Plugin; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; +import java.util.UUID; + +public class PlayerPointsAdapter { + + private static final String PLAYER_POINTS_STORAGE_FILE_NAME = "storage.yml"; + private final File dataFolder; + private final File storageFile; + private final boolean hasPlugin; + private final Plugin playerPointsPlugin; + + public PlayerPointsAdapter() { + // If not found PlayerPoints, throw an exception + this.playerPointsPlugin = Bukkit.getPluginManager().getPlugin("PlayerPoints"); + this.hasPlugin = playerPointsPlugin != null; + // If found, doing the process + this.dataFolder = (playerPointsPlugin != null) ? playerPointsPlugin.getDataFolder(): null; + this.storageFile = new File(getDataFolder(), PLAYER_POINTS_STORAGE_FILE_NAME); + } + + public Plugin getPlayerPointsPlugin() { + return playerPointsPlugin; + } + + public void disable() { + OptEco.getInstance().getPluginLoader().disablePlugin(getPlayerPointsPlugin()); + } + + public boolean isHasPlugin() { + return hasPlugin; + } + + public File getDataFolder() { + return dataFolder; + } + + public List collect() { + ArrayList playerPointsObjects = new ArrayList<>(); + // Scan the PlayerPoints/storage.yml + FileConfiguration fileConfiguration = YamlConfiguration.loadConfiguration(storageFile); + // File configuration loader + if (fileConfiguration.contains("Points")) { + ConfigurationSection points = fileConfiguration.getConfigurationSection("Points"); + // Not null check + Preconditions.checkNotNull(points); + // Collect as set + Set uuids = points.getKeys(false); + // Put set + for (String uuid : uuids) { + playerPointsObjects.add(new PlayerPointsObject(UUID.fromString(uuid), + points.getInt(uuid))); + } + } + + return playerPointsObjects; + } + + public File getStorageFile() { + return storageFile; + } + + public static class PlayerPointsObject { + + private final UUID uuid; + private final int point; + + public PlayerPointsObject(UUID uuid, int point) { + this.uuid = uuid; + this.point = point; + } + + public int getPoint() { + return point; + } + + public UUID getUUID() { + return uuid; + } + } + + + +} diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index fbccffc..5f9175c 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -11,6 +11,10 @@ commands: usage: /opteco help description: Command of the OptEco plugin aliases: [ points, pts, p ] + playerpointtoopteco: + usage: /ppto + description: To convert all data to the OptEco with one command + aliases: [ppto, pptoopteco, pptoopt] permissions: opteco.admin: description: Grant access to admin tool / command