Speed improvements for Move and Interact Event.

This commit is contained in:
snowleo 2012-01-16 00:50:52 +01:00
parent c860b1c668
commit 359ea194b8
5 changed files with 128 additions and 104 deletions

View File

@ -282,22 +282,22 @@ public class EssentialsPlayerListener extends PlayerListener
{ {
final User user = ess.getUser(event.getPlayer()); final User user = ess.getUser(event.getPlayer());
user.updateActivity(true); 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(); final ItemStack is = user.getItemInHand();
if (is == null || is.getType() == Material.AIR || !user.arePowerToolsEnabled()) int id;
if (is == null || (id = is.getTypeId()) == 0)
{ {
return; return;
} }
final List<String> commandList = user.getPowertool(is); final List<String> commandList = user.getPowertool(id);
if (commandList == null || commandList.isEmpty()) if (commandList == null || commandList.isEmpty())
{ {
return; return;
@ -317,7 +317,7 @@ public class EssentialsPlayerListener extends PlayerListener
} }
else else
{ {
user.getServer().dispatchCommand(event.getPlayer(), command); user.getServer().dispatchCommand(user.getBase(), command);
} }
} }
} }

View File

@ -23,34 +23,37 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
private transient long lastOnlineActivity; private transient long lastOnlineActivity;
private transient long lastActivity = System.currentTimeMillis(); private transient long lastActivity = System.currentTimeMillis();
private boolean hidden = false; private boolean hidden = false;
private transient Location afkPosition; private transient Location afkPosition = null;
private static final Logger logger = Logger.getLogger("Minecraft"); private static final Logger logger = Logger.getLogger("Minecraft");
User(final Player base, final IEssentials ess) User(final Player base, final IEssentials ess)
{ {
super(base, ess); super(base, ess);
teleport = new Teleport(this, ess); teleport = new Teleport(this, ess);
afkPosition = getLocation(); if (isAfk())
{
afkPosition = getLocation();
}
} }
User update(final Player base) User update(final Player base)
{ {
setBase(base); setBase(base);
return this; return this;
} }
@Override @Override
public boolean isAuthorized(final IEssentialsCommand cmd) public boolean isAuthorized(final IEssentialsCommand cmd)
{ {
return isAuthorized(cmd, "essentials."); return isAuthorized(cmd, "essentials.");
} }
@Override @Override
public boolean isAuthorized(final IEssentialsCommand cmd, final String permissionPrefix) public boolean isAuthorized(final IEssentialsCommand cmd, final String permissionPrefix)
{ {
return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName())); return isAuthorized(permissionPrefix + (cmd.getName().equals("r") ? "msg" : cmd.getName()));
} }
@Override @Override
public boolean isAuthorized(final String node) public boolean isAuthorized(final String node)
{ {
@ -58,20 +61,20 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
{ {
return false; return false;
} }
if (isOp()) if (isOp())
{ {
return true; return true;
} }
if (isJailed()) if (isJailed())
{ {
return false; return false;
} }
return ess.getPermissionsHandler().hasPermission(base, node); return ess.getPermissionsHandler().hasPermission(base, node);
} }
public void healCooldown() throws Exception public void healCooldown() throws Exception
{ {
final Calendar now = new GregorianCalendar(); final Calendar now = new GregorianCalendar();
@ -89,13 +92,13 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
setLastHealTimestamp(now.getTimeInMillis()); setLastHealTimestamp(now.getTimeInMillis());
} }
@Override @Override
public void giveMoney(final double value) public void giveMoney(final double value)
{ {
giveMoney(value, null); giveMoney(value, null);
} }
public void giveMoney(final double value, final CommandSender initiator) public void giveMoney(final double value, final CommandSender initiator)
{ {
if (value == 0) if (value == 0)
@ -109,7 +112,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
initiator.sendMessage(_("addedToOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName())); initiator.sendMessage(_("addedToOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()));
} }
} }
public void payUser(final User reciever, final double value) throws Exception public void payUser(final User reciever, final double value) throws Exception
{ {
if (value == 0) if (value == 0)
@ -128,13 +131,13 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
throw new Exception(_("notEnoughMoney")); throw new Exception(_("notEnoughMoney"));
} }
} }
@Override @Override
public void takeMoney(final double value) public void takeMoney(final double value)
{ {
takeMoney(value, null); takeMoney(value, null);
} }
public void takeMoney(final double value, final CommandSender initiator) public void takeMoney(final double value, final CommandSender initiator)
{ {
if (value == 0) if (value == 0)
@ -148,36 +151,36 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
initiator.sendMessage(_("takenFromOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName())); initiator.sendMessage(_("takenFromOthersAccount", Util.formatCurrency(value, ess), this.getDisplayName()));
} }
} }
public boolean canAfford(final double cost) public boolean canAfford(final double cost)
{ {
final double mon = getMoney(); final double mon = getMoney();
return mon >= cost || isAuthorized("essentials.eco.loan"); return mon >= cost || isAuthorized("essentials.eco.loan");
} }
public void dispose() public void dispose()
{ {
this.base = new OfflinePlayer(getName(), ess); this.base = new OfflinePlayer(getName(), ess);
} }
@Override @Override
public void setReplyTo(final CommandSender user) public void setReplyTo(final CommandSender user)
{ {
replyTo = user; replyTo = user;
} }
@Override @Override
public CommandSender getReplyTo() public CommandSender getReplyTo()
{ {
return replyTo; return replyTo;
} }
@Override @Override
public int compareTo(final User other) public int compareTo(final User other)
{ {
return Util.stripColor(this.getDisplayName()).compareToIgnoreCase(Util.stripColor(other.getDisplayName())); return Util.stripColor(this.getDisplayName()).compareToIgnoreCase(Util.stripColor(other.getDisplayName()));
} }
@Override @Override
public boolean equals(final Object object) public boolean equals(final Object object)
{ {
@ -186,58 +189,58 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
return false; return false;
} }
return this.getName().equalsIgnoreCase(((User)object).getName()); return this.getName().equalsIgnoreCase(((User)object).getName());
} }
@Override @Override
public int hashCode() public int hashCode()
{ {
return this.getName().hashCode(); return this.getName().hashCode();
} }
public Boolean canSpawnItem(final int itemId) public Boolean canSpawnItem(final int itemId)
{ {
return !ess.getSettings().itemSpawnBlacklist().contains(itemId); return !ess.getSettings().itemSpawnBlacklist().contains(itemId);
} }
public Location getHome() throws Exception public Location getHome() throws Exception
{ {
return getHome(getHomes().get(0)); return getHome(getHomes().get(0));
} }
public void setHome() public void setHome()
{ {
setHome("home", getLocation()); setHome("home", getLocation());
} }
public void setHome(final String name) public void setHome(final String name)
{ {
setHome(name, getLocation()); setHome(name, getLocation());
} }
@Override @Override
public void setLastLocation() public void setLastLocation()
{ {
setLastLocation(getLocation()); setLastLocation(getLocation());
} }
public void requestTeleport(final User player, final boolean here) public void requestTeleport(final User player, final boolean here)
{ {
teleportRequestTime = System.currentTimeMillis(); teleportRequestTime = System.currentTimeMillis();
teleportRequester = player; teleportRequester = player;
teleportRequestHere = here; teleportRequestHere = here;
} }
public User getTeleportRequest() public User getTeleportRequest()
{ {
return teleportRequester; return teleportRequester;
} }
public boolean isTeleportRequestHere() public boolean isTeleportRequestHere()
{ {
return teleportRequestHere; return teleportRequestHere;
} }
public String getNick(boolean addprefixsuffix) public String getNick(boolean addprefixsuffix)
{ {
final StringBuilder nickname = new StringBuilder(); final StringBuilder nickname = new StringBuilder();
@ -261,7 +264,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
{ {
} }
} }
if (addprefixsuffix && ess.getSettings().addPrefixSuffix()) if (addprefixsuffix && ess.getSettings().addPrefixSuffix())
{ {
if (!ess.getSettings().disablePrefix()) if (!ess.getSettings().disablePrefix())
@ -283,10 +286,10 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
nickname.append("§f"); nickname.append("§f");
} }
} }
return nickname.toString(); return nickname.toString();
} }
public void setDisplayNick() public void setDisplayNick()
{ {
String name = getNick(true); String name = getNick(true);
@ -308,7 +311,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
logger.log(Level.INFO, "Playerlist for " + name + " was not updated. Use a shorter displayname prefix."); logger.log(Level.INFO, "Playerlist for " + name + " was not updated. Use a shorter displayname prefix.");
} }
} }
@Override @Override
public String getDisplayName() public String getDisplayName()
{ {
@ -318,22 +321,22 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
return super.getDisplayName() == null ? super.getName() : super.getDisplayName(); return super.getDisplayName() == null ? super.getName() : super.getDisplayName();
} }
public Teleport getTeleport() public Teleport getTeleport()
{ {
return teleport; return teleport;
} }
public long getLastOnlineActivity() public long getLastOnlineActivity()
{ {
return lastOnlineActivity; return lastOnlineActivity;
} }
public void setLastOnlineActivity(final long timestamp) public void setLastOnlineActivity(final long timestamp)
{ {
lastOnlineActivity = timestamp; lastOnlineActivity = timestamp;
} }
@Override @Override
public double getMoney() public double getMoney()
{ {
@ -355,7 +358,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
return super.getMoney(); return super.getMoney();
} }
@Override @Override
public void setMoney(final double value) public void setMoney(final double value)
{ {
@ -377,7 +380,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
super.setMoney(value); super.setMoney(value);
} }
@Override @Override
public void setAfk(final boolean set) public void setAfk(final boolean set)
{ {
@ -386,9 +389,13 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
{ {
afkPosition = getLocation(); afkPosition = getLocation();
} }
else if (!set && isAfk())
{
afkPosition = null;
}
super.setAfk(set); super.setAfk(set);
} }
@Override @Override
public boolean toggleAfk() public boolean toggleAfk()
{ {
@ -396,13 +403,13 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now); this.setSleepingIgnored(this.isAuthorized("essentials.sleepingignored") ? true : now);
return now; return now;
} }
@Override @Override
public boolean isHidden() public boolean isHidden()
{ {
return hidden; return hidden;
} }
public void setHidden(final boolean hidden) public void setHidden(final boolean hidden)
{ {
this.hidden = hidden; this.hidden = hidden;
@ -453,7 +460,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
return false; return false;
} }
public void updateActivity(final boolean broadcast) public void updateActivity(final boolean broadcast)
{ {
if (isAfk()) if (isAfk())
@ -466,7 +473,7 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
lastActivity = System.currentTimeMillis(); lastActivity = System.currentTimeMillis();
} }
public void checkActivity() public void checkActivity()
{ {
final long autoafkkick = ess.getSettings().getAutoAfkKick(); final long autoafkkick = ess.getSettings().getAutoAfkKick();
@ -476,8 +483,8 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
final String kickReason = _("autoAfkKickReason", autoafkkick / 60.0); final String kickReason = _("autoAfkKickReason", autoafkkick / 60.0);
lastActivity = 0; lastActivity = 0;
kickPlayer(kickReason); kickPlayer(kickReason);
for (Player player : ess.getServer().getOnlinePlayers()) for (Player player : ess.getServer().getOnlinePlayers())
{ {
final User user = ess.getUser(player); final User user = ess.getUser(player);
@ -497,12 +504,12 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
} }
} }
public Location getAfkPosition() public Location getAfkPosition()
{ {
return afkPosition; return afkPosition;
} }
@Override @Override
public boolean toggleGodModeEnabled() public boolean toggleGodModeEnabled()
{ {
@ -512,36 +519,36 @@ public class User extends UserData implements Comparable<User>, IReplyTo, IUser
} }
return super.toggleGodModeEnabled(); return super.toggleGodModeEnabled();
} }
@Override @Override
public boolean isGodModeEnabled() public boolean isGodModeEnabled()
{ {
return (super.isGodModeEnabled() && !ess.getSettings().getNoGodWorlds().contains(getLocation().getWorld().getName())) return (super.isGodModeEnabled() && !ess.getSettings().getNoGodWorlds().contains(getLocation().getWorld().getName()))
|| (isAfk() && ess.getSettings().getFreezeAfkPlayers()); || (isAfk() && ess.getSettings().getFreezeAfkPlayers());
} }
public boolean isGodModeEnabledRaw() public boolean isGodModeEnabledRaw()
{ {
return super.isGodModeEnabled(); return super.isGodModeEnabled();
} }
public String getGroup() public String getGroup()
{ {
return ess.getPermissionsHandler().getGroup(base); return ess.getPermissionsHandler().getGroup(base);
} }
public boolean inGroup(final String group) public boolean inGroup(final String group)
{ {
return ess.getPermissionsHandler().inGroup(base, group); return ess.getPermissionsHandler().inGroup(base, group);
} }
public boolean canBuild() public boolean canBuild()
{ {
return ess.getPermissionsHandler().canBuild(base, getGroup()); return ess.getPermissionsHandler().canBuild(base, getGroup());
} }
public long getTeleportRequestTime() public long getTeleportRequestTime()
{ {
return teleportRequestTime; return teleportRequestTime;
} }
} }

