PageRenderTime 64ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vChat/com/gmail/nossr50/vChat/vUsers.java

https://github.com/Rob4001/vminecraft-plugin
Java | 570 lines | 322 code | 54 blank | 194 comment | 35 complexity | 684a051bba65514726edd56cedd95c4d MD5 | raw file
  1. package com.gmail.nossr50.vChat;
  2. import java.io.*;
  3. import java.util.ArrayList;
  4. import java.util.Properties;
  5. import java.util.logging.Level;
  6. import java.util.logging.Logger;
  7. import org.bukkit.entity.*;
  8. public class vUsers {
  9. private static volatile vUsers instance;
  10. protected static final Logger log = Logger.getLogger("Minecraft");
  11. String location = "vChat.users";
  12. public static PlayerList players = new PlayerList();
  13. private Properties properties = new Properties();
  14. //To load
  15. public void load() throws IOException {
  16. properties.load(new FileInputStream(location));
  17. }
  18. //To save
  19. public void save() {
  20. try {
  21. properties.store(new FileOutputStream(location), null);
  22. }catch(IOException ex) {
  23. }
  24. }
  25. public void loadUsers(){
  26. File theDir = new File(location);
  27. if(!theDir.exists()){
  28. //properties = new PropertiesFile(location);
  29. FileWriter writer = null;
  30. try {
  31. writer = new FileWriter(location);
  32. writer.write("#Storage place for user information\r\n");
  33. writer.write("#username:nickname:suffix:tag:ignore,list,names:alias,commands,here\r\n");
  34. } catch (Exception e) {
  35. log.log(Level.SEVERE, "Exception while creating " + location, e);
  36. } finally {
  37. try {
  38. if (writer != null) {
  39. writer.close();
  40. }
  41. } catch (IOException e) {
  42. log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
  43. }
  44. }
  45. } else {
  46. //properties = new PropertiesFile(location);
  47. try {
  48. load();
  49. } catch (IOException e) {
  50. log.log(Level.SEVERE, "Exception while loading " + location, e);
  51. }
  52. }
  53. }
  54. //=====================================================================
  55. //Function: addUser
  56. //Input: Player player: The player to create a profile for
  57. //Output: none
  58. //Use: Loads the profile for the specified player
  59. //=====================================================================
  60. public static void addUser(Player player){
  61. players.addPlayer(player);
  62. }
  63. //=====================================================================
  64. //Function: removeUser
  65. //Input: Player player: The player to stop following
  66. //Output: none
  67. //Use: Creates the player profile
  68. //=====================================================================
  69. public static void removeUser(Player player){
  70. players.removePlayer(player);
  71. }
  72. //=====================================================================
  73. //Function: getProfile
  74. //Input: Player player: The player to find the profile for
  75. //Output: PlayerList.PlayerProfile: The profile
  76. //Use: Gets the player profile
  77. //=====================================================================
  78. public static PlayerList.PlayerProfile getProfile(Player player){
  79. return players.findProfile(player);
  80. }
  81. public static vUsers getInstance() {
  82. if (instance == null) {
  83. instance = new vUsers();
  84. }
  85. return instance;
  86. }
  87. public static void getRow(){
  88. }
  89. }
  90. class PlayerList
  91. {
  92. protected static final Logger log = Logger.getLogger("Minecraft");
  93. ArrayList<PlayerProfile> players;
  94. //=====================================================================
  95. //Function: PlayerList
  96. //Input: Player player: The player to create a profile object for
  97. //Output: none
  98. //Use: Initializes the ArrayList
  99. //=====================================================================
  100. public PlayerList() { players = new ArrayList<PlayerProfile>(); }
  101. //=====================================================================
  102. //Function: addPlayer
  103. //Input: Player player: The player to add
  104. //Output: None
  105. //Use: Add a profile of the specified player
  106. //=====================================================================
  107. public void addPlayer(Player player)
  108. {
  109. players.add(new PlayerProfile(player));
  110. }
  111. //=====================================================================
  112. //Function: removePlayer
  113. //Input: Player player: The player to remove
  114. //Output: None
  115. //Use: Remove the profile of the specified player
  116. //=====================================================================
  117. public void removePlayer(Player player)
  118. {
  119. players.remove(findProfile(player));
  120. }
  121. //=====================================================================
  122. //Function: findProfile
  123. //Input: Player player: The player to find's profile
  124. //Output: PlayerProfile: The profile of the specified player
  125. //Use: Get the profile for the specified player
  126. //=====================================================================
  127. public PlayerProfile findProfile(Player player)
  128. {
  129. for(PlayerProfile ply : players)
  130. {
  131. if(ply.isPlayer(player))
  132. return ply;
  133. }
  134. return null;
  135. }
  136. //=====================================================================
  137. //Class: PlayerProfile
  138. //Use: Encapsulates all commands for player options
  139. //Author: cerevisiae
  140. //=====================================================================
  141. class PlayerProfile
  142. {
  143. protected final Logger log = Logger.getLogger("Minecraft");
  144. private String playerName,
  145. lastMessage,
  146. nickName,
  147. tag,
  148. suffix,
  149. prefix,
  150. party;
  151. private boolean dead,
  152. silent;
  153. char defaultColor;
  154. String location = "vChat.users";
  155. private ArrayList<String> ignoreList;
  156. //private commandList aliasList;
  157. static final int EXIT_FAIL = 0,
  158. EXIT_SUCCESS = 1,
  159. EXIT_CONTINUE = 2;
  160. //=====================================================================
  161. //Function: PlayerProfile
  162. //Input: Player player: The player to create a profile object for
  163. //Output: none
  164. //Use: Loads settings for the player or creates them if they don't
  165. // exist.
  166. //=====================================================================
  167. public PlayerProfile(Player player)
  168. {
  169. //Declare things
  170. playerName = player.getName();
  171. tag = new String();
  172. nickName = new String();
  173. suffix = new String();
  174. prefix = new String();
  175. party = new String();
  176. party = null;
  177. defaultColor = 'f';
  178. ignoreList = new ArrayList<String>();
  179. dead = false;
  180. //Try to load the player and if they aren't found, append them
  181. if(!load())
  182. addPlayer();
  183. }
  184. public boolean load()
  185. {
  186. try {
  187. //Open the user file
  188. FileReader file = new FileReader(location);
  189. BufferedReader in = new BufferedReader(file);
  190. String line = "";
  191. while((line = in.readLine()) != null)
  192. {
  193. //Find if the line contains the player we want.
  194. String[] character = line.split(":");
  195. if(!character[0].equals(playerName)){continue;}
  196. //Get the tag
  197. if(character.length > 1)
  198. tag = character[1];
  199. //Get the nickname
  200. if(character.length > 2)
  201. nickName = character[2];
  202. //Get the suffix
  203. if(character.length > 3)
  204. suffix = character[3];
  205. //Get the color
  206. if(character.length > 4)
  207. defaultColor = character[4].charAt(0);
  208. //Ignore previously ignored players
  209. if(character.length > 5)
  210. {
  211. String[] ignores = character[5].split(",");
  212. if(ignores.length > 0)
  213. {
  214. for(String ignore : ignores)
  215. ignoreList.add(ignore);
  216. }
  217. }
  218. in.close();
  219. return true;
  220. }
  221. in.close();
  222. } catch (Exception e) {
  223. log.log(Level.SEVERE, "Exception while reading "
  224. + location + " (Are you sure you formatted it correctly?)", e);
  225. }
  226. return false;
  227. }
  228. //=====================================================================
  229. // Function: save
  230. // Input: none
  231. // Output: None
  232. // Use: Writes current values of PlayerProfile to disk
  233. // Call this function to save current values
  234. //=====================================================================
  235. public void save()
  236. {
  237. try {
  238. //Open the file
  239. FileReader file = new FileReader(location);
  240. BufferedReader in = new BufferedReader(file);
  241. StringBuilder writer = new StringBuilder();
  242. String line = "";
  243. //While not at the end of the file
  244. while((line = in.readLine()) != null)
  245. {
  246. //Read the line in and copy it to the output it's not the player
  247. //we want to edit
  248. if(!line.split(":")[0].equalsIgnoreCase(playerName))
  249. {
  250. writer.append(line).append("\r\n");
  251. //Otherwise write the new player information
  252. } else {
  253. writer.append(playerName + ":");
  254. writer.append(tag + ":");
  255. writer.append(nickName + ":");
  256. writer.append(suffix + ":");
  257. writer.append(defaultColor + ":");
  258. writer.append(prefix + ":");
  259. int i = 0;
  260. for(String ignore : ignoreList)
  261. {
  262. writer.append(ignore);
  263. if(i < ignoreList.size() - 1)
  264. writer.append(",");
  265. }
  266. writer.append("\r\n");
  267. }
  268. }
  269. in.close();
  270. //Write the new file
  271. FileWriter out = new FileWriter(location);
  272. out.write(writer.toString());
  273. out.close();
  274. } catch (Exception e) {
  275. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  276. }
  277. }
  278. public void addPlayer()
  279. {
  280. try {
  281. //Open the file to write the player
  282. FileWriter file = new FileWriter(location, true);
  283. BufferedWriter out = new BufferedWriter(file);
  284. //Add the player to the end
  285. out.append(playerName + ":");
  286. out.append("" + ":");
  287. out.append(nickName + ":");
  288. out.append(suffix + ":");
  289. out.append("f" + ":");
  290. out.append("f" + ":");
  291. int i = 0;
  292. for(String ignore : ignoreList)
  293. {
  294. out.append(ignore);
  295. if(i < ignoreList.size() - 1)
  296. out.append(",");
  297. }
  298. out.newLine();
  299. out.close();
  300. } catch (Exception e) {
  301. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  302. }
  303. }
  304. //=====================================================================
  305. //Function: isPlayer
  306. //Input: None
  307. //Output: Player: The player this profile belongs to
  308. //Use: Finds if this profile belongs to a specified player
  309. //=====================================================================
  310. public boolean isPlayer(Player player)
  311. {
  312. return player.getName().equals(playerName);
  313. }
  314. //=====================================================================
  315. //Function: isIgnored
  316. //Input: Player player: Checks if a player is ignored
  317. //Output: boolean: If they're ignored
  318. //Use: Finds if the specified player is in the ignore list
  319. //=====================================================================
  320. public boolean isIgnored(Player player){
  321. return ignoreList.contains(player.getName());
  322. }
  323. //=====================================================================
  324. //Function: addIgnore
  325. //Input: Player name: The player to ignore
  326. //Output: boolean: If the player was successfully ignored
  327. //Use: Ignores a player.
  328. //=====================================================================
  329. public boolean addIgnore(Player name)
  330. {
  331. if(!ignoreList.contains(name))
  332. {
  333. ignoreList.add(name.getName());
  334. save();
  335. return true;
  336. }
  337. return false;
  338. }
  339. //=====================================================================
  340. //Function: removeIgnore
  341. //Input: Player name: The player to unignore
  342. //Output: boolean: If the player was successfully unignored
  343. //Use: Stops ignoring a player.
  344. //=====================================================================
  345. public boolean removeIgnore(Player name)
  346. {
  347. if(ignoreList.contains(name.getName()))
  348. {
  349. ignoreList.remove(name.getName());
  350. save();
  351. return true;
  352. }
  353. return false;
  354. }
  355. //=====================================================================
  356. //Function: removeIgnore
  357. //Input: Player name: The player to unignore
  358. //Output: boolean: If the player was successfully unignored
  359. //Use: Stops ignoring a player.
  360. //=====================================================================
  361. public String[] listIgnore()
  362. {
  363. return ignoreList.toArray(new String[ignoreList.size()]);
  364. }
  365. //=====================================================================
  366. //Function: setTag
  367. //Input: String newTag: The tag to set for the player
  368. //Output: None
  369. //Use: Sets a player tag
  370. //=====================================================================
  371. public void setTag(String newTag)
  372. {
  373. tag = newTag;
  374. save();
  375. }
  376. //=====================================================================
  377. //Function: getTag
  378. //Input: None
  379. //Output: String: The player tag
  380. //Use: Gets a player tag
  381. //=====================================================================
  382. public String getTag() { return tag; }
  383. //=====================================================================
  384. //Function: setNick
  385. //Input: String newTag: The nickname to set for the player
  386. //Output: None
  387. //Use: Sets a player nickname
  388. //=====================================================================
  389. public void setNick(String newNick)
  390. {
  391. nickName = newNick;
  392. save();
  393. }
  394. public void setSilent(){
  395. silent = true;
  396. }
  397. public void disableSilent(){
  398. silent = false;
  399. }
  400. public boolean isSilent(){
  401. return silent;
  402. }
  403. //Store the player's party
  404. public void setParty(String newParty)
  405. {
  406. party = newParty;
  407. save();
  408. }
  409. //Retrieve the player's party
  410. public String getParty() {return party;}
  411. //Remove party
  412. public void removeParty() {
  413. party = null;
  414. save();
  415. }
  416. //Retrieve whether or not the player is in a party
  417. public boolean inParty() {
  418. if(party != null){
  419. return true;
  420. } else {
  421. return false;
  422. }
  423. }
  424. //=====================================================================
  425. //Function: getNick
  426. //Input: None
  427. //Output: String: The player nickname
  428. //Use: Gets a player nickname
  429. //=====================================================================
  430. public String getNick() { return nickName; }
  431. //=====================================================================
  432. //Function: setSuffix
  433. //Input: String newTag: The suffix to set for the player
  434. //Output: None
  435. //Use: Sets a player suffix
  436. //=====================================================================
  437. public void setSuffix(String newSuffix)
  438. {
  439. suffix = newSuffix;
  440. save();
  441. }
  442. //=====================================================================
  443. //Function: getSuffix
  444. //Input: None
  445. //Output: String: The player suffix
  446. //Use: Gets a player suffix
  447. //=====================================================================
  448. public String getSuffix() { return suffix; }
  449. public void setPrefix(String newPrefix)
  450. {
  451. prefix = newPrefix;
  452. save();
  453. }
  454. public String getPrefix() {
  455. if(prefix != null && !prefix.equals("") && !prefix.equals("null")){
  456. return prefix;
  457. } else {
  458. return "f";
  459. }
  460. }
  461. //=====================================================================
  462. //Function: setColor
  463. //Input: String newTag: The color to set for the player
  464. //Output: None
  465. //Use: Sets a player color
  466. //=====================================================================
  467. public void setColor(String newColor)
  468. {
  469. defaultColor = newColor.charAt(0);
  470. save();
  471. }
  472. //=====================================================================
  473. //Function: getColor
  474. //Input: None
  475. //Output: String: The player color
  476. //Use: Gets a player color
  477. //=====================================================================
  478. public String getColor() {return vPlayerListener.colorChange(defaultColor);}
  479. //=====================================================================
  480. //Function: setMessage
  481. //Input: String newName: The name of the player they last messaged
  482. // or recieved a message from.
  483. //Output: None
  484. //Use: Sets a player tag
  485. //=====================================================================
  486. public void setMessage(Player newName){ lastMessage = newName.getName(); }
  487. //=====================================================================
  488. //Function: getMessage
  489. //Input: None
  490. //Output: String: The player name
  491. //Use: Gets the name of the player they last messaged or recieved
  492. // a message from.
  493. //=====================================================================
  494. public Player getMessage()
  495. {
  496. //if(lastMessage != null)
  497. //We need the bukkit equivalent of this
  498. //return matchPlayer(lastMessage);
  499. return null;
  500. }
  501. //=====================================================================
  502. //Function: isDead
  503. //Input: None
  504. //Output: boolean: If the player is dead or not
  505. //Use: Gets the player is dead or not.
  506. //=====================================================================
  507. public boolean isDead() {return dead;}
  508. //=====================================================================
  509. //Function: isDead
  510. //Input: boolean isded: if the player is dead or not.
  511. //Output: None
  512. //Use: Sets if the player is dead or not
  513. //=====================================================================
  514. public void isDead(boolean isded){dead = isded;}
  515. }
  516. }