From 29d4e09983a6c4602e0a8d5fbf6bf716da9ae0db Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sat, 14 Jan 2012 16:36:19 +0000 Subject: [PATCH 1/9] Fixing {WorldDate} Test #1353 --- .../src/com/earth2me/essentials/textreader/KeywordReplacer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java index 182dba9d3..782798bff 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java +++ b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java @@ -41,7 +41,7 @@ public class KeywordReplacer implements IText world = user.getLocation().getWorld().getName(); worldTime12 = DescParseTickFormat.format12(user.getWorld().getTime()); worldTime24 = DescParseTickFormat.format24(user.getWorld().getTime()); - worldDate = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(DescParseTickFormat.ticksToDate(user.getWorld().getTime())); + worldDate = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(DescParseTickFormat.ticksToDate(user.getWorld().getFullTime())); } else { From 3f6b9586b4da7fc5d9f2ec0a9c2d76d28b1b5a7c Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sat, 14 Jan 2012 16:42:53 +0000 Subject: [PATCH 2/9] Fixing double charges on /home Test #1426 --- .../src/com/earth2me/essentials/commands/Commandhome.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java index d1db6c523..55bd75d0c 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhome.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhome.java @@ -50,7 +50,7 @@ public class Commandhome extends EssentialsCommand if (bed != null) { user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND); - return; + throw new NoChargeException(); } } user.getTeleport().home(player, homeName.toLowerCase(Locale.ENGLISH), charge); @@ -64,10 +64,10 @@ public class Commandhome extends EssentialsCommand if (bed != null) { user.getTeleport().teleport(bed, charge, TeleportCause.COMMAND); - return; + throw new NoChargeException(); } user.getTeleport().respawn(charge, TeleportCause.COMMAND); - return; + } else if (homes.isEmpty()) { @@ -76,7 +76,6 @@ public class Commandhome extends EssentialsCommand else if (homes.size() == 1 && player.equals(user)) { user.getTeleport().home(player, homes.get(0), charge); - return; } else { From 6e82419c1573b7f8ee912862cedc65e83af2b93d Mon Sep 17 00:00:00 2001 From: KHobbits Date: Sat, 14 Jan 2012 16:53:18 +0000 Subject: [PATCH 3/9] Allowing use of aliases in help/info/motd, for command suggestions. Test #1441 --- .../src/com/earth2me/essentials/commands/Commandhelp.java | 2 +- .../src/com/earth2me/essentials/commands/Commandinfo.java | 2 +- .../src/com/earth2me/essentials/commands/Commandmotd.java | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java index 328f692ea..769aac483 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandhelp.java @@ -41,7 +41,7 @@ public class Commandhelp extends EssentialsCommand output = new KeywordReplacer(input, user, ess); } final TextPager pager = new TextPager(output); - pager.showPage(pageStr, chapterPageStr, "help", user); + pager.showPage(pageStr, chapterPageStr, commandLabel, user); } @Override diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java b/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java index e75eeed9a..afe1add72 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandinfo.java @@ -21,6 +21,6 @@ public class Commandinfo extends EssentialsCommand final IText input = new TextInput(sender, "info", true, ess); final IText output = new KeywordReplacer(input, sender, ess); final TextPager pager = new TextPager(output); - pager.showPage(args.length > 0 ? args[0] : null, args.length > 1 ? args[1] : null, "info", sender); + pager.showPage(args.length > 0 ? args[0] : null, args.length > 1 ? args[1] : null, commandLabel, sender); } } diff --git a/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java b/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java index 78ca5c306..0031504e8 100644 --- a/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java +++ b/Essentials/src/com/earth2me/essentials/commands/Commandmotd.java @@ -21,6 +21,6 @@ public class Commandmotd extends EssentialsCommand final IText input = new TextInput(sender, "motd", true, ess); final IText output = new KeywordReplacer(input, sender, ess); final TextPager pager = new TextPager(output); - pager.showPage(args.length > 0 ? args[0] : null, args.length > 1 ? args[1] : null, "motd", sender); + pager.showPage(args.length > 0 ? args[0] : null, args.length > 1 ? args[1] : null, commandLabel, sender); } } From c584d74852b219b8f217a909aa124e3a0c63c30a Mon Sep 17 00:00:00 2001 From: snowleo Date: Sat, 14 Jan 2012 23:13:58 +0100 Subject: [PATCH 4/9] Temporary fix for Spawns and Jails when Worlds are loaded after Essentials. This will be replaced by BetterLocation fix in 3.0 branch. --- .../com/earth2me/essentials/Essentials.java | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index fe550279e..223363c3a 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -49,6 +49,9 @@ import org.bukkit.event.Event.Priority; import org.bukkit.event.Event.Type; import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerListener; +import org.bukkit.event.world.WorldListener; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.plugin.InvalidDescriptionException; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.PluginDescriptionFile; @@ -242,6 +245,10 @@ public class Essentials extends JavaPlugin implements IEssentials pm.registerEvent(Type.ENTITY_DEATH, entityListener, Priority.Lowest, this); pm.registerEvent(Type.ENTITY_REGAIN_HEALTH, entityListener, Priority.Lowest, this); pm.registerEvent(Type.FOOD_LEVEL_CHANGE, entityListener, Priority.Lowest, this); + + final EssentialsWorldListener worldListener = new EssentialsWorldListener(this); + pm.registerEvent(Type.WORLD_LOAD, worldListener, Priority.Monitor, this); + pm.registerEvent(Type.WORLD_UNLOAD, worldListener, Priority.Monitor, this); //TODO: Check if this should be here, and not above before reload() jails = new Jails(this); @@ -594,4 +601,32 @@ public class Essentials extends JavaPlugin implements IEssentials { return i18n; } + + private static class EssentialsWorldListener extends WorldListener implements Runnable { + private transient final IEssentials ess; + + public EssentialsWorldListener(IEssentials ess) + { + this.ess = ess; + } + + + @Override + public void onWorldLoad(WorldLoadEvent event) + { + ess.scheduleSyncDelayedTask(this); + } + + @Override + public void onWorldUnload(WorldUnloadEvent event) + { + ess.scheduleSyncDelayedTask(this); + } + + @Override + public void run() + { + ess.reload(); + } + } } From c860b1c6685240222ad10c94ca8f873a21148ac9 Mon Sep 17 00:00:00 2001 From: snowleo Date: Sat, 14 Jan 2012 23:25:52 +0100 Subject: [PATCH 5/9] Reload less --- .../com/earth2me/essentials/Essentials.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/Essentials.java b/Essentials/src/com/earth2me/essentials/Essentials.java index 223363c3a..5287e299b 100644 --- a/Essentials/src/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/com/earth2me/essentials/Essentials.java @@ -614,13 +614,27 @@ public class Essentials extends JavaPlugin implements IEssentials @Override public void onWorldLoad(WorldLoadEvent event) { - ess.scheduleSyncDelayedTask(this); + ess.getJails().onReload(); + ess.getWarps().reloadConfig(); + for (IConf iConf : ((Essentials)ess).confList) + { + if (iConf instanceof IEssentialsModule) { + iConf.reloadConfig(); + } + } } @Override public void onWorldUnload(WorldUnloadEvent event) { - ess.scheduleSyncDelayedTask(this); + ess.getJails().onReload(); + ess.getWarps().reloadConfig(); + for (IConf iConf : ((Essentials)ess).confList) + { + if (iConf instanceof IEssentialsModule) { + iConf.reloadConfig(); + } + } } @Override From 359ea194b8d5348b5d2f0b3c67f83ee7a6539513 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 00:50:52 +0100 Subject: [PATCH 6/9] Speed improvements for Move and Interact Event. --- .../essentials/EssentialsPlayerListener.java | 20 +-- .../src/com/earth2me/essentials/User.java | 125 +++++++++--------- .../src/com/earth2me/essentials/UserData.java | 5 + .../src/com/earth2me/essentials/UserMap.java | 77 ++++++----- .../src/com/earth2me/essentials/Util.java | 5 +- 5 files changed, 128 insertions(+), 104 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java index bb6741d30..9edc80364 100644 --- a/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java +++ b/Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java @@ -282,22 +282,22 @@ public class EssentialsPlayerListener extends PlayerListener { final User user = ess.getUser(event.getPlayer()); user.updateActivity(true); - usePowertools(event); + if (event.getAnimationType() == PlayerAnimationType.ARM_SWING + && user.hasPowerTools() && user.arePowerToolsEnabled()) + { + usePowertools(user); + } } - private void usePowertools(final PlayerAnimationEvent event) + private void usePowertools(final User user) { - if (event.getAnimationType() != PlayerAnimationType.ARM_SWING) - { - return; - } - final User user = ess.getUser(event.getPlayer()); final ItemStack is = user.getItemInHand(); - if (is == null || is.getType() == Material.AIR || !user.arePowerToolsEnabled()) + int id; + if (is == null || (id = is.getTypeId()) == 0) { return; } - final List commandList = user.getPowertool(is); + final List commandList = user.getPowertool(id); if (commandList == null || commandList.isEmpty()) { return; @@ -317,7 +317,7 @@ public class EssentialsPlayerListener extends PlayerListener } else { - user.getServer().dispatchCommand(event.getPlayer(), command); + user.getServer().dispatchCommand(user.getBase(), command); } } } diff --git a/Essentials/src/com/earth2me/essentials/User.java b/Essentials/src/com/earth2me/essentials/User.java index daf756e5b..e23fe1de0 100644 --- a/Essentials/src/com/earth2me/essentials/User.java +++ b/Essentials/src/com/earth2me/essentials/User.java @@ -23,34 +23,37 @@ public class User extends UserData implements Comparable, IReplyTo, IUser private transient long lastOnlineActivity; private transient long lastActivity = System.currentTimeMillis(); private boolean hidden = false; - private transient Location afkPosition; + private transient Location afkPosition = null; private static final Logger logger = Logger.getLogger("Minecraft"); - + User(final Player base, final IEssentials ess) { super(base, ess); teleport = new Teleport(this, ess); - afkPosition = getLocation(); + if (isAfk()) + { + afkPosition = getLocation(); + } } - + User update(final Player base) { setBase(base); return this; } - + @Override public boolean isAuthorized(final IEssentialsCommand cmd) { return isAuthorized(cmd, "essentials."); } - + @Override public boolean isAuthorized(final IEssentialsCommand cmd, final String permissionPrefix) { return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName())); } - + @Override public boolean isAuthorized(final String node) { @@ -58,20 +61,20 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { return false; } - + if (isOp()) { return true; } - + if (isJailed()) { return false; } - + return ess.getPermissionsHandler().hasPermission(base, node); } - + public void healCooldown() throws Exception { final Calendar now = new GregorianCalendar(); @@ -89,13 +92,13 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } setLastHealTimestamp(now.getTimeInMillis()); } - + @Override public void giveMoney(final double value) { giveMoney(value, null); } - + public void giveMoney(final double value, final CommandSender initiator) { if (value == 0) @@ -109,7 +112,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser initiator.sendMessage(_("addedToOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName())); } } - + public void payUser(final User reciever, final double value) throws Exception { if (value == 0) @@ -128,13 +131,13 @@ public class User extends UserData implements Comparable, IReplyTo, IUser throw new Exception(_("notEnoughMoney")); } } - + @Override public void takeMoney(final double value) { takeMoney(value, null); } - + public void takeMoney(final double value, final CommandSender initiator) { if (value == 0) @@ -148,36 +151,36 @@ public class User extends UserData implements Comparable, IReplyTo, IUser initiator.sendMessage(_("takenFromOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName())); } } - + public boolean canAfford(final double cost) { final double mon = getMoney(); return mon >= cost || isAuthorized("essentials.eco.loan"); } - + public void dispose() { this.base = new OfflinePlayer(getName(), ess); } - + @Override public void setReplyTo(final CommandSender user) { replyTo = user; } - + @Override public CommandSender getReplyTo() { return replyTo; } - + @Override public int compareTo(final User other) { return Util.stripColor(this.getDisplayName()).compareToIgnoreCase(Util.stripColor(other.getDisplayName())); } - + @Override public boolean equals(final Object object) { @@ -186,58 +189,58 @@ public class User extends UserData implements Comparable, IReplyTo, IUser return false; } return this.getName().equalsIgnoreCase(((User)object).getName()); - + } - + @Override public int hashCode() { return this.getName().hashCode(); } - + public Boolean canSpawnItem(final int itemId) { return !ess.getSettings().itemSpawnBlacklist().contains(itemId); } - + public Location getHome() throws Exception { return getHome(getHomes().get(0)); } - + public void setHome() { setHome("home", getLocation()); } - + public void setHome(final String name) { setHome(name, getLocation()); } - + @Override public void setLastLocation() { setLastLocation(getLocation()); } - + public void requestTeleport(final User player, final boolean here) { teleportRequestTime = System.currentTimeMillis(); teleportRequester = player; teleportRequestHere = here; } - + public User getTeleportRequest() { return teleportRequester; } - + public boolean isTeleportRequestHere() { return teleportRequestHere; } - + public String getNick(boolean addprefixsuffix) { final StringBuilder nickname = new StringBuilder(); @@ -261,7 +264,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { } } - + if (addprefixsuffix && ess.getSettings().addPrefixSuffix()) { if (!ess.getSettings().disablePrefix()) @@ -283,10 +286,10 @@ public class User extends UserData implements Comparable, IReplyTo, IUser nickname.append("§f"); } } - + return nickname.toString(); } - + public void setDisplayNick() { String name = getNick(true); @@ -308,7 +311,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser logger.log(Level.INFO, "Playerlist for " + name + " was not updated. Use a shorter displayname prefix."); } } - + @Override public String getDisplayName() { @@ -318,22 +321,22 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } return super.getDisplayName() == null ? super.getName() : super.getDisplayName(); } - + public Teleport getTeleport() { return teleport; } - + public long getLastOnlineActivity() { return lastOnlineActivity; } - + public void setLastOnlineActivity(final long timestamp) { lastOnlineActivity = timestamp; } - + @Override public double getMoney() { @@ -355,7 +358,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } return super.getMoney(); } - + @Override public void setMoney(final double value) { @@ -377,7 +380,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } super.setMoney(value); } - + @Override public void setAfk(final boolean set) { @@ -386,9 +389,13 @@ public class User extends UserData implements Comparable, IReplyTo, IUser { afkPosition = getLocation(); } + else if (!set && isAfk()) + { + afkPosition = null; + } super.setAfk(set); } - + @Override public boolean toggleAfk() { @@ -396,13 +403,13 @@ public class User extends UserData implements Comparable, IReplyTo, IUser this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now); return now; } - + @Override public boolean isHidden() { return hidden; } - + public void setHidden(final boolean hidden) { this.hidden = hidden; @@ -453,7 +460,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } return false; } - + public void updateActivity(final boolean broadcast) { if (isAfk()) @@ -466,7 +473,7 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } lastActivity = System.currentTimeMillis(); } - + public void checkActivity() { final long autoafkkick = ess.getSettings().getAutoAfkKick(); @@ -476,8 +483,8 @@ public class User extends UserData implements Comparable, IReplyTo, IUser final String kickReason = _("autoAfkKickReason", autoafkkick / 60.0); lastActivity = 0; kickPlayer(kickReason); - - + + for (Player player : ess.getServer().getOnlinePlayers()) { final User user = ess.getUser(player); @@ -497,12 +504,12 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } } } - + public Location getAfkPosition() { return afkPosition; } - + @Override public boolean toggleGodModeEnabled() { @@ -512,36 +519,36 @@ public class User extends UserData implements Comparable, IReplyTo, IUser } return super.toggleGodModeEnabled(); } - + @Override public boolean isGodModeEnabled() { return (super.isGodModeEnabled() && !ess.getSettings().getNoGodWorlds().contains(getLocation().getWorld().getName())) || (isAfk() && ess.getSettings().getFreezeAfkPlayers()); } - + public boolean isGodModeEnabledRaw() { return super.isGodModeEnabled(); } - + public String getGroup() { return ess.getPermissionsHandler().getGroup(base); } - + public boolean inGroup(final String group) { return ess.getPermissionsHandler().inGroup(base, group); } - + public boolean canBuild() { return ess.getPermissionsHandler().canBuild(base, getGroup()); } - + public long getTeleportRequestTime() { return teleportRequestTime; - } + } } diff --git a/Essentials/src/com/earth2me/essentials/UserData.java b/Essentials/src/com/earth2me/essentials/UserData.java index d08704484..e2a365ad2 100644 --- a/Essentials/src/com/earth2me/essentials/UserData.java +++ b/Essentials/src/com/earth2me/essentials/UserData.java @@ -264,6 +264,11 @@ public abstract class UserData extends PlayerExtension implements IConf { return (List)powertools.get(stack.getTypeId()); } + + public List getPowertool(int id) + { + return (List)powertools.get(id); + } public void setPowertool(ItemStack stack, List commandList) { diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java index f6b75c3a5..c979e5046 100644 --- a/Essentials/src/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -1,23 +1,20 @@ package com.earth2me.essentials; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.CacheLoader; -import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.File; -import java.util.Collections; -import java.util.Locale; +import java.lang.ref.SoftReference; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; import java.util.Set; -import java.util.concurrent.ConcurrentSkipListSet; -import java.util.concurrent.ExecutionException; import org.bukkit.entity.Player; -public class UserMap extends CacheLoader implements IConf +public class UserMap implements IConf { private final transient IEssentials ess; - private final transient Cache users = CacheBuilder.newBuilder().softValues().build(this); - private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); + private final transient Map> users = new HashMap>(); + //CacheBuilder.newBuilder().softValues().build(this); + //private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); public UserMap(final IEssentials ess) { @@ -38,16 +35,19 @@ public class UserMap extends CacheLoader implements IConf { return; } - keys.clear(); - users.invalidateAll(); - for (String string : userdir.list()) + synchronized (users) { - if (!string.endsWith(".yml")) + users.clear(); + + for (String string : userdir.list()) { - continue; + if (!string.endsWith(".yml")) + { + continue; + } + final String name = string.substring(0, string.length() - 4); + users.put(Util.sanitizeFileName(name), null); } - final String name = string.substring(0, string.length() - 4); - keys.add(Util.sanitizeFileName(name)); } } }); @@ -55,40 +55,43 @@ public class UserMap extends CacheLoader implements IConf public boolean userExists(final String name) { - return keys.contains(Util.sanitizeFileName(name)); + return users.containsKey(Util.sanitizeFileName(name)); } public User getUser(final String name) { try { - return users.get(Util.sanitizeFileName(name)); + synchronized (users) + { + final SoftReference softRef = users.get(Util.sanitizeFileName(name)); + User user = softRef == null ? null : softRef.get(); + if (user == null) + { + user = load(name); + users.put(name, new SoftReference(user)); + } + return user; + } } - catch (ExecutionException ex) - { - return null; - } - catch (UncheckedExecutionException ex) + catch (Exception ex) { return null; } } - @Override public User load(final String name) throws Exception { for (Player player : ess.getServer().getOnlinePlayers()) { if (player.getName().equalsIgnoreCase(name)) { - keys.add(Util.sanitizeFileName(name)); return new User(player, ess); } } final File userFile = getUserFile(name); if (userFile.exists()) { - keys.add(Util.sanitizeFileName(name)); return new User(new OfflinePlayer(name, ess), ess); } throw new Exception("User not found!"); @@ -102,20 +105,28 @@ public class UserMap extends CacheLoader implements IConf public void removeUser(final String name) { - keys.remove(Util.sanitizeFileName(name)); - users.invalidate(Util.sanitizeFileName(name)); + synchronized (users) + { + users.remove(Util.sanitizeFileName(name)); + } } public Set getAllUniqueUsers() { - return Collections.unmodifiableSet(keys); + synchronized (users) + { + return new HashSet(users.keySet()); + } } public int getUniqueUsers() { - return keys.size(); + synchronized (users) + { + return users.size(); + } } - + public File getUserFile(final String name) { final File userFolder = new File(ess.getDataFolder(), "userdata"); diff --git a/Essentials/src/com/earth2me/essentials/Util.java b/Essentials/src/com/earth2me/essentials/Util.java index 6a1df197f..a8a0cb1e4 100644 --- a/Essentials/src/com/earth2me/essentials/Util.java +++ b/Essentials/src/com/earth2me/essentials/Util.java @@ -21,10 +21,11 @@ public class Util { } private final static Logger logger = Logger.getLogger("Minecraft"); + private final static Pattern INVALIDCHARS = Pattern.compile("[^a-z0-9]"); - public static String sanitizeFileName(String name) + public static String sanitizeFileName(final String name) { - return name.toLowerCase(Locale.ENGLISH).replaceAll("[^a-z0-9]", "_"); + return INVALIDCHARS.matcher(name.toLowerCase(Locale.ENGLISH)).replaceAll("_"); } public static String formatDateDiff(long date) From 91563e9dca63e8a850be0ac5accb70d10efa8b3d Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 01:55:04 +0100 Subject: [PATCH 7/9] Optimize Break in Protect --- .../EssentialsProtectBlockListener.java | 62 +++++++++++++++++-- 1 file changed, 58 insertions(+), 4 deletions(-) diff --git a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java index 03ad19cb4..a1d16af25 100644 --- a/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java +++ b/EssentialsProtect/src/com/earth2me/essentials/protect/EssentialsProtectBlockListener.java @@ -56,7 +56,7 @@ public class EssentialsProtectBlockListener extends BlockListener final Block below = blockPlaced.getRelative(BlockFace.DOWN); if ((below.getType() == Material.RAILS || below.getType() == Material.POWERED_RAIL || below.getType() == Material.DETECTOR_RAIL) && prot.getSettingBool(ProtectConfig.prevent_block_on_rail) - && prot.getStorage().isProtected(below, user.getName())) + && isProtected(below, user)) { event.setCancelled(true); return; @@ -69,7 +69,7 @@ public class EssentialsProtectBlockListener extends BlockListener { protect.add(blockPlaced); if (prot.getSettingBool(ProtectConfig.protect_below_rails) - && !prot.getStorage().isProtected(blockPlaced.getRelative(BlockFace.DOWN), user.getName())) + && !isProtected(blockPlaced.getRelative(BlockFace.DOWN), user)) { protect.add(blockPlaced.getRelative(BlockFace.DOWN)); } @@ -82,7 +82,7 @@ public class EssentialsProtectBlockListener extends BlockListener if (prot.getSettingBool(ProtectConfig.protect_against_signs) && event.getBlockAgainst().getType() != Material.SIGN_POST && event.getBlockAgainst().getType() != Material.WALL_SIGN - && !prot.getStorage().isProtected(event.getBlockAgainst(), user.getName())) + && !isProtected(event.getBlockAgainst(), user)) { protect.add(event.getBlockAgainst()); } @@ -283,7 +283,7 @@ public class EssentialsProtectBlockListener extends BlockListener else { - final boolean isProtected = storage.isProtected(block, user.getName()); + final boolean isProtected = isProtected(block, user); if (isProtected) { event.setCancelled(true); @@ -422,4 +422,58 @@ public class EssentialsProtectBlockListener extends BlockListener } } } + + private boolean isProtected(final Block block, final User user) + { + final Material type = block.getType(); + if (prot.getSettingBool(ProtectConfig.protect_signs)) + { + if (type == Material.WALL_SIGN || type == Material.SIGN_POST) + { + return prot.getStorage().isProtected(block, user.getName()); + } + if (prot.getSettingBool(ProtectConfig.protect_against_signs)) + { + final Block up = block.getRelative(BlockFace.UP); + if (up != null && up.getType() == Material.SIGN_POST) + { + return prot.getStorage().isProtected(block, user.getName()); + } + final BlockFace[] directions = new BlockFace[] + { + BlockFace.NORTH, + BlockFace.EAST, + BlockFace.SOUTH, + BlockFace.WEST + }; + for (BlockFace blockFace : directions) + { + final Block signblock = block.getRelative(blockFace); + if (signblock.getType() == Material.WALL_SIGN) + { + final org.bukkit.material.Sign signMat = (org.bukkit.material.Sign)signblock.getState().getData(); + if (signMat != null && signMat.getFacing() == blockFace) + { + return prot.getStorage().isProtected(block, user.getName()); + } + } + } + } + } + if (prot.getSettingBool(ProtectConfig.protect_rails)) { + if (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL) + { + return prot.getStorage().isProtected(block, user.getName()); + } + if (prot.getSettingBool(ProtectConfig.protect_below_rails)) + { + final Block up = block.getRelative(BlockFace.UP); + if (up != null && (type == Material.RAILS || type == Material.POWERED_RAIL || type == Material.DETECTOR_RAIL)) + { + return prot.getStorage().isProtected(block, user.getName()); + } + } + } + return false; + } } From 81ec87d893db63a749828fba9af851ed07076cca Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 02:12:20 +0100 Subject: [PATCH 8/9] Revert changes to Usermap --- .../src/com/earth2me/essentials/UserMap.java | 76 ++++++++----------- 1 file changed, 32 insertions(+), 44 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/UserMap.java b/Essentials/src/com/earth2me/essentials/UserMap.java index c979e5046..d15438c7d 100644 --- a/Essentials/src/com/earth2me/essentials/UserMap.java +++ b/Essentials/src/com/earth2me/essentials/UserMap.java @@ -1,20 +1,22 @@ package com.earth2me.essentials; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.CacheLoader; +import com.google.common.util.concurrent.UncheckedExecutionException; import java.io.File; -import java.lang.ref.SoftReference; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; +import java.util.Collections; import java.util.Set; +import java.util.concurrent.ConcurrentSkipListSet; +import java.util.concurrent.ExecutionException; import org.bukkit.entity.Player; -public class UserMap implements IConf +public class UserMap extends CacheLoader implements IConf { private final transient IEssentials ess; - private final transient Map> users = new HashMap>(); - //CacheBuilder.newBuilder().softValues().build(this); - //private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); + private final transient Cache users = CacheBuilder.newBuilder().softValues().build(this); + private final transient ConcurrentSkipListSet keys = new ConcurrentSkipListSet(); public UserMap(final IEssentials ess) { @@ -35,19 +37,16 @@ public class UserMap implements IConf { return; } - synchronized (users) + keys.clear(); + users.invalidateAll(); + for (String string : userdir.list()) { - users.clear(); - - for (String string : userdir.list()) + if (!string.endsWith(".yml")) { - if (!string.endsWith(".yml")) - { - continue; - } - final String name = string.substring(0, string.length() - 4); - users.put(Util.sanitizeFileName(name), null); + continue; } + final String name = string.substring(0, string.length() - 4); + keys.add(Util.sanitizeFileName(name)); } } }); @@ -55,43 +54,40 @@ public class UserMap implements IConf public boolean userExists(final String name) { - return users.containsKey(Util.sanitizeFileName(name)); + return keys.contains(Util.sanitizeFileName(name)); } public User getUser(final String name) { try { - synchronized (users) - { - final SoftReference softRef = users.get(Util.sanitizeFileName(name)); - User user = softRef == null ? null : softRef.get(); - if (user == null) - { - user = load(name); - users.put(name, new SoftReference(user)); - } - return user; - } + return users.get(Util.sanitizeFileName(name)); } - catch (Exception ex) + catch (ExecutionException ex) + { + return null; + } + catch (UncheckedExecutionException ex) { return null; } } + @Override public User load(final String name) throws Exception { for (Player player : ess.getServer().getOnlinePlayers()) { if (player.getName().equalsIgnoreCase(name)) { + keys.add(Util.sanitizeFileName(name)); return new User(player, ess); } } final File userFile = getUserFile(name); if (userFile.exists()) { + keys.add(Util.sanitizeFileName(name)); return new User(new OfflinePlayer(name, ess), ess); } throw new Exception("User not found!"); @@ -105,28 +101,20 @@ public class UserMap implements IConf public void removeUser(final String name) { - synchronized (users) - { - users.remove(Util.sanitizeFileName(name)); - } + keys.remove(Util.sanitizeFileName(name)); + users.invalidate(Util.sanitizeFileName(name)); } public Set getAllUniqueUsers() { - synchronized (users) - { - return new HashSet(users.keySet()); - } + return Collections.unmodifiableSet(keys); } public int getUniqueUsers() { - synchronized (users) - { - return users.size(); - } + return keys.size(); } - + public File getUserFile(final String name) { final File userFolder = new File(ess.getDataFolder(), "userdata"); From f26cccb663b3cd255bbfe3b72499b0efdd3da279 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 04:51:27 +0100 Subject: [PATCH 9/9] Optimize TextInput to cache motd and info textfiles. --- .../textreader/KeywordReplacer.java | 13 ++-- .../essentials/textreader/TextInput.java | 77 +++++++++++++------ 2 files changed, 62 insertions(+), 28 deletions(-) diff --git a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java index 782798bff..a7aab67ba 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java +++ b/Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.DescParseTickFormat; import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import java.text.DateFormat; +import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; @@ -16,15 +17,17 @@ import org.bukkit.plugin.Plugin; public class KeywordReplacer implements IText { private final transient IText input; + private final transient List replaced; private final transient IEssentials ess; - + public KeywordReplacer(final IText input, final CommandSender sender, final IEssentials ess) { this.input = input; + this.replaced = new ArrayList(this.input.getLines().size()); this.ess = ess; replaceKeywords(sender); } - + private void replaceKeywords(final CommandSender sender) { String displayName, ipAddress, balance, mails, world; @@ -98,7 +101,7 @@ public class KeywordReplacer implements IText date = DateFormat.getDateInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); time = DateFormat.getTimeInstance(DateFormat.MEDIUM, ess.getI18n().getCurrentLocale()).format(new Date()); - + version = ess.getServer().getVersion(); for (int i = 0; i < input.getLines().size(); i++) @@ -120,14 +123,14 @@ public class KeywordReplacer implements IText line = line.replace("{WORLDDATE}", worldDate); line = line.replace("{PLUGINS}", plugins); line = line.replace("{VERSION}", version); - input.getLines().set(i, line); + replaced.add(line); } } @Override public List getLines() { - return input.getLines(); + return replaced; } @Override diff --git a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java b/Essentials/src/com/earth2me/essentials/textreader/TextInput.java index b25c30d51..316a0b576 100644 --- a/Essentials/src/com/earth2me/essentials/textreader/TextInput.java +++ b/Essentials/src/com/earth2me/essentials/textreader/TextInput.java @@ -4,6 +4,7 @@ import com.earth2me.essentials.IEssentials; import com.earth2me.essentials.User; import com.earth2me.essentials.Util; import java.io.*; +import java.lang.ref.SoftReference; import java.util.*; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; @@ -11,9 +12,11 @@ import org.bukkit.entity.Player; public class TextInput implements IText { - private final transient List lines = new ArrayList(); - private final transient List chapters = new ArrayList(); - private final transient Map bookmarks = new HashMap(); + private final transient List lines; + private final transient List chapters; + private final transient Map bookmarks; + private final transient long lastChange; + private final static HashMap> cache = new HashMap>(); public TextInput(final CommandSender sender, final String filename, final boolean createFile, final IEssentials ess) throws IOException { @@ -34,33 +37,62 @@ public class TextInput implements IText } if (file.exists()) { - final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); - try + lastChange = file.lastModified(); + boolean readFromfile; + synchronized (cache) { - int lineNumber = 0; - while (bufferedReader.ready()) + final SoftReference inputRef = cache.get(file.getName()); + TextInput input; + if (inputRef == null || (input = inputRef.get()) == null || input.lastChange < lastChange) { - final String line = bufferedReader.readLine(); - if (line == null) - { - break; - } - if (line.length() > 0 && line.charAt(0) == '#') - { - bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber); - chapters.add(line.substring(1).replace('&', '§').replace("§§", "&")); - } - lines.add(line.replace('&', '§').replace("§§", "&")); - lineNumber++; + lines = new ArrayList(); + chapters = new ArrayList(); + bookmarks = new HashMap(); + cache.put(file.getName(), new SoftReference(this)); + readFromfile = true; + } + else + { + lines = Collections.unmodifiableList(input.getLines()); + chapters = Collections.unmodifiableList(input.getChapters()); + bookmarks = Collections.unmodifiableMap(input.getBookmarks()); + readFromfile = false; } } - finally + if (readFromfile) { - bufferedReader.close(); + final BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + try + { + int lineNumber = 0; + while (bufferedReader.ready()) + { + final String line = bufferedReader.readLine(); + if (line == null) + { + break; + } + if (line.length() > 0 && line.charAt(0) == '#') + { + bookmarks.put(line.substring(1).toLowerCase(Locale.ENGLISH).replaceAll("&[0-9a-f]", ""), lineNumber); + chapters.add(line.substring(1).replace('&', '§').replace("§§", "&")); + } + lines.add(line.replace('&', '§').replace("§§", "&")); + lineNumber++; + } + } + finally + { + bufferedReader.close(); + } } } else { + lastChange = 0; + lines = Collections.emptyList(); + chapters = Collections.emptyList(); + bookmarks = Collections.emptyMap(); if (createFile) { final InputStream input = ess.getResource(filename + ".txt"); @@ -68,8 +100,7 @@ public class TextInput implements IText try { final byte[] buffer = new byte[1024]; - int length = 0; - length = input.read(buffer); + int length = input.read(buffer); while (length > 0) { output.write(buffer, 0, length);