diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java index b723c9475..92081bad4 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/BukkitMain.java @@ -7,6 +7,7 @@ import java.util.List; import java.util.UUID; import com.intellectualcrafters.plot.object.BukkitPlayer; + import net.milkbowl.vault.economy.Economy; import org.bukkit.Bukkit; @@ -109,12 +110,14 @@ import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.BlockUpdateUtil; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.ConsoleColors; +import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.MainUtil; import com.intellectualcrafters.plot.util.PlayerManager; import com.intellectualcrafters.plot.util.SetupUtils; import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.bukkit.BukkitChunkManager; +import com.intellectualcrafters.plot.util.bukkit.BukkitEconHandler; import com.intellectualcrafters.plot.util.bukkit.BukkitEventUtil; import com.intellectualcrafters.plot.util.bukkit.BukkitPlayerManager; import com.intellectualcrafters.plot.util.bukkit.BukkitSetBlockManager; @@ -411,13 +414,10 @@ public class BukkitMain extends JavaPlugin implements Listener, IPlotMain { } @Override - public Economy getEconomy() { - if ((getServer().getPluginManager().getPlugin("Vault") != null) && getServer().getPluginManager().getPlugin("Vault").isEnabled()) { - final RegisteredServiceProvider economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); - if (economyProvider != null) { - MainCommand.subCommands.add(new Buy()); - return economyProvider.getProvider(); - } + public EconHandler getEconomyHandler() { + BukkitEconHandler econ = new BukkitEconHandler(); + if (econ.init()) { + return econ; } return null; } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java index a6f8ba4ef..1314088fa 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/IPlotMain.java @@ -4,6 +4,7 @@ import java.io.File; import java.util.UUID; import com.intellectualcrafters.plot.config.C; + import net.milkbowl.vault.economy.Economy; import org.bukkit.generator.ChunkGenerator; @@ -13,6 +14,7 @@ import com.intellectualcrafters.plot.listeners.APlotListener; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.ChunkManager; +import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.PlayerManager; import com.intellectualcrafters.plot.util.SetupUtils; @@ -48,7 +50,7 @@ public interface IPlotMain { public void registerTNTListener(); - public Economy getEconomy(); + public EconHandler getEconomyHandler(); public BlockManager initBlockManager(); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java index 817c0d8ed..be01e657a 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/PlotSquared.java @@ -63,6 +63,7 @@ import com.intellectualcrafters.plot.object.comment.CommentManager; import com.intellectualcrafters.plot.util.BlockManager; import com.intellectualcrafters.plot.util.ChunkManager; import com.intellectualcrafters.plot.util.ClusterManager; +import com.intellectualcrafters.plot.util.EconHandler; import com.intellectualcrafters.plot.util.EventUtil; import com.intellectualcrafters.plot.util.ExpireManager; import com.intellectualcrafters.plot.util.Logger; @@ -89,7 +90,6 @@ public class PlotSquared { public static String VERSION = null; public static TaskManager TASK = null; private static boolean LOADING_WORLD = false; - public static Economy economy = null; public static WorldEditPlugin worldEdit = null; private final static HashMap plotworlds = new HashMap<>(); private final static HashMap plotmanagers = new HashMap<>(); @@ -510,7 +510,7 @@ public class PlotSquared { log("Could not determine file path"); } VERSION = IMP.getVersion(); - economy = IMP.getEconomy(); + EconHandler.manager = IMP.getEconomyHandler(); C.setupTranslations(); C.saveTranslations(); if (getJavaVersion() < 1.7) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java index d33f972da..a4456d010 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/object/PlotPlayer.java @@ -31,6 +31,27 @@ public interface PlotPlayer { public void setCompassTarget(Location loc); + /** + * Set player data that will persist restarts + * - Please note that this is not intended to store large values + * - For session only data use meta + * @param key + * @param value + */ + public void setAttribute(String key); + + /** + * The attribute will be either true or false + * @param key + */ + public boolean getAttribute(String key); + + /** + * Remove an attribute from a player + * @param key + */ + public void removeAttribute(String key); + public void setMeta(String key, Object value); public Object getMeta(String key); public void deleteMeta(String key); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java index db525a4d5..8c42f8ea4 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/EconHandler.java @@ -1,24 +1,15 @@ package com.intellectualcrafters.plot.util; -import com.intellectualcrafters.plot.PlotSquared; import com.intellectualcrafters.plot.object.OfflinePlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer; -public class EconHandler { - // TODO economy shit - public static double getBalance(final PlotPlayer player) { - return PlotSquared.economy.getBalance(player.getName()); - } - - public static void withdrawPlayer(final PlotPlayer player, final double amount) { - PlotSquared.economy.withdrawPlayer(player.getName(), amount); - } - - public static void depositPlayer(final PlotPlayer player, final double amount) { - PlotSquared.economy.depositPlayer(player.getName(), amount); - } - - public static void depositPlayer(final OfflinePlotPlayer player, final double amount) { - PlotSquared.economy.depositPlayer(player.getName(), amount); - } +public abstract class EconHandler { + public static EconHandler manager; + + public abstract double getMoney(PlotPlayer player); + public abstract double withdrawMoney(PlotPlayer player, double amount); + public abstract double depositMoney(PlotPlayer player, double amount); + public abstract double depositMoney(OfflinePlotPlayer player, double amount); + public abstract void setPermission(PlotPlayer player, String perm, boolean value); + public abstract boolean getPermission(PlotPlayer player, String perm); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitEconHandler.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitEconHandler.java new file mode 100644 index 000000000..3e45510b7 --- /dev/null +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitEconHandler.java @@ -0,0 +1,85 @@ +package com.intellectualcrafters.plot.util.bukkit; + +import org.bukkit.Bukkit; +import org.bukkit.plugin.RegisteredServiceProvider; + +import net.milkbowl.vault.economy.Economy; +import net.milkbowl.vault.permission.Permission; + +import com.intellectualcrafters.plot.object.BukkitOfflinePlayer; +import com.intellectualcrafters.plot.object.BukkitPlayer; +import com.intellectualcrafters.plot.object.OfflinePlotPlayer; +import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.util.EconHandler; + +public class BukkitEconHandler extends EconHandler { + + private Economy econ; + private Permission perms; + + public boolean init() { + if (econ == null || perms == null) { + setupPermissions(); + setupEconomy(); + } + return econ != null && perms != null; + } + + private boolean setupPermissions() + { + RegisteredServiceProvider permissionProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class); + if (permissionProvider != null) { + perms = permissionProvider.getProvider(); + } + return (perms != null); + } + + private boolean setupEconomy() + { + RegisteredServiceProvider economyProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class); + if (economyProvider != null) { + econ = economyProvider.getProvider(); + } + + return (econ != null); + } + + @Override + public double getMoney(PlotPlayer player) { + econ.getBalance(player.getName()); + return 0; + } + + @Override + public double withdrawMoney(PlotPlayer player, double amount) { + econ.withdrawPlayer(player.getName(), amount); + return 0; + } + + @Override + public double depositMoney(PlotPlayer player, double amount) { + econ.depositPlayer(player.getName(), amount); + return 0; + } + + @Override + public double depositMoney(OfflinePlotPlayer player, double amount) { + econ.depositPlayer(((BukkitOfflinePlayer) player).player, amount); + return 0; + } + + @Override + public void setPermission(PlotPlayer player, String perm, boolean value) { + if (value) { + perms.playerAdd(((BukkitPlayer) player).player, perm); + } + else { + perms.playerRemove(((BukkitPlayer) player).player, perm); + } + } + + @Override + public boolean getPermission(PlotPlayer player, String perm) { + return player.hasPermission(perm); + } +}