From 9853ef824e92dbc1fa0b87bd2e2c7c6d91b26a2e Mon Sep 17 00:00:00 2001 From: Sam Sun Date: Tue, 24 May 2016 12:58:49 -0400 Subject: [PATCH] QA Pass #2 - Comment WorldManager, optimize searching --- .../game/clans/clans/ClansUtility.java | 70 +++++++++---------- .../clans/tutorial/command/FinishCommand.java | 2 +- .../tutorial/command/TutorialCommand.java | 6 +- .../game/clans/world/WorldManager.java | 2 + 4 files changed, 37 insertions(+), 43 deletions(-) diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java index 9e4e2e1af..ff6c1fc70 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/clans/ClansUtility.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.ChatColor; import org.bukkit.Chunk; @@ -28,7 +29,9 @@ import mineplex.game.clans.spawn.Spawn; public class ClansUtility { + // The maximum number of clans to search before exiting early. Inclusive private static final int MAX_CLAN_SEARCH = 10; + // The maximum number of players to search before exiting early. Inclusive private static final int MAX_PLAYER_SEARCH = 10; private ClansManager _clansManager; @@ -116,7 +119,7 @@ public class ClansUtility public ClanInfo searchClanPlayer(Player caller, String name, boolean inform) { // CLAN - LinkedList clanMatchList = new LinkedList(); + List clanMatchList = new ArrayList<>(MAX_CLAN_SEARCH); for (ClanInfo cur : _clansManager.getClanMap().values()) { @@ -128,25 +131,9 @@ public class ClansUtility } if (clanMatchList.size() == 1) return clanMatchList.get(0); - - // No / Non-Unique - String clanMatchString = ""; - if (clanMatchList.size() <= MAX_CLAN_SEARCH) - { - if (clanMatchList.size() > 1) - { - for (ClanInfo cur : clanMatchList) - clanMatchString += cur.getName() + " "; - } - - if (clanMatchString.length() == 0) - { - clanMatchString = "None"; - } - } - + // PLAYER - LinkedList playerMatchList = new LinkedList(); + List playerMatchList = new ArrayList<>(MAX_PLAYER_SEARCH); outer: for (ClanInfo clanInfo : _clansManager.getClanMap().values()) { @@ -154,7 +141,12 @@ public class ClansUtility { if (player.getPlayerName().equalsIgnoreCase(name)) return clanInfo; - if (player.getPlayerName().toLowerCase().contains(name.toLowerCase())) playerMatchList.add(clanInfo); + if (player.getPlayerName().toLowerCase().contains(name.toLowerCase())) + { + playerMatchList.add(clanInfo); + // No duplicate results please + continue outer; + } if (playerMatchList.size() > MAX_PLAYER_SEARCH) break outer; } @@ -162,22 +154,6 @@ public class ClansUtility if (playerMatchList.size() == 1) return playerMatchList.get(0); - // No / Non-Unique - String playerMatchString = ""; - if (playerMatchList.size() <= MAX_PLAYER_SEARCH) - { - if (playerMatchList.size() > 1) - { - for (ClanInfo cur : playerMatchList) - playerMatchString += cur.getName() + " "; - } - - if (playerMatchString.length() == 0) - { - playerMatchString = "None"; - } - } - if (inform) { UtilPlayer.message(caller, F.main("Clan Search", "" + C.mCount + (clanMatchList.size() + playerMatchList.size()) + C.mBody + " matches for [" + C.mElem + name + C.mBody + "].")); @@ -186,17 +162,35 @@ public class ClansUtility { UtilPlayer.message(caller, F.main("Clan Search", "Too many clans matched. Try a more specific search")); } + else if (clanMatchList.size() == 0) + { + UtilPlayer.message(caller, F.main("Clan Search", "No clans matched. Try a more specific search")); + } else { - UtilPlayer.message(caller, F.desc("Matches via Clan", clanMatchString)); + StringBuilder clanMatchString = new StringBuilder(); + for (ClanInfo clanInfo : clanMatchList) + { + clanMatchString.append(clanInfo.getName()).append(" "); + } + UtilPlayer.message(caller, F.desc("Matches via Clan", clanMatchString.toString())); } if (playerMatchList.size() > MAX_PLAYER_SEARCH) { UtilPlayer.message(caller, F.main("Clan Search", "Too many players matched. Try a more specific search")); } + else if (playerMatchList.size() == 0) + { + UtilPlayer.message(caller, F.main("Clan Search", "No players matched. Try a more specific search")); + } else { - UtilPlayer.message(caller, F.desc("Matches via Player", playerMatchString)); + StringBuilder playerMatchString = new StringBuilder(); + for (ClanInfo clanInfo : playerMatchList) + { + playerMatchString.append(clanInfo.getName()).append(" "); + } + UtilPlayer.message(caller, F.desc("Matches via Player", playerMatchString.toString())); } } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/FinishCommand.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/FinishCommand.java index 723e6974f..e0e32c1dc 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/FinishCommand.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/FinishCommand.java @@ -17,7 +17,7 @@ public class FinishCommand extends CommandBase public void Execute(Player caller, String[] args) { boolean testServer = Plugin.getPlugin().getConfig().getString("serverstatus.group").equalsIgnoreCase("Testing"); - if (CommandCenter.Instance.GetClientManager().hasRank(caller, testServer ? Rank.ALL : Rank.JNR_DEV)) + if (_commandCenter.GetClientManager().hasRank(caller, testServer ? Rank.ALL : Rank.JNR_DEV)) { Plugin.finishTutorial(caller); } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/TutorialCommand.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/TutorialCommand.java index d444a81b9..f13e5bfde 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/TutorialCommand.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/tutorial/command/TutorialCommand.java @@ -1,13 +1,11 @@ package mineplex.game.clans.tutorial.command; -import org.bukkit.entity.Player; - -import mineplex.core.command.CommandBase; import mineplex.core.command.MultiCommandBase; import mineplex.core.common.Rank; import mineplex.core.common.util.F; import mineplex.core.common.util.UtilPlayer; import mineplex.game.clans.tutorial.TutorialManager; +import org.bukkit.entity.Player; public class TutorialCommand extends MultiCommandBase { @@ -29,7 +27,7 @@ public class TutorialCommand extends MultiCommandBase public void Execute(Player caller, String[] args) { boolean testServer = Plugin.getPlugin().getConfig().getString("serverstatus.group").equalsIgnoreCase("Testing"); - if (CommandCenter.GetClientManager().hasRank(caller, testServer ? Rank.ALL : Rank.JNR_DEV)) + if (_commandCenter.GetClientManager().hasRank(caller, testServer ? Rank.ALL : Rank.JNR_DEV)) { super.Execute(caller, args); } diff --git a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/world/WorldManager.java b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/world/WorldManager.java index 6bc3b41f5..e6b7bf983 100644 --- a/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/world/WorldManager.java +++ b/Plugins/Mineplex.Game.Clans/src/mineplex/game/clans/world/WorldManager.java @@ -106,6 +106,8 @@ public class WorldManager extends MiniPlugin boolean cull = true; for (Player player : players) { + // Using NMS because this is going to be called quite a few times + // and each getLocation() call creates a new Location object if (UtilWorld.distanceSquared(player, entity) <= MIN_RANGE_SQUARED) { cull = false;