Party.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50.party;
  15. import java.io.EOFException;
  16. import java.io.File;
  17. import java.io.FileInputStream;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.ObjectInputStream;
  22. import java.io.ObjectOutputStream;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.Iterator;
  26. import org.bukkit.Bukkit;
  27. import org.bukkit.entity.Player;
  28. import com.gmail.nossr50.Users;
  29. import com.gmail.nossr50.mcMMO;
  30. import com.gmail.nossr50.datatypes.PlayerProfile;
  31. import com.gmail.nossr50.locale.mcLocale;
  32. public class Party
  33. {
  34. /*
  35. * This file is part of mmoMinecraft (http://code.google.com/p/mmo-minecraft/).
  36. *
  37. * mmoMinecraft is free software: you can redistribute it and/or modify
  38. * it under the terms of the GNU General Public License as published by
  39. * the Free Software Foundation, either version 3 of the License, or
  40. * (at your option) any later version.
  41. *
  42. * This program is distributed in the hope that it will be useful,
  43. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  44. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  45. * GNU General Public License for more details.
  46. * You should have received a copy of the GNU General Public License
  47. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  48. */
  49. public static String partyPlayersFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPlayers";
  50. public static String partyLocksFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyLocks";
  51. public static String partyPasswordsFile = mcMMO.maindirectory + File.separator + "FlatFileStuff" + File.separator + "partyPasswords";
  52. HashMap<String, HashMap<String, Boolean>> partyPlayers = new HashMap<String, HashMap<String, Boolean>>();
  53. HashMap<String, Boolean> partyLocks = new HashMap<String, Boolean>();
  54. HashMap<String, String> partyPasswords = new HashMap<String, String>();
  55. private static mcMMO plugin;
  56. public Party(mcMMO instance) {
  57. new File(mcMMO.maindirectory + File.separator + "FlatFileStuff").mkdir();
  58. plugin = instance;
  59. }
  60. private static volatile Party instance;
  61. public static Party getInstance()
  62. {
  63. if (instance == null) {
  64. instance = new Party(plugin);
  65. }
  66. return instance;
  67. }
  68. public boolean inSameParty(Player playera, Player playerb){
  69. if(Users.getProfile(playera).inParty() && Users.getProfile(playerb).inParty())
  70. {
  71. if(Users.getProfile(playera).getParty().equals(Users.getProfile(playerb).getParty()))
  72. {
  73. return true;
  74. } else
  75. {
  76. return false;
  77. }
  78. } else
  79. {
  80. return false;
  81. }
  82. }
  83. public int partyCount(Player player, Player[] players)
  84. {
  85. int x = 0;
  86. for(Player hurrdurr : players)
  87. {
  88. if(player != null && hurrdurr != null)
  89. {
  90. if(Users.getProfile(player).getParty().equals(Users.getProfile(hurrdurr).getParty()))
  91. x++;
  92. }
  93. }
  94. return x;
  95. }
  96. public void informPartyMembers(Player player)
  97. {
  98. informPartyMembers(player, Bukkit.getServer().getOnlinePlayers());
  99. }
  100. public void informPartyMembers(Player player, Player[] players)
  101. {
  102. for(Player p : players)
  103. {
  104. if(player != null && p != null)
  105. {
  106. if(inSameParty(player, p) && !p.getName().equals(player.getName()))
  107. {
  108. p.sendMessage(mcLocale.getString("Party.InformedOnJoin", new Object[] {player.getName()}));
  109. }
  110. }
  111. }
  112. }
  113. public ArrayList<Player> getPartyMembers(Player player)
  114. {
  115. ArrayList<Player> players = new ArrayList<Player>();
  116. for(Player p : Bukkit.getServer().getOnlinePlayers())
  117. {
  118. if(p.isOnline() && player != null && p != null)
  119. {
  120. if(inSameParty(player, p) && !p.getName().equals(player.getName()))
  121. {
  122. players.add(p);
  123. }
  124. }
  125. }
  126. return players;
  127. }
  128. public void informPartyMembersOwnerChange(String newOwner) {
  129. Player newOwnerPlayer = plugin.getServer().getPlayer(newOwner);
  130. informPartyMembersOwnerChange(newOwnerPlayer, Bukkit.getServer().getOnlinePlayers());
  131. }
  132. public void informPartyMembersOwnerChange(Player newOwner, Player[] players) {
  133. for(Player p : players){
  134. if(newOwner != null && p != null){
  135. if(inSameParty(newOwner, p))
  136. {
  137. //TODO: Needs more locale.
  138. p.sendMessage(newOwner.getName()+" is the new party owner.");
  139. }
  140. }
  141. }
  142. }
  143. public void informPartyMembersQuit(Player player)
  144. {
  145. informPartyMembersQuit(player, Bukkit.getServer().getOnlinePlayers());
  146. }
  147. public void informPartyMembersQuit(Player player, Player[] players)
  148. {
  149. for(Player p : players){
  150. if(player != null && p != null){
  151. if(inSameParty(player, p) && !p.getName().equals(player.getName()))
  152. {
  153. p.sendMessage(mcLocale.getString("Party.InformedOnQuit", new Object[] {player.getName()}));
  154. }
  155. }
  156. }
  157. }
  158. public void removeFromParty(Player player, PlayerProfile PP)
  159. {
  160. //Stop NPE... hopefully
  161. if(!isParty(PP.getParty()) || !isInParty(player, PP))
  162. addToParty(player, PP, PP.getParty(), false);
  163. informPartyMembersQuit(player);
  164. String party = PP.getParty();
  165. if(isPartyLeader(player.getName(), party))
  166. {
  167. if(isPartyLocked(party)) {
  168. unlockParty(party);
  169. }
  170. }
  171. this.partyPlayers.get(party).remove(player.getName());
  172. if(isPartyEmpty(party)) deleteParty(party);
  173. PP.removeParty();
  174. savePartyPlayers();
  175. }
  176. public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite) {
  177. newParty = newParty.replace(":", ".");
  178. addToParty(player, PP, newParty, invite, null);
  179. }
  180. public void addToParty(Player player, PlayerProfile PP, String newParty, Boolean invite, String password)
  181. {
  182. //Fix for FFS
  183. newParty = newParty.replace(":", ".");
  184. //Don't care about passwords on invites
  185. if(!invite)
  186. {
  187. //Don't care about passwords if it isn't locked
  188. if(isPartyLocked(newParty))
  189. {
  190. if(isPartyPasswordProtected(newParty))
  191. {
  192. if(password == null)
  193. {
  194. //TODO: Needs more locale.
  195. player.sendMessage("This party requires a password. Use /party <party> <password> to join it.");
  196. return;
  197. } else if(!password.equalsIgnoreCase(getPartyPassword(newParty)))
  198. {
  199. //TODO: Needs more locale.
  200. player.sendMessage("Party password incorrect.");
  201. return;
  202. }
  203. } else
  204. {
  205. //TODO: Needs more locale.
  206. player.sendMessage("Party is locked.");
  207. return;
  208. }
  209. }
  210. } else
  211. {
  212. PP.acceptInvite();
  213. }
  214. //New party?
  215. if(!isParty(newParty))
  216. {
  217. putNestedEntry(this.partyPlayers, newParty, player.getName(), true);
  218. //Get default locking behavior from config?
  219. this.partyLocks.put(newParty, false);
  220. this.partyPasswords.put(newParty, null);
  221. saveParties();
  222. } else
  223. {
  224. putNestedEntry(this.partyPlayers, newParty, player.getName(), false);
  225. savePartyPlayers();
  226. }
  227. PP.setParty(newParty);
  228. informPartyMembers(player);
  229. if(!invite)
  230. {
  231. player.sendMessage(mcLocale.getString("mcPlayerListener.JoinedParty", new Object[] { newParty }));
  232. } else
  233. {
  234. player.sendMessage(mcLocale.getString("mcPlayerListener.InviteAccepted", new Object[]{ PP.getParty() }));
  235. }
  236. }
  237. private static <U,V,W> W putNestedEntry(
  238. HashMap<U,HashMap<V,W>> nest,
  239. U nestKey,
  240. V nestedKey,
  241. W nestedValue)
  242. {
  243. HashMap<V,W> nested = nest.get(nestKey);
  244. if (nested == null) {
  245. nested = new HashMap<V,W>();
  246. nest.put(nestKey, nested);
  247. }
  248. return nested.put(nestedKey, nestedValue);
  249. }
  250. public void dump(Player player) {
  251. player.sendMessage(partyPlayers.toString());
  252. player.sendMessage(partyLocks.toString());
  253. player.sendMessage(partyPasswords.toString());
  254. Iterator<String> i = partyPlayers.keySet().iterator();
  255. while(i.hasNext()) {
  256. String nestkey = i.next();
  257. player.sendMessage(nestkey);
  258. Iterator<String> j = partyPlayers.get(nestkey).keySet().iterator();
  259. while(j.hasNext()) {
  260. String nestedkey = j.next();
  261. player.sendMessage("."+nestedkey);
  262. if(partyPlayers.get(nestkey).get(nestedkey)) {
  263. player.sendMessage("..True");
  264. } else {
  265. player.sendMessage("..False");
  266. }
  267. }
  268. }
  269. }
  270. public void lockParty(String partyName) {
  271. this.partyLocks.put(partyName, true);
  272. savePartyLocks();
  273. }
  274. public void unlockParty(String partyName) {
  275. this.partyLocks.put(partyName, false);
  276. savePartyLocks();
  277. }
  278. public void deleteParty(String partyName) {
  279. this.partyPlayers.remove(partyName);
  280. this.partyLocks.remove(partyName);
  281. this.partyPasswords.remove(partyName);
  282. saveParties();
  283. }
  284. public void setPartyPassword(String partyName, String password) {
  285. if(password.equalsIgnoreCase("\"\"")) password = null;
  286. this.partyPasswords.put(partyName, password);
  287. savePartyPasswords();
  288. }
  289. public void setPartyLeader(String partyName, String playerName) {
  290. Iterator<String> i = partyPlayers.get(partyName).keySet().iterator();
  291. while(i.hasNext()) {
  292. String playerKey = i.next();
  293. if(playerKey.equalsIgnoreCase(playerName)) {
  294. partyPlayers.get(partyName).put(playerName, true);
  295. informPartyMembersOwnerChange(playerName);
  296. //TODO: Needs more locale.
  297. plugin.getServer().getPlayer(playerName).sendMessage("You are now the party owner.");
  298. continue;
  299. }
  300. if(partyPlayers.get(partyName).get(playerKey)) {
  301. //TODO: Needs more locale.
  302. plugin.getServer().getPlayer(playerKey).sendMessage("You are no longer party owner.");
  303. partyPlayers.get(partyName).put(playerKey, false);
  304. }
  305. }
  306. }
  307. public String getPartyPassword(String partyName) {
  308. return this.partyPasswords.get(partyName);
  309. }
  310. public boolean canInvite(Player player, PlayerProfile PP) {
  311. return (isPartyLocked(PP.getParty()) && !isPartyLeader(player.getName(), PP.getParty())) ? false : true;
  312. }
  313. public boolean isParty(String partyName) {
  314. return this.partyPlayers.containsKey(partyName);
  315. }
  316. public boolean isPartyEmpty(String partyName) {
  317. return this.partyPlayers.get(partyName).isEmpty();
  318. }
  319. public boolean isPartyLeader(String playerName, String partyName) {
  320. if(this.partyPlayers.get(partyName) != null)
  321. {
  322. if(this.partyPlayers.get(partyName).get(playerName) == null) return false;
  323. return this.partyPlayers.get(partyName).get(playerName);
  324. }
  325. else
  326. return false;
  327. }
  328. public boolean isPartyLocked(String partyName) {
  329. if(this.partyLocks.get(partyName) == null) return false;
  330. return this.partyLocks.get(partyName);
  331. }
  332. public boolean isPartyPasswordProtected(String partyName) {
  333. return !(this.partyPasswords.get(partyName) == null);
  334. }
  335. public boolean isInParty(Player player, PlayerProfile PP) {
  336. return partyPlayers.get(PP.getParty()).containsKey(player.getName());
  337. }
  338. @SuppressWarnings("unchecked")
  339. public void loadParties() {
  340. if(new File(partyPlayersFile).exists()) {
  341. try {
  342. ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPlayersFile));
  343. this.partyPlayers = (HashMap<String, HashMap<String, Boolean>>)obj.readObject();
  344. } catch (FileNotFoundException e) { e.printStackTrace();
  345. } catch (EOFException e) { mcMMO.log.info("partyPlayersFile empty.");
  346. } catch (IOException e) { e.printStackTrace();
  347. } catch (ClassNotFoundException e) { e.printStackTrace(); }
  348. }
  349. if(new File(partyLocksFile).exists()) {
  350. try {
  351. ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyLocksFile));
  352. this.partyLocks = (HashMap<String, Boolean>)obj.readObject();
  353. } catch (FileNotFoundException e) { e.printStackTrace();
  354. } catch (EOFException e) { mcMMO.log.info("partyLocksFile empty.");
  355. } catch (IOException e) { e.printStackTrace();
  356. } catch (ClassNotFoundException e) { e.printStackTrace(); }
  357. }
  358. if(new File(partyPasswordsFile).exists()) {
  359. try {
  360. ObjectInputStream obj = new ObjectInputStream(new FileInputStream(partyPasswordsFile));
  361. this.partyPasswords = (HashMap<String, String>)obj.readObject();
  362. } catch (FileNotFoundException e) { e.printStackTrace();
  363. } catch (EOFException e) { mcMMO.log.info("partyPasswordsFile empty.");
  364. } catch (IOException e) { e.printStackTrace();
  365. } catch (ClassNotFoundException e) { e.printStackTrace(); }
  366. }
  367. }
  368. public void saveParties() {
  369. savePartyPlayers();
  370. savePartyLocks();
  371. savePartyPasswords();
  372. }
  373. public void savePartyPlayers() {
  374. try {
  375. new File(partyPlayersFile).createNewFile();
  376. ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyPlayersFile));
  377. obj.writeObject(this.partyPlayers);
  378. obj.close();
  379. } catch (FileNotFoundException e) { e.printStackTrace();
  380. } catch (IOException e) { e.printStackTrace(); }
  381. }
  382. public void savePartyLocks() {
  383. try {
  384. new File(partyLocksFile).createNewFile();
  385. ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyLocksFile));
  386. obj.writeObject(this.partyLocks);
  387. obj.close();
  388. } catch (FileNotFoundException e) { e.printStackTrace();
  389. } catch (IOException e) { e.printStackTrace(); }
  390. }
  391. public void savePartyPasswords() {
  392. try {
  393. new File(partyPasswordsFile).createNewFile();
  394. ObjectOutputStream obj = new ObjectOutputStream(new FileOutputStream(partyPasswordsFile));
  395. obj.writeObject(this.partyPasswords);
  396. obj.close();
  397. } catch (FileNotFoundException e) { e.printStackTrace();
  398. } catch (IOException e) { e.printStackTrace(); }
  399. }
  400. }