Optimize player / user iteration.

* Add a method for backwards compatability with unmapped code.
* Convert all getOnlinePlayers() calls to use this method, part of the IEssentials interface
* Add a new method getOnlineUsers() Ljava/lang/Iterable;
* Convert appropriate calls to use this method
* Update Bukkit to #1945
* Update CraftBukkit to #3103
This commit is contained in:
md_5 2014-07-12 16:03:36 +10:00 committed by KHobbits
parent ed56afda26
commit 465041b98b
27 changed files with 126 additions and 63 deletions

View File

@ -24,7 +24,7 @@ public class Backup implements Runnable
{
this.ess = ess;
server = ess.getServer();
if (server.getOnlinePlayers().length > 0)
if (!ess.getOnlinePlayers().isEmpty())
{
ess.runTaskAsynchronously(new Runnable()
{
@ -152,7 +152,7 @@ public class Backup implements Runnable
public void run()
{
server.dispatchCommand(cs, "save-on");
if (server.getOnlinePlayers().length == 0)
if (ess.getOnlinePlayers().isEmpty())
{
stopTask();
}

View File

@ -35,10 +35,17 @@ import com.earth2me.essentials.textreader.IText;
import com.earth2me.essentials.textreader.KeywordReplacer;
import com.earth2me.essentials.textreader.SimpleTextInput;
import com.earth2me.essentials.utils.DateUtil;
import com.google.common.base.Function;
import com.google.common.base.Throwables;
import com.google.common.collect.Iterables;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import java.util.logging.Level;
@ -100,6 +107,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
private transient Metrics metrics;
private transient EssentialsTimer timer;
private final transient List<String> vanishedPlayers = new ArrayList<String>();
private transient Method oldGetOnlinePlayers;
public Essentials()
{
@ -181,6 +189,16 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
LOGGER.log(Level.INFO, getServer().getBukkitVersion());
}
execTimer.mark("BukkitCheck");
for (Method method : Server.class.getDeclaredMethods())
{
if (method.getName().endsWith("getOnlinePlayers") && method.getReturnType() == Player[].class)
{
oldGetOnlinePlayers = method;
break;
}
}
try
{
final EssentialsUpgrade upgrade = new EssentialsUpgrade(this);
@ -784,7 +802,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
IText broadcast = new SimpleTextInput(message);
final Player[] players = getServer().getOnlinePlayers();
final Collection<Player> players = getOnlinePlayers();
for (Player player : players)
{
@ -803,7 +821,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
}
}
return players.length;
return players.size();
}
@Override
@ -890,6 +908,43 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials
return vanishedPlayers;
}
@Override
public Collection<Player> getOnlinePlayers()
{
try
{
return (Collection<Player>)getServer().getOnlinePlayers(); // Needed for sanity here, the Bukkit API is a bit broken in the sense it only allows subclasses of Player to this list
}
catch (NoSuchMethodError ex)
{
try
{
return Arrays.asList((Player[])oldGetOnlinePlayers.invoke(getServer()));
}
catch (InvocationTargetException ex1)
{
throw Throwables.propagate(ex.getCause());
}
catch (IllegalAccessException ex1)
{
throw new RuntimeException("Error invoking oldGetOnlinePlayers", ex1);
}
}
}
@Override
public Iterable<User> getOnlineUsers()
{
return Iterables.transform(getOnlinePlayers(), new Function<Player, User>()
{
@Override
public User apply(Player player)
{
return getUser(player);
}
});
}
private static class EssentialsWorldListener implements Listener, Runnable
{

View File

@ -435,10 +435,9 @@ public class EssentialsPlayerListener implements Listener
final String cmd = event.getMessage().toLowerCase(Locale.ENGLISH).split(" ")[0].replace("/", "").toLowerCase(Locale.ENGLISH);
if (ess.getSettings().getSocialSpyCommands().contains(cmd) || ess.getSettings().getSocialSpyCommands().contains("*"))
{
for (Player onlinePlayer : ess.getServer().getOnlinePlayers())
for (User spyer : ess.getOnlineUsers())
{
final User spyer = ess.getUser(onlinePlayer);
if (spyer.isSocialSpyEnabled() && !player.equals(onlinePlayer))
if (spyer.isSocialSpyEnabled() && !player.equals(spyer.getBase()))
{
spyer.sendMessage(player.getDisplayName() + " : " + event.getMessage());
}

View File

@ -48,7 +48,7 @@ public class EssentialsTimer implements Runnable
}
lastPoll = startTime;
int count = 0;
for (Player player : ess.getServer().getOnlinePlayers())
for (Player player : ess.getOnlinePlayers())
{
count++;
if (skip1 > 0)

View File

@ -6,6 +6,7 @@ import com.earth2me.essentials.api.IWarps;
import com.earth2me.essentials.metrics.Metrics;
import com.earth2me.essentials.perm.PermissionsHandler;
import com.earth2me.essentials.register.payment.Methods;
import java.util.Collection;
import java.util.List;
import java.util.UUID;
import org.bukkit.World;
@ -91,4 +92,8 @@ public interface IEssentials extends Plugin
EssentialsTimer getTimer();
List<String> getVanishedPlayers();
Collection<Player> getOnlinePlayers();
Iterable<User> getOnlineUsers();
}

View File

@ -49,12 +49,12 @@ public class PlayerList
Server server = ess.getServer();
int playerHidden = 0;
int hiddenCount = 0;
for (Player onlinePlayer : server.getOnlinePlayers())
for (User onlinePlayer : ess.getOnlineUsers())
{
if (ess.getUser(onlinePlayer).isHidden() || (user != null && !user.getBase().canSee(onlinePlayer)))
if (onlinePlayer.isHidden() || (user != null && !user.getBase().canSee(onlinePlayer.getBase())))
{
playerHidden++;
if (showHidden || user.getBase().canSee(onlinePlayer))
if (showHidden || user.getBase().canSee(onlinePlayer.getBase()))
{
hiddenCount++;
}
@ -63,11 +63,11 @@ public class PlayerList
String online;
if (hiddenCount > 0)
{
online = tl("listAmountHidden", server.getOnlinePlayers().length - playerHidden, hiddenCount, server.getMaxPlayers());
online = tl("listAmountHidden", ess.getOnlinePlayers().size() - playerHidden, hiddenCount, server.getMaxPlayers());
}
else
{
online = tl("listAmount", server.getOnlinePlayers().length - playerHidden, server.getMaxPlayers());
online = tl("listAmount", ess.getOnlinePlayers().size() - playerHidden, server.getMaxPlayers());
}
return online;
}
@ -77,11 +77,10 @@ public class PlayerList
{
Server server = ess.getServer();
final Map<String, List<User>> playerList = new HashMap<String, List<User>>();
for (Player onlinePlayer : server.getOnlinePlayers())
for (User onlineUser : ess.getOnlineUsers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if ((sender == null && !showHidden && onlineUser.isHidden()) ||
(sender != null && !showHidden && !sender.getBase().canSee(onlinePlayer)))
(sender != null && !showHidden && !sender.getBase().canSee(onlineUser.getBase())))
{
continue;
}

View File

@ -623,9 +623,8 @@ public class User extends UserData implements Comparable<User>, IReplyTo, net.es
this.getBase().kickPlayer(kickReason);
for (Player player : ess.getServer().getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
final User user = ess.getUser(player);
if (user.isAuthorized("essentials.kick.notify"))
{
user.sendMessage(tl("playerKicked", Console.NAME, getName(), kickReason));
@ -765,11 +764,11 @@ public class User extends UserData implements Comparable<User>, IReplyTo, net.es
vanished = set;
if (set)
{
for (Player p : ess.getServer().getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
if (!ess.getUser(p).isAuthorized("essentials.vanish.see"))
if (!user.isAuthorized("essentials.vanish.see"))
{
p.hidePlayer(getBase());
user.getBase().hidePlayer(getBase());
}
}
setHidden(true);

View File

@ -5,8 +5,7 @@ import static com.earth2me.essentials.I18n.tl;
import com.earth2me.essentials.User;
import com.earth2me.essentials.utils.NumberUtil;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import java.util.Locale;
import org.bukkit.Server;
import org.bukkit.entity.Player;
@ -36,7 +35,7 @@ public class Commandclearinventory extends EssentialsCommand
private void parseCommand(Server server, CommandSource sender, String[] args, boolean allowOthers, boolean allowAll) throws Exception
{
List<Player> players = new ArrayList<Player>();
Collection<Player> players = new ArrayList<Player>();
int offset = 0;
if (sender.isPlayer())
@ -48,7 +47,7 @@ public class Commandclearinventory extends EssentialsCommand
{
sender.sendMessage(tl("inventoryClearingFromAll"));
offset = 1;
players = Arrays.asList(server.getOnlinePlayers());
players = ess.getOnlinePlayers();
}
else if (allowOthers && args.length > 0 && args[0].trim().length() > 2)
{

View File

@ -165,7 +165,7 @@ public class Commandessentials extends EssentialsCommand
{
return;
}
for (Player onlinePlayer : server.getOnlinePlayers())
for (Player onlinePlayer : ess.getOnlinePlayers())
{
onlinePlayer.playSound(onlinePlayer.getLocation(), Sound.NOTE_PIANO, 1, noteMap.get(note));
}
@ -214,7 +214,7 @@ public class Commandessentials extends EssentialsCommand
{
logger.info(s);
}
for (Player player : ess.getServer().getOnlinePlayers())
for (Player player : ess.getOnlinePlayers())
{
player.sendMessage(playerMoo);
player.playSound(player.getLocation(), Sound.COW_IDLE, 1, 1.0f);
@ -348,7 +348,7 @@ public class Commandessentials extends EssentialsCommand
UUID onlineUUID = null;
for (Player player : server.getOnlinePlayers())
for (Player player : ess.getOnlinePlayers())
{
if (player.getName().equalsIgnoreCase(name))
{

View File

@ -20,7 +20,7 @@ public class Commandkickall extends EssentialsCommand
String kickReason = args.length > 0 ? getFinalArg(args, 0) : tl("kickDefault");
kickReason = FormatUtil.replaceFormat(kickReason.replace("\\n", "\n").replace("|", "\n"));
for (Player onlinePlayer : server.getOnlinePlayers())
for (Player onlinePlayer : ess.getOnlinePlayers())
{
if (!sender.isPlayer() || !onlinePlayer.getName().equalsIgnoreCase(sender.getPlayer().getName()))
{

View File

@ -109,7 +109,7 @@ public class Commandnick extends EssentialsLoopCommand
private boolean nickInUse(final Server server, final User target, String nick)
{
final String lowerNick = FormatUtil.stripFormat(nick.toLowerCase(Locale.ENGLISH));
for (final Player onlinePlayer : server.getOnlinePlayers())
for (final Player onlinePlayer : ess.getOnlinePlayers())
{
if (target.getBase().getName().equals(onlinePlayer.getName()))
{

View File

@ -3,8 +3,7 @@ package com.earth2me.essentials.commands;
import com.earth2me.essentials.CommandSource;
import static com.earth2me.essentials.I18n.tl;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
@ -22,7 +21,7 @@ public class Commandnuke extends EssentialsCommand
@Override
protected void run(final Server server, final CommandSource sender, final String commandLabel, final String[] args) throws NoSuchFieldException, NotEnoughArgumentsException
{
List<Player> targets;
Collection<Player> targets;
if (args.length > 0)
{
targets = new ArrayList<Player>();
@ -35,7 +34,7 @@ public class Commandnuke extends EssentialsCommand
}
else
{
targets = Arrays.asList(server.getOnlinePlayers());
targets = ess.getOnlinePlayers();
}
ess.getTNTListener().enable();
for (Player player : targets)

View File

@ -197,9 +197,9 @@ public class Commandptime extends EssentialsCommand
}
else
{
for (Player player : server.getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
users.add(ess.getUser(player));
users.add(user);
}
}
return users;

View File

@ -147,9 +147,9 @@ public class Commandpweather extends EssentialsCommand
}
else
{
for (Player player : server.getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
users.add(ess.getUser(player));
users.add(user);
}
}
return users;

View File

@ -26,10 +26,9 @@ public class Commandrealname extends EssentialsCommand
final String whois = args[0].toLowerCase(Locale.ENGLISH);
boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).canInteractVanished();
boolean foundUser = false;
for (Player onlinePlayer : server.getOnlinePlayers())
for (User u: ess.getOnlineUsers())
{
final User u = ess.getUser(onlinePlayer);
if (skipHidden && u.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlinePlayer))
if (skipHidden && u.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(u.getBase()))
{
continue;
}

View File

@ -34,9 +34,8 @@ public class Commandtpaall extends EssentialsCommand
private void teleportAAllPlayers(final Server server, final CommandSource sender, final User target)
{
sender.sendMessage(tl("teleportAAll"));
for (Player onlinePlayer : server.getOnlinePlayers())
for (User player : ess.getOnlineUsers())
{
final User player = ess.getUser(onlinePlayer);
if (target == player)
{
continue;

View File

@ -37,9 +37,8 @@ public class Commandtpall extends EssentialsCommand
{
sender.sendMessage(tl("teleportAll"));
final Location loc = target.getLocation();
for (Player onlinePlayer : server.getOnlinePlayers())
for (User player : ess.getOnlineUsers())
{
final User player = ess.getUser(onlinePlayer);
if (target == player)
{
continue;

View File

@ -137,10 +137,8 @@ public abstract class EssentialsCommand implements IEssentialsCommand
if (matches.isEmpty())
{
final String matchText = searchTerm.toLowerCase(Locale.ENGLISH);
for (Player onlinePlayer : server.getOnlinePlayers())
for (User userMatch : ess.getOnlineUsers())
{
final User userMatch = ess.getUser(onlinePlayer);
if (getHidden || canInteractWith(sourceUser, userMatch))
{
final String displayName = FormatUtil.stripFormat(userMatch.getDisplayName()).toLowerCase(Locale.ENGLISH);

View File

@ -38,10 +38,9 @@ public abstract class EssentialsLoopCommand extends EssentialsCommand
else if (matchWildcards && searchTerm.contentEquals("*"))
{
boolean skipHidden = sender.isPlayer() && !ess.getUser(sender.getPlayer()).canInteractVanished();
for (Player onlinePlayer : server.getOnlinePlayers())
for (User onlineUser : ess.getOnlineUsers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlinePlayer))
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlineUser.getBase()))
{
continue;
}
@ -85,10 +84,9 @@ public abstract class EssentialsLoopCommand extends EssentialsCommand
if (matchWildcards && (searchTerm.contentEquals("**") || searchTerm.contentEquals("*")))
{
for (Player onlinePlayer : server.getOnlinePlayers())
for (User onlineUser : ess.getOnlineUsers())
{
final User onlineUser = ess.getUser(onlinePlayer);
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlinePlayer))
if (skipHidden && onlineUser.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlineUser.getBase()))
{
continue;
}
@ -107,10 +105,9 @@ public abstract class EssentialsLoopCommand extends EssentialsCommand
if (matchedPlayers.isEmpty())
{
final String matchText = searchTerm.toLowerCase(Locale.ENGLISH);
for (Player onlinePlayer : server.getOnlinePlayers())
for (User player : ess.getOnlineUsers())
{
final User player = ess.getUser(onlinePlayer);
if (skipHidden && player.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(onlinePlayer))
if (skipHidden && player.isHidden(sender.getPlayer()) && !sender.getPlayer().canSee(player.getBase()))
{
continue;
}

View File

@ -24,6 +24,7 @@ package com.earth2me.essentials.metrics;
* should not be interpreted as representing official policies, either expressed or implied, of anybody else.
*/
import com.earth2me.essentials.IEssentials;
import java.io.*;
import java.net.Proxy;
import java.net.URL;
@ -352,7 +353,7 @@ public class Metrics
boolean onlineMode = Bukkit.getServer().getOnlineMode(); // TRUE if online mode is enabled
String pluginVersion = description.getVersion();
String serverVersion = Bukkit.getVersion();
int playersOnline = Bukkit.getServer().getOnlinePlayers().length;
int playersOnline = ((IEssentials)plugin).getOnlinePlayers().size();
// END server software specific section -- all code below does not use any code outside of this class / Java

View File

@ -166,14 +166,14 @@ public class KeywordReplacer implements IText
break;
case ONLINE:
int playerHidden = 0;
for (Player p : ess.getServer().getOnlinePlayers())
for (User u : ess.getOnlineUsers())
{
if (ess.getUser(p).isHidden())
if (u.isHidden())
{
playerHidden++;
}
}
replacer = Integer.toString(ess.getServer().getOnlinePlayers().length - playerHidden);
replacer = Integer.toString(ess.getOnlinePlayers().size() - playerHidden);
break;
case UNIQUE:
replacer = Integer.toString(ess.getUserMap().getUniqueUsers());

View File

@ -74,11 +74,17 @@ public class FakeServer implements Server
}
@Override
public Player[] getOnlinePlayers()
public Player[] _INVALID_getOnlinePlayers()
{
return players.toArray(new Player[0]);
}
@Override
public Collection<? extends Player> getOnlinePlayers()
{
return players;
}
public void setOnlinePlayers(List<Player> players)
{
this.players = players;

View File

@ -149,4 +149,10 @@ public class EssentialsXMPP extends JavaPlugin implements IEssentialsXMPP
// Ignore exceptions
}
}
@Override
public IEssentials getEss()
{
return ess;
}
}

View File

@ -1,5 +1,6 @@
package com.earth2me.essentials.xmpp;
import com.earth2me.essentials.IEssentials;
import net.ess3.api.IUser;
import java.util.List;
import org.bukkit.entity.Player;
@ -25,4 +26,6 @@ public interface IEssentialsXMPP extends Plugin
boolean toggleSpy(final Player user);
void broadcastMessage(final IUser sender, final String message, final String xmppAddress);
IEssentials getEss();
}

View File

@ -163,7 +163,7 @@ public class XMPPManager extends Handler implements MessageListener, ChatManager
final int usercount;
final StringBuilder stringBuilder = new StringBuilder();
usercount = parent.getServer().getOnlinePlayers().length;
usercount = parent.getEss().getOnlinePlayers().size();
if (usercount == 0)
{

Binary file not shown.

Binary file not shown.