Skip to content

Commit

Permalink
Add the PlayerPoints Convert command to convert data from PlayerPoint…
Browse files Browse the repository at this point in the history
…s to OptEco
  • Loading branch information
PlayerNguyen committed Oct 19, 2020
1 parent 5ef86e7 commit 4ab996e
Show file tree
Hide file tree
Showing 8 changed files with 233 additions and 1 deletion.
16 changes: 16 additions & 0 deletions src/main/java/me/playernguyen/opteco/OptEco.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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() {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/me/playernguyen/opteco/OptEcoImplementation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PlayerPointsAdapter.PlayerPointsObject> 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<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private boolean execute(CommandSender sender, ArrayList<String> 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))
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ public enum OptEcoPermission {
CANCEL("opteco.cancel"),
TOP("opteco.top"),

PPTO("opteco.ppto")

;

private final String permission;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<PlayerPointsObject> collect() {
ArrayList<PlayerPointsObject> 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<String> 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;
}
}



}
4 changes: 4 additions & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ab996e

Please sign in to comment.