diff options
12 files changed, 567 insertions, 292 deletions
diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 7f85ab2c3..663f96b7b 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -35,13 +35,13 @@ import java.math.BigInteger; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.bukkit.command.PluginCommand; -import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.entity.Player; import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Type; import org.bukkit.event.server.ServerListener; import org.bukkit.plugin.*; import org.bukkit.plugin.java.*; +import org.bukkit.scheduler.BukkitScheduler; public class Essentials extends JavaPlugin implements IEssentials @@ -64,7 +64,9 @@ public class Essentials extends JavaPlugin implements IEssentials private transient final static boolean enableErrorLogging = false; private transient final EssentialsErrorHandler errorHandler = new EssentialsErrorHandler(); private transient IPermissionsHandler permissionsHandler; + private transient UserMap userMap; + @Override public ISettings getSettings() { return settings; @@ -85,10 +87,12 @@ public class Essentials extends JavaPlugin implements IEssentials LOGGER.log(Level.INFO, dataFolder.toString()); this.initialize(null, server, new PluginDescriptionFile(new FileReader(new File("src" + File.separator + "plugin.yml"))), dataFolder, null, null); settings = new Settings(this); + userMap = new UserMap(this); permissionsHandler = new ConfigPermissionsHandler(this); Economy.setEss(this); } + @Override public void onEnable() { final String[] javaversion = System.getProperty("java.version").split("\\.", 3); @@ -100,13 +104,15 @@ public class Essentials extends JavaPlugin implements IEssentials { LOGGER.addHandler(errorHandler); } - EssentialsUpgrade upgrade = new EssentialsUpgrade(this.getDescription().getVersion(), this); + final EssentialsUpgrade upgrade = new EssentialsUpgrade(this); upgrade.beforeSettings(); confList = new ArrayList<IConf>(); settings = new Settings(this); confList.add(settings); upgrade.afterSettings(); Util.updateLocale(settings.getLocale(), this); + userMap = new UserMap(this); + confList.add(userMap); spawn = new Spawn(getServer(), this.getDataFolder()); confList.add(spawn); warps = new Warps(getServer(), this.getDataFolder()); @@ -212,12 +218,14 @@ public class Essentials extends JavaPlugin implements IEssentials LOGGER.info(Util.format("loadinfo", this.getDescription().getName(), this.getDescription().getVersion(), Util.joinList(this.getDescription().getAuthors()))); } + @Override public void onDisable() { Trade.closeLog(); LOGGER.removeHandler(errorHandler); } + @Override public void reload() { Trade.closeLog(); @@ -238,12 +246,14 @@ public class Essentials extends JavaPlugin implements IEssentials getConfiguration().load(); } - public String[] getMotd(CommandSender sender, String def) + @Override + public String[] getMotd(final CommandSender sender, final String def) { return getLines(sender, "motd", def); } - public String[] getLines(CommandSender sender, String node, String def) + @Override + public String[] getLines(final CommandSender sender, final String node, final String def) { List<String> lines = (List<String>)getConfiguration().getProperty(node); if (lines == null) @@ -348,12 +358,13 @@ public class Essentials extends JavaPlugin implements IEssentials } @Override - public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) + public boolean onCommand(final CommandSender sender, final Command command, final String commandLabel, final String[] args) { return onCommandEssentials(sender, command, commandLabel, args, Essentials.class.getClassLoader(), "com.earth2me.essentials.commands.Command", "essentials."); } - public boolean onCommandEssentials(CommandSender sender, Command command, String commandLabel, String[] args, ClassLoader classLoader, String commandPath, String permissionPrefix) + @Override + public boolean onCommandEssentials(final CommandSender sender, final Command command, final String commandLabel, final String[] args, final ClassLoader classLoader, final String commandPath, final String permissionPrefix) { // Allow plugins to override the command via onCommand if (!getSettings().isCommandOverridden(command.getName()) && !commandLabel.startsWith("e")) @@ -365,7 +376,7 @@ public class Essentials extends JavaPlugin implements IEssentials continue; } - PluginDescriptionFile desc = p.getDescription(); + final PluginDescriptionFile desc = p.getDescription(); if (desc == null) { continue; @@ -376,7 +387,7 @@ public class Essentials extends JavaPlugin implements IEssentials continue; } - PluginCommand pc = getServer().getPluginCommand(desc.getName() + ":" + commandLabel); + final PluginCommand pc = getServer().getPluginCommand(desc.getName() + ":" + commandLabel); if (pc != null) { return pc.execute(sender, commandLabel, args); @@ -462,6 +473,7 @@ public class Essentials extends JavaPlugin implements IEssentials } } + @Override public void showError(final CommandSender sender, final Throwable exception, final String commandLabel) { sender.sendMessage(Util.format("errorWithMessage", exception.getMessage())); @@ -481,46 +493,64 @@ public class Essentials extends JavaPlugin implements IEssentials } } - public CraftScheduler getScheduler() + @Override + public BukkitScheduler getScheduler() { - return (CraftScheduler)this.getServer().getScheduler(); + return this.getServer().getScheduler(); } + @Override public Jail getJail() { return jail; } + @Override public Warps getWarps() { return warps; } + @Override public Worth getWorth() { return worth; } + @Override public Backup getBackup() { return backup; } + @Override public Spawn getSpawn() { return spawn; } - public User getUser(Object base) + @Override + public User getUser(final Object base) { if (base instanceof Player) { return getUser((Player)base); } + if (base instanceof String) + { + try + { + return userMap.getUser((String)base); + } + catch (NullPointerException ex) + { + return null; + } + } return null; } - private <T extends Player> User getUser(T base) + private <T extends Player> User getUser(final T base) { if (base == null) { @@ -532,70 +562,57 @@ public class Essentials extends JavaPlugin implements IEssentials return (User)base; } - if (users.containsKey(base.getName().toLowerCase())) - { - return users.get(base.getName().toLowerCase()).update(base); - } - - User u = new User(base, this); - users.put(u.getName().toLowerCase(), u); - return u; - } - - public Map<String, User> getAllUsers() - { - return users; + return userMap.getUser(base.getName()).update(base); } - public User getOfflineUser(String name) + @Override + public User getOfflineUser(final String name) { - // Don't create a new offline user, if we already have that user loaded. - User u = users.get(name.toLowerCase()); - if (u != null) + try { - return u; + return userMap.getUser(name); } - File userFolder = new File(getDataFolder(), "userdata"); - File userFile = new File(userFolder, Util.sanitizeFileName(name) + ".yml"); - if (userFile.exists()) - { //Users do not get offline changes saved without being reproccessed as Users! ~ Xeology :) - return getUser((Player)new OfflinePlayer(name, this)); - + catch (NullPointerException ex) + { + return null; } - return null; } + @Override public World getWorld(final String name) { if (name.matches("[0-9]+")) { - final int id = Integer.parseInt(name); - if (id < getServer().getWorlds().size()) + final int worldId = Integer.parseInt(name); + if (worldId < getServer().getWorlds().size()) { - return getServer().getWorlds().get(id); + return getServer().getWorlds().get(worldId); } } return getServer().getWorld(name); } + @Override public void addReloadListener(final IConf listener) { confList.add(listener); } + @Override public Methods getPaymentMethod() { return paymentMethod; } + @Override public int broadcastMessage(final String name, final String message) { - Player[] players = getServer().getOnlinePlayers(); + final Player[] players = getServer().getOnlinePlayers(); for (Player player : players) { - User u = getUser(player); - if (!u.isIgnoredPlayer(name)) + final User user = getUser(player); + if (!user.isIgnoredPlayer(name)) { player.sendMessage(message); } @@ -609,48 +626,63 @@ public class Essentials extends JavaPlugin implements IEssentials return errorHandler.getErrors(); } + @Override public int scheduleAsyncDelayedTask(final Runnable run) { return this.getScheduler().scheduleAsyncDelayedTask(this, run); } + @Override public int scheduleSyncDelayedTask(final Runnable run) { return this.getScheduler().scheduleSyncDelayedTask(this, run); } + @Override public int scheduleSyncDelayedTask(final Runnable run, final long delay) { return this.getScheduler().scheduleSyncDelayedTask(this, run, delay); } + @Override public int scheduleSyncRepeatingTask(final Runnable run, final long delay, final long period) { return this.getScheduler().scheduleSyncRepeatingTask(this, run, delay, period); } + @Override public TNTExplodeListener getTNTListener() { return tntListener; } + @Override public IPermissionsHandler getPermissionsHandler() { return permissionsHandler; } - - public void setPermissionsHandler(IPermissionsHandler handler) + + @Override + public void setPermissionsHandler(final IPermissionsHandler handler) { this.permissionsHandler = handler; } + @Override public BanWorkaround getBans() { return bans; } + @Override public ItemDb getItemDb() { return itemDb; } + + @Override + public UserMap getUserMap() + { + return userMap; + } } diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java index d6299f387..ea9e6d170 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java @@ -1,8 +1,6 @@ package com.earth2me.essentials; -import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.logging.Level; @@ -308,6 +306,11 @@ public class EssentialsPlayerListener extends PlayerListener final User user = ess.getUser(event.getPlayer()); user.setNPC(false); + final long currentTime = System.currentTimeMillis(); + user.checkBanTimeout(currentTime); + user.checkMuteTimeout(currentTime); + user.checkJailTimeout(currentTime); + if (user.isBanned()) { final String banReason = user.getBanReason(); diff --git a/Essentials/src/com/earth2me/essentials/EssentialsTimer.java b/Essentials/src/com/earth2me/essentials/EssentialsTimer.java index 320c86cfe..639802776 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsTimer.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsTimer.java @@ -1,79 +1,44 @@ package com.earth2me.essentials; -import java.io.File; import java.util.HashSet; +import java.util.Iterator; import java.util.Set; import org.bukkit.entity.Player; -public class EssentialsTimer implements Runnable, IConf +public class EssentialsTimer implements Runnable { - private final IEssentials ess; - private final Set<User> allUsers = new HashSet<User>(); - - EssentialsTimer(IEssentials ess) + private final transient IEssentials ess; + private final transient Set<User> onlineUsers = new HashSet<User>(); + + EssentialsTimer(final IEssentials ess) { this.ess = ess; - File userdir = new File(ess.getDataFolder(), "userdata"); - if (!userdir.exists()) { - return; - } - for (String string : userdir.list()) - { - if (!string.endsWith(".yml")) { - continue; - } - String name = string.substring(0, string.length()-4); - User u = ess.getUser(new OfflinePlayer(name, ess)); - allUsers.add(u); - } } + @Override public void run() { - long currentTime = System.currentTimeMillis(); + final long currentTime = System.currentTimeMillis(); for (Player player : ess.getServer().getOnlinePlayers()) { - User u = ess.getUser(player); - allUsers.add(u); - u.setLastActivity(currentTime); + final User user = ess.getUser(player); + onlineUsers.add(user); + user.setLastActivity(currentTime); } - - for (User user: allUsers) { - if (user.getBanTimeout() > 0 && user.getBanTimeout() < currentTime) { - user.setBanTimeout(0); - ess.getBans().unbanByName(user.getName()); - } - if (user.getMuteTimeout() > 0 && user.getMuteTimeout() < currentTime && user.isMuted()) { - user.setMuteTimeout(0); - user.sendMessage(Util.i18n("canTalkAgain")); - user.setMuted(false); - } - if (user.getJailTimeout() > 0 && user.getJailTimeout() < currentTime && user.isJailed()) { - user.setJailTimeout(0); - user.setJailed(false); - user.sendMessage(Util.i18n("haveBeenReleased")); - user.setJail(null); - try - { - user.getTeleport().back(); - } - catch (Exception ex) - { - } - } - - if (user.getLastActivity() < currentTime && user.getLastActivity() > user.getLastLogout()) { - user.setLastLogout(user.getLastActivity()); - } - } - } - public void reloadConfig() - { - for (User user : allUsers) + final Iterator<User> iterator = onlineUsers.iterator(); + while (iterator.hasNext()) { - user.reloadConfig(); + final User user = iterator.next(); + if (user.getLastActivity() < currentTime && user.getLastActivity() > user.getLastLogout()) + { + user.setLastLogout(user.getLastActivity()); + iterator.remove(); + continue; + } + user.checkMuteTimeout(currentTime); + user.checkJailTimeout(currentTime); } } } diff --git a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java index 31a4f8492..c6c7effd8 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsUpgrade.java @@ -16,63 +16,70 @@ import org.bukkit.inventory.ItemStack; public class EssentialsUpgrade { - private static boolean alreadyRun = false; - private final static Logger logger = Logger.getLogger("Minecraft"); - private final IEssentials ess; + private final static Logger LOGGER = Logger.getLogger("Minecraft"); + private final transient IEssentials ess; + private final transient EssentialsConf doneFile; - EssentialsUpgrade(String version, IEssentials essentials) + EssentialsUpgrade(final IEssentials essentials) { ess = essentials; - if (alreadyRun) + if (!ess.getDataFolder().exists()) { - return; + ess.getDataFolder().mkdirs(); } - alreadyRun = true; + doneFile = new EssentialsConf(new File(ess.getDataFolder(), "upgrades-done.yml")); + doneFile.load(); } private void moveWorthValuesToWorthYml() { + if (doneFile.getBoolean("moveWorthValuesToWorthYml", false)) + { + return; + } try { - File configFile = new File(ess.getDataFolder(), "config.yml"); + final File configFile = new File(ess.getDataFolder(), "config.yml"); if (!configFile.exists()) { return; } - EssentialsConf conf = new EssentialsConf(configFile); + final EssentialsConf conf = new EssentialsConf(configFile); conf.load(); - Worth w = new Worth(ess.getDataFolder()); + final Worth worth = new Worth(ess.getDataFolder()); boolean found = false; for (Material mat : Material.values()) { - int id = mat.getId(); - double value = conf.getDouble("worth-" + id, Double.NaN); + final int id = mat.getId(); + final double value = conf.getDouble("worth-" + id, Double.NaN); if (!Double.isNaN(value)) { found = true; - w.setPrice(new ItemStack(mat, 1, (short)0, (byte)0), value); + worth.setPrice(new ItemStack(mat, 1, (short)0, (byte)0), value); } } if (found) { removeLinesFromConfig(configFile, "\\s*#?\\s*worth-[0-9]+.*", "# Worth values have been moved to worth.yml"); } + doneFile.setProperty("moveWorthValuesToWorthYml", true); + doneFile.save(); } catch (Throwable e) { - logger.log(Level.SEVERE, Util.i18n("upgradingFilesError"), e); + LOGGER.log(Level.SEVERE, Util.i18n("upgradingFilesError"), e); } } private void removeLinesFromConfig(File file, String regex, String info) throws Exception { boolean needUpdate = false; - BufferedReader br = new BufferedReader(new FileReader(file)); - File tempFile = File.createTempFile("essentialsupgrade", ".tmp.yml", ess.getDataFolder()); - BufferedWriter bw = new BufferedWriter(new FileWriter(tempFile)); + final BufferedReader bReader = new BufferedReader(new FileReader(file)); + final File tempFile = File.createTempFile("essentialsupgrade", ".tmp.yml", ess.getDataFolder()); + final BufferedWriter bWriter = new BufferedWriter(new FileWriter(tempFile)); do { - String line = br.readLine(); + final String line = bReader.readLine(); if (line == null) { break; @@ -81,8 +88,8 @@ public class EssentialsUpgrade { if (!needUpdate && info != null) { - bw.write(info, 0, info.length()); - bw.newLine(); + bWriter.write(info, 0, info.length()); + bWriter.newLine(); } needUpdate = true; } @@ -90,22 +97,22 @@ public class EssentialsUpgrade { if (line.endsWith("\r\n")) { - bw.write(line, 0, line.length() - 2); + bWriter.write(line, 0, line.length() - 2); } else if (line.endsWith("\r") || line.endsWith("\n")) { - bw.write(line, 0, line.length() - 1); + bWriter.write(line, 0, line.length() - 1); } else { - bw.write(line, 0, line.length()); + bWriter.write(line, 0, line.length()); } - bw.newLine(); + bWriter.newLine(); } } while (true); - br.close(); - bw.close(); + bReader.close(); + bWriter.close(); if (needUpdate) { if (!file.renameTo(new File(file.getParentFile(), file.getName().concat("." + System.currentTimeMillis() + ".upgradebackup")))) @@ -116,19 +123,25 @@ public class EssentialsUpgrade { throw new Exception(Util.i18n("configFileRenameError")); } - } else { + } + else + { tempFile.delete(); } } private void updateUsersToNewDefaultHome() { - File userdataFolder = new File(ess.getDataFolder(), "userdata"); + if (doneFile.getBoolean("updateUsersToNewDefaultHome", false)) + { + return; + } + final File userdataFolder = new File(ess.getDataFolder(), "userdata"); if (!userdataFolder.exists() || !userdataFolder.isDirectory()) { return; } - File[] userFiles = userdataFolder.listFiles(); + final File[] userFiles = userdataFolder.listFiles(); for (File file : userFiles) { @@ -136,15 +149,16 @@ public class EssentialsUpgrade { continue; } - EssentialsConf config = new EssentialsConf(file); + final EssentialsConf config = new EssentialsConf(file); try { config.load(); if (config.hasProperty("home") && !config.hasProperty("home.default")) { @SuppressWarnings("unchecked") - List<Object> vals = (List<Object>)config.getProperty("home"); - if (vals == null) { + final List<Object> vals = (List<Object>)config.getProperty("home"); + if (vals == null) + { continue; } World world = ess.getServer().getWorlds().get(0); @@ -154,7 +168,7 @@ public class EssentialsUpgrade } if (world != null) { - Location loc = new Location( + final Location loc = new Location( world, ((Number)vals.get(0)).doubleValue(), ((Number)vals.get(1)).doubleValue(), @@ -162,7 +176,7 @@ public class EssentialsUpgrade ((Number)vals.get(3)).floatValue(), ((Number)vals.get(4)).floatValue()); - String worldName = world.getName().toLowerCase(); + final String worldName = world.getName().toLowerCase(); if (worldName != null && !worldName.isEmpty()) { config.removeProperty("home"); @@ -175,30 +189,32 @@ public class EssentialsUpgrade } catch (RuntimeException ex) { - logger.log(Level.INFO, "File: "+file.toString()); + LOGGER.log(Level.INFO, "File: " + file.toString()); throw ex; } } + doneFile.setProperty("updateUsersToNewDefaultHome", true); + doneFile.save(); } private void moveUsersDataToUserdataFolder() { - File usersFile = new File(ess.getDataFolder(), "users.yml"); + final File usersFile = new File(ess.getDataFolder(), "users.yml"); if (!usersFile.exists()) { return; } - EssentialsConf usersConfig = new EssentialsConf(usersFile); + final EssentialsConf usersConfig = new EssentialsConf(usersFile); usersConfig.load(); for (String username : usersConfig.getKeys(null)) { - User user = new User(new OfflinePlayer(username, ess), ess); - String nickname = usersConfig.getString(username + ".nickname"); + final User user = new User(new OfflinePlayer(username, ess), ess); + final String nickname = usersConfig.getString(username + ".nickname"); if (nickname != null && !nickname.isEmpty() && !nickname.equals(username)) { user.setNickname(nickname); } - List<String> mails = usersConfig.getStringList(username + ".mail", null); + final List<String> mails = usersConfig.getStringList(username + ".mail", null); if (mails != null && !mails.isEmpty()) { user.setMails(mails); @@ -206,8 +222,9 @@ public class EssentialsUpgrade if (!user.hasHome()) { @SuppressWarnings("unchecked") - List<Object> vals = (List<Object>)usersConfig.getProperty(username + ".home"); - if (vals != null) { + final List<Object> vals = (List<Object>)usersConfig.getProperty(username + ".home"); + if (vals != null) + { World world = ess.getServer().getWorlds().get(0); if (vals.size() > 5) { @@ -230,40 +247,56 @@ public class EssentialsUpgrade private void convertWarps() { - File warpsFolder = new File(ess.getDataFolder(), "warps"); + final File warpsFolder = new File(ess.getDataFolder(), "warps"); if (!warpsFolder.exists()) { warpsFolder.mkdirs(); } - File[] listOfFiles = warpsFolder.listFiles(); + final File[] listOfFiles = warpsFolder.listFiles(); if (listOfFiles.length >= 1) { for (int i = 0; i < listOfFiles.length; i++) { - String filename = listOfFiles[i].getName(); + final String filename = listOfFiles[i].getName(); if (listOfFiles[i].isFile() && filename.endsWith(".dat")) { try { - BufferedReader rx = new BufferedReader(new FileReader(listOfFiles[i])); + final BufferedReader rx = new BufferedReader(new FileReader(listOfFiles[i])); double x, y, z; float yaw, pitch; String worldName; try { - if (!rx.ready()) continue; + if (!rx.ready()) + { + continue; + } x = Double.parseDouble(rx.readLine().trim()); - if (!rx.ready()) continue; + if (!rx.ready()) + { + continue; + } y = Double.parseDouble(rx.readLine().trim()); - if (!rx.ready()) continue; + if (!rx.ready()) + { + continue; + } z = Double.parseDouble(rx.readLine().trim()); - if (!rx.ready()) continue; + if (!rx.ready()) + { + continue; + } yaw = Float.parseFloat(rx.readLine().trim()); - if (!rx.ready()) continue; + if (!rx.ready()) + { + continue; + } pitch = Float.parseFloat(rx.readLine().trim()); worldName = rx.readLine(); } - finally { + finally + { rx.close(); } World w = null; @@ -285,7 +318,7 @@ public class EssentialsUpgrade w = w1; } } - Location loc = new Location(w, x, y, z, yaw, pitch); + final Location loc = new Location(w, x, y, z, yaw, pitch); ess.getWarps().setWarp(filename.substring(0, filename.length() - 4), loc); if (!listOfFiles[i].renameTo(new File(warpsFolder, filename + ".old"))) { @@ -294,52 +327,52 @@ public class EssentialsUpgrade } catch (Exception ex) { - logger.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } } } - File warpFile = new File(ess.getDataFolder(), "warps.txt"); + final File warpFile = new File(ess.getDataFolder(), "warps.txt"); if (warpFile.exists()) { try { - BufferedReader rx = new BufferedReader(new FileReader(warpFile)); - 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) + for (String[] parts = new String[0]; rx.ready(); parts = rx.readLine().split(":")) { - continue; - } - String name = parts[0]; - double x = Double.parseDouble(parts[1].trim()); - double y = Double.parseDouble(parts[2].trim()); - double z = Double.parseDouble(parts[3].trim()); - float yaw = Float.parseFloat(parts[4].trim()); - 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) + if (parts.length < 6) { - w = world; - break; + 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(w, x, y, z, yaw, pitch); + ess.getWarps().setWarp(name, loc); + if (!warpFile.renameTo(new File(ess.getDataFolder(), "warps.txt.old"))) + { + throw new Exception(Util.format("fileRenameError", "warps.txt")); } } - Location loc = new Location(w, x, y, z, yaw, pitch); - ess.getWarps().setWarp(name, loc); - if (!warpFile.renameTo(new File(ess.getDataFolder(), "warps.txt.old"))) - { - throw new Exception(Util.format("fileRenameError", "warps.txt")); - } - } } finally { @@ -348,52 +381,60 @@ public class EssentialsUpgrade } catch (Exception ex) { - logger.log(Level.SEVERE, null, ex); + LOGGER.log(Level.SEVERE, null, ex); } } } private void sanitizeAllUserFilenames() { - File usersFolder = new File(ess.getDataFolder(), "userdata"); + if (doneFile.getBoolean("sanitizeAllUserFilenames", false)) + { + return; + } + final File usersFolder = new File(ess.getDataFolder(), "userdata"); if (!usersFolder.exists()) { return; } - File[] listOfFiles = usersFolder.listFiles(); + final File[] listOfFiles = usersFolder.listFiles(); for (int i = 0; i < listOfFiles.length; i++) { - String filename = listOfFiles[i].getName(); + final String filename = listOfFiles[i].getName(); if (!listOfFiles[i].isFile() || !filename.endsWith(".yml")) { continue; } - String sanitizedFilename = Util.sanitizeFileName(filename.substring(0, filename.length() - 4)) + ".yml"; + final String sanitizedFilename = Util.sanitizeFileName(filename.substring(0, filename.length() - 4)) + ".yml"; if (sanitizedFilename.equals(filename)) { continue; } - File tmpFile = new File(listOfFiles[i].getParentFile(), sanitizedFilename + ".tmp"); - File newFile = new File(listOfFiles[i].getParentFile(), sanitizedFilename); - if (!listOfFiles[i].renameTo(tmpFile)) { - logger.log(Level.WARNING, Util.format("userdataMoveError", filename, sanitizedFilename)); + final File tmpFile = new File(listOfFiles[i].getParentFile(), sanitizedFilename + ".tmp"); + final File newFile = new File(listOfFiles[i].getParentFile(), sanitizedFilename); + if (!listOfFiles[i].renameTo(tmpFile)) + { + LOGGER.log(Level.WARNING, Util.format("userdataMoveError", filename, sanitizedFilename)); continue; } if (newFile.exists()) { - logger.log(Level.WARNING, Util.format("duplicatedUserdata", filename, sanitizedFilename)); + LOGGER.log(Level.WARNING, Util.format("duplicatedUserdata", filename, sanitizedFilename)); continue; } - if (!tmpFile.renameTo(newFile)) { - logger.log(Level.WARNING, Util.format("userdataMoveBackError", sanitizedFilename, sanitizedFilename)); + if (!tmpFile.renameTo(newFile)) + { + LOGGER.log(Level.WARNING, Util.format("userdataMoveBackError", sanitizedFilename, sanitizedFilename)); } } + doneFile.setProperty("sanitizeAllUserFilenames", true); + doneFile.save(); } - - private World getFakeWorld(String name) + + private World getFakeWorld(final String name) { - File bukkitDirectory = ess.getDataFolder().getParentFile().getParentFile(); - File worldDirectory = new File(bukkitDirectory, name); + final File bukkitDirectory = ess.getDataFolder().getParentFile().getParentFile(); + final File worldDirectory = new File(bukkitDirectory, name); if (worldDirectory.exists() && worldDirectory.isDirectory()) { return new FakeWorld(worldDirectory.getName(), World.Environment.NORMAL); diff --git a/Essentials/src/com/earth2me/essentials/IEssentials.java b/Essentials/src/com/earth2me/essentials/IEssentials.java index f867daea8..9f427b220 100644 --- a/Essentials/src/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/com/earth2me/essentials/IEssentials.java @@ -1,12 +1,11 @@ package com.earth2me.essentials; import com.earth2me.essentials.register.payment.Methods; -import java.util.Map; import org.bukkit.World; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; -import org.bukkit.craftbukkit.scheduler.CraftScheduler; import org.bukkit.plugin.Plugin; +import org.bukkit.scheduler.BukkitScheduler; public interface IEssentials extends Plugin @@ -27,7 +26,7 @@ public interface IEssentials extends Plugin ISettings getSettings(); - CraftScheduler getScheduler(); + BukkitScheduler getScheduler(); String[] getMotd(CommandSender sender, String def); @@ -63,7 +62,7 @@ public interface IEssentials extends Plugin void showError(final CommandSender sender, final Throwable exception, final String commandLabel); - Map<String, User> getAllUsers(); - ItemDb getItemDb(); + + UserMap getUserMap(); } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index a0b072752..6c18c03ae 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -20,44 +20,44 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser private final Teleport teleport; private long lastActivity; private boolean hidden = false; - + User(Player base, IEssentials ess) { super(base, ess); teleport = new Teleport(this, ess); } - + User update(Player base) { setBase(base); return this; } - + public boolean isAuthorized(IEssentialsCommand cmd) { return isAuthorized(cmd, "essentials."); } - + public boolean isAuthorized(IEssentialsCommand cmd, String permissionPrefix) { return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName())); } - + public boolean isAuthorized(String node) { if (isOp()) { return true; } - + if (isJailed()) { return false; } - + return ess.getPermissionsHandler().hasPermission(this, node); } - + public void healCooldown() throws Exception { Calendar now = new GregorianCalendar(); @@ -75,12 +75,12 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } setLastHealTimestamp(now.getTimeInMillis()); } - + public void giveMoney(double value) { giveMoney(value, null); } - + public void giveMoney(double value, CommandSender initiator) { if (value == 0) @@ -94,7 +94,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser initiator.sendMessage((Util.format("addedToOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()))); } } - + public void payUser(User reciever, double value) throws Exception { if (value == 0) @@ -113,12 +113,12 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser reciever.sendMessage(Util.format("moneyRecievedFrom", Util.formatCurrency(value, ess), getDisplayName())); } } - + public void takeMoney(double value) { takeMoney(value, null); } - + public void takeMoney(double value, CommandSender initiator) { if (value == 0) @@ -132,43 +132,43 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser initiator.sendMessage((Util.format("takenFromOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()))); } } - + public boolean canAfford(double cost) { double mon = getMoney(); return mon >= cost || isAuthorized("essentials.eco.loan"); } - + public void dispose() { this.base = new OfflinePlayer(getName(), ess); } - + public boolean getJustPortaled() { return justPortaled; } - + public void setJustPortaled(boolean value) { justPortaled = value; } - + public void setReplyTo(CommandSender user) { replyTo = user; } - + public CommandSender getReplyTo() { return replyTo; } - + public int compareTo(User t) { return ChatColor.stripColor(this.getDisplayName()).compareToIgnoreCase(ChatColor.stripColor(t.getDisplayName())); } - + @Override public boolean equals(Object o) { @@ -177,51 +177,51 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser return false; } return ChatColor.stripColor(this.getDisplayName()).equalsIgnoreCase(ChatColor.stripColor(((User)o).getDisplayName())); - + } - + @Override public int hashCode() { return ChatColor.stripColor(this.getDisplayName()).hashCode(); } - + public Boolean canSpawnItem(int itemId) { return !ess.getSettings().itemSpawnBlacklist().contains(itemId); } - + public void setHome() { setHome(getLocation(), true); } - + public void setHome(boolean defaultHome) { setHome(getLocation(), defaultHome); } - + public void setLastLocation() { setLastLocation(getLocation()); } - + public void requestTeleport(User player, boolean here) { teleportRequester = player; teleportRequestHere = here; } - + public User getTeleportRequest() { return teleportRequester; } - + public boolean isTeleportRequestHere() { return teleportRequestHere; } - + public String getNick() { final StringBuilder nickname = new StringBuilder(); @@ -245,12 +245,12 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser { } } - + if (ess.getSettings().addPrefixSuffix()) { final String prefix = ess.getPermissionsHandler().getPrefix(this).replace('&', '§').replace("{WORLDNAME}", this.getWorld().getName()); final String suffix = ess.getPermissionsHandler().getSuffix(this).replace('&', '§').replace("{WORLDNAME}", this.getWorld().getName()); - + nickname.insert(0, prefix); nickname.append(suffix); if (suffix.length() < 2 || !suffix.substring(suffix.length() - 2, suffix.length() - 1).equals("§")) @@ -258,25 +258,25 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser nickname.append("§f"); } } - + return nickname.toString(); } - + public Teleport getTeleport() { return teleport; } - + public long getLastActivity() { return lastActivity; } - + public void setLastActivity(long timestamp) { lastActivity = timestamp; } - + @Override public double getMoney() { @@ -298,7 +298,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } return super.getMoney(); } - + @Override public void setMoney(double value) { @@ -320,14 +320,14 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser } super.setMoney(value); } - + @Override public void setAfk(boolean set) { this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : set); super.setAfk(set); } - + @Override public boolean toggleAfk() { @@ -335,14 +335,51 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now); return now; } - + public boolean isHidden() { return hidden; } - + public void setHidden(boolean hidden) { this.hidden = hidden; } + + public void checkJailTimeout(final long currentTime) + { + if (getJailTimeout() > 0 && getJailTimeout() < currentTime && isJailed()) + { + setJailTimeout(0); + setJailed(false); + sendMessage(Util.i18n("haveBeenReleased")); + setJail(null); + try + { + getTeleport().back(); + } + catch (Exception ex) + { + } + } + } + + public void checkMuteTimeout(final long currentTime) + { + if (getMuteTimeout() > 0 && getMuteTimeout() < currentTime && isMuted()) + { + setMuteTimeout(0); + sendMessage(Util.i18n("canTalkAgain")); + setMuted(false); + } + } + + public void checkBanTimeout(final long currentTime) + { + if (getBanTimeout() > 0 && getBanTimeout() < currentTime && ess.getBans().isNameBanned(getName())) + { + setBanTimeout(0); + ess.getBans().unbanByName(getName()); + } + } } diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java new file mode 100644 index 000000000..7a7fdb4af --- /dev/null +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -0,0 +1,107 @@ +package com.earth2me.essentials; + +import com.google.common.base.Function; +import com.google.common.collect.MapMaker; +import java.io.File; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import org.bukkit.entity.Player; + + +public class UserMap implements Function<String, User>, IConf +{ + private final transient IEssentials ess; + private final transient ConcurrentMap<String, User> users = new MapMaker().softValues().makeComputingMap(this); + + public UserMap(final IEssentials ess) + { + this.ess = ess; + loadAllUsersAsync(ess); + } + + private void loadAllUsersAsync(final IEssentials ess) + { + ess.scheduleAsyncDelayedTask(new Runnable() + { + @Override + public void run() + { + final File userdir = new File(ess.getDataFolder(), "userdata"); + if (!userdir.exists()) + { + return; + } + for (String string : userdir.list()) + { + if (!string.endsWith(".yml")) + { + continue; + } + final String name = string.substring(0, string.length() - 4); + try + { + users.get(name.toLowerCase()); + } + catch (NullPointerException ex) + { + // Ignore these + } + } + } + }); + } + + public boolean userExists(final String name) + { + return users.containsKey(name.toLowerCase()); + } + + public User getUser(final String name) throws NullPointerException + { + return users.get(name.toLowerCase()); + } + + @Override + public User apply(final String name) + { + for (Player player : ess.getServer().getOnlinePlayers()) + { + if (player.getName().equalsIgnoreCase(name)) + { + return new User(player, ess); + } + } + final File userFolder = new File(ess.getDataFolder(), "userdata"); + final File userFile = new File(userFolder, Util.sanitizeFileName(name) + ".yml"); + if (userFile.exists()) + { + return new User(new OfflinePlayer(name, ess), ess); + } + return null; + } + + @Override + public void reloadConfig() + { + for (User user : users.values()) + { + user.reloadConfig(); + } + } + + public void removeUser(final String name) + { + users.remove(name.toLowerCase()); + } + + public Set<User> getAllUsers() + { + final Set<User> userSet = new HashSet<User>(); + for (String name : users.keySet()) + { + userSet.add(users.get(name)); + } + return userSet; + } +} diff --git a/Essentials/src/com/earth2me/essentials/api/Economy.java b/Essentials/src/com/earth2me/essentials/api/Economy.java index ddafacd5f..940207a6f 100644 --- a/Essentials/src/com/earth2me/essentials/api/Economy.java +++ b/Essentials/src/com/earth2me/essentials/api/Economy.java @@ -60,7 +60,7 @@ public final class Economy { logger.log(Level.WARNING, Util.format("deleteFileError", config)); } - ess.getAllUsers().remove(name.toLowerCase()); + ess.getUserMap().removeUser(name); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java index 1d8b2e619..a162ea73f 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandbalancetop.java @@ -39,7 +39,7 @@ public class Commandbalancetop extends EssentialsCommand } } final Map<User, Double> balances = new HashMap<User, Double>(); - for (User u : ess.getAllUsers().values()) + for (User u : ess.getUserMap().getAllUsers()) { balances.put(u, u.getMoney()); } diff --git a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java index 459e48f8f..c99cdfdc9 100644 --- a/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java +++ b/Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java @@ -1,5 +1,6 @@ package com.earth2me.essentials.commands; +import com.earth2me.essentials.ChargeException; import com.earth2me.essentials.Trade; import java.util.List; import org.bukkit.Server; @@ -15,16 +16,16 @@ import java.util.logging.Logger; public abstract class EssentialsCommand implements IEssentialsCommand { - private final String name; - protected IEssentials ess; + private final transient String name; + protected transient IEssentials ess; protected final static Logger logger = Logger.getLogger("Minecraft"); - protected EssentialsCommand(String name) + protected EssentialsCommand(final String name) { this.name = name; } - public void setEssentials(IEssentials ess) + public void setEssentials(final IEssentials ess) { this.ess = ess; } @@ -34,74 +35,72 @@ public abstract class EssentialsCommand implements IEssentialsCommand return name; } - protected User getPlayer(Server server, String[] args, int pos) throws NoSuchFieldException, NotEnoughArgumentsException + protected User getPlayer(final Server server, final String[] args, final int pos) throws NoSuchFieldException, NotEnoughArgumentsException { return getPlayer(server, args, pos, false); } - protected User getPlayer(Server server, String[] args, int pos, boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException + protected User getPlayer(final Server server, final String[] args, final int pos, final boolean getOffline) throws NoSuchFieldException, NotEnoughArgumentsException { - if (args.length <= pos) throw new NotEnoughArgumentsException(); - User user = ess.getAllUsers().get(args[pos].toLowerCase()); + if (args.length <= pos) + { + throw new NotEnoughArgumentsException(); + } + final User user = ess.getUser(args[pos]); if (user != null) { - if(!getOffline && (user.getBase() instanceof OfflinePlayer || user.isHidden())) + if (!getOffline && (user.getBase() instanceof OfflinePlayer || user.isHidden())) { throw new NoSuchFieldException(Util.i18n("playerNotFound")); } return user; - } - List<Player> matches = server.matchPlayer(args[pos]); - - if (matches.size() < 1) - { - if (!getOffline) throw new NoSuchFieldException(Util.i18n("playerNotFound")); - User u = ess.getOfflineUser(args[pos]); - if (u == null) throw new NoSuchFieldException(Util.i18n("playerNotFound")); - return u; } + final List<Player> matches = server.matchPlayer(args[pos]); - for (Player p : matches) + if (!matches.isEmpty()) { - final User u = ess.getUser(p); - if (u.getDisplayName().startsWith(args[pos]) && (getOffline || !u.isHidden())) + for (Player player : matches) { - return u; + final User userMatch = ess.getUser(player); + if (userMatch.getDisplayName().startsWith(args[pos]) && (getOffline || !userMatch.isHidden())) + { + return userMatch; + } + } + final User userMatch = ess.getUser(matches.get(0)); + if (getOffline || !userMatch.isHidden()) + { + return userMatch; } } - final User u = ess.getUser(matches.get(0)); - if (!getOffline && u.isHidden()) - { - throw new NoSuchFieldException(Util.i18n("playerNotFound")); - } - return u; + throw new NoSuchFieldException(Util.i18n("playerNotFound")); } @Override - public final void run(Server server, User user, String commandLabel, Command cmd, String[] args) throws Exception + public final void run(final Server server, final User user, final String commandLabel, final Command cmd, final String[] args) throws Exception { run(server, user, commandLabel, args); } - protected void run(Server server, User user, String commandLabel, String[] args) throws Exception + protected void run(final Server server, final User user, final String commandLabel, final String[] args) throws Exception { run(server, (CommandSender)user.getBase(), commandLabel, args); } @Override - public final void run(Server server, CommandSender sender, String commandLabel, Command cmd, String[] args) throws Exception + public final void run(final Server server, final CommandSender sender, final String commandLabel, final Command cmd, final String[] args) throws Exception { run(server, sender, commandLabel, args); } - protected void run(Server server, CommandSender sender, String commandLabel, String[] args) throws Exception + protected void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception { throw new Exception(Util.format("onlyPlayers", commandLabel)); } - public static String getFinalArg(String[] args, int start) + public static String getFinalArg(final String[] args, final int start) { - StringBuilder bldr = new StringBuilder(); + final StringBuilder bldr = new StringBuilder(); for (int i = start; i < args.length; i++) { if (i != start) @@ -113,11 +112,11 @@ public abstract class EssentialsCommand implements IEssentialsCommand return bldr.toString(); } - protected void charge(CommandSender sender) throws Exception + protected void charge(final CommandSender sender) throws ChargeException { if (sender instanceof Player) { - Trade charge = new Trade(this.getName(), ess); + final Trade charge = new Trade(this.getName(), ess); charge.charge(ess.getUser((Player)sender)); } } diff --git a/Essentials/test/com/earth2me/essentials/FakeServer.java b/Essentials/test/com/earth2me/essentials/FakeServer.java index a8e8dd89b..e3c0e2364 100644 --- a/Essentials/test/com/earth2me/essentials/FakeServer.java +++ b/Essentials/test/com/earth2me/essentials/FakeServer.java @@ -5,6 +5,8 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.UUID; +import java.util.concurrent.Callable; +import java.util.concurrent.Future; import java.util.logging.Logger; import org.bukkit.Location; import org.bukkit.Server; @@ -15,9 +17,12 @@ import org.bukkit.command.PluginCommand; import org.bukkit.entity.Player; import org.bukkit.generator.ChunkGenerator; import org.bukkit.inventory.Recipe; +import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginManager; import org.bukkit.plugin.ServicesManager; import org.bukkit.scheduler.BukkitScheduler; +import org.bukkit.scheduler.BukkitTask; +import org.bukkit.scheduler.BukkitWorker; public class FakeServer implements Server @@ -118,7 +123,93 @@ public class FakeServer implements Server public BukkitScheduler getScheduler() { - throw new UnsupportedOperationException("Not supported yet."); + return new BukkitScheduler() { + + @Override + public int scheduleSyncDelayedTask(Plugin plugin, Runnable r, long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int scheduleSyncDelayedTask(Plugin plugin, Runnable r) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int scheduleSyncRepeatingTask(Plugin plugin, Runnable r, long l, long l1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int scheduleAsyncDelayedTask(Plugin plugin, Runnable r, long l) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public int scheduleAsyncDelayedTask(Plugin plugin, Runnable r) + { + r.run(); + return 0; + } + + @Override + public int scheduleAsyncRepeatingTask(Plugin plugin, Runnable r, long l, long l1) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public <T> Future<T> callSyncMethod(Plugin plugin, Callable<T> clbl) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void cancelTask(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void cancelTasks(Plugin plugin) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void cancelAllTasks() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isCurrentlyRunning(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public boolean isQueued(int i) + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List<BukkitWorker> getActiveWorkers() + { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public List<BukkitTask> getPendingTasks() + { + throw new UnsupportedOperationException("Not supported yet."); + } + }; } public ServicesManager getServicesManager() diff --git a/Essentials/test/com/earth2me/essentials/UserTest.java b/Essentials/test/com/earth2me/essentials/UserTest.java index 2b138d687..4724c96d6 100644 --- a/Essentials/test/com/earth2me/essentials/UserTest.java +++ b/Essentials/test/com/earth2me/essentials/UserTest.java @@ -33,6 +33,7 @@ public class UserTest extends TestCase } base1 = server.createPlayer("testPlayer1", ess); server.addPlayer(base1); + ess.getUser(base1); } private void should(String what) |