From f24811d44a6107aec636eabb887d9cff5f817858 Mon Sep 17 00:00:00 2001 From: Zrips Date: Sat, 19 Nov 2016 13:11:04 +0200 Subject: [PATCH] iConomy 6/7 support and moved vault as soft dependent --- .classpath | 1 + com/gamingmesh/jobs/HookEconomyTask.java | 67 ++++++++++++----- com/gamingmesh/jobs/Jobs.java | 3 + com/gamingmesh/jobs/commands/list/glog.java | 1 - com/gamingmesh/jobs/economy/.gitignore | 2 + .../jobs/economy/IConomy6Adapter.java | 75 +++++++++++++++++++ plugin.yml | 3 +- 7 files changed, 129 insertions(+), 23 deletions(-) create mode 100644 com/gamingmesh/jobs/economy/IConomy6Adapter.java diff --git a/.classpath b/.classpath index f7fdd02d..93f4b84c 100644 --- a/.classpath +++ b/.classpath @@ -7,5 +7,6 @@ + diff --git a/com/gamingmesh/jobs/HookEconomyTask.java b/com/gamingmesh/jobs/HookEconomyTask.java index a656a8ec..3bd1be85 100644 --- a/com/gamingmesh/jobs/HookEconomyTask.java +++ b/com/gamingmesh/jobs/HookEconomyTask.java @@ -20,14 +20,13 @@ package com.gamingmesh.jobs; import net.milkbowl.vault.economy.Economy; -import org.bukkit.ChatColor; import org.bukkit.Bukkit; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.RegisteredServiceProvider; import com.gamingmesh.jobs.economy.BlackholeEconomy; import com.gamingmesh.jobs.economy.VaultEconomy; +import com.gamingmesh.jobs.economy.IConomy6Adapter; public class HookEconomyTask implements Runnable { private Jobs plugin; @@ -38,29 +37,57 @@ public class HookEconomyTask implements Runnable { @Override public void run() { - Plugin eco = Bukkit.getServer().getPluginManager().getPlugin("Vault"); - if (eco != null) { - RegisteredServiceProvider provider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); - if (provider != null) { - Economy economy = provider.getProvider(); - if (economy != null) { - Jobs.setEconomy(this.plugin, new VaultEconomy(economy)); - String message = ChatColor.translateAlternateColorCodes('&', "&e[" + this.plugin.getDescription().getName() + "] Successfully linked with Vault."); - ConsoleCommandSender console = Bukkit.getServer().getConsoleSender(); - console.sendMessage(message); - return; - } - } - } - // no Vault found + if (setVault()) + return; + + if (setIConomy()) + return; + + // no Economy found Jobs.setEconomy(this.plugin, new BlackholeEconomy()); Bukkit.getServer().getLogger().severe("==================== " + this.plugin.getDescription().getName() + " ===================="); - Bukkit.getServer().getLogger().severe("Vault is required by this plugin for economy support!"); - Bukkit.getServer().getLogger().severe("Please install Vault and economy manager first!"); - Bukkit.getServer().getLogger().severe("You can find the latest version here:"); + Bukkit.getServer().getLogger().severe("Vault or Iconomy is required by this plugin for economy support!"); + Bukkit.getServer().getLogger().severe("Please install them first!"); + Bukkit.getServer().getLogger().severe("You can find the latest versions here:"); Bukkit.getServer().getLogger().severe("http://dev.bukkit.org/bukkit-plugins/vault/"); + Bukkit.getServer().getLogger().severe("https://dev.bukkit.org/bukkit-plugins/iconomy-7/"); Bukkit.getServer().getLogger().severe("=============================================="); } + private boolean setVault() { + Plugin eco = Bukkit.getServer().getPluginManager().getPlugin("Vault"); + if (eco == null) + return false; + + RegisteredServiceProvider provider = Bukkit.getServer().getServicesManager().getRegistration(Economy.class); + if (provider == null) + return false; + + Economy economy = provider.getProvider(); + if (economy == null) + return false; + + Jobs.setEconomy(this.plugin, new VaultEconomy(economy)); + Jobs.consoleMsg("&e[" + this.plugin.getDescription().getName() + "] Successfully linked with Vault."); + return true; + } + + private boolean setIConomy() { + Plugin p = Bukkit.getServer().getPluginManager().getPlugin("iConomy"); + if (p == null) + return false; + + try { + Jobs.setEconomy(this.plugin, new IConomy6Adapter((com.iCo6.iConomy) p)); + } catch (Exception e) { + Jobs.consoleMsg("&e[" + this.plugin.getDescription().getName() + "] UNKNOWN iConomy version."); + return false; + } + + Jobs.consoleMsg("&e[" + this.plugin.getDescription().getName() + "] Successfully linked with iConomy! Version: " + p.getDescription().getVersion()); + return true; + + } + } diff --git a/com/gamingmesh/jobs/Jobs.java b/com/gamingmesh/jobs/Jobs.java index 97efc04b..be61d612 100644 --- a/com/gamingmesh/jobs/Jobs.java +++ b/com/gamingmesh/jobs/Jobs.java @@ -1261,4 +1261,7 @@ public class Jobs extends JavaPlugin { return true; } + public static void consoleMsg(String msg){ + Bukkit.getServer().getConsoleSender().sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); + } } diff --git a/com/gamingmesh/jobs/commands/list/glog.java b/com/gamingmesh/jobs/commands/list/glog.java index 711d852e..c3b95efd 100644 --- a/com/gamingmesh/jobs/commands/list/glog.java +++ b/com/gamingmesh/jobs/commands/list/glog.java @@ -15,7 +15,6 @@ import com.gamingmesh.jobs.container.JobsPlayer; import com.gamingmesh.jobs.container.Log; import com.gamingmesh.jobs.container.LogAmounts; import com.gamingmesh.jobs.container.PlayerInfo; -import com.gamingmesh.jobs.stuff.Debug; import com.gamingmesh.jobs.stuff.Sorting; import com.gamingmesh.jobs.stuff.TimeManage; diff --git a/com/gamingmesh/jobs/economy/.gitignore b/com/gamingmesh/jobs/economy/.gitignore index 80ea2039..978564c7 100644 --- a/com/gamingmesh/jobs/economy/.gitignore +++ b/com/gamingmesh/jobs/economy/.gitignore @@ -5,3 +5,5 @@ /VaultEconomy.class /PaymentData.class /PointsData.class +/IConomy6Adapter.class +/IConomy7Adapter.class diff --git a/com/gamingmesh/jobs/economy/IConomy6Adapter.java b/com/gamingmesh/jobs/economy/IConomy6Adapter.java new file mode 100644 index 00000000..60db4da7 --- /dev/null +++ b/com/gamingmesh/jobs/economy/IConomy6Adapter.java @@ -0,0 +1,75 @@ +package com.gamingmesh.jobs.economy; + +import org.bukkit.OfflinePlayer; + +import com.iCo6.iConomy; +import com.iCo6.system.Accounts; + +public class IConomy6Adapter implements Economy { + + iConomy icon; + + public IConomy6Adapter(iConomy iconomy) { + icon = iconomy; + } + + public double getBalance(String playerName) { + checkExist(playerName); + return new Accounts().get(playerName).getHoldings().getBalance(); + } + + @Override + public boolean hasMoney(OfflinePlayer offlinePlayer, double amount) { + return hasMoney(offlinePlayer.getName(), amount); + } + + @Override + public boolean hasMoney(String PlayerName, double amount) { + checkExist(PlayerName); + double holdings = this.getBalance(PlayerName); + if (holdings >= amount) { + return true; + } + return false; + } + + @Override + public boolean depositPlayer(String PlayerName, double amount) { + checkExist(PlayerName); + new Accounts().get(PlayerName).getHoldings().add(amount); + return true; + } + + @Override + public boolean depositPlayer(OfflinePlayer offlinePlayer, double amount) { + return depositPlayer(offlinePlayer.getName(), amount); + } + + @Override + public boolean withdrawPlayer(String playerName, double amount) { + checkExist(playerName); + if (this.hasMoney(playerName, amount)) { + new Accounts().get(playerName).getHoldings().subtract(amount); + return true; + } + return false; + } + + @Override + public boolean withdrawPlayer(OfflinePlayer offlinePlayer, double amount) { + return withdrawPlayer(offlinePlayer.getName(), amount); + } + + @Override + public String format(double money) { + return iConomy.format(money); + } + + private static void checkExist(String playerName) { + Accounts acc = new Accounts(); + if (!acc.exists(playerName)) { + acc.create(playerName); + } + } + +} diff --git a/plugin.yml b/plugin.yml index 1e16905b..85ebb51a 100644 --- a/plugin.yml +++ b/plugin.yml @@ -3,8 +3,7 @@ description: Jobs Plugin for the BukkitAPI main: com.gamingmesh.jobs.Jobs version: 3.7.0 author: phrstbrn -depend: [Vault] -softdepend: [MythicMobs, McMMO] +softdepend: [Vault, iConomy, MythicMobs, McMMO] commands: jobs: description: Jobs