diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index b5c819559..c99f381a6 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -58,14 +58,14 @@ public class Essentials extends JavaPlugin return settings; } - public void setupForTesting() throws IOException, InvalidDescriptionException + public void setupForTesting(Server server) throws IOException, InvalidDescriptionException { File dataFolder = File.createTempFile("essentialstest", ""); dataFolder.delete(); dataFolder.mkdir(); logger.log(Level.INFO, Util.i18n("usingTempFolderForTesting")); logger.log(Level.INFO, dataFolder.toString()); - this.initialize(null, null, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null); + this.initialize(null, server, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null); settings = new Settings(dataFolder); setStatic(); } diff --git a/Essentials/src/com/earth2me/essentials/OfflinePlayer.java b/Essentials/src/com/earth2me/essentials/OfflinePlayer.java index 5d7104914..ab513d87b 100644 --- a/Essentials/src/com/earth2me/essentials/OfflinePlayer.java +++ b/Essentials/src/com/earth2me/essentials/OfflinePlayer.java @@ -25,6 +25,7 @@ public class OfflinePlayer implements Player { private final String name; private Location location = new Location(null, 0, 0, 0, 0, 0); + private World world = null; public OfflinePlayer(String name) { @@ -132,7 +133,13 @@ public class OfflinePlayer implements Player public World getWorld() { - return null; + return world; + } + + public void setLocation(Location loc) + { + location = loc; + world = loc.getWorld(); } public void teleportTo(Location lctn) diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index e53162b8d..bcbed1064 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -8,6 +8,7 @@ import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.text.DecimalFormat; +import java.text.DecimalFormatSymbols; import java.text.MessageFormat; import java.util.Calendar; import java.util.Enumeration; @@ -270,13 +271,13 @@ public class Util } return isBlockAboveAir(world, x, y, z); } - private static DecimalFormat df = new DecimalFormat("#0.##"); + private static DecimalFormat df = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.US)); public static String formatCurrency(double value) { String str = Essentials.getStatic().getSettings().getCurrencySymbol()+df.format(value); - if (str.endsWith(".")) { - return str.substring(0, str.length()-1); + if (str.endsWith(".00")) { + str = str.substring(0, str.length()-3); } return str; } diff --git a/Essentials/src/com/earth2me/essentials/api/Economy.java b/Essentials/src/com/earth2me/essentials/api/Economy.java index 82b3cda5c..95962d7a2 100644 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/com/earth2me/essentials/api/Economy.java @@ -18,6 +18,10 @@ public class Economy private static void createNPCFile(String name) { File folder = new File(Essentials.getStatic().getDataFolder(), "userdata"); + if (!folder.exists()) + { + folder.mkdirs(); + } EssentialsConf npcConfig = new EssentialsConf(new File(folder, Util.sanitizeFileName(name) + ".yml")); npcConfig.load(); npcConfig.setProperty("npc", true); @@ -28,6 +32,10 @@ public class Economy private static void deleteNPC(String name) { File folder = new File(Essentials.getStatic().getDataFolder(), "userdata"); + if (!folder.exists()) + { + folder.mkdirs(); + } File config = new File(folder, Util.sanitizeFileName(name) + ".yml"); EssentialsConf npcConfig = new EssentialsConf(config); npcConfig.load(); @@ -235,13 +243,16 @@ public class Economy /** * 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 void createNPC(String name) + public static boolean createNPC(String name) { User user = getUserByName(name); if (user == null) { createNPCFile(name); + return true; } + return false; } /** diff --git a/Essentials/src/config.yml b/Essentials/src/config.yml index ddafda644..8fffa244b 100644 --- a/Essentials/src/config.yml +++ b/Essentials/src/config.yml @@ -155,6 +155,7 @@ restricted-commands: - unbanip - togglejail - setjail + - eco.loan # Note: All items MUST be followed by a quantity! # Times are measured in seconds. diff --git a/Essentials/test/com/earth2me/essentials/EconomyTest.java b/Essentials/test/com/earth2me/essentials/EconomyTest.java new file mode 100644 index 000000000..575cff81a --- /dev/null +++ b/Essentials/test/com/earth2me/essentials/EconomyTest.java @@ -0,0 +1,124 @@ +package com.earth2me.essentials; + +import com.earth2me.essentials.api.Economy; +import com.earth2me.essentials.api.NoLoanPermittedException; +import com.earth2me.essentials.api.UserDoesNotExistException; +import java.io.IOException; +import java.util.logging.Level; +import java.util.logging.Logger; +import junit.framework.TestCase; +import org.bukkit.plugin.InvalidDescriptionException; + + +public class EconomyTest extends TestCase +{ + private OfflinePlayer base1; + private Essentials ess; + + public EconomyTest(String testName) + { + super(testName); + ess = new Essentials(); + FakeServer server = new FakeServer(); + try + { + ess.setupForTesting(server); + } + catch (InvalidDescriptionException ex) + { + fail("InvalidDescriptionException"); + } + catch (IOException ex) + { + fail("IOException"); + } + base1 = new OfflinePlayer("TestPlayer1"); + server.addPlayer(base1); + } + + // only one big test, since we use static instances + public void testEconomy() + { + // test NPC + String npcName = "npc1"; + assertFalse(Economy.playerExists(npcName)); + assertTrue(Economy.createNPC(npcName)); + assertTrue(Economy.playerExists(npcName)); + assertNotNull(ess.getOfflineUser(npcName)); + try + { + Economy.removeNPC(npcName); + } + catch (UserDoesNotExistException ex) + { + fail(ex.getMessage()); + } + assertFalse(Economy.playerExists(npcName)); + + //test Math + try + { + String playerName = "TestPlayer1"; + assertTrue(Economy.playerExists(playerName)); + Economy.resetBalance(playerName); + assertEquals(0.0, Economy.getMoney(playerName)); + Economy.add(playerName, 10.0); + assertEquals(10.0, Economy.getMoney(playerName)); + Economy.subtract(playerName, 5.0); + assertEquals(5.0, Economy.getMoney(playerName)); + Economy.multiply(playerName, 2.0); + assertEquals(10.0, Economy.getMoney(playerName)); + Economy.divide(playerName, 2.0); + assertEquals(5.0, Economy.getMoney(playerName)); + Economy.setMoney(playerName, 10.0); + assertEquals(10.0, Economy.getMoney(playerName)); + } + catch (NoLoanPermittedException ex) + { + fail(ex.getMessage()); + } + catch (UserDoesNotExistException ex) + { + fail(ex.getMessage()); + } + + //test Format + assertEquals("$10", Economy.format(10.0)); + assertEquals("$10.10", Economy.format(10.10)); + assertEquals("$10.10", Economy.format(10.102)); + assertEquals("$10.11", Economy.format(10.109)); + + + //test Exceptions + try + { + String playerName = "TestPlayer1"; + assertTrue(Economy.playerExists(playerName)); + Economy.resetBalance(playerName); + assertEquals(0.0, Economy.getMoney(playerName)); + Economy.subtract(playerName, 5.0); + fail(); + } + catch (NoLoanPermittedException ex) + { + } + catch (UserDoesNotExistException ex) + { + fail(ex.getMessage()); + } + + try + { + String playerName = "UnknownPlayer"; + Economy.resetBalance(playerName); + fail(); + } + catch (NoLoanPermittedException ex) + { + fail(ex.getMessage()); + } + catch (UserDoesNotExistException ex) + { + } + } +} diff --git a/Essentials/test/com/earth2me/essentials/FakeServer.java b/Essentials/test/com/earth2me/essentials/FakeServer.java new file mode 100644 index 000000000..a867c116b --- /dev/null +++ b/Essentials/test/com/earth2me/essentials/FakeServer.java @@ -0,0 +1,200 @@ +package com.earth2me.essentials; + +import com.avaje.ebean.config.ServerConfig; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Logger; +import org.bukkit.Location; +import org.bukkit.Server; +import org.bukkit.World; +import org.bukkit.World.Environment; +import org.bukkit.command.CommandSender; +import org.bukkit.command.PluginCommand; +import org.bukkit.entity.Player; +import org.bukkit.inventory.Recipe; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.ServicesManager; +import org.bukkit.scheduler.BukkitScheduler; + + +public class FakeServer implements Server +{ + private List players = new ArrayList(); + private List worlds = new ArrayList(); + + public String getName() + { + return "Test Server"; + } + + public String getVersion() + { + return "1.0"; + } + + public Player[] getOnlinePlayers() + { + return players.toArray(new Player[0]); + } + + public void setOnlinePlayers(List players) + { + this.players = players; + } + + public int getMaxPlayers() + { + return 100; + } + + public int getPort() + { + return 25565; + } + + public String getIp() + { + return "127.0.0.1"; + } + + public String getServerName() + { + return "Test Server"; + } + + public String getServerId() + { + return "Test Server"; + } + + public int broadcastMessage(String string) + { + int i = 0; + for (Player player : players) + { + player.sendMessage(string); + i++; + } + return i; + } + + public String getUpdateFolder() + { + return "update"; + } + + public Player getPlayer(String string) + { + for (Player player : players) + { + if (player.getName().equalsIgnoreCase(string)) + { + return player; + } + } + return null; + } + + public List matchPlayer(String string) + { + List matches = new ArrayList(); + for (Player player : players) + { + if (player.getName().substring(0, Math.min(player.getName().length(), string.length())).equalsIgnoreCase(string)) + { + matches.add(player); + } + } + return matches; + } + + public PluginManager getPluginManager() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public BukkitScheduler getScheduler() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public ServicesManager getServicesManager() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public List getWorlds() + { + return worlds; + } + + public World createWorld(String string, Environment e) + { + World w = new FakeWorld(string, e); + worlds.add(w); + return w; + } + + public World createWorld(String string, Environment e, long l) + { + World w = new FakeWorld(string, e); + worlds.add(w); + return w; + } + + public World getWorld(String string) + { + for (World world : worlds) + { + if (world.getName().equalsIgnoreCase(string)) { + return world; + } + } + return null; + } + + public void reload() + { + } + + public Logger getLogger() + { + return Logger.getLogger("Minecraft"); + } + + public PluginCommand getPluginCommand(String string) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void savePlayers() + { + } + + public boolean dispatchCommand(CommandSender cs, String string) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void configureDbConfig(ServerConfig sc) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean addRecipe(Recipe recipe) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + void addPlayer(Player base1) + { + players.add(base1); + } + + OfflinePlayer createPlayer(String name) + { + OfflinePlayer player = new OfflinePlayer(name); + player.setLocation(new Location(worlds.get(0), 0, 0, 0, 0, 0)); + return player; + } +} diff --git a/Essentials/test/com/earth2me/essentials/FakeWorld.java b/Essentials/test/com/earth2me/essentials/FakeWorld.java new file mode 100644 index 000000000..16472c56c --- /dev/null +++ b/Essentials/test/com/earth2me/essentials/FakeWorld.java @@ -0,0 +1,326 @@ +package com.earth2me.essentials; + +import java.util.List; +import org.bukkit.BlockChangeDelegate; +import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.TreeType; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Arrow; +import org.bukkit.entity.Boat; +import org.bukkit.entity.CreatureType; +import org.bukkit.entity.Entity; +import org.bukkit.entity.Item; +import org.bukkit.entity.LightningStrike; +import org.bukkit.entity.LivingEntity; +import org.bukkit.entity.Minecart; +import org.bukkit.entity.Player; +import org.bukkit.entity.PoweredMinecart; +import org.bukkit.entity.StorageMinecart; +import org.bukkit.inventory.ItemStack; +import org.bukkit.util.Vector; + + +public class FakeWorld implements World +{ + + private String name; + private Environment env; + FakeWorld(String string, Environment environment) + { + this.name = string; + this.env = environment; + } + + public Block getBlockAt(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Block getBlockAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getBlockTypeIdAt(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getBlockTypeIdAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getHighestBlockYAt(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getHighestBlockYAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Chunk getChunkAt(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Chunk getChunkAt(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Chunk getChunkAt(Block block) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isChunkLoaded(Chunk chunk) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Chunk[] getLoadedChunks() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void loadChunk(Chunk chunk) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isChunkLoaded(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void loadChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean loadChunk(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean unloadChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean unloadChunk(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean unloadChunk(int i, int i1, boolean bln, boolean bln1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean unloadChunkRequest(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean unloadChunkRequest(int i, int i1, boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean regenerateChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean refreshChunk(int i, int i1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Item dropItem(Location lctn, ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Item dropItemNaturally(Location lctn, ItemStack is) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Arrow spawnArrow(Location lctn, Vector vector, float f, float f1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean generateTree(Location lctn, TreeType tt) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean generateTree(Location lctn, TreeType tt, BlockChangeDelegate bcd) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Minecart spawnMinecart(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public StorageMinecart spawnStorageMinecart(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public PoweredMinecart spawnPoweredMinecart(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Boat spawnBoat(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public LivingEntity spawnCreature(Location lctn, CreatureType ct) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public LightningStrike strikeLightning(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public LightningStrike strikeLightningEffect(Location lctn) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public List getEntities() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public List getLivingEntities() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public List getPlayers() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public String getName() + { + return name; + } + + public long getId() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Location getSpawnLocation() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean setSpawnLocation(int i, int i1, int i2) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public long getTime() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setTime(long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public long getFullTime() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setFullTime(long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean hasStorm() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setStorm(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getWeatherDuration() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setWeatherDuration(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean isThundering() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setThundering(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public int getThunderDuration() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setThunderDuration(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public Environment getEnvironment() + { + return env; + } + + public long getSeed() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public boolean getPVP() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void setPVP(boolean bln) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + public void save() + { + throw new UnsupportedOperationException("Not supported yet."); + } + +} diff --git a/Essentials/test/com/earth2me/essentials/UserTest.java b/Essentials/test/com/earth2me/essentials/UserTest.java index 6b7a651c3..1acaeb55a 100644 --- a/Essentials/test/com/earth2me/essentials/UserTest.java +++ b/Essentials/test/com/earth2me/essentials/UserTest.java @@ -2,6 +2,9 @@ package com.earth2me.essentials; import java.io.IOException; import junit.framework.TestCase; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.World.Environment; import org.bukkit.plugin.InvalidDescriptionException; @@ -9,14 +12,17 @@ public class UserTest extends TestCase { private OfflinePlayer base1; private Essentials ess; + private FakeServer server; public UserTest(String testName) { super(testName); ess = new Essentials(); + server = new FakeServer(); + server.createWorld("testWorld", Environment.NORMAL); try { - ess.setupForTesting(); + ess.setupForTesting(server); } catch (InvalidDescriptionException ex) { @@ -26,7 +32,8 @@ public class UserTest extends TestCase { fail("IOException"); } - base1 = new OfflinePlayer("TestPlayer1"); + base1 = server.createPlayer("testPlayer1"); + server.addPlayer(base1); } private void should(String what) @@ -34,36 +41,28 @@ public class UserTest extends TestCase System.out.println(getName() + " should " + what); } - @Override - protected void setUp() throws Exception - { - super.setUp(); - } - - @Override - protected void tearDown() throws Exception - { - super.tearDown(); - } - public void testUpdate() { - should("update an existing player with the same name, rather than creating a new player"); - ess.getUser(base1); - //int size1 = User.size(); - OfflinePlayer base1alt = new OfflinePlayer(base1.getName()); + OfflinePlayer base1alt = server.createPlayer(base1.getName()); assertEquals(base1alt, ess.getUser(base1alt).getBase()); - //assertTrue(size1 == User.size()); + } + + public void testHome() + { + User user = ess.getUser(base1); + Location loc = base1.getLocation(); + user.setHome(); + OfflinePlayer base2 = server.createPlayer(base1.getName()); + User user2 = ess.getUser(base2); + Location home = user2.getHome(); + assertEquals(loc.getWorld().getName(), home.getWorld().getName()); + assertEquals(loc.getX(), home.getX()); + assertEquals(loc.getY(), home.getY()); + assertEquals(loc.getZ(), home.getZ()); + assertEquals(loc.getYaw(), home.getYaw()); + assertEquals(loc.getPitch(), home.getPitch()); } - /*public void testHome() throws Exception - { - should("return the home set by setHome"); - Location home = new Location(null, 1, 2, 3, 4, 5); - User user = User.get(base1); - user.setHome(home); - assertEquals(user.getHome(), home); - }*/ public void testMoney() { should("properly set, take, give, and get money");