Advertisement
Guest User

Clan.Java

a guest
Apr 6th, 2013
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.90 KB | None | 0 0
  1. import engine.util.Misc;
  2. import game.Client;
  3. import game.Config;
  4. import game.Server;
  5. import game.player.PlayerHandler;
  6. import game.player.PlayerSave;
  7.  
  8. import java.util.Collections;
  9. import java.util.LinkedList;
  10.  
  11. /**
  12. * This class stores all information about the clan. This includes
  13. * active members, banned members, ranked members and their ranks,
  14. * clan title, and clan founder. All clan joining, leaving, and
  15. * moderation/setup is also handled in this class.
  16. * @author Galkon
  17. *
  18. */
  19. public class Clan {
  20.  
  21. /**
  22. * Adds a member to the clan.
  23. * @param player
  24. */
  25. public void addMember(Client player) {
  26. if (isBanned(player.playerName)) {
  27. player.sendMessage("<col=FF0000>You are currently banned from this clan chat.</col>");
  28. return;
  29. }
  30. if (whoCanJoin > Rank.ANYONE && !isFounder(player.playerName)) {
  31. if (getRank(player.playerName) < whoCanJoin) {
  32. player.sendMessage("Only " + getRankTitle(whoCanJoin) + "s+ may join this chat.");
  33. return;
  34. }
  35. }
  36. player.clan = this;
  37. player.lastClanChat = getFounder();
  38. activeMembers.add(player.playerName);
  39. player.getPA().sendFrame126("Leave chat", 18135);
  40. player.getPA().sendFrame126("Talking in: <col=FFFF64>" + getTitle() + "</col>", 18139);
  41. player.getPA().sendFrame126("Owner: <col=FFFFFF>" + Misc.formatPlayerName(getFounder()) + "</col>", 18140);
  42. player.sendMessage("Now talking in clan chat <col=FFFF64><shad=0>" + getTitle() + "</shad></col>.");
  43. player.sendMessage("To talk, start each line of chat with the / symbol.");
  44. updateMembers();
  45. }
  46.  
  47. /**
  48. * Removes the player from the clan.
  49. * @param player
  50. */
  51. public void removeMember(Client player) {
  52. for (int index = 0; index < activeMembers.size(); index++) {
  53. if (activeMembers.get(index).equalsIgnoreCase(player.playerName)) {
  54. player.clan = null;
  55. resetInterface(player);
  56. activeMembers.remove(index);
  57. }
  58. }
  59. player.getPA().refreshSkill(21);
  60. player.getPA().refreshSkill(22);
  61. player.getPA().refreshSkill(23);
  62. updateMembers();
  63. }
  64.  
  65. /**
  66. * Removes the player from the clan.
  67. * @param player
  68. */
  69. public void removeMember(String name) {
  70. for (int index = 0; index < activeMembers.size(); index++) {
  71. if (activeMembers.get(index).equalsIgnoreCase(name)) {
  72. Client player = (Client) PlayerHandler.getPlayer(name);
  73. if (player != null) {
  74. player.clan = null;
  75. resetInterface(player);
  76. activeMembers.remove(index);
  77. }
  78. }
  79. }
  80. updateMembers();
  81. }
  82.  
  83. /**
  84. * Updates the members on the interface for the player.
  85. * @param player
  86. */
  87. public void updateInterface(Client player) {
  88. player.getPA().sendFrame126("Talking in: <col=FFFF64>" + getTitle() + "</col>", 18139);
  89. player.getPA().sendFrame126("Owner: <col=FFFFFF>" + Misc.formatPlayerName(getFounder()) + "</col>", 18140);
  90. Collections.sort(activeMembers);
  91. for (int index = 0; index < 100; index++) {
  92. if (index < activeMembers.size()) {
  93. player.getPA().sendFrame126("<clan=" + getRank(activeMembers.get(index)) + ">" + Misc.formatPlayerName(activeMembers.get(index)), 18144 + index);
  94. } else {
  95. player.getPA().sendFrame126("", 18144 + index);
  96. }
  97. }
  98. }
  99.  
  100. /**
  101. * Updates the interface for all members.
  102. */
  103. public void updateMembers() {
  104. for (int index = 0; index < Config.MAX_PLAYERS; index++) {
  105. Client player = (Client) PlayerHandler.players[index];
  106. if (player != null && activeMembers != null) {
  107. if (activeMembers.contains(player.playerName)) {
  108. updateInterface(player);
  109. }
  110. }
  111. }
  112. }
  113.  
  114. /**
  115. * Resets the clan interface.
  116. * @param player
  117. */
  118. public void resetInterface(Client player) {
  119. player.getPA().sendFrame126("Join chat", 18135);
  120. player.getPA().sendFrame126("Talking in: Not in chat", 18139);
  121. player.getPA().sendFrame126("Owner: None", 18140);
  122. for (int index = 0; index < 100; index++) {
  123. player.getPA().sendFrame126("", 18144 + index);
  124. }
  125. }
  126.  
  127. /**
  128. * Sends a message to the clan.
  129. * @param player
  130. * @param message
  131. */
  132. public void sendChat(Client player, String message) {
  133. if (getRank(player.playerName) < whoCanTalk) {
  134. player.sendMessage("Only " + getRankTitle(whoCanTalk) + "s+ may talk in this chat.");
  135. return;
  136. }
  137. for (int index = 0; index < Config.MAX_PLAYERS; index++) {
  138. Client p = (Client) PlayerHandler.players[index];
  139. if (p != null) {
  140. if (activeMembers.contains(p.playerName)) {
  141. p.getPA().sendClan(player.playerName, message, getTitle(), player.playerRights);
  142. }
  143. }
  144. }
  145. }
  146.  
  147. /**
  148. * Sends a message to the clan.
  149. * @param player
  150. * @param message
  151. */
  152. public void sendMessage(String message) {
  153. for (int index = 0; index < Config.MAX_PLAYERS; index++) {
  154. Client p = (Client) PlayerHandler.players[index];
  155. if (p != null) {
  156. if (activeMembers.contains(p.playerName)) {
  157. p.sendMessage(message);
  158. }
  159. }
  160. }
  161. }
  162.  
  163. /**
  164. * Sets the rank for the specified name.
  165. * @param name
  166. * @param rank
  167. */
  168. public void setRank(String name, int rank) {
  169. if (rankedMembers.contains(name)) {
  170. ranks.set(rankedMembers.indexOf(name), rank);
  171. } else {
  172. rankedMembers.add(name);
  173. ranks.add(rank);
  174. }
  175. save();
  176. }
  177.  
  178. /**
  179. * Demotes the specified name.
  180. * @param name
  181. */
  182. public void demote(String name) {
  183. if (!rankedMembers.contains(name)) {
  184. return;
  185. }
  186. int index = rankedMembers.indexOf(name);
  187. rankedMembers.remove(index);
  188. ranks.remove(index);
  189. save();
  190. }
  191.  
  192. /**
  193. * Gets the rank of the specified name.
  194. * @param name
  195. * @return
  196. */
  197. public int getRank(String name) {
  198. name = Misc.formatPlayerName(name);
  199. if (rankedMembers.contains(name)) {
  200. return ranks.get(rankedMembers.indexOf(name));
  201. }
  202. if (isFounder(name)) {
  203. return Rank.OWNER;
  204. }
  205. if (PlayerSave.isFriend(getFounder(), name)) {
  206. return Rank.FRIEND;
  207. }
  208. return -1;
  209. }
  210.  
  211. /**
  212. * Can they kick?
  213. * @param name
  214. * @return
  215. */
  216. public boolean canKick(String name) {
  217. if (isFounder(name)) {
  218. return true;
  219. }
  220. if (getRank(name) >= whoCanKick) {
  221. return true;
  222. }
  223. return false;
  224. }
  225.  
  226. /**
  227. * Can they ban?
  228. * @param name
  229. * @return
  230. */
  231. public boolean canBan(String name) {
  232. if (isFounder(name)) {
  233. return true;
  234. }
  235. if (getRank(name) >= whoCanBan) {
  236. return true;
  237. }
  238. return false;
  239. }
  240.  
  241. /**
  242. * Returns whether or not the specified name is the founder.
  243. * @param name
  244. * @return
  245. */
  246. public boolean isFounder(String name) {
  247. if (getFounder().equalsIgnoreCase(name)) {
  248. return true;
  249. }
  250. return false;
  251. }
  252.  
  253. /**
  254. * Returns whether or not the specified name is a ranked user.
  255. * @param name
  256. * @return
  257. */
  258. public boolean isRanked(String name) {
  259. name = Misc.formatPlayerName(name);
  260. if (rankedMembers.contains(name)) {
  261. return true;
  262. }
  263. return false;
  264. }
  265.  
  266. /**
  267. * Returns whether or not the specified name is banned.
  268. * @param name
  269. * @return
  270. */
  271. public boolean isBanned(String name) {
  272. name = Misc.formatPlayerName(name);
  273. if (bannedMembers.contains(name)) {
  274. return true;
  275. }
  276. return false;
  277. }
  278.  
  279. /**
  280. * Kicks the name from the clan chat.
  281. * @param name
  282. */
  283. public void kickMember(String name) {
  284. if (!activeMembers.contains(name)) {
  285. return;
  286. }
  287. if (name.equalsIgnoreCase(getFounder())) {
  288. return;
  289. }
  290. removeMember(name);
  291. Client player = (Client) PlayerHandler.getPlayer(name);
  292. if (player != null) {
  293. player.sendMessage("You have been kicked from the clan chat.");
  294. }
  295. sendMessage(Misc.formatPlayerName(name) + " has been kicked from the clan chat.");
  296. }
  297.  
  298. /**
  299. * Bans the name from entering the clan chat.
  300. * @param name
  301. */
  302. public void banMember(String name) {
  303. name = Misc.formatPlayerName(name);
  304. if (bannedMembers.contains(name)) {
  305. return;
  306. }
  307. if (name.equalsIgnoreCase(getFounder())) {
  308. return;
  309. }
  310. if (isRanked(name)) {
  311. return;
  312. }
  313. removeMember(name);
  314. bannedMembers.add(name);
  315. save();
  316. Client player = (Client) PlayerHandler.getPlayer(name);
  317. if (player != null && player.clan == this) {
  318. player.sendMessage("You have been banned from the clan chat.");
  319. }
  320. sendMessage(Misc.formatPlayerName(name) + " has been banned from the clan chat.");
  321. }
  322.  
  323. /**
  324. * Unbans the name from the clan chat.
  325. * @param name
  326. */
  327. public void unbanMember(String name) {
  328. name = Misc.formatPlayerName(name);
  329. if (bannedMembers.contains(name)) {
  330. bannedMembers.remove(name);
  331. save();
  332. }
  333. }
  334.  
  335. /**
  336. * Saves the clan.
  337. */
  338. public void save() {
  339. Server.clanManager.save(this);
  340. updateMembers();
  341. }
  342.  
  343. /**
  344. * Deletes the clan.
  345. */
  346. public void delete() {
  347. for (String name : activeMembers) {
  348. removeMember(name);
  349. Client player = (Client) PlayerHandler.getPlayer(name);
  350. player.sendMessage("The clan you were in has been deleted.");
  351. }
  352. Server.clanManager.delete(this);
  353. }
  354.  
  355. /**
  356. * Creates a new clan for the specified player.
  357. * @param player
  358. */
  359. public Clan(Client player) {
  360. setTitle(player.playerName + "'s Clan");
  361. setFounder(player.playerName.toLowerCase());
  362. }
  363.  
  364. /**
  365. * Creates a new clan for the specified title and founder.
  366. * @param title
  367. * @param founder
  368. */
  369. public Clan(String title, String founder) {
  370. setTitle(title);
  371. setFounder(founder);
  372. }
  373.  
  374. /**
  375. * Gets the founder of the clan.
  376. * @return
  377. */
  378. public String getFounder() {
  379. return founder;
  380. }
  381.  
  382. /**
  383. * Sets the founder.
  384. * @param founder
  385. */
  386. public void setFounder(String founder) {
  387. this.founder = founder;
  388. }
  389.  
  390. /**
  391. * Gets the title of the clan.
  392. * @return
  393. */
  394. public String getTitle() {
  395. return title;
  396. }
  397.  
  398. /**
  399. * Sets the title.
  400. * @param title
  401. * @return
  402. */
  403. public void setTitle(String title) {
  404. this.title = title;
  405. }
  406.  
  407. /**
  408. * The title of the clan.
  409. */
  410. public String title;
  411.  
  412. /**
  413. * The founder of the clan.
  414. */
  415. public String founder;
  416.  
  417. /**
  418. * The active clan members.
  419. */
  420. public LinkedList<String> activeMembers = new LinkedList<String>();
  421.  
  422. /**
  423. * The banned members.
  424. */
  425. public LinkedList<String> bannedMembers = new LinkedList<String>();
  426.  
  427. /**
  428. * The ranked clan members.
  429. */
  430. public LinkedList<String> rankedMembers = new LinkedList<String>();
  431.  
  432. /**
  433. * The clan member ranks.
  434. */
  435. public LinkedList<Integer> ranks = new LinkedList<Integer>();
  436.  
  437. /**
  438. * The clan ranks.
  439. * @author Galkon
  440. *
  441. */
  442. public static class Rank {
  443. public final static int ANYONE = -1;
  444. public final static int FRIEND = 0;
  445. public final static int RECRUIT = 1;
  446. public final static int CORPORAL = 2;
  447. public final static int SERGEANT = 3;
  448. public final static int LIEUTENANT = 4;
  449. public final static int CAPTAIN = 5;
  450. public final static int GENERAL = 6;
  451. public final static int OWNER = 7;
  452. }
  453.  
  454. /**
  455. * Gets the rank title as a string.
  456. * @param rank
  457. * @return
  458. */
  459. public String getRankTitle(int rank) {
  460. switch (rank) {
  461. case -1:
  462. return "Anyone";
  463. case 0:
  464. return "Friend";
  465. case 1:
  466. return "Recruit";
  467. case 2:
  468. return "Corporal";
  469. case 3:
  470. return "Sergeant";
  471. case 4:
  472. return "Lieutenant";
  473. case 5:
  474. return "Captain";
  475. case 6:
  476. return "General";
  477. case 7:
  478. return "Only Me";
  479. }
  480. return "";
  481. }
  482.  
  483. /**
  484. * Sets the minimum rank that can join.
  485. * @param rank
  486. */
  487. public void setRankCanJoin(int rank) {
  488. whoCanJoin = rank;
  489. }
  490.  
  491. /**
  492. * Sets the minimum rank that can talk.
  493. * @param rank
  494. */
  495. public void setRankCanTalk(int rank) {
  496. whoCanTalk = rank;
  497. }
  498.  
  499. /**
  500. * Sets the minimum rank that can kick.
  501. * @param rank
  502. */
  503. public void setRankCanKick(int rank) {
  504. whoCanKick = rank;
  505. }
  506.  
  507. /**
  508. * Sets the minimum rank that can ban.
  509. * @param rank
  510. */
  511. public void setRankCanBan(int rank) {
  512. whoCanBan = rank;
  513. }
  514.  
  515. /**
  516. * The ranks privileges require (joining, talking, kicking, banning).
  517. */
  518. public int whoCanJoin = Rank.ANYONE;
  519. public int whoCanTalk = Rank.ANYONE;
  520. public int whoCanKick = Rank.GENERAL;
  521. public int whoCanBan = Rank.OWNER;
  522.  
  523. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement