vMinecraftUsers.java 21 KB

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