Skip to content
Permalink
Browse files

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
  • Loading branch information...
md-5 authored and khobbits committed Jul 12, 2014
1 parent 310e413 commit 035182fcda8acf77d3727ce21993e9bb57c90077
Showing with 126 additions and 63 deletions.
  1. +2 −2 Essentials/src/com/earth2me/essentials/Backup.java
  2. +57 −2 Essentials/src/com/earth2me/essentials/Essentials.java
  3. +2 −3 Essentials/src/com/earth2me/essentials/EssentialsPlayerListener.java
  4. +1 −1 Essentials/src/com/earth2me/essentials/EssentialsTimer.java
  5. +5 −0 Essentials/src/com/earth2me/essentials/IEssentials.java
  6. +7 −8 Essentials/src/com/earth2me/essentials/PlayerList.java
  7. +4 −5 Essentials/src/com/earth2me/essentials/User.java
  8. +3 −4 Essentials/src/com/earth2me/essentials/commands/Commandclearinventory.java
  9. +3 −3 Essentials/src/com/earth2me/essentials/commands/Commandessentials.java
  10. +1 −1 Essentials/src/com/earth2me/essentials/commands/Commandkickall.java
  11. +1 −1 Essentials/src/com/earth2me/essentials/commands/Commandnick.java
  12. +3 −4 Essentials/src/com/earth2me/essentials/commands/Commandnuke.java
  13. +2 −2 Essentials/src/com/earth2me/essentials/commands/Commandptime.java
  14. +2 −2 Essentials/src/com/earth2me/essentials/commands/Commandpweather.java
  15. +2 −3 Essentials/src/com/earth2me/essentials/commands/Commandrealname.java
  16. +1 −2 Essentials/src/com/earth2me/essentials/commands/Commandtpaall.java
  17. +1 −2 Essentials/src/com/earth2me/essentials/commands/Commandtpall.java
  18. +1 −3 Essentials/src/com/earth2me/essentials/commands/EssentialsCommand.java
  19. +6 −9 Essentials/src/com/earth2me/essentials/commands/EssentialsLoopCommand.java
  20. +2 −1 Essentials/src/com/earth2me/essentials/metrics/Metrics.java
  21. +3 −3 Essentials/src/com/earth2me/essentials/textreader/KeywordReplacer.java
  22. +7 −1 Essentials/test/com/earth2me/essentials/FakeServer.java
  23. +6 −0 EssentialsXMPP/src/com/earth2me/essentials/xmpp/EssentialsXMPP.java
  24. +3 −0 EssentialsXMPP/src/com/earth2me/essentials/xmpp/IEssentialsXMPP.java
  25. +1 −1 EssentialsXMPP/src/com/earth2me/essentials/xmpp/XMPPManager.java
@@ -24,7 +24,7 @@ public Backup(final IEssentials ess)
{
this.ess = ess;
server = ess.getServer();
if (server.getOnlinePlayers().length > 0)
if (!ess.getOnlinePlayers().isEmpty())
{
ess.runTaskAsynchronously(new Runnable()
{
@@ -152,7 +152,7 @@ public void run()
public void run()
{
server.dispatchCommand(cs, "save-on");
if (server.getOnlinePlayers().length == 0)
if (ess.getOnlinePlayers().isEmpty())
{
stopTask();
}
@@ -35,10 +35,17 @@
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 @@
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 void onEnable()
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 @@ private int broadcastMessage(final IUser sender, final String permission, final

IText broadcast = new SimpleTextInput(message);

final Player[] players = getServer().getOnlinePlayers();
final Collection<Player> players = getOnlinePlayers();

for (Player player : players)
{
@@ -803,7 +821,7 @@ private int broadcastMessage(final IUser sender, final String permission, final
}
}

return players.length;
return players.size();
}

@Override
@@ -890,6 +908,43 @@ public EssentialsTimer getTimer()
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
{
@@ -435,10 +435,9 @@ public void onPlayerCommandPreprocess(final PlayerCommandPreprocessEvent event)
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());
}
@@ -48,7 +48,7 @@ public void run()
}
lastPoll = startTime;
int count = 0;
for (Player player : ess.getServer().getOnlinePlayers())
for (Player player : ess.getOnlinePlayers())
{
count++;
if (skip1 > 0)
@@ -6,6 +6,7 @@
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 @@
EssentialsTimer getTimer();

List<String> getVanishedPlayers();

Collection<Player> getOnlinePlayers();

Iterable<User> getOnlineUsers();
}
@@ -49,12 +49,12 @@ public static String listSummary(final IEssentials ess, final User user, final b
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 static String listSummary(final IEssentials ess, final User user, final b
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 static String listSummary(final IEssentials ess, final User user, final b
{
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;
}
@@ -623,9 +623,8 @@ public void checkActivity()
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 void setVanished(final boolean set)
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);
@@ -5,8 +5,7 @@
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 @@ protected void run(Server server, CommandSource sender, String commandLabel, Str

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 @@ private void parseCommand(Server server, CommandSource sender, String[] args, bo
{
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)
{
@@ -165,7 +165,7 @@ public void run()
{
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 @@ private void run_moo(final Server server, final CommandSource sender, final Stri
{
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 @@ private void run_uuidtest(final Server server, final CommandSource sender, final

UUID onlineUUID = null;

for (Player player : server.getOnlinePlayers())
for (Player player : ess.getOnlinePlayers())
{
if (player.getName().equalsIgnoreCase(name))
{
@@ -20,7 +20,7 @@ public void run(final Server server, final CommandSource sender, final String co
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()))
{
@@ -109,7 +109,7 @@ else if (FormatUtil.stripFormat(newNick).length() < 1)
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()))
{
@@ -3,8 +3,7 @@
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 Commandnuke()
@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 @@ protected void run(final Server server, final CommandSource sender, final String
}
else
{
targets = Arrays.asList(server.getOnlinePlayers());
targets = ess.getOnlinePlayers();
}
ess.getTNTListener().enable();
for (Player player : targets)
@@ -197,9 +197,9 @@ private void setUsersTime(final CommandSource sender, final Collection<User> use
}
else
{
for (Player player : server.getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
users.add(ess.getUser(player));
users.add(user);
}
}
return users;
@@ -147,9 +147,9 @@ private void setUsersWeather(final CommandSource sender, final Collection<User>
}
else
{
for (Player player : server.getOnlinePlayers())
for (User user : ess.getOnlineUsers())
{
users.add(ess.getUser(player));
users.add(user);
}
}
return users;
@@ -26,10 +26,9 @@ protected void run(final Server server, final CommandSource sender, final String
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;
}
@@ -34,9 +34,8 @@ public void run(final Server server, final CommandSource sender, final String co
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;
@@ -37,9 +37,8 @@ private void teleportAllPlayers(Server server, CommandSource sender, User target
{
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;

0 comments on commit 035182f

Please sign in to comment.
You can’t perform that action at this time.