From f05b9ae365032e3ebd8e96c550a6781c448f0fad Mon Sep 17 00:00:00 2001 From: TfT_02 Date: Sat, 13 Apr 2013 18:43:22 +0200 Subject: [PATCH] Use LinkedHashSet instead of List --- .../java/com/gmail/nossr50/api/PartyAPI.java | 3 ++- .../gmail/nossr50/datatypes/party/Party.java | 5 ++-- .../com/gmail/nossr50/party/PartyManager.java | 24 +++++++------------ 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/PartyAPI.java b/src/main/java/com/gmail/nossr50/api/PartyAPI.java index 6f7800ffe..616acaa05 100644 --- a/src/main/java/com/gmail/nossr50/api/PartyAPI.java +++ b/src/main/java/com/gmail/nossr50/api/PartyAPI.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.api; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import org.bukkit.OfflinePlayer; @@ -148,7 +149,7 @@ public final class PartyAPI { * @param player The player to check * @return all the player names in the player's party */ - public static List getMembers(Player player) { + public static LinkedHashSet getMembers(Player player) { return PartyManager.getAllMembers(player); } diff --git a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java index e9ba7f71d..135ad10bf 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/party/Party.java +++ b/src/main/java/com/gmail/nossr50/datatypes/party/Party.java @@ -1,6 +1,7 @@ package com.gmail.nossr50.datatypes.party; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import org.bukkit.OfflinePlayer; @@ -10,7 +11,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.party.ShareHandler; public class Party { - private List members = new ArrayList(); + private LinkedHashSet members = new LinkedHashSet(); private String leader; private String name; private String password; @@ -24,7 +25,7 @@ public class Party { private boolean shareHerbalismDrops = true; private boolean shareWoodcuttingDrops = true; - public List getMembers() { + public LinkedHashSet getMembers() { return members; } diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index dc013041b..63868e3e5 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -2,6 +2,7 @@ package com.gmail.nossr50.party; import java.io.File; import java.util.ArrayList; +import java.util.LinkedHashSet; import java.util.List; import org.bukkit.OfflinePlayer; @@ -97,7 +98,7 @@ public final class PartyManager { * @param player The player to check * @return all the players in the player's party */ - public static List getAllMembers(Player player) { + public static LinkedHashSet getAllMembers(Player player) { Party party = UserManager.getPlayer(player).getParty(); if (party == null) { @@ -189,12 +190,9 @@ public final class PartyManager { * @param party The party */ public static void removeFromParty(OfflinePlayer player, Party party) { - List members = party.getMembers(); + LinkedHashSet members = party.getMembers(); - while (members.contains(player.getName())) { - // Remove all the duplicates as well - members.remove(player.getName()); - } + members.remove(player.getName()); if (members.isEmpty()) { parties.remove(party); @@ -202,7 +200,7 @@ public final class PartyManager { else { // If the leaving player was the party leader, appoint a new leader from the party members if (party.getLeader().equalsIgnoreCase(player.getName())) { - String newLeader = members.get(0); + String newLeader = members.iterator().next(); party.setLeader(newLeader); } @@ -223,7 +221,7 @@ public final class PartyManager { * @param party The party to remove */ public static void disbandParty(Party party) { - List members = party.getMembers(); + LinkedHashSet members = party.getMembers(); for (String memberName : members) { McMMOPlayer mcMMOPlayer = UserManager.getPlayer(memberName); @@ -363,9 +361,7 @@ public final class PartyManager { informPartyMembersJoin(player, party); mcMMOPlayer.setParty(party); - if (!party.getMembers().contains(player.getName())) { - party.getMembers().add(player.getName()); - } + party.getMembers().add(player.getName()); } /** @@ -469,12 +465,10 @@ public final class PartyManager { party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode"))); List memberNames = partiesFile.getStringList(partyName + ".Members"); - List members = party.getMembers(); + LinkedHashSet members = party.getMembers(); for (String memberName : memberNames) { - if (!members.contains(memberName)) { - members.add(memberName); - } + members.add(memberName); } parties.add(party);