From 359ea194b8d5348b5d2f0b3c67f83ee7a6539513 Mon Sep 17 00:00:00 2001 From: snowleo Date: Mon, 16 Jan 2012 00:50:52 +0100 Subject: [PATCH] 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)