summaryrefslogtreecommitdiffstats
path: root/Essentials2Compat
diff options
context:
space:
mode:
authorementalo <ementalodev@gmx.co.uk>2012-07-17 12:26:55 +0100
committerementalo <ementalodev@gmx.co.uk>2012-07-17 14:21:03 +0100
commita661bce7b3de3f53e2b7b79c1283f0affa6fe9c3 (patch)
tree2aa10b6300f6c8d3cb2b298c124180fade74857a /Essentials2Compat
parent3c385e69271dfe8530fadc3f67e13ee495e4b0e1 (diff)
parent9f05e43ecf8e6e1a8fcaef757678e762f0d82573 (diff)
downloadEssentials-a661bce7b3de3f53e2b7b79c1283f0affa6fe9c3.tar
Essentials-a661bce7b3de3f53e2b7b79c1283f0affa6fe9c3.tar.gz
Essentials-a661bce7b3de3f53e2b7b79c1283f0affa6fe9c3.tar.lz
Essentials-a661bce7b3de3f53e2b7b79c1283f0affa6fe9c3.tar.xz
Essentials-a661bce7b3de3f53e2b7b79c1283f0affa6fe9c3.zip
Merge of server-layer branch
Diffstat (limited to 'Essentials2Compat')
-rw-r--r--Essentials2Compat/src/com/earth2me/essentials/Economy.java245
-rw-r--r--Essentials2Compat/src/com/earth2me/essentials/Essentials.java8
-rw-r--r--Essentials2Compat/src/com/earth2me/essentials/EssentialsConf.java101
-rw-r--r--Essentials2Compat/src/com/earth2me/essentials/EssentialsUpgrade.java116
-rw-r--r--Essentials2Compat/src/plugin.yml1
5 files changed, 396 insertions, 75 deletions
diff --git a/Essentials2Compat/src/com/earth2me/essentials/Economy.java b/Essentials2Compat/src/com/earth2me/essentials/Economy.java
new file mode 100644
index 000000000..bf1c9a61d
--- /dev/null
+++ b/Essentials2Compat/src/com/earth2me/essentials/Economy.java
@@ -0,0 +1,245 @@
+package com.earth2me.essentials;
+
+import net.ess3.api.IEssentials;
+import net.ess3.api.NoLoanPermittedException;
+import net.ess3.api.UserDoesNotExistException;
+import net.ess3.utils.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 net.ess3.api.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 net.ess3.api.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.displayCurrency(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/Essentials2Compat/src/com/earth2me/essentials/Essentials.java b/Essentials2Compat/src/com/earth2me/essentials/Essentials.java
index dc612e8d6..840ef7d37 100644
--- a/Essentials2Compat/src/com/earth2me/essentials/Essentials.java
+++ b/Essentials2Compat/src/com/earth2me/essentials/Essentials.java
@@ -9,14 +9,8 @@ public class Essentials extends JavaPlugin
@Override
public void onEnable()
{
- Bukkit.getLogger().info("You can remove this compatibility plugin, when all plugins are updated to Essentials 3");
+ Bukkit.getLogger().info("You can remove this compatibility plugin, when all plugins are updated to Essentials-3");
//TODO: Update files to new 3.0 format
//TODO: Move Eco Api here
}
-
- @Override
- public void onDisable()
- {
- throw new UnsupportedOperationException("Not supported yet.");
- }
}
diff --git a/Essentials2Compat/src/com/earth2me/essentials/EssentialsConf.java b/Essentials2Compat/src/com/earth2me/essentials/EssentialsConf.java
index dbdc9483b..cbc51eddd 100644
--- a/Essentials2Compat/src/com/earth2me/essentials/EssentialsConf.java
+++ b/Essentials2Compat/src/com/earth2me/essentials/EssentialsConf.java
@@ -1,7 +1,14 @@
package com.earth2me.essentials;
import static net.ess3.I18n._;
+import com.google.common.io.Files;
import java.io.*;
+import java.nio.ByteBuffer;
+import java.nio.CharBuffer;
+import java.nio.channels.FileChannel;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.CoderResult;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
@@ -25,6 +32,7 @@ public class EssentialsConf extends YamlConfiguration
private transient File configFile;
private transient String templateName = null;
private transient Class<?> resourceClass = EssentialsConf.class;
+ private static final Charset UTF8 = Charset.forName("UTF-8");
public EssentialsConf(final File configFile)
{
@@ -32,7 +40,7 @@ public class EssentialsConf extends YamlConfiguration
this.configFile = configFile;
}
- public void load()
+ public synchronized void load()
{
configFile = configFile.getAbsoluteFile();
if (!configFile.getParentFile().exists())
@@ -105,15 +113,48 @@ public class EssentialsConf extends YamlConfiguration
try
{
- super.load(configFile);
- }
- catch (FileNotFoundException ex)
- {
- LOGGER.log(Level.SEVERE, null, ex);
+ final FileInputStream inputStream = new FileInputStream(configFile);
+ try
+ {
+ final FileChannel channel = inputStream.getChannel();
+ final ByteBuffer buffer = ByteBuffer.allocate((int)configFile.length());
+ channel.read(buffer);
+ buffer.rewind();
+ final CharBuffer data = CharBuffer.allocate((int)configFile.length());
+ CharsetDecoder decoder = UTF8.newDecoder();
+ CoderResult result = decoder.decode(buffer, data, true);
+ if (result.isError())
+ {
+ buffer.rewind();
+ data.clear();
+ LOGGER.log(Level.INFO, "File " + configFile.getAbsolutePath().toString() + " is not utf-8 encoded, trying " + Charset.defaultCharset().displayName());
+ decoder = Charset.defaultCharset().newDecoder();
+ result = decoder.decode(buffer, data, true);
+ if (result.isError())
+ {
+ throw new InvalidConfigurationException("Invalid Characters in file " + configFile.getAbsolutePath().toString());
+ }
+ else
+ {
+ decoder.flush(data);
+ }
+ }
+ else
+ {
+ decoder.flush(data);
+ }
+ final int end = data.position();
+ data.rewind();
+ super.loadFromString(data.subSequence(0, end).toString());
+ }
+ finally
+ {
+ inputStream.close();
+ }
}
catch (IOException ex)
{
- LOGGER.log(Level.SEVERE, null, ex);
+ LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
}
catch (InvalidConfigurationException ex)
{
@@ -302,27 +343,55 @@ public class EssentialsConf extends YamlConfiguration
return def;
}
}
-
- public void save() {
+
+ public void save()
+ {
try
{
save(configFile);
}
catch (IOException ex)
{
- LOGGER.log(Level.SEVERE, null, ex);
+ LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
}
}
-
- public Object getProperty(String path) {
+
+ @Override
+ public synchronized void save(final File file) throws IOException
+ {
+ if (file == null)
+ {
+ throw new IllegalArgumentException("File cannot be null");
+ }
+
+ Files.createParentDirs(file);
+
+ final String data = saveToString();
+
+ final OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file), UTF8);
+
+ try
+ {
+ writer.write(data);
+ }
+ finally
+ {
+ writer.close();
+ }
+ }
+
+ public Object getProperty(String path)
+ {
return get(path);
}
-
- public void setProperty(String path, Object object) {
+
+ public void setProperty(String path, Object object)
+ {
set(path, object);
}
-
- public void removeProperty(String path) {
+
+ public void removeProperty(String path)
+ {
set(path, null);
}
}
diff --git a/Essentials2Compat/src/com/earth2me/essentials/EssentialsUpgrade.java b/Essentials2Compat/src/com/earth2me/essentials/EssentialsUpgrade.java
index 859c08982..605262151 100644
--- a/Essentials2Compat/src/com/earth2me/essentials/EssentialsUpgrade.java
+++ b/Essentials2Compat/src/com/earth2me/essentials/EssentialsUpgrade.java
@@ -387,7 +387,6 @@ public class EssentialsUpgrade
* ((Number)vals.get(4)).floatValue())); } } } } usersFile.renameTo(new File(usersFile.getAbsolutePath() + ".old"));
* }
*/
-
private void convertWarps()
{
final File warpsFolder = new File(ess.getDataFolder(), "warps");
@@ -462,56 +461,56 @@ public class EssentialsUpgrade
}
/*final File warpFile = new File(ess.getDataFolder(), "warps.txt");
- if (warpFile.exists())
- {
- try
- {
- final BufferedReader rx = new BufferedReader(new FileReader(warpFile));
- try
- {
- for (String[] parts = new String[0]; rx.ready(); parts = rx.readLine().split(":"))
- {
- if (parts.length < 6)
- {
- continue;
- }
- final String name = parts[0];
- final double x = Double.parseDouble(parts[1].trim());
- final double y = Double.parseDouble(parts[2].trim());
- final double z = Double.parseDouble(parts[3].trim());
- final float yaw = Float.parseFloat(parts[4].trim());
- final float pitch = Float.parseFloat(parts[5].trim());
- if (name.isEmpty())
- {
- continue;
- }
- World w = null;
- for (World world : ess.getServer().getWorlds())
- {
- if (world.getEnvironment() != World.Environment.NETHER)
- {
- w = world;
- break;
- }
- }
- final Location loc = new Location(name, x, y, z, yaw, pitch);
- ess.getWarps().setWarp(name, loc);
- if (!warpFile.renameTo(new File(ess.getDataFolder(), "warps.txt.old")))
- {
- throw new Exception(_("fileRenameError", "warps.txt"));
- }
- }
- }
- finally
- {
- rx.close();
- }
- }
- catch (Exception ex)
- {
- LOGGER.log(Level.SEVERE, null, ex);
- }
- }*/
+ if (warpFile.exists())
+ {
+ try
+ {
+ final BufferedReader rx = new BufferedReader(new FileReader(warpFile));
+ try
+ {
+ for (String[] parts = new String[0]; rx.ready(); parts = rx.readLine().split(":"))
+ {
+ if (parts.length < 6)
+ {
+ continue;
+ }
+ final String name = parts[0];
+ final double x = Double.parseDouble(parts[1].trim());
+ final double y = Double.parseDouble(parts[2].trim());
+ final double z = Double.parseDouble(parts[3].trim());
+ final float yaw = Float.parseFloat(parts[4].trim());
+ final float pitch = Float.parseFloat(parts[5].trim());
+ if (name.isEmpty())
+ {
+ continue;
+ }
+ World w = null;
+ for (World world : ess.getServer().getWorlds())
+ {
+ if (world.getEnvironment() != World.Environment.NETHER)
+ {
+ w = world;
+ break;
+ }
+ }
+ final Location loc = new Location(name, x, y, z, yaw, pitch);
+ ess.getWarps().setWarp(name, loc);
+ if (!warpFile.renameTo(new File(ess.getDataFolder(), "warps.txt.old")))
+ {
+ throw new Exception(_("fileRenameError", "warps.txt"));
+ }
+ }
+ }
+ finally
+ {
+ rx.close();
+ }
+ }
+ catch (Exception ex)
+ {
+ LOGGER.log(Level.SEVERE, null, ex);
+ }
+ }*/
}
/*
@@ -533,7 +532,7 @@ public class EssentialsUpgrade
* ess.getDataFolder().getParentFile().getParentFile(); final File worldDirectory = new File(bukkitDirectory, name);
* if (worldDirectory.exists() && worldDirectory.isDirectory()) { return new FakeWorld(worldDirectory.getName(),
* World.Environment.NORMAL); } return null;
- }
+ }
*/
public StoredLocation getFakeLocation(EssentialsConf config, String path)
{
@@ -691,6 +690,18 @@ public class EssentialsUpgrade
doneFile.save();
}
+ private void warnMetrics()
+ {
+ if (doneFile.getBoolean("warnMetrics", false))
+ {
+ return;
+ }
+ //todo - metrics
+ // ess.getSettings().setMetricsEnabled(false);
+ doneFile.setProperty("warnMetrics", true);
+ doneFile.save();
+ }
+
public void beforeSettings()
{
if (!ess.getDataFolder().exists())
@@ -714,5 +725,6 @@ public class EssentialsUpgrade
deleteOldItemsCsv();
updateSpawnsToNewSpawnsConfig();
updateJailsToNewJailsConfig();
+ warnMetrics();
}
}
diff --git a/Essentials2Compat/src/plugin.yml b/Essentials2Compat/src/plugin.yml
index fc318c962..bc721f650 100644
--- a/Essentials2Compat/src/plugin.yml
+++ b/Essentials2Compat/src/plugin.yml
@@ -4,3 +4,4 @@ version: 2.9
website: http://tiny.cc/EssentialsWiki
description: Compatibility plugin for older plugins
authors: [Zenexer, ementalo, Aelux, Brettflan, KimKandor, snowleo, ceulemans, Xeology, KHobbits]
+depend: [Essentials-3]