Party.java 16 KB

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