mirror of
https://github.com/essentials/Essentials.git
synced 2025-08-17 03:54:19 +02:00
Merge of server-layer branch
This commit is contained in:
245
Essentials2Compat/src/com/earth2me/essentials/Economy.java
Normal file
245
Essentials2Compat/src/com/earth2me/essentials/Economy.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
@@ -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);
|
||||
}
|
||||
}
|
||||
|
@@ -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();
|
||||
}
|
||||
}
|
||||
|
@@ -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]
|
||||
|
Reference in New Issue
Block a user