From 539700cb215e088546e5d7f7bba4e1035466626e Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 6 Feb 2012 22:13:39 +0100 Subject: New Groups code, supports groups.yml file, gm and vault, new economy api. --- .../src/com/earth2me/essentials/Economy.java | 210 +++++++++++++ .../src/com/earth2me/essentials/Essentials.java | 44 ++- .../src/com/earth2me/essentials/api/Economy.java | 344 --------------------- .../src/com/earth2me/essentials/api/IEconomy.java | 21 ++ .../com/earth2me/essentials/api/IEssentials.java | 9 +- .../src/com/earth2me/essentials/api/IGroups.java | 8 +- .../src/com/earth2me/essentials/api/IUser.java | 4 - .../earth2me/essentials/commands/Commandlist.java | 2 +- .../listener/EssentialsPluginListener.java | 65 +++- .../src/com/earth2me/essentials/perm/GMGroups.java | 143 +++++++++ .../com/earth2me/essentials/perm/VaultGroups.java | 126 ++++++++ .../com/earth2me/essentials/settings/General.java | 14 + .../earth2me/essentials/settings/GroupsHolder.java | 72 +++-- .../com/earth2me/essentials/settings/Money.java | 17 + .../earth2me/essentials/settings/MoneyHolder.java | 22 ++ .../earth2me/essentials/textreader/TextInput.java | 2 +- .../src/com/earth2me/essentials/user/User.java | 18 -- .../test/com/earth2me/essentials/EconomyTest.java | 51 ++- .../test/com/earth2me/essentials/UserTest.java | 9 +- .../src/com/earth2me/essentials/api/Economy.java | 231 ++++++++++++++ .../essentials/chat/EssentialsChatPlayer.java | 18 +- .../src/com/earth2me/essentials/signs/SignKit.java | 2 +- .../earth2me/essentials/signs/SignProtection.java | 44 +-- .../com/earth2me/essentials/signs/SignWarp.java | 2 +- .../earth2me/essentials/spawn/Commandspawn.java | 2 +- .../spawn/EssentialsSpawnPlayerListener.java | 2 +- .../earth2me/essentials/spawn/SpawnStorage.java | 2 +- 27 files changed, 999 insertions(+), 485 deletions(-) create mode 100644 Essentials/src/com/earth2me/essentials/Economy.java delete mode 100644 Essentials/src/com/earth2me/essentials/api/Economy.java create mode 100644 Essentials/src/com/earth2me/essentials/api/IEconomy.java create mode 100644 Essentials/src/com/earth2me/essentials/perm/GMGroups.java create mode 100644 Essentials/src/com/earth2me/essentials/perm/VaultGroups.java create mode 100644 Essentials/src/com/earth2me/essentials/settings/Money.java create mode 100644 Essentials/src/com/earth2me/essentials/settings/MoneyHolder.java create mode 100644 Essentials2Compat/src/com/earth2me/essentials/api/Economy.java diff --git a/Essentials/src/com/earth2me/essentials/Economy.java b/Essentials/src/com/earth2me/essentials/Economy.java new file mode 100644 index 000000000..b0b55a624 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/Economy.java @@ -0,0 +1,210 @@ +package com.earth2me.essentials; + +import com.earth2me.essentials.api.*; +import com.earth2me.essentials.perm.Permissions; +import com.earth2me.essentials.settings.MoneyHolder; +import java.util.HashMap; +import java.util.Locale; +import java.util.Map; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.bukkit.plugin.ServicePriority; + + +public class Economy implements IEconomy +{ + private final IEssentials ess; + private final MoneyHolder npcs; + + public Economy(IEssentials ess) + { + this.ess = ess; + this.npcs = new MoneyHolder(ess); + } + + private double getNPCBalance(String name) throws UserDoesNotExistException + { + npcs.acquireReadLock(); + try + { + Map balances = npcs.getData().getBalances(); + if (balances == null) + { + throw new UserDoesNotExistException(name); + } + Double balance = npcs.getData().getBalances().get(name.toLowerCase(Locale.ENGLISH)); + if (balance == null) + { + throw new UserDoesNotExistException(name); + } + return balance; + } + finally + { + npcs.unlock(); + } + } + + private void setNPCBalance(String name, double balance, boolean checkExistance) throws UserDoesNotExistException + { + npcs.acquireWriteLock(); + try + { + Map balances = npcs.getData().getBalances(); + if (balances == null) + { + balances = new HashMap(); + npcs.getData().setBalances(balances); + } + if (checkExistance && !balances.containsKey(name.toLowerCase(Locale.ENGLISH))) + { + throw new UserDoesNotExistException(name); + } + balances.put(name.toLowerCase(Locale.ENGLISH), balance); + } + finally + { + npcs.unlock(); + } + } + + private double getStartingBalance() + { + double startingBalance = 0; + ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + try + { + startingBalance = settings.getData().getEconomy().getStartingBalance(); + } + finally + { + settings.unlock(); + } + return startingBalance; + } + + @Override + public void onReload() + { + this.npcs.onReload(false); + } + + @Override + public double getMoney(String name) throws UserDoesNotExistException + { + IUser user = ess.getUser(name); + if (user == null) + { + return getNPCBalance(name); + } + return user.getMoney(); + } + + @Override + public void setMoney(String name, double balance) throws NoLoanPermittedException, UserDoesNotExistException + { + IUser user = ess.getUser(name); + if (user == null) + { + setNPCBalance(name, balance, true); + return; + } + if (balance < 0.0 && !Permissions.ECO_LOAN.isAuthorized(user)) + { + throw new NoLoanPermittedException(); + } + user.setMoney(balance); + } + + @Override + public void resetBalance(String name) throws NoLoanPermittedException, UserDoesNotExistException + { + setMoney(name, getStartingBalance()); + } + + @Override + public String format(double amount) + { + return Util.formatCurrency(amount, ess); + } + + @Override + public boolean playerExists(String name) + { + try + { + getMoney(name); + return true; + } + catch (UserDoesNotExistException ex) + { + return false; + } + } + + @Override + public boolean isNPC(String name) throws UserDoesNotExistException + { + boolean result = ess.getUser(name) == null; + if (result) + { + getNPCBalance(name); + } + return result; + } + + @Override + public boolean createNPC(String name) + { + try + { + if (isNPC(name)) + { + + setNPCBalance(name, getStartingBalance(), false); + return true; + } + } + catch (UserDoesNotExistException ex) + { + try + { + setNPCBalance(name, getStartingBalance(), false); + return true; + } + catch (UserDoesNotExistException ex1) + { + //This should never happen! + } + } + return false; + } + + @Override + public void removeNPC(String name) throws UserDoesNotExistException + { + npcs.acquireWriteLock(); + try + { + Map balances = npcs.getData().getBalances(); + if (balances == null) + { + balances = new HashMap(); + npcs.getData().setBalances(balances); + } + if (balances.containsKey(name.toLowerCase(Locale.ENGLISH))) + { + balances.remove(name.toLowerCase(Locale.ENGLISH)); + } + else + { + throw new UserDoesNotExistException(name); + } + } + finally + { + npcs.unlock(); + } + } +} diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 8dfd79207..393b6c749 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -66,11 +66,12 @@ public class Essentials extends JavaPlugin implements IEssentials private transient IItemDb itemDb; private transient IGroups groups; private transient final Methods paymentMethod = new Methods(); - private transient PermissionsHandler permissionsHandler; + //private transient PermissionsHandler permissionsHandler; private transient IUserMap userMap; private transient ExecuteTimer execTimer; private transient I18n i18n; private transient ICommandHandler commandHandler; + private transient Economy economy; public transient boolean testing; @Override @@ -99,8 +100,8 @@ public class Essentials extends JavaPlugin implements IEssentials settings = new SettingsHolder(this); i18n.updateLocale("en"); userMap = new UserMap(this); - permissionsHandler = new PermissionsHandler(this); - Economy.setEss(this); + //permissionsHandler = new PermissionsHandler(this); + economy = new Economy(this); } @Override @@ -155,7 +156,7 @@ public class Essentials extends JavaPlugin implements IEssentials reloadList.add(userMap); execTimer.mark("Init(Usermap)"); groups = new GroupsHolder(this); - reloadList.add(groups); + reloadList.add((GroupsHolder)groups); warps = new Warps(this); reloadList.add(warps); execTimer.mark("Init(Spawn/Warp)"); @@ -168,6 +169,8 @@ public class Essentials extends JavaPlugin implements IEssentials reloadList.add(kits); commandHandler = new EssentialsCommandHandler(Essentials.class.getClassLoader(), "com.earth2me.essentials.commands.Command", "essentials.", this); reloadList.add(commandHandler); + economy = new Economy(this); + reloadList.add(economy); reload(); } catch (YAMLException exception) @@ -197,7 +200,7 @@ public class Essentials extends JavaPlugin implements IEssentials return; } backup = new Backup(this); - permissionsHandler = new PermissionsHandler(this); + //permissionsHandler = new PermissionsHandler(this); final EssentialsPluginListener serverListener = new EssentialsPluginListener(this); pm.registerEvents(serverListener, this); reloadList.add(serverListener); @@ -219,7 +222,6 @@ public class Essentials extends JavaPlugin implements IEssentials final EssentialsTimer timer = new EssentialsTimer(this); getServer().getScheduler().scheduleSyncRepeatingTask(this, timer, 1, 100); - Economy.setEss(this); execTimer.mark("RegListeners"); final String timeroutput = execTimer.end(); if (getSettings().isDebug()) @@ -232,7 +234,6 @@ public class Essentials extends JavaPlugin implements IEssentials public void onDisable() { i18n.onDisable(); - Economy.setEss(null); Trade.closeLog(); } @@ -262,7 +263,7 @@ public class Essentials extends JavaPlugin implements IEssentials { return jails; } - + @Override public IKits getKits() { @@ -286,13 +287,13 @@ public class Essentials extends JavaPlugin implements IEssentials { return backup; } - + @Override public IUser getUser(final Player player) { return userMap.getUser(player); } - + @Override public IUser getUser(final String playerName) { @@ -374,18 +375,17 @@ public class Essentials extends JavaPlugin implements IEssentials return this.getServer().getScheduler().scheduleSyncRepeatingTask(this, run, delay, period); } - @Override public TNTExplodeListener getTNTListener() { return tntListener; } - @Override + /*@Override public PermissionsHandler getPermissionsHandler() { return permissionsHandler; - } + }*/ @Override public IItemDb getItemDb() @@ -416,4 +416,22 @@ public class Essentials extends JavaPlugin implements IEssentials { return commandHandler; } + + @Override + public void setGroups(final IGroups groups) + { + this.groups = groups; + } + + @Override + public void removeReloadListener(IReload groups) + { + this.reloadList.remove(groups); + } + + @Override + public IEconomy getEconomy() + { + return economy; + } } diff --git a/Essentials/src/com/earth2me/essentials/api/Economy.java b/Essentials/src/com/earth2me/essentials/api/Economy.java deleted file mode 100644 index 942cf6666..000000000 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ /dev/null @@ -1,344 +0,0 @@ -package com.earth2me.essentials.api; - -import com.earth2me.essentials.Util; -import com.earth2me.essentials.craftbukkit.DummyOfflinePlayer; -import com.earth2me.essentials.perm.Permissions; -import com.earth2me.essentials.user.User; -import java.io.File; -import java.util.logging.Level; -import java.util.logging.Logger; -import lombok.Cleanup; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; - - -/** - * Instead of using this api directly, we recommend to use the register plugin: - * http://bit.ly/RegisterMethod - */ -public final class Economy -{ - private Economy() - { - } - private static final Logger logger = Logger.getLogger("Minecraft"); - private static IEssentials ess; - private static final String noCallBeforeLoad = "Essentials API is called before Essentials is loaded."; - - /** - * @param aEss the ess to set - */ - public static void setEss(IEssentials aEss) - { - ess = aEss; - } - - private static void createNPCFile(String name) - { - File folder = new File(ess.getDataFolder(), "userdata"); - if (!folder.exists()) - { - folder.mkdirs(); - } - double startingBalance = 0; - ISettings settings = ess.getSettings(); - settings.acquireReadLock(); - try { - startingBalance = settings.getData().getEconomy().getStartingBalance(); - } finally { - settings.unlock(); - } - IUser npc = new User(new DummyOfflinePlayer(name), ess); - npc.acquireWriteLock(); - try { - npc.getData().setNpc(true); - npc.setMoney(startingBalance); - } finally { - npc.unlock(); - } - - /*EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml")); - npcConfig.load(); - npcConfig.setProperty("npc", true); - npcConfig.setProperty("money", ess.getSettings().getStartingBalance()); - npcConfig.save();*/ - } - - private static void deleteNPC(final String name) - { - File folder = new File(ess.getDataFolder(), "userdata"); - if (!folder.exists()) - { - folder.mkdirs(); - } - IUser user = ess.getUser(name); - if (user != null) { - boolean npc = false; - user.acquireReadLock(); - try { - npc = user.getData().isNpc(); - } finally { - user.unlock(); - } - if (npc) { - try - { - ess.getUserMap().removeUser(name); - } - catch (InvalidNameException ex) - { - Bukkit.getLogger().log(Level.INFO, name, ex); - } - } - } - } - - private static IUser getUserByName(String name) - { - if (ess == null) - { - throw new RuntimeException(noCallBeforeLoad); - } - IUser user; - Player player = ess.getServer().getPlayer(name); - if (player != null) - { - user = ess.getUser(player); - } - else - { - user = ess.getUser(name); - } - return user; - } - - /** - * Returns the balance of a user - * @param name Name of the user - * @return balance - * @throws UserDoesNotExistException - */ - public static double getMoney(String name) throws UserDoesNotExistException - { - IUser user = getUserByName(name); - if (user == null) - { - throw new UserDoesNotExistException(name); - } - return user.getMoney(); - } - - /** - * Sets the balance of a user - * @param name Name of the user - * @param balance The balance you want to set - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException - { - IUser user = getUserByName(name); - if (user == null) - { - throw new UserDoesNotExistException(name); - } - if (balance < 0.0 && !Permissions.ECO_LOAN.isAuthorized(user)) - { - throw new NoLoanPermittedException(); - } - user.setMoney(balance); - } - - /** - * Adds money to the balance of a user - * @param name Name of the user - * @param amount The money you want to add - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException - { - double result = getMoney(name) + amount; - setMoney(name, result); - } - - /** - * Substracts money from the balance of a user - * @param name Name of the user - * @param amount The money you want to substract - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException - { - double result = getMoney(name) - amount; - setMoney(name, result); - } - - /** - * Divides the balance of a user by a value - * @param name Name of the user - * @param value The balance is divided by this value - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void divide(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException - { - double result = getMoney(name) / value; - setMoney(name, result); - } - - /** - * Multiplies the balance of a user by a value - * @param name Name of the user - * @param value The balance is multiplied by this value - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void multiply(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException - { - double result = getMoney(name) * value; - setMoney(name, result); - } - - /** - * Resets the balance of a user to the starting balance - * @param name Name of the user - * @throws UserDoesNotExistException If a user by that name does not exists - * @throws NoLoanPermittedException If the user is not allowed to have a negative balance - */ - public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException - { - if (ess == null) - { - throw new RuntimeException(noCallBeforeLoad); - } - double startingBalance = 0; - ISettings settings = ess.getSettings(); - settings.acquireReadLock(); - try { - startingBalance = settings.getData().getEconomy().getStartingBalance(); - } finally { - settings.unlock(); - } - setMoney(name, startingBalance); - } - - /** - * @param name Name of the user - * @param amount The amount of money the user should have - * @return true, if the user has more or an equal amount of money - * @throws UserDoesNotExistException If a user by that name does not exists - */ - public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException - { - return amount <= getMoney(name); - } - - /** - * @param name Name of the user - * @param amount The amount of money the user should have - * @return true, if the user has more money - * @throws UserDoesNotExistException If a user by that name does not exists - */ - public static boolean hasMore(String name, double amount) throws UserDoesNotExistException - { - return amount < getMoney(name); - } - - /** - * @param name Name of the user - * @param amount The amount of money the user should not have - * @return true, if the user has less money - * @throws UserDoesNotExistException If a user by that name does not exists - */ - public static boolean hasLess(String name, double amount) throws UserDoesNotExistException - { - return amount > getMoney(name); - } - - /** - * Test if the user has a negative balance - * @param name Name of the user - * @return true, if the user has a negative balance - * @throws UserDoesNotExistException If a user by that name does not exists - */ - public static boolean isNegative(String name) throws UserDoesNotExistException - { - return getMoney(name) < 0.0; - } - - /** - * Formats the amount of money like all other Essentials functions. - * Example: $100000 or $12345.67 - * @param amount The amount of money - * @return Formatted money - */ - public static String format(double amount) - { - if (ess == null) - { - throw new RuntimeException(noCallBeforeLoad); - } - return Util.formatCurrency(amount, ess); - } - - /** - * Test if a player exists to avoid the UserDoesNotExistException - * @param name Name of the user - * @return true, if the user exists - */ - public static boolean playerExists(String name) - { - return getUserByName(name) != null; - } - - /** - * Test if a player is a npc - * @param name Name of the player - * @return true, if it's a npc - * @throws UserDoesNotExistException - */ - public static boolean isNPC(String name) throws UserDoesNotExistException - { - @Cleanup - IUser user = getUserByName(name); - if (user == null) - { - throw new UserDoesNotExistException(name); - } - user.acquireReadLock(); - return user.getData().isNpc(); - } - - /** - * Creates dummy files for a npc, if there is no player yet with that name. - * @param name Name of the player - * @return true, if a new npc was created - */ - public static boolean createNPC(String name) - { - IUser user = getUserByName(name); - if (user == null) - { - createNPCFile(name); - return true; - } - return false; - } - - /** - * Deletes a user, if it is marked as npc. - * @param name Name of the player - * @throws UserDoesNotExistException - */ - public static void removeNPC(String name) throws UserDoesNotExistException - { - IUser user = getUserByName(name); - if (user == null) - { - throw new UserDoesNotExistException(name); - } - deleteNPC(name); - } -} diff --git a/Essentials/src/com/earth2me/essentials/api/IEconomy.java b/Essentials/src/com/earth2me/essentials/api/IEconomy.java new file mode 100644 index 000000000..092d86c88 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/api/IEconomy.java @@ -0,0 +1,21 @@ +package com.earth2me.essentials.api; + + +public interface IEconomy extends IReload +{ + public double getMoney(String name) throws UserDoesNotExistException; + + public void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException; + + public void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException; + + public String format(double amount); + + public boolean playerExists(String name); + + public boolean isNPC(String name) throws UserDoesNotExistException; + + public boolean createNPC(String name); + + public void removeNPC(String name) throws UserDoesNotExistException; +} diff --git a/Essentials/src/com/earth2me/essentials/api/IEssentials.java b/Essentials/src/com/earth2me/essentials/api/IEssentials.java index 9da294ca0..831d13344 100644 --- a/Essentials/src/com/earth2me/essentials/api/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/api/IEssentials.java @@ -1,6 +1,7 @@ package com.earth2me.essentials.api; import com.earth2me.essentials.listener.TNTExplodeListener; +import com.earth2me.essentials.perm.GMGroups; import com.earth2me.essentials.perm.IPermissionsHandler; import com.earth2me.essentials.register.payment.Methods; import org.bukkit.World; @@ -52,9 +53,15 @@ public interface IEssentials extends Plugin int scheduleSyncRepeatingTask(Runnable run, long delay, long period); - IPermissionsHandler getPermissionsHandler(); + //IPermissionsHandler getPermissionsHandler(); void reload(); TNTExplodeListener getTNTListener(); + + void setGroups(IGroups groups); + + void removeReloadListener(IReload groups); + + IEconomy getEconomy(); } diff --git a/Essentials/src/com/earth2me/essentials/api/IGroups.java b/Essentials/src/com/earth2me/essentials/api/IGroups.java index 8e8e3f26a..ec8986ea0 100644 --- a/Essentials/src/com/earth2me/essentials/api/IGroups.java +++ b/Essentials/src/com/earth2me/essentials/api/IGroups.java @@ -1,12 +1,14 @@ package com.earth2me.essentials.api; -import com.earth2me.essentials.settings.Groups; -import com.earth2me.essentials.storage.IStorageObjectHolder; import java.text.MessageFormat; -public interface IGroups extends IStorageObjectHolder +public interface IGroups { + String getMainGroup(IUser player); + + boolean inGroup(IUser player, String groupname); + double getHealCooldown(IUser player); double getTeleportCooldown(IUser player); diff --git a/Essentials/src/com/earth2me/essentials/api/IUser.java b/Essentials/src/com/earth2me/essentials/api/IUser.java index 29150e246..3a23d595d 100644 --- a/Essentials/src/com/earth2me/essentials/api/IUser.java +++ b/Essentials/src/com/earth2me/essentials/api/IUser.java @@ -33,10 +33,6 @@ public interface IUser extends Player, IStorageObjectHolder, IReload, void payUser(final IUser reciever, final double value) throws Exception; - String getGroup(); - - boolean inGroup(String group); - void setLastLocation(); Location getHome(String name) throws Exception; diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java index 0e7d38ede..c069d93f1 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandlist.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandlist.java @@ -61,7 +61,7 @@ public class Commandlist extends EssentialsCommand { continue; } - final String group = player.getGroup(); + final String group = ess.getGroups().getMainGroup(player); List list = sort.get(group); if (list == null) { diff --git a/Essentials/src/com/earth2me/essentials/listener/EssentialsPluginListener.java b/Essentials/src/com/earth2me/essentials/listener/EssentialsPluginListener.java index 8d8879f4a..8add570cd 100644 --- a/Essentials/src/com/earth2me/essentials/listener/EssentialsPluginListener.java +++ b/Essentials/src/com/earth2me/essentials/listener/EssentialsPluginListener.java @@ -2,18 +2,24 @@ package com.earth2me.essentials.listener; import com.earth2me.essentials.api.IEssentials; import com.earth2me.essentials.api.IReload; +import com.earth2me.essentials.api.ISettings; +import com.earth2me.essentials.perm.GMGroups; +import com.earth2me.essentials.perm.VaultGroups; +import com.earth2me.essentials.settings.General; +import com.earth2me.essentials.settings.GroupsHolder; import java.util.logging.Level; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.server.PluginDisableEvent; import org.bukkit.event.server.PluginEnableEvent; +import org.bukkit.plugin.Plugin; public class EssentialsPluginListener implements Listener, IReload { private final transient IEssentials ess; - + public EssentialsPluginListener(final IEssentials ess) { super(); @@ -23,18 +29,23 @@ public class EssentialsPluginListener implements Listener, IReload @EventHandler(priority = EventPriority.MONITOR) public void onPluginEnable(final PluginEnableEvent event) { - ess.getPermissionsHandler().checkPermissions(); + checkGroups(); + //ess.getPermissionsHandler().checkPermissions(); ess.getCommandHandler().addPlugin(event.getPlugin()); if (!ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().setMethod(ess.getServer().getPluginManager())) { - ess.getLogger().log(Level.INFO, "Payment method found ({0} version: {1})", new Object[]{ess.getPaymentMethod().getMethod().getName(), ess.getPaymentMethod().getMethod().getVersion()}); + ess.getLogger().log(Level.INFO, "Payment method found ({0} version: {1})", new Object[] + { + ess.getPaymentMethod().getMethod().getName(), ess.getPaymentMethod().getMethod().getVersion() + }); } } @EventHandler(priority = EventPriority.MONITOR) public void onPluginDisable(final PluginDisableEvent event) { - ess.getPermissionsHandler().checkPermissions(); + checkGroups(); + //ess.getPermissionsHandler().checkPermissions(); ess.getCommandHandler().removePlugin(event.getPlugin()); // Check to see if the plugin thats being disabled is the one we are using if (ess.getPaymentMethod() != null && ess.getPaymentMethod().hasMethod() && ess.getPaymentMethod().checkDisabled(event.getPlugin())) @@ -49,4 +60,50 @@ public class EssentialsPluginListener implements Listener, IReload { //ess.getPermissionsHandler().setUseSuperperms(ess.getSettings().useBukkitPermissions()); } + + private void checkGroups() + { + ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + General.GroupStorage storage = General.GroupStorage.FILE; + try + { + storage = settings.getData().getGeneral().getGroupStorage(); + } + finally + { + settings.unlock(); + } + if (storage == General.GroupStorage.GROUPMANAGER) + { + Plugin groupManager = ess.getServer().getPluginManager().getPlugin("GroupManager"); + if (groupManager != null && groupManager.isEnabled() && !(ess.getGroups() instanceof GMGroups)) + { + if (ess.getGroups() instanceof GroupsHolder) + { + ess.removeReloadListener((GroupsHolder)ess.getGroups()); + } + ess.setGroups(new GMGroups(ess, groupManager)); + return; + } + } + if (storage == General.GroupStorage.VAULT) + { + Plugin vault = ess.getServer().getPluginManager().getPlugin("Vault"); + if (vault != null && vault.isEnabled() && !(ess.getGroups() instanceof VaultGroups)) + { + if (ess.getGroups() instanceof GroupsHolder) + { + ess.removeReloadListener((GroupsHolder)ess.getGroups()); + } + ess.setGroups(new VaultGroups(ess)); + return; + } + } + if (!(ess.getGroups() instanceof GroupsHolder)) + { + ess.setGroups(new GroupsHolder(ess)); + ess.addReloadListener((GroupsHolder)ess.getGroups()); + } + } } diff --git a/Essentials/src/com/earth2me/essentials/perm/GMGroups.java b/Essentials/src/com/earth2me/essentials/perm/GMGroups.java new file mode 100644 index 000000000..f301af3ca --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/perm/GMGroups.java @@ -0,0 +1,143 @@ +package com.earth2me.essentials.perm; + +import com.earth2me.essentials.Util; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.IGroups; +import com.earth2me.essentials.api.ISettings; +import com.earth2me.essentials.api.IUser; +import java.text.MessageFormat; +import lombok.Cleanup; +import org.anjocaido.groupmanager.GroupManager; +import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; +import org.bukkit.plugin.Plugin; + +public class GMGroups implements IGroups { + private final transient IEssentials ess; + private final transient GroupManager groupManager; + + public GMGroups(final IEssentials ess, final Plugin groupManager) + { + this.ess = ess; + this.groupManager = (GroupManager)groupManager; + } + + @Override + public double getHealCooldown(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return 0; + } + return handler.getPermissionDouble(player.getName(), "healcooldown"); + } + + @Override + public double getTeleportCooldown(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return 0; + } + return handler.getPermissionDouble(player.getName(), "teleportcooldown"); + } + + @Override + public double getTeleportDelay(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return 0; + } + return handler.getPermissionDouble(player.getName(), "teleportdelay"); + } + + @Override + public String getPrefix(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return null; + } + return handler.getUserPrefix(player.getName()); + } + + @Override + public String getSuffix(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return null; + } + return handler.getUserSuffix(player.getName()); + } + + @Override + public int getHomeLimit(IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return 0; + } + return handler.getPermissionInteger(player.getName(), "homes"); + } + + @Override + public MessageFormat getChatFormat(final IUser player) + { + String format = getRawChatFormat(player); + format = Util.replaceColor(format); + format = format.replace("{DISPLAYNAME}", "%1$s"); + format = format.replace("{GROUP}", "{0}"); + format = format.replace("{MESSAGE}", "%2$s"); + format = format.replace("{WORLDNAME}", "{1}"); + format = format.replace("{SHORTWORLDNAME}", "{2}"); + format = format.replaceAll("\\{(\\D*)\\}", "\\[$1\\]"); + MessageFormat mFormat = new MessageFormat(format); + return mFormat; + } + + private String getRawChatFormat(final IUser player) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler != null) + { + String chatformat = handler.getPermissionString(player.getName(), "chatformat"); + if (chatformat != null && !chatformat.isEmpty()) { + return chatformat; + } + } + + @Cleanup + ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + return settings.getData().getChat().getDefaultFormat(); + } + + @Override + public String getMainGroup(IUser player) + { + final AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return null; + } + return handler.getGroup(player.getName()); + } + + @Override + public boolean inGroup(IUser player, String groupname) + { + AnjoPermissionsHandler handler = groupManager.getWorldsHolder().getWorldPermissions(player.getBase()); + if (handler == null) + { + return false; + } + return handler.inGroup(player.getName(), groupname); + } +} diff --git a/Essentials/src/com/earth2me/essentials/perm/VaultGroups.java b/Essentials/src/com/earth2me/essentials/perm/VaultGroups.java new file mode 100644 index 000000000..3001043bb --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/perm/VaultGroups.java @@ -0,0 +1,126 @@ +package com.earth2me.essentials.perm; + +import com.earth2me.essentials.Util; +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.api.IGroups; +import com.earth2me.essentials.api.ISettings; +import com.earth2me.essentials.api.IUser; +import java.text.MessageFormat; +import lombok.Cleanup; +import net.milkbowl.vault.chat.Chat; +import org.anjocaido.groupmanager.permissions.AnjoPermissionsHandler; +import org.bukkit.plugin.RegisteredServiceProvider; + + +public class VaultGroups implements IGroups +{ + private final IEssentials ess; + + public VaultGroups(final IEssentials ess) + { + this.ess = ess; + + } + + @Override + public double getHealCooldown(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerInfoDouble(player.getBase(), "healcooldown", 0); + } + + @Override + public double getTeleportCooldown(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerInfoDouble(player.getBase(), "teleportcooldown", 0); + } + + @Override + public double getTeleportDelay(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerInfoDouble(player.getBase(), "teleportdelay", 0); + } + + @Override + public String getPrefix(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerPrefix(player.getBase()); + } + + @Override + public String getSuffix(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerSuffix(player.getBase()); + } + + @Override + public int getHomeLimit(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPlayerInfoInteger(player.getBase(), "teleportdelay", 0); + } + + @Override + public MessageFormat getChatFormat(final IUser player) + { + String format = getRawChatFormat(player); + format = Util.replaceColor(format); + format = format.replace("{DISPLAYNAME}", "%1$s"); + format = format.replace("{GROUP}", "{0}"); + format = format.replace("{MESSAGE}", "%2$s"); + format = format.replace("{WORLDNAME}", "{1}"); + format = format.replace("{SHORTWORLDNAME}", "{2}"); + format = format.replaceAll("\\{(\\D*)\\}", "\\[$1\\]"); + MessageFormat mFormat = new MessageFormat(format); + return mFormat; + } + + private String getRawChatFormat(final IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + String chatformat = chat.getPlayerInfoString(player.getBase(), "chatformat", ""); + if (chatformat != null && !chatformat.isEmpty()) + { + return chatformat; + } + + @Cleanup + ISettings settings = ess.getSettings(); + settings.acquireReadLock(); + return settings.getData().getChat().getDefaultFormat(); + } + + @Override + public String getMainGroup(IUser player) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + return chat.getPrimaryGroup(player.getBase()); + } + + @Override + public boolean inGroup(IUser player, String groupname) + { + RegisteredServiceProvider rsp = ess.getServer().getServicesManager().getRegistration(Chat.class); + Chat chat = rsp.getProvider(); + for (String group : chat.getPlayerGroups(player.getBase())) + { + if (group.equalsIgnoreCase(groupname)) + { + return true; + } + } + return false; + } +} diff --git a/Essentials/src/com/earth2me/essentials/settings/General.java b/Essentials/src/com/earth2me/essentials/settings/General.java index 3b808fb41..85238977f 100644 --- a/Essentials/src/com/earth2me/essentials/settings/General.java +++ b/Essentials/src/com/earth2me/essentials/settings/General.java @@ -37,4 +37,18 @@ public class General implements StorageObject "How many items should be in a oversized stack?" }) private int oversizedStacksize = 64; + + + public enum GroupStorage + { + FILE, GROUPMANAGER, VAULT + } + @Comment( + { + "Sets the place where group options should be stored:", + " FILE: Options are stored inside groups.yml in the Essentials folder", + " GROUPMANAGER: Options are stored using the GroupManager groups", + " VAULT: Options are stored using a permissions plugin supported by Vault" + }) + private GroupStorage groupStorage = GroupStorage.FILE; } diff --git a/Essentials/src/com/earth2me/essentials/settings/GroupsHolder.java b/Essentials/src/com/earth2me/essentials/settings/GroupsHolder.java index 7c5403c7b..00ecfec27 100644 --- a/Essentials/src/com/earth2me/essentials/settings/GroupsHolder.java +++ b/Essentials/src/com/earth2me/essentials/settings/GroupsHolder.java @@ -22,6 +22,7 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG public GroupsHolder(final IEssentials ess) { super(ess, Groups.class); + onReload(); } @Override @@ -30,7 +31,7 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG return new File(ess.getDataFolder(), "groups.yml"); } - public Collection getGroups(final IUser player) + public Collection> getGroups(final IUser player) { acquireReadLock(); try @@ -40,14 +41,14 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG { return Collections.emptyList(); } - final ArrayList list = new ArrayList(); + final ArrayList> list = new ArrayList(); for (Entry entry : groups.entrySet()) { if (GroupsPermissions.getPermission(entry.getKey()).isAuthorized(player)) { if(entry.getValue() != null) { - list.add(entry.getValue()); + list.add(entry); } } } @@ -62,11 +63,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public double getHealCooldown(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getHealCooldown() != null) + if (groupOptions.getValue().getHealCooldown() != null) { - return groupOptions.getHealCooldown(); + return groupOptions.getValue().getHealCooldown(); } } return 0; @@ -75,11 +76,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public double getTeleportCooldown(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getTeleportCooldown() != null) + if (groupOptions.getValue().getTeleportCooldown() != null) { - return groupOptions.getTeleportCooldown(); + return groupOptions.getValue().getTeleportCooldown(); } } return 0; @@ -88,11 +89,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public double getTeleportDelay(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getTeleportDelay() != null) + if (groupOptions.getValue().getTeleportDelay() != null) { - return groupOptions.getTeleportDelay(); + return groupOptions.getValue().getTeleportDelay(); } } return 0; @@ -101,11 +102,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public String getPrefix(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getPrefix() != null) + if (groupOptions.getValue().getPrefix() != null) { - return groupOptions.getPrefix(); + return groupOptions.getValue().getPrefix(); } } return ""; @@ -114,11 +115,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public String getSuffix(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getSuffix() != null) + if (groupOptions.getValue().getSuffix() != null) { - return groupOptions.getSuffix(); + return groupOptions.getValue().getSuffix(); } } return ""; @@ -127,11 +128,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG @Override public int getHomeLimit(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions.getHomes() != null) + if (groupOptions.getValue().getHomes() != null) { - return groupOptions.getHomes(); + return groupOptions.getValue().getHomes(); } } return 0; @@ -155,11 +156,11 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG private String getRawChatFormat(final IUser player) { - for (GroupOptions groupOptions : getGroups(player)) + for (Entry groupOptions : getGroups(player)) { - if (groupOptions != null && groupOptions.getMessageFormat() != null) + if (groupOptions.getValue().getMessageFormat() != null) { - return groupOptions.getMessageFormat(); + return groupOptions.getValue().getMessageFormat(); } } @Cleanup @@ -167,5 +168,28 @@ public class GroupsHolder extends AsyncStorageObjectHolder implements IG settings.acquireReadLock(); return settings.getData().getChat().getDefaultFormat(); } + + @Override + public boolean inGroup(IUser player, String groupname) + { + for (Entry groupOptions : getGroups(player)) + { + if (groupOptions.getKey().equalsIgnoreCase(groupname)) + { + return true; + } + } + return false; + } + + @Override + public String getMainGroup(IUser player) + { + for (Entry groupOptions : getGroups(player)) + { + return groupOptions.getKey(); + } + return "default"; + } } diff --git a/Essentials/src/com/earth2me/essentials/settings/Money.java b/Essentials/src/com/earth2me/essentials/settings/Money.java new file mode 100644 index 000000000..7cc530857 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/settings/Money.java @@ -0,0 +1,17 @@ +package com.earth2me.essentials.settings; + +import com.earth2me.essentials.storage.MapValueType; +import com.earth2me.essentials.storage.StorageObject; +import java.util.HashMap; +import java.util.Map; +import lombok.Data; +import lombok.EqualsAndHashCode; + + +@Data +@EqualsAndHashCode(callSuper = false) +public class Money implements StorageObject +{ + @MapValueType(Double.class) + private Map balances = new HashMap(); +} diff --git a/Essentials/src/com/earth2me/essentials/settings/MoneyHolder.java b/Essentials/src/com/earth2me/essentials/settings/MoneyHolder.java new file mode 100644 index 000000000..5ab216251 --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/settings/MoneyHolder.java @@ -0,0 +1,22 @@ +package com.earth2me.essentials.settings; + +import com.earth2me.essentials.api.IEssentials; +import com.earth2me.essentials.storage.AsyncStorageObjectHolder; +import java.io.File; +import java.io.IOException; + + +public class MoneyHolder extends AsyncStorageObjectHolder +{ + public MoneyHolder(IEssentials ess) + { + super(ess, Money.class); + onReload(); + } + + @Override + public File getStorageFile() throws IOException + { + return new File(ess.getDataFolder(), "economy_npcs.yml"); + } +} diff --git a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java b/Essentials/src/com/earth2me/essentials/textreader/TextInput.java index 0856d5ff6..e870b3194 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java +++ b/Essentials/src/com/earth2me/essentials/textreader/TextInput.java @@ -33,7 +33,7 @@ public class TextInput implements IText file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(user.getName()) + ".txt"); if (!file.exists()) { - file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(user.getGroup()) + ".txt"); + file = new File(ess.getDataFolder(), filename + "_" + Util.sanitizeFileName(ess.getGroups().getMainGroup(user)) + ".txt"); } } catch (InvalidNameException ex) diff --git a/Essentials/src/com/earth2me/essentials/user/User.java b/Essentials/src/com/earth2me/essentials/user/User.java index ecd71f34d..56329e0ed 100644 --- a/Essentials/src/com/earth2me/essentials/user/User.java +++ b/Essentials/src/com/earth2me/essentials/user/User.java @@ -214,9 +214,7 @@ public class User extends UserBase implements IUser @Cleanup final ISettings settings = ess.getSettings(); settings.acquireReadLock(); - @Cleanup final IGroups groups = ess.getGroups(); - groups.acquireReadLock(); // default: {PREFIX}{NICKNAMEPREFIX}{NAME}{SUFFIX} String displayname = settings.getData().getChat().getDisplaynameFormat(); if (settings.getData().getCommands().isDisabled("nick") || nick == null || nick.isEmpty() || nick.equals(getName())) @@ -533,22 +531,6 @@ public class User extends UserBase implements IUser } } - @Override - public String getGroup() - { - return ess.getPermissionsHandler().getGroup(base); - } - - public boolean inGroup(final String group) - { - return ess.getPermissionsHandler().inGroup(base, group); - } - - public boolean canBuild() - { - return ess.getPermissionsHandler().canBuild(base, getGroup()); - } - @Override public Location getHome(String name) throws Exception { diff --git a/Essentials/test/com/earth2me/essentials/EconomyTest.java b/Essentials/test/com/earth2me/essentials/EconomyTest.java index 1fe8c76c5..b7157dd15 100644 --- a/Essentials/test/com/earth2me/essentials/EconomyTest.java +++ b/Essentials/test/com/earth2me/essentials/EconomyTest.java @@ -1,6 +1,5 @@ package com.earth2me.essentials; -import com.earth2me.essentials.api.Economy; import com.earth2me.essentials.api.NoLoanPermittedException; import com.earth2me.essentials.api.UserDoesNotExistException; import com.earth2me.essentials.craftbukkit.DummyOfflinePlayer; @@ -44,37 +43,29 @@ public class EconomyTest extends TestCase public void testEconomy() { // test NPC - assertFalse("NPC does not exists", Economy.playerExists(NPCNAME)); - assertTrue("Create NPC", Economy.createNPC(NPCNAME)); - assertTrue("NPC exists", Economy.playerExists(NPCNAME)); - assertNotNull("NPC can be accessed", ess.getUser(NPCNAME)); + assertFalse("NPC does not exists", ess.getEconomy().playerExists(NPCNAME)); + assertTrue("Create NPC", ess.getEconomy().createNPC(NPCNAME)); + assertTrue("NPC exists", ess.getEconomy().playerExists(NPCNAME)); + assertNull("NPC can not be accessed", ess.getUser(NPCNAME)); try { - Economy.removeNPC(NPCNAME); + ess.getEconomy().removeNPC(NPCNAME); } catch (UserDoesNotExistException ex) { fail(ex.getMessage()); } - assertFalse("NPC can be removed", Economy.playerExists(NPCNAME)); + assertFalse("NPC can be removed",ess.getEconomy().playerExists(NPCNAME)); //test Math try { - assertTrue("Player exists", Economy.playerExists(PLAYERNAME)); - Economy.resetBalance(PLAYERNAME); - assertEquals("Player has no money", 0.0, Economy.getMoney(PLAYERNAME)); - Economy.add(PLAYERNAME, 10.0); - assertEquals("Add money", 10.0, Economy.getMoney(PLAYERNAME)); - Economy.subtract(PLAYERNAME, 5.0); - assertEquals("Subtract money", 5.0, Economy.getMoney(PLAYERNAME)); - Economy.multiply(PLAYERNAME, 2.0); - assertEquals("Multiply money", 10.0, Economy.getMoney(PLAYERNAME)); - Economy.divide(PLAYERNAME, 2.0); - assertEquals("Divide money", 5.0, Economy.getMoney(PLAYERNAME)); - Economy.setMoney(PLAYERNAME, 10.0); - assertEquals("Set money", 10.0, Economy.getMoney(PLAYERNAME)); + assertTrue("Player exists", ess.getEconomy().playerExists(PLAYERNAME)); + ess.getEconomy().resetBalance(PLAYERNAME); + assertEquals("Player has no money", 0.0, ess.getEconomy().getMoney(PLAYERNAME)); + ess.getEconomy().setMoney(PLAYERNAME, 10.0); + assertEquals("Set money", 10.0, ess.getEconomy().getMoney(PLAYERNAME)); } catch (NoLoanPermittedException ex) { @@ -86,20 +77,20 @@ public class EconomyTest extends TestCase } //test Format - assertEquals("Format $1000", "$1000", Economy.format(1000.0)); - assertEquals("Format $10", "$10", Economy.format(10.0)); - assertEquals("Format $10.10", "$10.10", Economy.format(10.10)); - assertEquals("Format $10.10", "$10.10", Economy.format(10.102)); - assertEquals("Format $10.11", "$10.11", Economy.format(10.109)); + assertEquals("Format $1000", "$1000", ess.getEconomy().format(1000.0)); + assertEquals("Format $10", "$10", ess.getEconomy().format(10.0)); + assertEquals("Format $10.10", "$10.10", ess.getEconomy().format(10.10)); + assertEquals("Format $10.10", "$10.10", ess.getEconomy().format(10.102)); + assertEquals("Format $10.11", "$10.11", ess.getEconomy().format(10.109)); //test Exceptions try { - assertTrue("Player exists", Economy.playerExists(PLAYERNAME)); - Economy.resetBalance(PLAYERNAME); - assertEquals("Reset balance", 0.0, Economy.getMoney(PLAYERNAME)); - Economy.subtract(PLAYERNAME, 5.0); + assertTrue("Player exists", ess.getEconomy().playerExists(PLAYERNAME)); + ess.getEconomy().resetBalance(PLAYERNAME); + assertEquals("Reset balance", 0.0, ess.getEconomy().getMoney(PLAYERNAME)); + ess.getEconomy().setMoney(PLAYERNAME, -5.0); fail("Did not throw exception"); } catch (NoLoanPermittedException ex) @@ -112,7 +103,7 @@ public class EconomyTest extends TestCase try { - Economy.resetBalance("UnknownPlayer"); + ess.getEconomy().resetBalance("UnknownPlayer"); fail("Did not throw exception"); } catch (NoLoanPermittedException ex) diff --git a/Essentials/test/com/earth2me/essentials/UserTest.java b/Essentials/test/com/earth2me/essentials/UserTest.java index c3daea21b..69873437d 100644 --- a/Essentials/test/com/earth2me/essentials/UserTest.java +++ b/Essentials/test/com/earth2me/essentials/UserTest.java @@ -80,10 +80,15 @@ public class UserTest extends TestCase assertEquals(user.getMoney(), i); }*/ - public void testGetGroup() + /*public void testGetGroup() { should("return the default group"); IUser user = ess.getUser(base1); - assertEquals(user.getGroup(), "default"); + //assertEquals(user.getGroup(), "default"); + }*/ + + public void testNoop() + { + assertTrue(true); } } diff --git a/Essentials2Compat/src/com/earth2me/essentials/api/Economy.java b/Essentials2Compat/src/com/earth2me/essentials/api/Economy.java new file mode 100644 index 000000000..0aeb7bdc9 --- /dev/null +++ b/Essentials2Compat/src/com/earth2me/essentials/api/Economy.java @@ -0,0 +1,231 @@ +package com.earth2me.essentials.api; + +import com.earth2me.essentials.Util; + + +/** + * Instead of using this api directly, we recommend to use the register plugin: + * http://bit.ly/RegisterMethod + */ +public final class Economy +{ + private Economy() + { + } + private static IEssentials ess; + private static final String noCallBeforeLoad = "Essentials API is called before Essentials is loaded."; + + /** + * Returns the balance of a user + * @param name Name of the user + * @return balance + * @throws UserDoesNotExistException + */ + public static double getMoney(String name) throws UserDoesNotExistException + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + return ess.getEconomy().getMoney(name); + } + + /** + * Sets the balance of a user + * @param name Name of the user + * @param balance The balance you want to set + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void setMoney(String name, double balance) throws UserDoesNotExistException, NoLoanPermittedException + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + ess.getEconomy().setMoney(name, balance); + } + + /** + * Adds money to the balance of a user + * @param name Name of the user + * @param amount The money you want to add + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void add(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException + { + double result = getMoney(name) + amount; + setMoney(name, result); + } + + /** + * Substracts money from the balance of a user + * @param name Name of the user + * @param amount The money you want to substract + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void subtract(String name, double amount) throws UserDoesNotExistException, NoLoanPermittedException + { + double result = getMoney(name) - amount; + setMoney(name, result); + } + + /** + * Divides the balance of a user by a value + * @param name Name of the user + * @param value The balance is divided by this value + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void divide(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException + { + double result = getMoney(name) / value; + setMoney(name, result); + } + + /** + * Multiplies the balance of a user by a value + * @param name Name of the user + * @param value The balance is multiplied by this value + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void multiply(String name, double value) throws UserDoesNotExistException, NoLoanPermittedException + { + double result = getMoney(name) * value; + setMoney(name, result); + } + + /** + * Resets the balance of a user to the starting balance + * @param name Name of the user + * @throws UserDoesNotExistException If a user by that name does not exists + * @throws NoLoanPermittedException If the user is not allowed to have a negative balance + */ + public static void resetBalance(String name) throws UserDoesNotExistException, NoLoanPermittedException + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + ess.getEconomy().resetBalance(name); + } + + /** + * @param name Name of the user + * @param amount The amount of money the user should have + * @return true, if the user has more or an equal amount of money + * @throws UserDoesNotExistException If a user by that name does not exists + */ + public static boolean hasEnough(String name, double amount) throws UserDoesNotExistException + { + return amount <= getMoney(name); + } + + /** + * @param name Name of the user + * @param amount The amount of money the user should have + * @return true, if the user has more money + * @throws UserDoesNotExistException If a user by that name does not exists + */ + public static boolean hasMore(String name, double amount) throws UserDoesNotExistException + { + return amount < getMoney(name); + } + + /** + * @param name Name of the user + * @param amount The amount of money the user should not have + * @return true, if the user has less money + * @throws UserDoesNotExistException If a user by that name does not exists + */ + public static boolean hasLess(String name, double amount) throws UserDoesNotExistException + { + return amount > getMoney(name); + } + + /** + * Test if the user has a negative balance + * @param name Name of the user + * @return true, if the user has a negative balance + * @throws UserDoesNotExistException If a user by that name does not exists + */ + public static boolean isNegative(String name) throws UserDoesNotExistException + { + return getMoney(name) < 0.0; + } + + /** + * Formats the amount of money like all other Essentials functions. + * Example: $100000 or $12345.67 + * @param amount The amount of money + * @return Formatted money + */ + public static String format(double amount) + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + return Util.formatCurrency(amount, ess); + } + + /** + * Test if a player exists to avoid the UserDoesNotExistException + * @param name Name of the user + * @return true, if the user exists + */ + public static boolean playerExists(String name) + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + return ess.getEconomy().playerExists(name); + } + + /** + * Test if a player is a npc + * @param name Name of the player + * @return true, if it's a npc + * @throws UserDoesNotExistException + */ + public static boolean isNPC(String name) throws UserDoesNotExistException + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + return ess.getEconomy().isNPC(name); + } + + /** + * Creates dummy files for a npc, if there is no player yet with that name. + * @param name Name of the player + * @return true, if a new npc was created + */ + public static boolean createNPC(String name) + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + return ess.getEconomy().createNPC(name); + } + + /** + * Deletes a user, if it is marked as npc. + * @param name Name of the player + * @throws UserDoesNotExistException + */ + public static void removeNPC(String name) throws UserDoesNotExistException + { + if (ess == null) + { + throw new RuntimeException(noCallBeforeLoad); + } + ess.getEconomy().removeNPC(name); + } +} diff --git a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java index cc21b6dae..0315408f7 100644 --- a/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java +++ b/EssentialsChat/src/com/earth2me/essentials/chat/EssentialsChatPlayer.java @@ -82,22 +82,14 @@ public abstract class EssentialsChatPlayer implements Listener { event.setMessage(Util.stripColor(event.getMessage())); } - String group = user.getGroup(); + String group = ess.getGroups().getMainGroup(user); String world = user.getWorld().getName(); IGroups groupSettings = ess.getGroups(); - groupSettings.acquireReadLock(); - try - { - event.setFormat(groupSettings.getChatFormat(user).format(new Object[] - { - group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH) - })); - } - finally - { - groupSettings.unlock(); - } + event.setFormat(groupSettings.getChatFormat(user).format(new Object[] + { + group, world, world.substring(0, 1).toUpperCase(Locale.ENGLISH) + })); } diff --git a/EssentialsSigns/src/com/earth2me/essentials/signs/SignKit.java b/EssentialsSigns/src/com/earth2me/essentials/signs/SignKit.java index 9964059ef..b2a766223 100644 --- a/EssentialsSigns/src/com/earth2me/essentials/signs/SignKit.java +++ b/EssentialsSigns/src/com/earth2me/essentials/signs/SignKit.java @@ -52,7 +52,7 @@ public class SignKit extends EssentialsSign { final String kitName = sign.getLine(1).toLowerCase(Locale.ENGLISH); final String group = sign.getLine(2); - if ((!group.isEmpty() && ("§2Everyone".equals(group) || player.inGroup(group))) + if ((!group.isEmpty() && ("§2Everyone".equals(group) || ess.getGroups().inGroup(player, group))) || (group.isEmpty() && KitPermissions.getPermission(kitName).isAuthorized(player))) { final Trade charge = getTrade(sign, 3, ess); diff --git a/EssentialsSigns/src/com/earth2me/essentials/signs/SignProtection.java b/EssentialsSigns/src/com/earth2me/essentials/signs/SignProtection.java index 8bcc5c8bc..c5733a11e 100644 --- a/EssentialsSigns/src/com/earth2me/essentials/signs/SignProtection.java +++ b/EssentialsSigns/src/com/earth2me/essentials/signs/SignProtection.java @@ -34,7 +34,7 @@ public class SignProtection extends EssentialsSign sign.setLine(3, "§4" + username); if (hasAdjacentBlock(sign.getBlock())) { - final SignProtectionState state = isBlockProtected(sign.getBlock(), player, username, true); + final SignProtectionState state = isBlockProtected(sign.getBlock(), player, username, true, ess); if (state == SignProtectionState.NOSIGN || state == SignProtectionState.OWNER || SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player)) { @@ -49,7 +49,7 @@ public class SignProtection extends EssentialsSign @Override protected boolean onSignBreak(final ISign sign, final IUser player, final String username, final IEssentials ess) throws SignException { - final SignProtectionState state = checkProtectionSign(sign, player, username); + final SignProtectionState state = checkProtectionSign(sign, player, username, ess); return state == SignProtectionState.OWNER; } @@ -75,7 +75,7 @@ public class SignProtection extends EssentialsSign private void checkIfSignsAreBroken(final Block block, final IUser player, final String username, final IEssentials ess) { - final Map signs = getConnectedSigns(block, player, username, false); + final Map signs = getConnectedSigns(block, player, username, false, ess); for (Map.Entry entry : signs.entrySet()) { if (entry.getValue() != SignProtectionState.NOSIGN) @@ -91,14 +91,14 @@ public class SignProtection extends EssentialsSign } } - private Map getConnectedSigns(final Block block, final IUser user, final String username, boolean secure) + private Map getConnectedSigns(final Block block, final IUser user, final String username, boolean secure, final IEssentials ess) { final Map signs = new HashMap(); - getConnectedSigns(block, signs, user, username, secure ? 4 : 2); + getConnectedSigns(block, signs, user, username, secure ? 4 : 2, ess); return signs; } - private void getConnectedSigns(final Block block, final Map signs, final IUser user, final String username, final int depth) + private void getConnectedSigns(final Block block, final Map signs, final IUser user, final String username, final int depth, final IEssentials ess) { final Block[] faces = getAdjacentBlocks(block); for (Block b : faces) @@ -108,12 +108,12 @@ public class SignProtection extends EssentialsSign { continue; } - final SignProtectionState check = checkProtectionSign(b, user, username); + final SignProtectionState check = checkProtectionSign(b, user, username, ess); signs.put(loc, check); if (protectedBlocks.contains(b.getType()) && depth > 0) { - getConnectedSigns(b, signs, user, username, depth - 1); + getConnectedSigns(b, signs, user, username, depth - 1, ess); } } } @@ -124,20 +124,20 @@ public class SignProtection extends EssentialsSign NOT_ALLOWED, ALLOWED, NOSIGN, OWNER } - private SignProtectionState checkProtectionSign(final Block block, final IUser user, final String username) + private SignProtectionState checkProtectionSign(final Block block, final IUser user, final String username, final IEssentials ess) { if (block.getType() == Material.SIGN_POST || block.getType() == Material.WALL_SIGN) { final BlockSign sign = new BlockSign(block); if (sign.getLine(0).equalsIgnoreCase(this.getSuccessName())) { - return checkProtectionSign(sign, user, username); + return checkProtectionSign(sign, user, username, ess); } } return SignProtectionState.NOSIGN; } - private SignProtectionState checkProtectionSign(final ISign sign, final IUser user, final String username) + private SignProtectionState checkProtectionSign(final ISign sign, final IUser user, final String username, final IEssentials ess) { if (user == null || username == null) { @@ -154,7 +154,7 @@ public class SignProtection extends EssentialsSign for (int i = 1; i <= 2; i++) { final String line = sign.getLine(i); - if (line.startsWith("(") && line.endsWith(")") && user.inGroup(line.substring(1, line.length() - 1))) + if (line.startsWith("(") && line.endsWith(")") && ess.getGroups().inGroup(user, line.substring(1, line.length() - 1))) { return SignProtectionState.ALLOWED; } @@ -179,9 +179,9 @@ public class SignProtection extends EssentialsSign }; } - public SignProtectionState isBlockProtected(final Block block, final IUser user, final String username, boolean secure) + public SignProtectionState isBlockProtected(final Block block, final IUser user, final String username, boolean secure, final IEssentials ess) { - final Map signs = getConnectedSigns(block, user, username, secure); + final Map signs = getConnectedSigns(block, user, username, secure, ess); SignProtectionState retstate = SignProtectionState.NOSIGN; for (SignProtectionState state : signs.values()) { @@ -251,7 +251,7 @@ public class SignProtection extends EssentialsSign { for (Block adjBlock : getAdjacentBlocks(block)) { - final SignProtectionState state = isBlockProtected(adjBlock, player, username, true); + final SignProtectionState state = isBlockProtected(adjBlock, player, username, true, ess); if ((state == SignProtectionState.ALLOWED || state == SignProtectionState.NOT_ALLOWED) && !SignsPermissions.PROTECTION_OVERRIDE.isAuthorized(player)) @@ -267,7 +267,7 @@ public class SignProtection extends EssentialsSign @Override protected boolean onBlockInteract(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException { - final SignProtectionState state = isBlockProtected(block, player, username, false); + final SignProtectionState state = isBlockProtected(block, player, username, false, ess); if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN || state == SignProtectionState.ALLOWED) { @@ -288,7 +288,7 @@ public class SignProtection extends EssentialsSign @Override protected boolean onBlockBreak(final Block block, final IUser player, final String username, final IEssentials ess) throws SignException { - final SignProtectionState state = isBlockProtected(block, player, username, false); + final SignProtectionState state = isBlockProtected(block, player, username, false, ess); if (state == SignProtectionState.OWNER || state == SignProtectionState.NOSIGN) { @@ -311,7 +311,7 @@ public class SignProtection extends EssentialsSign @Override public boolean onBlockBreak(final Block block, final IEssentials ess) { - final SignProtectionState state = isBlockProtected(block, null, null, false); + final SignProtectionState state = isBlockProtected(block, null, null, false, ess); return state == SignProtectionState.NOSIGN; } @@ -319,7 +319,7 @@ public class SignProtection extends EssentialsSign @Override public boolean onBlockExplode(final Block block, final IEssentials ess) { - final SignProtectionState state = isBlockProtected(block, null, null, false); + final SignProtectionState state = isBlockProtected(block, null, null, false, ess); return state == SignProtectionState.NOSIGN; } @@ -327,7 +327,7 @@ public class SignProtection extends EssentialsSign @Override public boolean onBlockBurn(final Block block, final IEssentials ess) { - final SignProtectionState state = isBlockProtected(block, null, null, false); + final SignProtectionState state = isBlockProtected(block, null, null, false, ess); return state == SignProtectionState.NOSIGN; } @@ -335,7 +335,7 @@ public class SignProtection extends EssentialsSign @Override public boolean onBlockIgnite(final Block block, final IEssentials ess) { - final SignProtectionState state = isBlockProtected(block, null, null, false); + final SignProtectionState state = isBlockProtected(block, null, null, false, ess); return state == SignProtectionState.NOSIGN; } @@ -343,7 +343,7 @@ public class SignProtection extends EssentialsSign @Override public boolean onBlockPush(final Block block, final IEssentials ess) { - final SignProtectionState state = isBlockProtected(block, null, null, false); + final SignProtectionState state = isBlockProtected(block, null, null, false, ess); return state == SignProtectionState.NOSIGN; } diff --git a/EssentialsSigns/src/com/earth2me/essentials/signs/SignWarp.java b/EssentialsSigns/src/com/earth2me/essentials/signs/SignWarp.java index bc16cdf9c..744593e76 100644 --- a/EssentialsSigns/src/com/earth2me/essentials/signs/SignWarp.java +++ b/EssentialsSigns/src/com/earth2me/essentials/signs/SignWarp.java @@ -51,7 +51,7 @@ public class SignWarp extends EssentialsSign final String warpName = sign.getLine(1); final String group = sign.getLine(2); - if ((!group.isEmpty() && ("§2Everyone".equals(group) || player.inGroup(group))) + if ((!group.isEmpty() && ("§2Everyone".equals(group) || ess.getGroups().inGroup(player, group))) || (group.isEmpty() && WarpPermissions.getPermission(warpName).isAuthorized(player))) { final Trade charge = getTrade(sign, 3, ess); diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java index 357b03a2c..b817c854a 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/Commandspawn.java @@ -50,7 +50,7 @@ public class Commandspawn extends EssentialsCommand private void respawn(final IUser user, final Trade charge) throws Exception { final SpawnStorage spawns = (SpawnStorage)this.module; - final Location spawn = spawns.getSpawn(user.getGroup()); + final Location spawn = spawns.getSpawn(ess.getGroups().getMainGroup(user)); user.getTeleport().teleport(spawn, charge, TeleportCause.COMMAND); } } diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java index bab6bd822..5dd913ee7 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/EssentialsSpawnPlayerListener.java @@ -60,7 +60,7 @@ public class EssentialsSpawnPlayerListener implements Listener return; } } - final Location spawn = spawns.getSpawn(user.getGroup()); + final Location spawn = spawns.getSpawn(ess.getGroups().getMainGroup(user)); if (spawn != null) { event.setRespawnLocation(spawn); diff --git a/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java index 98545eaf0..6171ed7ac 100644 --- a/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java +++ b/EssentialsSpawn/src/com/earth2me/essentials/spawn/SpawnStorage.java @@ -154,7 +154,7 @@ public class SpawnStorage extends AsyncStorageObjectHolder implements IE acquireReadLock(); try { - return getData().getNewPlayerAnnouncement().replace('&', '§').replace("§§", "&").replace("{PLAYER}", user.getDisplayName()).replace("{DISPLAYNAME}", user.getDisplayName()).replace("{GROUP}", user.getGroup()).replace("{USERNAME}", user.getName()).replace("{ADDRESS}", user.getAddress().toString()); + return getData().getNewPlayerAnnouncement().replace('&', '§').replace("§§", "&").replace("{PLAYER}", user.getDisplayName()).replace("{DISPLAYNAME}", user.getDisplayName()).replace("{GROUP}", ess.getGroups().getMainGroup(user)).replace("{USERNAME}", user.getName()).replace("{ADDRESS}", user.getAddress().toString()); } finally { -- cgit v1.2.3