Explorar el Código

BETA: Centralizing comparators for players in one class.

Suggested by: Nos
Zoey76 hace 11 años
padre
commit
c178d73404

+ 3 - 24
L2J_Server_BETA/java/com/l2jserver/gameserver/communitybbs/Manager/RegionBBSManager.java

@@ -19,8 +19,6 @@
 package com.l2jserver.gameserver.communitybbs.Manager;
 
 import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.StringTokenizer;
@@ -43,20 +41,12 @@ import com.l2jserver.gameserver.network.clientpackets.Say2;
 import com.l2jserver.gameserver.network.serverpackets.CreatureSay;
 import com.l2jserver.gameserver.network.serverpackets.ShowBoard;
 import com.l2jserver.gameserver.network.serverpackets.SystemMessage;
+import com.l2jserver.gameserver.util.Comparators;
 import com.l2jserver.util.StringUtil;
 
 public class RegionBBSManager extends BaseBBSManager
 {
-	private static Logger _logChat = Logger.getLogger("chat");
-	
-	private static final Comparator<L2PcInstance> PLAYER_NAME_COMPARATOR = new Comparator<L2PcInstance>()
-	{
-		@Override
-		public int compare(L2PcInstance p1, L2PcInstance p2)
-		{
-			return p1.getName().compareToIgnoreCase(p2.getName());
-		}
-	};
+	private static final Logger _logChat = Logger.getLogger("chat");
 	
 	private int _onlineCount = 0;
 	private int _onlineCountGm = 0;
@@ -280,22 +270,11 @@ public class RegionBBSManager extends BaseBBSManager
 	 */
 	public void changeCommunityBoard()
 	{
-		final List<L2PcInstance> sortedPlayers = new ArrayList<>();
-		for (L2PcInstance player : L2World.getInstance().getPlayers())
-		{
-			if (player != null)
-			{
-				sortedPlayers.add(player);
-			}
-		}
-		
-		Collections.sort(sortedPlayers, PLAYER_NAME_COMPARATOR);
-		
 		_onlinePlayers.clear();
 		_onlineCount = 0;
 		_onlineCountGm = 0;
 		
-		for (L2PcInstance player : sortedPlayers)
+		for (L2PcInstance player : L2World.getInstance().getPlayersSortedBy(Comparators.PLAYER_NAME_COMPARATOR))
 		{
 			addOnlinePlayer(player);
 		}

+ 14 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/model/L2World.java

@@ -19,7 +19,9 @@
 package com.l2jserver.gameserver.model;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Collection;
+import java.util.Comparator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
@@ -179,6 +181,18 @@ public final class L2World
 		return _allPlayers.values();
 	}
 	
+	/**
+	 * Gets all players sorted by the given comparator.
+	 * @param comparator the comparator
+	 * @return the players sorted by the comparator
+	 */
+	public L2PcInstance[] getPlayersSortedBy(Comparator<L2PcInstance> comparator)
+	{
+		final L2PcInstance[] players = _allPlayers.values().toArray(new L2PcInstance[_allPlayers.values().size()]);
+		Arrays.sort(players, comparator);
+		return players;
+	}
+	
 	public boolean forEachPlayer(IL2Procedure<L2PcInstance> procedure)
 	{
 		for (L2PcInstance player : _allPlayers.values())

+ 67 - 0
L2J_Server_BETA/java/com/l2jserver/gameserver/util/Comparators.java

@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2004-2013 L2J Server
+ * 
+ * This file is part of L2J Server.
+ * 
+ * L2J Server is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ * 
+ * L2J Server is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+package com.l2jserver.gameserver.util;
+
+import java.util.Comparator;
+
+import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
+
+/**
+ * Static comparators.
+ * @author Zoey76
+ */
+public class Comparators
+{
+	/** Compares its two arguments for order by it's name. */
+	public static final Comparator<L2PcInstance> PLAYER_NAME_COMPARATOR = new Comparator<L2PcInstance>()
+	{
+		@Override
+		public int compare(L2PcInstance p1, L2PcInstance p2)
+		{
+			return p1.getName().compareToIgnoreCase(p2.getName());
+		}
+	};
+	/** Compares its two arguments for order by it's up-time, the one that logged first. */
+	public static final Comparator<L2PcInstance> PLAYER_UPTIME_COMPARATOR = new Comparator<L2PcInstance>()
+	{
+		@Override
+		public int compare(L2PcInstance p1, L2PcInstance p2)
+		{
+			return Long.compare(p1.getUptime(), p2.getUptime());
+		}
+	};
+	/** Compares its two arguments for order by it's PVP kills. */
+	public static final Comparator<L2PcInstance> PLAYER_PVP_COMPARATOR = new Comparator<L2PcInstance>()
+	{
+		@Override
+		public int compare(L2PcInstance p1, L2PcInstance p2)
+		{
+			return Integer.compare(p1.getPvpKills(), p2.getPvpKills());
+		}
+	};
+	/** Compares its two arguments for order by it's PK kills. */
+	public static final Comparator<L2PcInstance> PLAYER_PK_COMPARATOR = new Comparator<L2PcInstance>()
+	{
+		@Override
+		public int compare(L2PcInstance p1, L2PcInstance p2)
+		{
+			return Integer.compare(p1.getPkKills(), p2.getPkKills());
+		}
+	};
+}