Advertisement
Guest User

ClanManager.Java

a guest
Apr 6th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.79 KB | None | 0 0
  1. import game.Client;
  2. import game.player.PlayerHandler;
  3.  
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.RandomAccessFile;
  7. import java.util.LinkedList;
  8.  
  9. public class ClanManager {
  10.  
  11. /**
  12. * Initializes the clan manager.
  13. */
  14. public ClanManager() {
  15. clans = new LinkedList<Clan>();
  16. }
  17.  
  18. /**
  19. * Gets the active clan count.
  20. * @return
  21. */
  22. public int getActiveClans() {
  23. return clans.size();
  24. }
  25.  
  26. /**
  27. * Gets the clan count.
  28. * @return
  29. */
  30. public int getTotalClans() {
  31. File directory = new File("/Data/clan/");
  32. return directory.listFiles().length;
  33. }
  34.  
  35. /**
  36. * Creates a new clan for the specified player.
  37. * @param player
  38. */
  39. public void create(Client player) {
  40. Clan clan = new Clan(player);
  41. clans.add(clan);
  42. clan.addMember(player);
  43. clan.save();
  44. player.getPA().setClanData();
  45. player.sendMessage("<col=FF0000>You may change your clan settings by clicking the 'Clan Setup' button.");
  46. }
  47.  
  48. /**
  49. * Gets the clan for the specified founder.
  50. * @param title
  51. * @return
  52. */
  53. public Clan getClan(String founder) {
  54. /*
  55. * Loop through the currently active clans and look for one
  56. * with the same founder as the one specified. If we find it,
  57. * return the clan instance.
  58. */
  59. for (int index = 0; index < clans.size(); index++) {
  60. if (clans.get(index).getFounder().equalsIgnoreCase(founder)) {
  61. return clans.get(index);
  62. }
  63. }
  64. /*
  65. * If the clan isn't found in the existing clans,
  66. * we will try to read the file for the specified founder.
  67. * If the read file isn't null, we'll add the read clan
  68. * to the list of clans. If the read data is null, we'll
  69. * return a null clan.
  70. */
  71. Clan read = read(founder);
  72. if (read != null) {
  73. clans.add(read);
  74. return read;
  75. } else {
  76. return null;
  77. }
  78. }
  79.  
  80. /**
  81. * Deletes the specified clan.
  82. * @param clan
  83. */
  84. public void delete(Clan clan) {
  85. /*
  86. * If the clan is null, stop the method.
  87. */
  88. if (clan == null) {
  89. return;
  90. }
  91. File file = new File("Data/clan/" + clan.getFounder() + ".cla");
  92. if (file.delete()) {
  93. Client player = (Client) PlayerHandler.getPlayer(clan.getFounder());
  94. if (player != null) {
  95. player.sendMessage("Your clan has been deleted.");
  96. }
  97. clans.remove(clan);
  98. }
  99. }
  100.  
  101. /**
  102. * Saves the clan data.
  103. */
  104. public void save(Clan clan) {
  105. /*
  106. * If the clan is null, stop the method.
  107. */
  108. if (clan == null) {
  109. return;
  110. }
  111. File file = new File("Data/clan/" + clan.getFounder() + ".cla");
  112. try {
  113. RandomAccessFile out = new RandomAccessFile(file, "rwd");
  114. //DataOutputStream out = new DataOutputStream(new FileOutputStream(file));
  115. /*
  116. * Write the clan information.
  117. */
  118. out.writeUTF(clan.getTitle());
  119. out.writeByte(clan.whoCanJoin);
  120. out.writeByte(clan.whoCanTalk);
  121. out.writeByte(clan.whoCanKick);
  122. out.writeByte(clan.whoCanBan);
  123. if (clan.rankedMembers != null && clan.rankedMembers.size() > 0) {
  124. out.writeShort(clan.rankedMembers.size());
  125. for (int index = 0; index < clan.rankedMembers.size(); index++) {
  126. out.writeUTF(clan.rankedMembers.get(index));
  127. out.writeShort(clan.ranks.get(index));
  128. }
  129. } else {
  130. out.writeShort(0);
  131. }
  132. /*
  133. * Close the file.
  134. */
  135. out.close();
  136. } catch (IOException e) {
  137. e.printStackTrace();
  138. }
  139. }
  140.  
  141. /**
  142. * Reads the clan file.
  143. * @param owner
  144. * @return
  145. */
  146. private Clan read(String owner) {
  147. File file = new File("Data/clan/" + owner + ".cla");
  148. if (!file.exists()) {
  149. /*
  150. * Clan doesn't exist.
  151. */
  152. return null;
  153. }
  154. try {
  155. RandomAccessFile in = new RandomAccessFile(file, "rwd");
  156. //DataInputStream in = new DataInputStream(new FileInputStream(file));
  157. /*
  158. * Create a new clan out of the read data.
  159. */
  160. Clan clan = new Clan(in.readUTF(), owner);
  161. clan.whoCanJoin = in.readByte();
  162. clan.whoCanTalk = in.readByte();
  163. clan.whoCanKick = in.readByte();
  164. clan.whoCanBan = in.readByte();
  165. int memberCount = in.readShort();
  166. if (memberCount != 0) {
  167. for (int index = 0; index < memberCount; index++) {
  168. clan.rankedMembers.add(in.readUTF());
  169. clan.ranks.add((int) in.readShort());
  170. }
  171. }
  172. in.close();
  173. /*
  174. * Data was read successfully.
  175. */
  176. return clan;
  177. } catch (IOException e) {
  178. /*
  179. * An error occurred while reading the data.
  180. */
  181. e.printStackTrace();
  182. return null;
  183. }
  184. }
  185.  
  186. /**
  187. * Does the clan for the specified owner exist?
  188. * @param owner
  189. * @return
  190. */
  191. public boolean clanExists(String owner) {
  192. File file = new File("Data/clan/" + owner + ".cla");
  193. return file.exists();
  194. }
  195.  
  196. /**
  197. * Gets the list of clans.
  198. * @return
  199. */
  200. public LinkedList<Clan> getClans() {
  201. return clans;
  202. }
  203.  
  204. /**
  205. * The clans.
  206. */
  207. public LinkedList<Clan> clans;
  208.  
  209. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement