Added persistent player attributes API

This commit is contained in:
boy0001 2015-06-05 22:39:31 +10:00
parent 28dc3f6c9a
commit ea669b7697
6 changed files with 127 additions and 28 deletions

View File

@ -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<Economy> 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;
}

View File

@ -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();

View File

@ -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<String, PlotWorld> plotworlds = new HashMap<>();
private final static HashMap<String, PlotManager> 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) {

View File

@ -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);

View File

@ -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);
}

View File

@ -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<Permission> permissionProvider = Bukkit.getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
if (permissionProvider != null) {
perms = permissionProvider.getProvider();
}
return (perms != null);
}
private boolean setupEconomy()
{
RegisteredServiceProvider<Economy> 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);
}
}