diff --git a/mcMMO/com/gmail/nossr50/mcMMO.java b/mcMMO/com/gmail/nossr50/mcMMO.java index 6fbcab656..d33cdd4b2 100644 --- a/mcMMO/com/gmail/nossr50/mcMMO.java +++ b/mcMMO/com/gmail/nossr50/mcMMO.java @@ -103,6 +103,8 @@ public class mcMMO extends JavaPlugin mcPermissions.initialize(getServer()); config.configCheck(); + Party.getInstance().loadParties(); + new Party(this); if(!LoadProperties.useMySQL) Users.getInstance().loadUsers(); //Load Users file @@ -1199,6 +1201,8 @@ public class mcMMO extends JavaPlugin //Invite Command else if(LoadProperties.inviteEnable && label.equalsIgnoreCase(LoadProperties.invite) && mcPermissions.getInstance().party(player)){ + Party Pinstance = Party.getInstance(); + if(!PP.inParty()) { player.sendMessage(mcLocale.getString("mcPlayerListener.NotInParty")); @@ -1211,27 +1215,34 @@ public class mcMMO extends JavaPlugin } if(PP.inParty() && split.length >= 2 && isPlayer(split[1])) { - Player target = getPlayer(split[1]); - PlayerProfile PPt = Users.getProfile(target); - PPt.modifyInvite(PP.getParty()); - - - player.sendMessage(mcLocale.getString("mcPlayerListener.InviteSuccess")); - //target.sendMessage(ChatColor.RED+"ALERT: "+ChatColor.GREEN+"You have received a party invite for "+PPt.getInvite()+" from "+player.getName()); - target.sendMessage(mcLocale.getString("mcPlayerListener.ReceivedInvite1", new Object[] {PPt.getInvite(), player.getName()})); - //target.sendMessage(ChatColor.YELLOW+"Type "+ChatColor.GREEN+LoadProperties.accept+ChatColor.YELLOW+" to accept the invite"); - target.sendMessage(mcLocale.getString("mcPlayerListener.ReceivedInvite2", new Object[] {LoadProperties.accept})); + if(Pinstance.canInvite(player, PP)) { + Player target = getPlayer(split[1]); + PlayerProfile PPt = Users.getProfile(target); + PPt.modifyInvite(PP.getParty()); + + + player.sendMessage(mcLocale.getString("mcPlayerListener.InviteSuccess")); + //target.sendMessage(ChatColor.RED+"ALERT: "+ChatColor.GREEN+"You have received a party invite for "+PPt.getInvite()+" from "+player.getName()); + target.sendMessage(mcLocale.getString("mcPlayerListener.ReceivedInvite1", new Object[] {PPt.getInvite(), player.getName()})); + //target.sendMessage(ChatColor.YELLOW+"Type "+ChatColor.GREEN+LoadProperties.accept+ChatColor.YELLOW+" to accept the invite"); + target.sendMessage(mcLocale.getString("mcPlayerListener.ReceivedInvite2", new Object[] {LoadProperties.accept})); + } else { + //TODO: Needs more locale. + player.sendMessage("Party is locked, only party leader may invite."); + return true; + } } } //Accept invite else if(LoadProperties.acceptEnable && label.equalsIgnoreCase(LoadProperties.accept) && mcPermissions.getInstance().party(player)){ if(PP.hasPartyInvite()){ - if(PP.inParty()){ - Party.getInstance().informPartyMembersQuit(player, getPlayersOnline()); + Party Pinstance = Party.getInstance(); + + if(PP.inParty()) { + Pinstance.removeFromParty(player, PP); } PP.acceptInvite(); - Party.getInstance().informPartyMembers(player, getPlayersOnline()); - player.sendMessage(mcLocale.getString("mcPlayerListener.InviteAccepted", new Object[]{PP.getParty()})); + Pinstance.addToParty(player, PP, PP.getParty(), true); } else { player.sendMessage(mcLocale.getString("mcPlayerListener.NoInvites")); } @@ -1242,40 +1253,159 @@ public class mcMMO extends JavaPlugin player.sendMessage(ChatColor.YELLOW+"[mcMMO]"+ChatColor.DARK_RED +mcLocale.getString("mcPlayerListener.NoPermission")); return true; } - if(split.length == 1 && !PP.inParty()){ - player.sendMessage("Proper usage is "+LoadProperties.party+" or 'q' to quit"); - return true; + + Party Pinstance = Party.getInstance(); + + if(PP.inParty() && (!Pinstance.isParty(PP.getParty()) || !Pinstance.isInParty(player, PP))) { + Pinstance.addToParty(player, PP, PP.getParty(), false); } - if(split.length == 1 && PP.inParty()){ + + if(args.length == 0 && !PP.inParty()){ + //TODO: Needs more locale. + player.sendMessage("Proper usage is "+LoadProperties.party+" or 'q' to quit"); + player.sendMessage("To join a password protected party use "+LoadProperties.party+" "); + player.sendMessage("Or "+LoadProperties.party+" ? for more information."); + return true; + } else if(args.length == 0 && PP.inParty()){ String tempList = ""; int x = 0; for(Player p : this.getServer().getOnlinePlayers()){ if(PP.getParty().equals(Users.getProfile(p).getParty())){ - if(p != null && x+1 >= Party.getInstance().partyCount(player, getPlayersOnline())){ + if(p != null && x+1 >= Pinstance.partyCount(player, getPlayersOnline())){ tempList+= p.getName(); x++; } - if(p != null && x < Party.getInstance().partyCount(player, getPlayersOnline())){ + if(p != null && x < Pinstance.partyCount(player, getPlayersOnline())){ tempList+= p.getName() +", "; x++; } } } player.sendMessage(mcLocale.getString("mcPlayerListener.YouAreInParty", new Object[] {PP.getParty()})); - player.sendMessage(mcLocale.getString("mcPlayerListener.PartyMembers")+" ("+ChatColor.WHITE+tempList+ChatColor.GREEN+")"); - } - if(split.length > 1 && split[1].equals("q") && PP.inParty()){ - Party.getInstance().informPartyMembersQuit(player, getPlayersOnline()); - PP.removeParty(); - player.sendMessage(mcLocale.getString("mcPlayerListener.LeftParty")); + player.sendMessage(mcLocale.getString("mcPlayerListener.PartyMembers")+" ("+ChatColor.WHITE+tempList+ChatColor.GREEN+")"); return true; - } - if(split.length >= 2){ - if(PP.inParty()) - Party.getInstance().informPartyMembersQuit(player, getPlayersOnline()); - PP.setParty(split[1]); - player.sendMessage(mcLocale.getString("mcPlayerListener.JoinedParty", new Object[] {split[1]})); - Party.getInstance().informPartyMembers(player, getPlayersOnline()); + } else if(args.length == 1){ + if(args[0].equals("q") && PP.inParty()) { + Pinstance.removeFromParty(player, PP); + player.sendMessage(mcLocale.getString("mcPlayerListener.LeftParty")); + return true; + } else if (args[0].equalsIgnoreCase("?")) { + //TODO: Needs more locale. + player.sendMessage("Use "+LoadProperties.party+" to join a party or 'q' to quit"); + player.sendMessage("To join a password protected party use "+LoadProperties.party+" "); + player.sendMessage("To lock your party use "+LoadProperties.party+" lock"); + player.sendMessage("To unlock your party use "+LoadProperties.party+" unlock"); + player.sendMessage("To password protect your party use "+LoadProperties.party+" password "); + player.sendMessage("To kick from your party use "+LoadProperties.party+" kick "); + player.sendMessage("To transfer ownership of your party use "+LoadProperties.party+" owner "); + } else if (args[0].equalsIgnoreCase("lock")) { + if(PP.inParty()) { + if(Pinstance.isPartyLeader(player, PP.getParty())) { + Pinstance.lockParty(PP.getParty()); + //TODO: Needs more locale. + player.sendMessage("Party locked."); + } else { + //TODO: Needs more locale. + player.sendMessage("You are not the party owner."); + } + } else { + //TODO: Needs more locale. + player.sendMessage("This is not a valid party name"); + } + } else if (args[0].equalsIgnoreCase("unlock")) { + if(PP.inParty()) { + if(Pinstance.isPartyLeader(player, PP.getParty())) { + Pinstance.unlockParty(PP.getParty()); + //TODO: Needs more locale. + player.sendMessage("Party unlocked"); + } else { + //TODO: Needs more locale. + player.sendMessage("You are not the party owner."); + } + } else { + //TODO: Needs more locale. + player.sendMessage("This is not a valid party name"); + } + //Party debugging command. + //} else if (args[0].equalsIgnoreCase("dump")) { + // Pinstance.dump(player); + } else { + if(PP.inParty()) { + Pinstance.removeFromParty(player, PP); + } + Pinstance.addToParty(player, PP, args[0], false); + return true; + } + } else if(args.length == 2 && PP.inParty()) { + if(args[0].equalsIgnoreCase("password")) { + if(Pinstance.isPartyLeader(player, PP.getParty())) { + if(Pinstance.isPartyLocked(PP.getParty())) { + Pinstance.setPartyPassword(PP.getParty(), args[1]); + //TODO: Needs more locale. + player.sendMessage("Party password set to "+args[1]); + } else { + //TODO: Needs more locale. + player.sendMessage("Party not locked."); + } + } else { + //TODO: Needs more locale. + player.sendMessage("You are not the party owner."); + } + } else if(args[0].equalsIgnoreCase("kick")) { + if(Pinstance.isPartyLeader(player, PP.getParty())) { + if(Pinstance.isPartyLocked(PP.getParty())) { + Player tPlayer = null; + if(this.getServer().getPlayer(args[1]) != null) tPlayer = this.getServer().getPlayer(args[1]); + if(tPlayer == null) { + //TODO: Needs more locale. + player.sendMessage("Could not kick player "+args[1]); + } + if(!Pinstance.inSameParty(player, tPlayer)) { + player.sendMessage(tPlayer.getName()+" not in your party."); + } else { + //Not an admin + if(!mcPermissions.getInstance().admin(player)) { + //Can't kick an admin + if(mcPermissions.getInstance().admin(tPlayer)) { + //TODO: Needs more locale. + player.sendMessage("Could not kick player "+tPlayer.getName()); + } + } + PlayerProfile tPP = Users.getProfile(tPlayer); + Pinstance.removeFromParty(tPlayer, tPP); + tPlayer.sendMessage(mcLocale.getString("mcPlayerListener.LeftParty")); + } + } else { + //TODO: Needs more locale. + player.sendMessage("Party not locked."); + } + } else { + //TODO: Needs more locale. + player.sendMessage("You are not the party owner."); + } + } else if(args[0].equalsIgnoreCase("owner")) { + if(Pinstance.isPartyLeader(player, PP.getParty())) { + Player tPlayer = null; + if(this.getServer().getPlayer(args[1]) != null) tPlayer = this.getServer().getPlayer(args[1]); + if(tPlayer == null) { + //TODO: Needs more locale. + player.sendMessage("Could not set owner to "+args[1]); + } + if(!Pinstance.inSameParty(player, tPlayer)) { + player.sendMessage("Could not set owner to "+tPlayer.getName()); + } else { + Pinstance.setPartyLeader(PP.getParty(), tPlayer.getName()); + } + } else { + //TODO: Needs more locale. + player.sendMessage("You are not the party owner."); + } + } else { + Pinstance.removeFromParty(player, PP); + Pinstance.addToParty(player, PP, args[0], false, args[1]); + } + } else if(args.length == 2 && !PP.inParty()) { + Pinstance.addToParty(player, PP, args[0], false, args[1]); } } else if(LoadProperties.partyEnable && label.equalsIgnoreCase("p")){ diff --git a/mcMMO/com/gmail/nossr50/mcPermissions.java b/mcMMO/com/gmail/nossr50/mcPermissions.java index 65f255917..71be16b3a 100644 --- a/mcMMO/com/gmail/nossr50/mcPermissions.java +++ b/mcMMO/com/gmail/nossr50/mcPermissions.java @@ -194,6 +194,13 @@ public class mcPermissions return true; } } + public boolean partyLock(Player player) { + if (permissionsEnabled) { + return permission(player, "mcmmo.chat.partylock"); + } else { + return true; + } + } public boolean partyTeleport(Player player) { if (permissionsEnabled) { return permission(player, "mcmmo.commands.ptp"); diff --git a/mcMMO/com/gmail/nossr50/party/Party.java b/mcMMO/com/gmail/nossr50/party/Party.java index d73c68b7f..376703105 100644 --- a/mcMMO/com/gmail/nossr50/party/Party.java +++ b/mcMMO/com/gmail/nossr50/party/Party.java @@ -1,28 +1,47 @@ package com.gmail.nossr50.party; +import java.io.EOFException; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; +import java.util.HashMap; +import java.util.Iterator; + import org.bukkit.entity.Player; import com.gmail.nossr50.Users; import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.LoadProperties; +import com.gmail.nossr50.datatypes.PlayerProfile; import com.gmail.nossr50.locale.mcLocale; public class Party { + 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> partyPlayers = new HashMap>(); + HashMap partyLocks = new HashMap(); + HashMap partyPasswords = new HashMap(); + private static mcMMO plugin; - public Party(mcMMO instance) - { + public Party(mcMMO instance) { plugin = instance; } private static volatile Party instance; public static Party getInstance() { - if (instance == null) - { - instance = new Party(plugin); + if (instance == null) { + instance = new Party(plugin); } return instance; - } + } public boolean inSameParty(Player playera, Player playerb){ if(Users.getProfile(playera) == null || Users.getProfile(playerb) == null) { @@ -57,6 +76,13 @@ public class Party } return x; } + + public void informPartyMembers(Player player) { + Player[] players = plugin.getPlayersOnline(); + informPartyMembers(player, players); + } + + public void informPartyMembers(Player player, Player[] players) { int x = 0; @@ -72,6 +98,32 @@ public class Party } } } + + public void informPartyMembersOwnerChange(String newOwner) { + Player newOwnerPlayer = plugin.getServer().getPlayer(newOwner); + Player[] players = plugin.getPlayersOnline(); + informPartyMembersOwnerChange(newOwnerPlayer, players); + } + + public void informPartyMembersOwnerChange(Player newOwner, Player[] players) { + int x = 0; + for(Player p : players){ + if(newOwner != null && p != null){ + if(inSameParty(newOwner, p) && !p.getName().equals(newOwner.getName())) + { + //TODO: Needs more locale. + p.sendMessage(newOwner.getName()+" is the new party owner."); + x++; + } + } + } + } + + public void informPartyMembersQuit(Player player) { + Player[] players = plugin.getPlayersOnline(); + informPartyMembersQuit(player, players); + } + public void informPartyMembersQuit(Player player, Player[] players) { int x = 0; @@ -85,5 +137,252 @@ public class Party } } } + + public void removeFromParty(Player player, PlayerProfile PP) { + informPartyMembersQuit(player); + String party = PP.getParty(); + if(isPartyLeader(player, 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) { + addToParty(player, PP, newParty, invite, null); + } + + public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite, String password) { + //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) { + //TODO: Needs more locale. + player.sendMessage("This party requires a password. Use "+LoadProperties.party+" to join it."); + return; + } else if(!password.equalsIgnoreCase(getPartyPassword(newParty))) { + //TODO: Needs more locale. + player.sendMessage("Party password incorrect."); + return; + } + } else { + //TODO: Needs more locale. + player.sendMessage("Party is locked."); + 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(); + } + 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 W putNestedEntry( + HashMap> nest, + U nestKey, + V nestedKey, + W nestedValue) + { + HashMap nested = nest.get(nestKey); + + if (nested == null) { + nested = new HashMap(); + nest.put(nestKey, nested); + } + + return nested.put(nestedKey, nestedValue); + } + + public void dump(Player player) { + player.sendMessage(partyPlayers.toString()); + player.sendMessage(partyLocks.toString()); + player.sendMessage(partyPasswords.toString()); + Iterator i = partyPlayers.keySet().iterator(); + while(i.hasNext()) { + String nestkey = i.next(); + player.sendMessage(nestkey); + Iterator j = partyPlayers.get(nestkey).keySet().iterator(); + while(j.hasNext()) { + String nestedkey = j.next(); + player.sendMessage("."+nestedkey); + if(partyPlayers.get(nestkey).get(nestedkey)) { + player.sendMessage("..True"); + } else { + player.sendMessage("..False"); + } + } + } + } + + public void lockParty(String partyName) { + this.partyLocks.put(partyName, true); + savePartyLocks(); + } + + public void unlockParty(String partyName) { + this.partyLocks.put(partyName, false); + savePartyLocks(); + } + + public void deleteParty(String partyName) { + this.partyPlayers.remove(partyName); + this.partyLocks.remove(partyName); + this.partyPasswords.remove(partyName); + saveParties(); + } + + public void setPartyPassword(String partyName, String password) { + if(password.equalsIgnoreCase("\"\"")) password = null; + this.partyPasswords.put(partyName, password); + savePartyPasswords(); + } + + public void setPartyLeader(String partyName, String playerName) { + Iterator i = partyPlayers.get(partyName).keySet().iterator(); + while(i.hasNext()) { + String playerKey = i.next(); + if(playerKey.equalsIgnoreCase(playerName)) { + partyPlayers.get(partyName).put(playerName, true); + informPartyMembersOwnerChange(playerName); + //TODO: Needs more locale. + plugin.getServer().getPlayer(playerName).sendMessage("You are now the party owner."); + continue; + } + if(partyPlayers.get(partyName).get(playerKey)) { + //TODO: Needs more locale. + plugin.getServer().getPlayer(playerKey).sendMessage("You are no longer party owner."); + partyPlayers.get(partyName).put(playerKey, false); + } + } + } + + public String getPartyPassword(String partyName) { + return this.partyPasswords.get(partyName); + } + + public boolean canInvite(Player player, PlayerProfile PP) { + return (isPartyLocked(PP.getParty()) && !isPartyLeader(player, PP.getParty())) ? false : true; + } + + public boolean isParty(String partyName) { + return this.partyPlayers.containsKey(partyName); + } + + public boolean isPartyEmpty(String partyName) { + return this.partyPlayers.get(partyName).isEmpty(); + } + + public boolean isPartyLeader(Player player, String partyName) { + if(this.partyPlayers.get(partyName).get(player.getName()) == null) return false; + return this.partyPlayers.get(partyName).get(player.getName()); + } + + public boolean isPartyLocked(String partyName) { + if(this.partyLocks.get(partyName) == null) return false; + return this.partyLocks.get(partyName); + } + + public boolean isPartyPasswordProtected(String partyName) { + return !(this.partyPasswords.get(partyName) == null); + } + + public boolean isInParty(Player player, PlayerProfile PP) { + return partyPlayers.get(PP.getParty()).containsKey(player.getName()); + } + + @SuppressWarnings("unchecked") + public void loadParties() { + if(new File(partyPlayersFile).exists()) { + try { + ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPlayersFile)); + this.partyPlayers = (HashMap>)obj.readObject(); + } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (EOFException e) { mcMMO.log.info("partyPlayersFile empty."); + } catch (IOException e) { e.printStackTrace(); + } catch (ClassNotFoundException e) { e.printStackTrace(); } + } + + if(new File(partyLocksFile).exists()) { + try { + ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyLocksFile)); + this.partyLocks = (HashMap)obj.readObject(); + } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (EOFException e) { mcMMO.log.info("partyLocksFile empty."); + } catch (IOException e) { e.printStackTrace(); + } catch (ClassNotFoundException e) { e.printStackTrace(); } + } + + if(new File(partyPasswordsFile).exists()) { + try { + ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPasswordsFile)); + this.partyPasswords = (HashMap)obj.readObject(); + } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (EOFException e) { mcMMO.log.info("partyPasswordsFile empty."); + } catch (IOException e) { e.printStackTrace(); + } catch (ClassNotFoundException e) { e.printStackTrace(); } + } + } + + public void saveParties() { + savePartyPlayers(); + savePartyLocks(); + savePartyPasswords(); + } + + public void savePartyPlayers() { + try { + new File(partyPlayersFile).createNewFile(); + ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyPlayersFile)); + obj.writeObject(this.partyPlayers); + obj.close(); + } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (IOException e) { e.printStackTrace(); } + } + + public void savePartyLocks() { + try { + new File(partyLocksFile).createNewFile(); + ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyLocksFile)); + obj.writeObject(this.partyLocks); + obj.close(); + } catch (FileNotFoundException e) { e.printStackTrace(); + } catch (IOException e) { 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(); } + } }