View File

@ -264,6 +264,11 @@ public abstract class UserData extends PlayerExtension implements IConf
{ {
return (List<String>)powertools.get(stack.getTypeId()); return (List<String>)powertools.get(stack.getTypeId());
} }
public List<String> getPowertool(int id)
{
return (List<String>)powertools.get(id);
}
public void setPowertool(ItemStack stack, List<String> commandList) public void setPowertool(ItemStack stack, List<String> commandList)
{ {

View File

@ -1,23 +1,20 @@
package com.earth2me.essentials; 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.io.File;
import java.util.Collections; import java.lang.ref.SoftReference;
import java.util.Locale; import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.ExecutionException;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
public class UserMap extends CacheLoader<String, User> implements IConf public class UserMap implements IConf
{ {
private final transient IEssentials ess; private final transient IEssentials ess;
private final transient Cache<String, User> users = CacheBuilder.newBuilder().softValues().build(this); private final transient Map<String, SoftReference<User>> users = new HashMap<String, SoftReference<User>>();
private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>(); //CacheBuilder.newBuilder().softValues().build(this);
//private final transient ConcurrentSkipListSet<String> keys = new ConcurrentSkipListSet<String>();
public UserMap(final IEssentials ess) public UserMap(final IEssentials ess)
{ {
@ -38,16 +35,19 @@ public class UserMap extends CacheLoader<String, User> implements IConf
{ {
return; return;
} }
keys.clear(); synchronized (users)
users.invalidateAll();
for (String string : userdir.list())
{ {
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<String, User> implements IConf
public boolean userExists(final String name) public boolean userExists(final String name)
{ {
return keys.contains(Util.sanitizeFileName(name)); return users.containsKey(Util.sanitizeFileName(name));
} }
public User getUser(final String name) public User getUser(final String name)
{ {
try try
{ {
return users.get(Util.sanitizeFileName(name)); synchronized (users)
{
final SoftReference<User> 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>(user));
}
return user;
}
} }
catch (ExecutionException ex) catch (Exception ex)
{
return null;
}
catch (UncheckedExecutionException ex)
{ {
return null; return null;
} }
} }
@Override
public User load(final String name) throws Exception public User load(final String name) throws Exception
{ {
for (Player player : ess.getServer().getOnlinePlayers()) for (Player player : ess.getServer().getOnlinePlayers())
{ {
if (player.getName().equalsIgnoreCase(name)) if (player.getName().equalsIgnoreCase(name))
{ {
keys.add(Util.sanitizeFileName(name));
return new User(player, ess); return new User(player, ess);
} }
} }
final File userFile = getUserFile(name); final File userFile = getUserFile(name);
if (userFile.exists()) if (userFile.exists())
{ {
keys.add(Util.sanitizeFileName(name));
return new User(new OfflinePlayer(name, ess), ess); return new User(new OfflinePlayer(name, ess), ess);
} }
throw new Exception("User not found!"); throw new Exception("User not found!");
@ -102,20 +105,28 @@ public class UserMap extends CacheLoader<String, User> implements IConf
public void removeUser(final String name) public void removeUser(final String name)
{ {
keys.remove(Util.sanitizeFileName(name)); synchronized (users)
users.invalidate(Util.sanitizeFileName(name)); {
users.remove(Util.sanitizeFileName(name));
}
} }
public Set<String> getAllUniqueUsers() public Set<String> getAllUniqueUsers()
{ {
return Collections.unmodifiableSet(keys); synchronized (users)
{
return new HashSet<String>(users.keySet());
}
} }
public int getUniqueUsers() public int getUniqueUsers()
{ {
return keys.size(); synchronized (users)
{
return users.size();
}
} }
public File getUserFile(final String name) public File getUserFile(final String name)
{ {
final File userFolder = new File(ess.getDataFolder(), "userdata"); final File userFolder = new File(ess.getDataFolder(), "userdata");

View File

@ -21,10 +21,11 @@ public class Util
{ {
} }
private final static Logger logger = Logger.getLogger("Minecraft"); 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) public static String formatDateDiff(long date)