From c659f8a9a948289f531ac037e59721385c6e1d2c Mon Sep 17 00:00:00 2001 From: bm01 Date: Sat, 9 Feb 2013 14:34:53 +0100 Subject: [PATCH] Party now store OfflinePlayer instead of String --- .../java/com/gmail/nossr50/api/PartyAPI.java | 20 +++-- .../java/com/gmail/nossr50/party/Party.java | 14 ++- .../com/gmail/nossr50/party/PartyManager.java | 85 ++++++++++--------- .../nossr50/party/commands/PartyCommand.java | 32 +++---- .../party/commands/PartyQuitCommand.java | 2 +- .../skills/runnables/PartyAutoKick.java | 15 +--- 6 files changed, 88 insertions(+), 80 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/api/PartyAPI.java b/src/main/java/com/gmail/nossr50/api/PartyAPI.java index 8e07f0163..9fe7af7f2 100644 --- a/src/main/java/com/gmail/nossr50/api/PartyAPI.java +++ b/src/main/java/com/gmail/nossr50/api/PartyAPI.java @@ -1,7 +1,9 @@ package com.gmail.nossr50.api; +import java.util.ArrayList; import java.util.List; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import com.gmail.nossr50.party.Party; @@ -69,15 +71,14 @@ public final class PartyAPI { */ public static void addToParty(Player player, String partyName) { Party party = PartyManager.getParty(partyName); - String playerName = player.getName(); if (party == null) { party = new Party(); party.setName(partyName); - party.setLeader(playerName); + party.setLeader(player.getName()); } - PartyManager.addToParty(playerName, Users.getPlayer(player), party); + PartyManager.addToParty(player, Users.getPlayer(player), party); } /** @@ -88,7 +89,7 @@ public final class PartyAPI { * @param player The player to remove */ public static void removeFromParty(Player player) { - PartyManager.removeFromParty(player.getName(), Users.getPlayer(player).getParty()); + PartyManager.removeFromParty(player, Users.getPlayer(player).getParty()); } /** @@ -122,9 +123,18 @@ public final class PartyAPI { * * @param player The player to check * @return all the players in the player's party + * @deprecated */ + // TODO: I naively tried to add another getAllMembers that returns a List, but that wasn't possible + // since the return type isn't part of the the method's signature. If anybody has an idea... public static List getAllMembers(Player player) { - return PartyManager.getAllMembers(player); + List memberNames = new ArrayList(); + + for (OfflinePlayer member : PartyManager.getAllMembers(player)) { + memberNames.add(member.getName()); + } + + return memberNames; } /** diff --git a/src/main/java/com/gmail/nossr50/party/Party.java b/src/main/java/com/gmail/nossr50/party/Party.java index 9998b5c72..ddd9d8248 100644 --- a/src/main/java/com/gmail/nossr50/party/Party.java +++ b/src/main/java/com/gmail/nossr50/party/Party.java @@ -3,12 +3,11 @@ package com.gmail.nossr50.party; import java.util.ArrayList; import java.util.List; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; -import com.gmail.nossr50.mcMMO; - public class Party { - private List members = new ArrayList(); + private List members = new ArrayList(); private String leader; private String name; private String password; @@ -16,17 +15,16 @@ public class Party { private ShareHandler.ShareMode xpShareMode; private ShareHandler.ShareMode itemShareMode; - public List getMembers() { + public List getMembers() { return members; } public List getOnlineMembers() { - Player[] onlinePlayers = mcMMO.p.getServer().getOnlinePlayers(); List onlineMembers = new ArrayList(); - for (Player onlinePlayer : onlinePlayers) { - if (members.contains(onlinePlayer.getName())) { - onlineMembers.add(onlinePlayer); + for (OfflinePlayer member : members) { + if (member.isOnline()) { + onlineMembers.add(member.getPlayer()); } } diff --git a/src/main/java/com/gmail/nossr50/party/PartyManager.java b/src/main/java/com/gmail/nossr50/party/PartyManager.java index 18ba78880..6b0d57832 100644 --- a/src/main/java/com/gmail/nossr50/party/PartyManager.java +++ b/src/main/java/com/gmail/nossr50/party/PartyManager.java @@ -4,6 +4,8 @@ import java.io.File; import java.util.ArrayList; import java.util.List; +import org.bukkit.OfflinePlayer; +import org.bukkit.Server; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; @@ -27,7 +29,7 @@ public final class PartyManager { return false; } - removeFromParty(player.getName(), oldParty); + removeFromParty(player, oldParty); } else if (!handlePartyChangeEvent(player, null, newPartyName, EventReason.JOINED_PARTY)) { return false; @@ -83,13 +85,13 @@ public final class PartyManager { /** * Notify party members when a player joins * - * @param playerName The name of the player that joins + * @param player The player that joins * @param party The concerned party */ - private static void informPartyMembersJoin(String playerName, Party party) { + private static void informPartyMembersJoin(OfflinePlayer player, Party party) { for (Player member : party.getOnlineMembers()) { - if (!member.getName().equals(playerName)) { - member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", playerName)); + if (!member.equals(player)) { + member.sendMessage(LocaleLoader.getString("Party.InformedOnJoin", player.getName())); } } } @@ -97,13 +99,13 @@ public final class PartyManager { /** * Notify party members when a party member quits. * - * @param playerName The name of the player that quits + * @param player The player that quits * @param party The concerned party */ - private static void informPartyMembersQuit(String playerName, Party party) { + private static void informPartyMembersQuit(OfflinePlayer player, Party party) { for (Player member : party.getOnlineMembers()) { - if (!member.getName().equals(playerName)) { - member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", playerName)); + if (!member.equals(player)) { + member.sendMessage(LocaleLoader.getString("Party.InformedOnQuit", player.getName())); } } } @@ -114,7 +116,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 List getAllMembers(Player player) { Party party = Users.getPlayer(player).getParty(); if (party == null) { @@ -193,28 +195,28 @@ public final class PartyManager { /** * Remove a player from a party. * - * @param playerName The name of the player to remove + * @param player The player to remove * @param party The party */ - public static void removeFromParty(String playerName, Party party) { - List members = party.getMembers(); + public static void removeFromParty(OfflinePlayer player, Party party) { + List members = party.getMembers(); - members.remove(playerName); + members.remove(player); if (members.isEmpty()) { parties.remove(party); } else { //If the leaving player was the party leader, appoint a new leader from the party members - if (party.getLeader().equals(playerName)) { - String newLeader = members.get(0); - party.setLeader(newLeader); + if (party.getLeader().equals(player)) { + String newLeader = members.get(0).toString(); + party.setLeader(newLeader); } - informPartyMembersQuit(playerName, party); + informPartyMembersQuit(player, party); } - McMMOPlayer mcMMOPlayer = Users.getPlayer(playerName); + McMMOPlayer mcMMOPlayer = Users.getPlayer(player.getName()); if (mcMMOPlayer != null) { mcMMOPlayer.removeParty(); @@ -227,11 +229,10 @@ public final class PartyManager { * @param party The party to remove */ public static void disbandParty(Party party) { - List members = party.getMembers(); + List members = party.getMembers(); - for (String member : party.getMembers()) { - - McMMOPlayer mcMMOPlayer = Users.getPlayer(member); + for (OfflinePlayer member : members) { + McMMOPlayer mcMMOPlayer = Users.getPlayer(member.getName()); if (mcMMOPlayer != null) { mcMMOPlayer.removeParty(); @@ -239,9 +240,7 @@ public final class PartyManager { } members.clear(); - if (members.isEmpty()) { - parties.remove(party); - } + parties.remove(party); } /** @@ -255,21 +254,21 @@ public final class PartyManager { public static void createParty(Player player, McMMOPlayer mcMMOPlayer, String partyName, String password) { partyName = partyName.replace(".", ""); Party party = getParty(partyName); - String playerName = player.getName(); if (party == null) { party = new Party(); party.setName(partyName); - party.setLeader(playerName); + party.setLeader(player.getName()); party.setXpShareMode(ShareHandler.ShareMode.NONE); - party.setLocked(true);//Parties are now invite-only by default, can be set to open with /party unlock + party.setLocked(true); // Parties are now invite-only by default, can be set to open with /party unlock if (password != null) { party.setPassword(password); party.setLocked(true); player.sendMessage(LocaleLoader.getString("Party.Password.Set", password)); } + parties.add(party); } else { @@ -278,7 +277,7 @@ public final class PartyManager { } player.sendMessage(LocaleLoader.getString("Commands.Party.Create", party.getName())); - addToParty(player.getName(), mcMMOPlayer, party); + addToParty(player, mcMMOPlayer, party); } /** @@ -292,13 +291,12 @@ public final class PartyManager { public static void joinParty(Player player, McMMOPlayer mcMMOPlayer, String partyName, String password) { partyName = partyName.replace(".", ""); Party party = getParty(partyName); - String playerName = player.getName(); if (party == null) { party = new Party(); party.setName(partyName); - party.setLeader(playerName); + party.setLeader(player.getName()); if (password != null) { party.setPassword(password); @@ -312,7 +310,7 @@ public final class PartyManager { } player.sendMessage(LocaleLoader.getString("Commands.Party.Join", party.getName())); - addToParty(player.getName(), mcMMOPlayer, party); + addToParty(player, mcMMOPlayer, party); } /** @@ -324,7 +322,7 @@ public final class PartyManager { * @return true if the player can join the party */ public static boolean checkPartyPassword(Player player, Party party, String password) { - //Don't care about passwords if it isn't locked + // Don't care about passwords if it isn't locked if (party.isLocked()) { String partyPassword = party.getPassword(); @@ -362,20 +360,20 @@ public final class PartyManager { player.sendMessage(LocaleLoader.getString("Commands.Invite.Accepted", invite.getName())); mcMMOPlayer.removePartyInvite(); - addToParty(player.getName(), mcMMOPlayer, invite); + addToParty(player, mcMMOPlayer, invite); } /** * Add a player to a party * - * @param playerName The name of the player to add to a party + * @param player The player to add to a party * @param mcMMOPlayer The player to add to the party * @param party The party */ - public static void addToParty(String playerName, McMMOPlayer mcMMOPlayer, Party party) { - informPartyMembersJoin(playerName, party); + public static void addToParty(OfflinePlayer player, McMMOPlayer mcMMOPlayer, Party party) { + informPartyMembersJoin(player, party); mcMMOPlayer.setParty(party); - party.getMembers().add(playerName); + party.getMembers().add(player); } /** @@ -476,7 +474,14 @@ public final class PartyManager { party.setLocked(partiesFile.getBoolean(partyName + ".Locked")); party.setXpShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ExpShareMode"))); party.setItemShareMode(ShareHandler.ShareMode.getFromString(partiesFile.getString(partyName + ".ItemShareMode"))); - party.getMembers().addAll(partiesFile.getStringList(partyName + ".Members")); + + List memberNames = partiesFile.getStringList(partyName + ".Members"); + List members = party.getMembers(); + Server server = mcMMO.p.getServer(); + + for (String memberName : memberNames) { + members.add(server.getOfflinePlayer(memberName)); + } parties.add(party); } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java index fbb1183c5..e334194f8 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyCommand.java @@ -1,7 +1,7 @@ package com.gmail.nossr50.party.commands; import org.bukkit.ChatColor; -import org.bukkit.Server; +import org.bukkit.OfflinePlayer; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; @@ -115,8 +115,6 @@ public class PartyCommand implements CommandExecutor { private boolean party() { if (mcMMOPlayer.inParty()) { Party party = mcMMOPlayer.getParty(); - - Server server = mcMMO.p.getServer(); String leader = party.getLeader(); StringBuilder tempList = new StringBuilder(); @@ -127,17 +125,18 @@ public class PartyCommand implements CommandExecutor { String ExpShare = ""; String Split = ""; - for (String otherPlayerName : party.getMembers()) { - if (leader.equals(otherPlayerName)) { + for (OfflinePlayer otherMember : party.getMembers()) { + if (leader.equals(otherMember.getName())) { tempList.append(ChatColor.GOLD); } - else if (server.getPlayer(otherPlayerName) != null) { + else if (otherMember.isOnline()) { tempList.append(ChatColor.WHITE); } else { tempList.append(ChatColor.GRAY); } - tempList.append(otherPlayerName).append(" "); + + tempList.append(otherMember).append(" "); } String status = LocaleLoader.getString("Party.Status.Locked"); @@ -174,6 +173,7 @@ public class PartyCommand implements CommandExecutor { else { return printUsage(); } + return true; } @@ -202,16 +202,17 @@ public class PartyCommand implements CommandExecutor { Party party = mcMMOPlayer.getParty(); if (party.getLeader().equals(playerName)) { - if (!party.getMembers().contains(args[1])) { + OfflinePlayer member = mcMMO.p.getServer().getOfflinePlayer(args[1]); + + if (!party.getMembers().contains(member)) { player.sendMessage(LocaleLoader.getString("Party.NotInYourParty", args[1])); return true; } - Player target = mcMMO.p.getServer().getOfflinePlayer(args[1]).getPlayer(); - - if (target != null) { + if (member.isOnline()) { + Player onlineMember = member.getPlayer(); String partyName = party.getName(); - McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(target, partyName, null, EventReason.KICKED_FROM_PARTY); + McMMOPartyChangeEvent event = new McMMOPartyChangeEvent(onlineMember, partyName, null, EventReason.KICKED_FROM_PARTY); mcMMO.p.getServer().getPluginManager().callEvent(event); @@ -219,10 +220,10 @@ public class PartyCommand implements CommandExecutor { return true; } - target.sendMessage(LocaleLoader.getString("Commands.Party.Kick", partyName)); + onlineMember.sendMessage(LocaleLoader.getString("Commands.Party.Kick", partyName)); } - PartyManager.removeFromParty(args[1], party); + PartyManager.removeFromParty(member, party); } else { player.sendMessage(LocaleLoader.getString("Party.NotOwner")); @@ -285,7 +286,7 @@ public class PartyCommand implements CommandExecutor { } if (party.getLeader().equals(playerName)) { - if (!party.getMembers().contains(args[1])) { + if (!party.getMembers().contains(mcMMO.p.getServer().getOfflinePlayer(args[1]))) { player.sendMessage(LocaleLoader.getString("Party.NotInYourParty", args[1])); return true; } @@ -324,6 +325,7 @@ public class PartyCommand implements CommandExecutor { party.setLocked(true); player.sendMessage(LocaleLoader.getString("Party.Locked")); } + return true; } diff --git a/src/main/java/com/gmail/nossr50/party/commands/PartyQuitCommand.java b/src/main/java/com/gmail/nossr50/party/commands/PartyQuitCommand.java index 8a6facd31..78efb537b 100644 --- a/src/main/java/com/gmail/nossr50/party/commands/PartyQuitCommand.java +++ b/src/main/java/com/gmail/nossr50/party/commands/PartyQuitCommand.java @@ -32,7 +32,7 @@ public class PartyQuitCommand implements CommandExecutor { return true; } - PartyManager.removeFromParty(player.getName(), playerParty); + PartyManager.removeFromParty(player, playerParty); sender.sendMessage(LocaleLoader.getString("Commands.Party.Leave")); return true; diff --git a/src/main/java/com/gmail/nossr50/skills/runnables/PartyAutoKick.java b/src/main/java/com/gmail/nossr50/skills/runnables/PartyAutoKick.java index 7eab717a7..5e6c1a331 100644 --- a/src/main/java/com/gmail/nossr50/skills/runnables/PartyAutoKick.java +++ b/src/main/java/com/gmail/nossr50/skills/runnables/PartyAutoKick.java @@ -1,14 +1,12 @@ package com.gmail.nossr50.skills.runnables; -import java.util.ArrayList; +import org.bukkit.OfflinePlayer; -import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.config.Config; import com.gmail.nossr50.party.Party; import com.gmail.nossr50.party.PartyManager; public class PartyAutoKick implements Runnable { - @Override public void run() { updatePartyMembers(); @@ -18,14 +16,9 @@ public class PartyAutoKick implements Runnable { long currentTime = System.currentTimeMillis(); long kickTime = 24L * 60L * 60L * 1000L * Config.getInstance().getAutoPartyKickTime(); - ArrayList parties = new ArrayList(PartyManager.getParties()); - - for (Party party : parties) { - ArrayList members = new ArrayList(party.getMembers()); - for (String member : members) { - long lastPlayed = mcMMO.p.getServer().getOfflinePlayer(member).getLastPlayed(); - - if (currentTime - lastPlayed > kickTime) { + for (Party party : PartyManager.getParties()) { + for (OfflinePlayer member : party.getMembers()) { + if (currentTime - member.getLastPlayed() > kickTime) { PartyManager.removeFromParty(member, party); } }