*CLEANUP* - Party.java

This commit is contained in:
GJ 2012-04-03 00:06:30 -04:00
parent 24a50346db
commit b70e868b5c
4 changed files with 524 additions and 356 deletions

View File

@ -100,5 +100,26 @@ public class PartyAPI {
return Party.getInstance().getPartyMembers(player); return Party.getInstance().getPartyMembers(player);
} }
/**
* Add a player to a party.
* </br>
* This function is designed for API usage.
*
* @param player The player to add to the party
* @param partyName The party to add the player to
*/
public void addToParty(Player player, String partyName) {
Party.getInstance().addToParty(player, Users.getProfile(player), partyName, false, null);
}
/**
* Remove a player from a party.
* </br>
* This function is designed for API usage.
*
* @param player The player to remove
*/
public void removeFromParty(Player player) {
Party.getInstance().removeFromParty(player, Users.getProfile(player));
}
} }

View File

@ -54,7 +54,7 @@ public class AcceptCommand implements CommandExecutor {
} }
} }
PP.acceptInvite(); PP.acceptInvite();
Pinstance.addToParty(player, PP, PP.getParty(), true); Pinstance.addToParty(player, PP, PP.getParty(), true, null);
} else { } else {
player.sendMessage(mcLocale.getString("mcPlayerListener.NoInvites")); player.sendMessage(mcLocale.getString("mcPlayerListener.NoInvites"));

View File

@ -35,7 +35,7 @@ public class PartyCommand implements CommandExecutor {
Party Pinstance = Party.getInstance(); Party Pinstance = Party.getInstance();
if (PP.inParty() && (!Pinstance.isParty(PP.getParty()) || !Pinstance.isInParty(player, PP))) { if (PP.inParty() && (!Pinstance.isParty(PP.getParty()) || !Pinstance.isInParty(player, PP))) {
Pinstance.addToParty(player, PP, PP.getParty(), false); Pinstance.addToParty(player, PP, PP.getParty(), false, null);
} }
if (args.length == 0 && !PP.inParty()) { if (args.length == 0 && !PP.inParty()) {
@ -48,7 +48,7 @@ public class PartyCommand implements CommandExecutor {
int x = 0; int x = 0;
for (Player p : Bukkit.getServer().getOnlinePlayers()) { for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (PP.getParty().equals(Users.getProfile(p).getParty())) { if (PP.getParty().equals(Users.getProfile(p).getParty())) {
if (p != null && x + 1 >= Pinstance.partyCount(player, Bukkit.getServer().getOnlinePlayers())) { if (p != null && x + 1 >= Pinstance.partyCount(player)) {
if (Pinstance.isPartyLeader(p.getName(), PP.getParty())) { if (Pinstance.isPartyLeader(p.getName(), PP.getParty())) {
tempList += ChatColor.GOLD + p.getName(); tempList += ChatColor.GOLD + p.getName();
x++; x++;
@ -57,7 +57,7 @@ public class PartyCommand implements CommandExecutor {
x++; x++;
} }
} }
if (p != null && x < Pinstance.partyCount(player, Bukkit.getServer().getOnlinePlayers())) { if (p != null && x < Pinstance.partyCount(player)) {
if (Pinstance.isPartyLeader(p.getName(), PP.getParty())) { if (Pinstance.isPartyLeader(p.getName(), PP.getParty())) {
tempList += ChatColor.GOLD + p.getName() + ", "; tempList += ChatColor.GOLD + p.getName() + ", ";
x++; x++;
@ -175,7 +175,7 @@ public class PartyCommand implements CommandExecutor {
return true; return true;
} }
} }
Pinstance.addToParty(player, PP, args[0], false); Pinstance.addToParty(player, PP, args[0], false, null);
return true; return true;
} }
} else if (args.length == 2 && PP.inParty()) { } else if (args.length == 2 && PP.inParty()) {

View File

@ -20,240 +20,252 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.datatypes.PlayerProfile;
import com.gmail.nossr50.locale.mcLocale; import com.gmail.nossr50.locale.mcLocale;
public class Party { public class Party {
/* /*
* This file is part of mmoMinecraft (http://code.google.com/p/mmo-minecraft/). * This file is part of mmoMinecraft (http://code.google.com/p/mmo-minecraft/).
* *
* mmoMinecraft is free software: you can redistribute it and/or modify * mmoMinecraft is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
* You should have received a copy of the GNU General Public License * You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
public static String partyPlayersFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPlayers";
public static String partyLocksFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyLocks";
public static String partyPasswordsFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPasswords";
HashMap<String, HashMap<String, Boolean>> partyPlayers = new HashMap<String, HashMap<String, Boolean>>(); public static String partyPlayersFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPlayers";
HashMap<String, Boolean> partyLocks = new HashMap<String, Boolean>(); public static String partyLocksFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyLocks";
HashMap<String, String> partyPasswords = new HashMap<String, String>(); public static String partyPasswordsFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPasswords";
private static mcMMO plugin; HashMap<String, HashMap<String, Boolean>> partyPlayers = new HashMap<String, HashMap<String, Boolean>>();
public Party(mcMMO instance) { HashMap<String, Boolean> partyLocks = new HashMap<String, Boolean>();
new File(mcMMO.maindirectory + File.separator + "FlatFileStuff").mkdir(); HashMap<String, String> partyPasswords = new HashMap<String, String>();
plugin = instance;
private static mcMMO plugin;
private static volatile Party instance;
public Party(mcMMO instance) {
new File(mcMMO.maindirectory + File.separator + "FlatFileStuff").mkdir();
plugin = instance;
} }
private static volatile Party instance;
public static Party getInstance() {
public static Party getInstance() if (instance == null) {
{ instance = new Party(plugin);
if (instance == null) { }
instance = new Party(plugin); return instance;
}
return instance;
} }
/**
* Check if two players are in the same party.
*
* @param playera The first player
* @param playerb The second player
* @return true if they are in the same party, false otherwise
*/
public boolean inSameParty(Player playera, Player playerb){ public boolean inSameParty(Player playera, Player playerb){
if(Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty()) PlayerProfile PPa = Users.getProfile(playera);
{ PlayerProfile PPb = Users.getProfile(playerb);
if(Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty()))
{ if ((PPa.inParty() && PPb.inParty()) && (PPa.getParty().equals(PPb.getParty()))) {
return true; return true;
} else }
{ else {
return false; return false;
}
} else
{
return false;
}
}
public int partyCount(Player player, Player[] players)
{
int x = 0;
for(Player hurrdurr : players)
{
if(player != null && hurrdurr != null)
{
if(Users.getProfile(player).getParty().equals(Users.getProfile(hurrdurr).getParty()))
x++;
}
} }
return x;
} }
public void informPartyMembers(Player player) /**
{ * Get the number of players in this player's party.
informPartyMembers(player, Bukkit.getServer().getOnlinePlayers()); *
* @param player The player to check
* @param players A list of players to
* @return the number of players in this player's party
*/
public int partyCount(Player player) {
PlayerProfile PP = Users.getProfile(player);
int partyMembers = 0;
for (Player p : Bukkit.getServer().getOnlinePlayers()) {
if (player != null && p != null) { //Is this even possible?
if (PP.getParty().equals(Users.getProfile(p).getParty())) {
partyMembers++;
}
}
}
return partyMembers;
} }
private void informPartyMembers(Player player) {
public void informPartyMembers(Player player, Player[] players) String playerName = player.getName();
{
for(Player p : players) for (Player p : Bukkit.getServer().getOnlinePlayers()) {
{ if (player != null && p != null) {
if(player != null && p != null) if (inSameParty(player, p) && !p.getName().equals(playerName)) {
{ p.sendMessage(mcLocale.getString("Party.InformedOnJoin", new Object[] {playerName}));
if(inSameParty(player, p) && !p.getName().equals(player.getName()))
{
p.sendMessage(mcLocale.getString("Party.InformedOnJoin", new Object[] {player.getName()}));
} }
} }
} }
} }
public ArrayList<Player> getPartyMembers(Player player) /**
{ * Get a list of all players in this player's party.
ArrayList<Player> players = new ArrayList<Player>(); *
* @param player The player to check
for(Player p : Bukkit.getServer().getOnlinePlayers()) * @return all the players in the player's party
{ */
if(p.isOnline() && player != null && p != null) public ArrayList<Player> getPartyMembers(Player player) {
{ ArrayList<Player> players = new ArrayList<Player>();
if(inSameParty(player, p) && !p.getName().equals(player.getName()))
{ for (Player p : Bukkit.getServer().getOnlinePlayers()) {
players.add(p); if (p.isOnline() && player != null && p != null) {
if (inSameParty(player, p) && !p.getName().equals(player.getName())) {
players.add(p);
} }
} }
} }
return players; return players;
}
public void informPartyMembersOwnerChange(String newOwner) {
Player newOwnerPlayer = plugin.getServer().getPlayer(newOwner);
informPartyMembersOwnerChange(newOwnerPlayer, Bukkit.getServer().getOnlinePlayers());
}
public void informPartyMembersOwnerChange(Player newOwner, Player[] players) {
for(Player p : players){
if(newOwner != null && p != null){
if(inSameParty(newOwner, p))
{
p.sendMessage(newOwner.getName()+" is the new party owner."); //TODO: Needs more locale
}
}
}
}
public void informPartyMembersQuit(Player player)
{
informPartyMembersQuit(player, Bukkit.getServer().getOnlinePlayers());
}
public void informPartyMembersQuit(Player player, Player[] players)
{
for(Player p : players){
if(player != null && p != null){
if(inSameParty(player, p) && !p.getName().equals(player.getName()))
{
p.sendMessage(mcLocale.getString("Party.InformedOnQuit", new Object[] {player.getName()}));
}
}
}
}
public void removeFromParty(Player player, PlayerProfile PP)
{
//Stop NPE... hopefully
if(!isParty(PP.getParty()) || !isInParty(player, PP))
addToParty(player, PP, PP.getParty(), false);
informPartyMembersQuit(player);
String party = PP.getParty();
if(isPartyLeader(player.getName(), party))
{
if(isPartyLocked(party)) {
unlockParty(party);
}
}
this.partyPlayers.get(party).remove(player.getName());
if(isPartyEmpty(party)) deleteParty(party);
PP.removeParty();
savePartyPlayers();
}
public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite) {
newParty = newParty.replace(":", ".");
addToParty(player, PP, newParty, invite, null);
} }
/**
public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite, String password) * Notify party members when the party owner changes.
{ *
//Fix for FFS * @param newOwnerName The name of the new party owner
newParty = newParty.replace(":", "."); */
private void informPartyMembersOwnerChange(String newOwnerName) {
//Don't care about passwords on invites Player newOwner = plugin.getServer().getPlayer(newOwnerName);
if(!invite)
{
//Don't care about passwords if it isn't locked
if(isPartyLocked(newParty))
{
if(isPartyPasswordProtected(newParty))
{
if(password == null)
{
player.sendMessage("This party requires a password. Use /party <party> <password> to join it."); //TODO: Needs more locale.
return;
} else if(!password.equalsIgnoreCase(getPartyPassword(newParty)))
{
player.sendMessage("Party password incorrect."); //TODO: Needs more locale.
return;
}
} else
{
player.sendMessage("Party is locked."); //TODO: Needs more locale.
return;
}
}
} else
{
PP.acceptInvite();
}
//New party?
if(!isParty(newParty))
{
putNestedEntry(this.partyPlayers, newParty, player.getName(), true);
//Get default locking behavior from config?
this.partyLocks.put(newParty, false);
this.partyPasswords.put(newParty, null);
saveParties();
} else
{
putNestedEntry(this.partyPlayers, newParty, player.getName(), false);
savePartyPlayers(); for (Player p : Bukkit.getServer().getOnlinePlayers()){
} if (newOwner != null && p != null) {
PP.setParty(newParty); if (inSameParty(newOwner, p)) {
informPartyMembers(player); p.sendMessage(newOwnerName + " is the new party owner."); //TODO: Needs more locale
}
if(!invite) }
{ }
player.sendMessage(mcLocale.getString("mcPlayerListener.JoinedParty", new Object[] { newParty }));
} else
{
player.sendMessage(mcLocale.getString("mcPlayerListener.InviteAccepted", new Object[]{ PP.getParty() }));
}
} }
private static <U,V,W> W putNestedEntry( /**
HashMap<U,HashMap<V,W>> nest, * Notify party members when the a party member quits.
U nestKey, *
V nestedKey, * @param player The player that quit
W nestedValue) */
{ private void informPartyMembersQuit(Player player) {
String playerName = player.getName();
for (Player p : Bukkit.getServer().getOnlinePlayers()){
if (player != null && p != null){
if (inSameParty(player, p) && !p.getName().equals(playerName)) {
p.sendMessage(mcLocale.getString("Party.InformedOnQuit", new Object[] {playerName}));
}
}
}
}
/**
* Remove a player from a party.
*
* @param player The player to remove
* @param PP The profile of the player to remove
*/
public void removeFromParty(Player player, PlayerProfile PP) {
String party = PP.getParty();
String playerName = player.getName();
//Stop NPE... hopefully
if (!isParty(party) || !isInParty(player, PP)) {
addToParty(player, PP, party, false, null);
}
informPartyMembersQuit(player);
if (isPartyLeader(playerName, party)) {
if (isPartyLocked(party)) {
unlockParty(party);
}
}
partyPlayers.get(party).remove(playerName);
if (isPartyEmpty(party)) {
deleteParty(party);
}
PP.removeParty();
savePartyFile(partyPlayersFile, partyPlayers);
}
/**
* Add a player to a party.
*
* @param player The player to add to the party
* @param PP The profile of the player to add to the party
* @param newParty The party to add the player to
* @param invite true if the player was invited to this party, false otherwise
* @param password the password for this party, null if there was no password
*/
public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite, String password) {
String playerName = player.getName();
//Fix for FFS
newParty = newParty.replace(":", ".");
//Don't care about passwords on invites
if (!invite) {
//Don't care about passwords if it isn't locked
if (isPartyLocked(newParty)) {
if (isPartyPasswordProtected(newParty)) {
if (password == null) {
player.sendMessage("This party requires a password. Use /party <party> <password> to join it."); //TODO: Needs more locale.
return;
}
else if(!password.equalsIgnoreCase(getPartyPassword(newParty))) {
player.sendMessage("Party password incorrect."); //TODO: Needs more locale.
return;
}
}
else {
player.sendMessage("Party is locked."); //TODO: Needs more locale.
return;
}
}
}
else {
PP.acceptInvite();
}
//New party?
if (!isParty(newParty)) {
putNestedEntry(partyPlayers, newParty, playerName, true);
//Get default locking behavior from config?
partyLocks.put(newParty, false);
partyPasswords.put(newParty, null);
saveParties();
}
else {
putNestedEntry(partyPlayers, newParty, playerName, false);
savePartyFile(partyPlayersFile, partyPlayers);
}
PP.setParty(newParty);
informPartyMembers(player);
if (!invite) {
player.sendMessage(mcLocale.getString("mcPlayerListener.JoinedParty", new Object[]{ newParty }));
}
else {
player.sendMessage(mcLocale.getString("mcPlayerListener.InviteAccepted", new Object[]{ PP.getParty() }));
}
}
private static <U,V,W> W putNestedEntry(HashMap<U, HashMap<V, W>> nest, U nestKey, V nestedKey, W nestedValue) {
HashMap<V,W> nested = nest.get(nestKey); HashMap<V,W> nested = nest.get(nestKey);
if (nested == null) { if (nested == null) {
@ -261,175 +273,310 @@ public class Party {
nest.put(nestKey, nested); nest.put(nestKey, nested);
} }
return nested.put(nestedKey, nestedValue); return nested.put(nestedKey, nestedValue);
} }
public void dump(Player player) { /*
player.sendMessage(partyPlayers.toString()); * Any reason why we need to keep this function around?
player.sendMessage(partyLocks.toString()); */
player.sendMessage(partyPasswords.toString()); private void dump(Player player) {
Iterator<String> i = partyPlayers.keySet().iterator(); player.sendMessage(partyPlayers.toString());
while(i.hasNext()) { player.sendMessage(partyLocks.toString());
String nestkey = i.next(); player.sendMessage(partyPasswords.toString());
player.sendMessage(nestkey); Iterator<String> i = partyPlayers.keySet().iterator();
Iterator<String> j = partyPlayers.get(nestkey).keySet().iterator(); while(i.hasNext()) {
while(j.hasNext()) { String nestkey = i.next();
String nestedkey = j.next(); player.sendMessage(nestkey);
player.sendMessage("."+nestedkey); Iterator<String> j = partyPlayers.get(nestkey).keySet().iterator();
if(partyPlayers.get(nestkey).get(nestedkey)) { while(j.hasNext()) {
player.sendMessage("..True"); String nestedkey = j.next();
} else { player.sendMessage("."+nestedkey);
player.sendMessage("..False"); if(partyPlayers.get(nestkey).get(nestedkey)) {
} player.sendMessage("..True");
} } else {
} player.sendMessage("..False");
}
}
}
} }
/**
* Lock a party.
*
* @param partyName The party to lock
*/
public void lockParty(String partyName) { public void lockParty(String partyName) {
this.partyLocks.put(partyName, true); partyLocks.put(partyName, true);
savePartyLocks(); savePartyFile(partyLocksFile, partyLocks);
} }
/**
* Unlock a party.
*
* @param partyName The party to unlock
*/
public void unlockParty(String partyName) { public void unlockParty(String partyName) {
this.partyLocks.put(partyName, false); partyLocks.put(partyName, false);
savePartyLocks(); savePartyFile(partyLocksFile, partyLocks);
} }
/**
* Delete a party.
*
* @param partyName The party to delete
*/
private void deleteParty(String partyName) { private void deleteParty(String partyName) {
this.partyPlayers.remove(partyName); partyPlayers.remove(partyName);
this.partyLocks.remove(partyName); partyLocks.remove(partyName);
this.partyPasswords.remove(partyName); partyPasswords.remove(partyName);
saveParties(); saveParties();
} }
/**
* Set the password for a party.
*
* @param partyName The party name
* @param password The new party password
*/
public void setPartyPassword(String partyName, String password) { public void setPartyPassword(String partyName, String password) {
if(password.equalsIgnoreCase("\"\"")) password = null; if (password.equalsIgnoreCase("\"\"")) { //What's with that password string?
this.partyPasswords.put(partyName, password); password = null;
savePartyPasswords(); }
partyPasswords.put(partyName, password);
savePartyFile(partyPasswordsFile, partyPasswords);
} }
/**
* Set the leader of a party.
*
* @param partyName The party name
* @param playerName The name of the player to set as leader
*/
public void setPartyLeader(String partyName, String playerName) { public void setPartyLeader(String partyName, String playerName) {
Iterator<String> i = partyPlayers.get(partyName).keySet().iterator(); for (String name : partyPlayers.get(partyName).keySet()) {
while(i.hasNext()) { if (name.equalsIgnoreCase(playerName)) {
String playerKey = i.next(); partyPlayers.get(partyName).put(playerName, true);
if(playerKey.equalsIgnoreCase(playerName)) { informPartyMembersOwnerChange(playerName);
partyPlayers.get(partyName).put(playerName, true); plugin.getServer().getPlayer(playerName).sendMessage("You are now the party owner."); //TODO: Needs more locale.
informPartyMembersOwnerChange(playerName); continue;
plugin.getServer().getPlayer(playerName).sendMessage("You are now the party owner."); //TODO: Needs more locale. }
continue;
} if (partyPlayers.get(partyName).get(name)) {
if(partyPlayers.get(partyName).get(playerKey)) { plugin.getServer().getPlayer(name).sendMessage("You are no longer party owner."); //TODO: Needs more locale.
plugin.getServer().getPlayer(playerKey).sendMessage("You are no longer party owner."); //TODO: Needs more locale. partyPlayers.get(partyName).put(name, false);
partyPlayers.get(partyName).put(playerKey, false); }
} }
}
} }
/**
* Get the password of a party.
*
* @param partyName The party name
* @return The password of this party
*/
public String getPartyPassword(String partyName) { public String getPartyPassword(String partyName) {
return this.partyPasswords.get(partyName); return partyPasswords.get(partyName);
} }
/**
* Check if a player can invite others to their party.
*
* @param player The player to check
* @param PP The profile of the given player
* @return true if the player can invite, false otherwise
*/
public boolean canInvite(Player player, PlayerProfile PP) { public boolean canInvite(Player player, PlayerProfile PP) {
return (isPartyLocked(PP.getParty()) && !isPartyLeader(player.getName(), PP.getParty())) ? false : true; String party = PP.getParty();
if (isPartyLocked(party) && !isPartyLeader(player.getName(), party)) {
return false;
}
else {
return true;
}
} }
/**
* Check if a string is a valid party name.
*
* @param partyName The party name to check
* @return true if this is a valid party, false otherwise
*/
public boolean isParty(String partyName) { public boolean isParty(String partyName) {
return this.partyPlayers.containsKey(partyName); return partyPlayers.containsKey(partyName);
} }
/**
* Check if a party is empty.
*
* @param partyName The party to check
* @return true if this party is empty, false otherwise
*/
public boolean isPartyEmpty(String partyName) { public boolean isPartyEmpty(String partyName) {
return this.partyPlayers.get(partyName).isEmpty(); return partyPlayers.get(partyName).isEmpty();
} }
/**
* Check if a player is the party leader.
*
* @param playerName The player name to check
* @param partyName The party name to check
* @return true if the player is the party leader, false otherwise
*/
public boolean isPartyLeader(String playerName, String partyName) { public boolean isPartyLeader(String playerName, String partyName) {
if(this.partyPlayers.get(partyName) != null) HashMap<String, Boolean> partyMembers = partyPlayers.get(partyName);
{
if(this.partyPlayers.get(partyName).get(playerName) == null) return false; if (partyMembers != null) {
return this.partyPlayers.get(partyName).get(playerName); Boolean isLeader = partyMembers.get(playerName);
}
else if (isLeader == null) {
return false; return false;
}
else {
return isLeader;
}
}
else {
return false;
}
} }
/**
* Check if this party is locked.
*
* @param partyName The party to check
* @return true if this party is locked, false otherwise
*/
public boolean isPartyLocked(String partyName) { public boolean isPartyLocked(String partyName) {
if(this.partyLocks.get(partyName) == null) return false; Boolean isLocked = partyLocks.get(partyName);
return this.partyLocks.get(partyName);
if (isLocked == null) {
return false;
}
else {
return isLocked;
}
} }
/**
* Check if this party is password protected.
*
* @param partyName The party to check
* @return true if this party is password protected, false otherwise
*/
public boolean isPartyPasswordProtected(String partyName) { public boolean isPartyPasswordProtected(String partyName) {
return !(this.partyPasswords.get(partyName) == null); String password = partyPasswords.get(partyName);
if (password == null) {
return false;
}
else {
return true;
}
} }
/**
* Check if a player is in the party reflected by their profile.
*
* @param player The player to check
* @param PP The profile of the player
* @return true if this player is in the right party, false otherwise
*/
public boolean isInParty(Player player, PlayerProfile PP) { public boolean isInParty(Player player, PlayerProfile PP) {
return partyPlayers.get(PP.getParty()).containsKey(player.getName()); return partyPlayers.get(PP.getParty()).containsKey(player.getName());
} }
/**
* Load all party related files.
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public void loadParties() { public void loadParties() {
if(new File(partyPlayersFile).exists()) { if (new File(partyPlayersFile).exists()) {
try { try {
ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPlayersFile)); ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPlayersFile));
this.partyPlayers = (HashMap<String, HashMap<String, Boolean>>)obj.readObject(); partyPlayers = (HashMap<String, HashMap<String, Boolean>>) obj.readObject();
} catch (FileNotFoundException e) { e.printStackTrace(); }
} catch (EOFException e) { Bukkit.getLogger().info("partyPlayersFile empty."); catch (FileNotFoundException e) {
} catch (IOException e) { e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { e.printStackTrace(); } }
} catch (EOFException e) {
Bukkit.getLogger().info("partyPlayersFile empty.");
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
if(new File(partyLocksFile).exists()) { if (new File(partyLocksFile).exists()) {
try { try {
ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyLocksFile)); ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyLocksFile));
this.partyLocks = (HashMap<String, Boolean>)obj.readObject(); partyLocks = (HashMap<String, Boolean>) obj.readObject();
} catch (FileNotFoundException e) { e.printStackTrace(); }
} catch (EOFException e) { Bukkit.getLogger().info("partyLocksFile empty."); catch (FileNotFoundException e) {
} catch (IOException e) { e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { e.printStackTrace(); } }
} catch (EOFException e) {
Bukkit.getLogger().info("partyLocksFile empty.");
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
if(new File(partyPasswordsFile).exists()) { if (new File(partyPasswordsFile).exists()) {
try { try {
ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPasswordsFile)); ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPasswordsFile));
this.partyPasswords = (HashMap<String, String>)obj.readObject(); this.partyPasswords = (HashMap<String, String>) obj.readObject();
} catch (FileNotFoundException e) { e.printStackTrace(); }
} catch (EOFException e) { Bukkit.getLogger().info("partyPasswordsFile empty."); catch (FileNotFoundException e) {
} catch (IOException e) { e.printStackTrace(); e.printStackTrace();
} catch (ClassNotFoundException e) { e.printStackTrace(); } }
} catch (EOFException e) {
Bukkit.getLogger().info("partyPasswordsFile empty.");
}
catch (IOException e) {
e.printStackTrace();
}
catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
} }
public void saveParties() { /**
savePartyPlayers(); * Save all party-related files.
savePartyLocks(); */
savePartyPasswords(); private void saveParties() {
savePartyFile(partyPlayersFile, partyPlayers);
savePartyFile(partyLocksFile, partyLocks);
savePartyFile(partyPasswordsFile, partyPasswords);
} }
public void savePartyPlayers() { /**
try { * Save a party-related file.
new File(partyPlayersFile).createNewFile(); *
ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyPlayersFile)); * @param fileName The filename to save as
obj.writeObject(this.partyPlayers); * @param partyData The Hashmap with the party data
obj.close(); */
} catch (FileNotFoundException e) { e.printStackTrace(); private void savePartyFile(String fileName, Object partyData) {
} catch (IOException e) { e.printStackTrace(); } try {
} new File(fileName).createNewFile();
ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(fileName));
public void savePartyLocks() { obj.writeObject(partyData);
try { obj.close();
new File(partyLocksFile).createNewFile(); }
ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyLocksFile)); catch (FileNotFoundException e) {
obj.writeObject(this.partyLocks); e.printStackTrace();
obj.close(); }
} catch (FileNotFoundException e) { e.printStackTrace(); catch (IOException e) {
} catch (IOException e) { e.printStackTrace(); } e.printStackTrace();
} }
public void savePartyPasswords() {
try {
new File(partyPasswordsFile).createNewFile();
ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyPasswordsFile));
obj.writeObject(this.partyPasswords);
obj.close();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) { e.printStackTrace(); }
} }
} }