vMinecraftUsers.java 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. private double tpx,
  141. tpy,
  142. tpz;
  143. private boolean dead;
  144. char defaultColor;
  145. String location = "vminecraft.users";
  146. private ArrayList<String> ignoreList;
  147. private commandList aliasList;
  148. static final int EXIT_FAIL = 0,
  149. EXIT_SUCCESS = 1,
  150. EXIT_CONTINUE = 2;
  151. //=====================================================================
  152. //Function: PlayerProfile
  153. //Input: Player player: The player to create a profile object for
  154. //Output: none
  155. //Use: Loads settings for the player or creates them if they don't
  156. // exist.
  157. //=====================================================================
  158. public PlayerProfile(Player player)
  159. {
  160. //Declare things
  161. playerName = player.getName();
  162. tag = new String();
  163. nickName = new String();
  164. suffix = new String();
  165. defaultColor = 'f';
  166. ignoreList = new ArrayList<String>();
  167. aliasList = new commandList();
  168. dead = false;
  169. //Try to load the player and if they aren't found, append them
  170. if(!load())
  171. addPlayer();
  172. }
  173. public boolean load()
  174. {
  175. try {
  176. //Open the user file
  177. FileReader file = new FileReader(location);
  178. BufferedReader in = new BufferedReader(file);
  179. String line = "";
  180. while((line = in.readLine()) != null)
  181. {
  182. //Find if the line contains the player we want.
  183. String[] character = line.split(":");
  184. if(!character[0].equals(playerName)){continue;}
  185. //Get the tag
  186. if(character.length > 1)
  187. tag = character[1];
  188. //Get the nickname
  189. if(character.length > 2)
  190. nickName = character[2];
  191. //Get the suffix
  192. if(character.length > 3)
  193. suffix = character[3];
  194. //Get the color
  195. if(character.length > 4)
  196. defaultColor = character[4].charAt(0);
  197. //Ignore previously ignored players
  198. if(character.length > 5)
  199. {
  200. String[] ignores = character[5].split(",");
  201. if(ignores.length > 0)
  202. {
  203. for(String ignore : ignores)
  204. ignoreList.add(ignore);
  205. }
  206. }
  207. //Register the aliases
  208. if(character.length > 6)
  209. {
  210. String[] allAliases = character[6].split(",");
  211. if(allAliases.length > 0)
  212. {
  213. for(String singleAlias : allAliases)
  214. {
  215. String[] parts = singleAlias.split("@");
  216. if(parts.length > 1)
  217. {
  218. aliasList.registerAlias(parts[0], parts[1]);
  219. }
  220. }
  221. }
  222. }
  223. //XYZ TP Back value
  224. //Not sure if declaring a double this way will work or not
  225. if(character.length > 7)
  226. {
  227. double tpx = new Double(character[7]).doubleValue();
  228. }
  229. if(character.length > 8)
  230. {
  231. double tpy = new Double(character[8]).doubleValue();
  232. }
  233. if(character.length > 9)
  234. {
  235. double tpz = new Double(character[9]).doubleValue();
  236. }
  237. in.close();
  238. return true;
  239. }
  240. in.close();
  241. } catch (Exception e) {
  242. log.log(Level.SEVERE, "Exception while reading "
  243. + location + " (Are you sure you formatted it correctly?)", e);
  244. }
  245. return false;
  246. }
  247. //=====================================================================
  248. // Function: save
  249. // Input: none
  250. // Output: None
  251. // Use: Writes current values of PlayerProfile to disk
  252. // Call this function to save current values
  253. //=====================================================================
  254. public void save()
  255. {
  256. try {
  257. //Open the file
  258. FileReader file = new FileReader(location);
  259. BufferedReader in = new BufferedReader(file);
  260. StringBuilder writer = new StringBuilder();
  261. String line = "";
  262. //While not at the end of the file
  263. while((line = in.readLine()) != null)
  264. {
  265. //Read the line in and copy it to the output it's not the player
  266. //we want to edit
  267. if(!line.split(":")[0].equalsIgnoreCase(playerName))
  268. {
  269. writer.append(line).append("\r\n");
  270. //Otherwise write the new player information
  271. } else {
  272. writer.append(playerName + ":");
  273. writer.append(tag + ":");
  274. writer.append(nickName + ":");
  275. writer.append(suffix + ":");
  276. writer.append(defaultColor + ":");
  277. int i = 0;
  278. for(String ignore : ignoreList)
  279. {
  280. writer.append(ignore);
  281. if(i < ignoreList.size() - 1)
  282. writer.append(",");
  283. }
  284. writer.append(":");
  285. writer.append(aliasList.toString());
  286. writer.append(tpx + ":");
  287. writer.append(tpy + ":");
  288. writer.append(tpz + ":");
  289. writer.append("\r\n");
  290. }
  291. }
  292. in.close();
  293. //Write the new file
  294. FileWriter out = new FileWriter(location);
  295. out.write(writer.toString());
  296. out.close();
  297. } catch (Exception e) {
  298. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  299. }
  300. }
  301. public void addPlayer()
  302. {
  303. try {
  304. //Open the file to write the player
  305. FileWriter file = new FileWriter(location, true);
  306. BufferedWriter out = new BufferedWriter(file);
  307. //Add the player to the end
  308. out.append(playerName + ":");
  309. out.append(tag + ":");
  310. out.append(nickName + ":");
  311. out.append(suffix + ":");
  312. out.append(defaultColor + ":");
  313. int i = 0;
  314. for(String ignore : ignoreList)
  315. {
  316. out.append(ignore);
  317. if(i < ignoreList.size() - 1)
  318. out.append(",");
  319. }
  320. out.append(":");
  321. out.append(tpx + ":");
  322. out.append(tpy + ":");
  323. out.append(tpz + ":");
  324. out.append(aliasList.toString());
  325. out.newLine();
  326. out.close();
  327. } catch (Exception e) {
  328. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  329. }
  330. }
  331. //=====================================================================
  332. //Function: isPlayer
  333. //Input: None
  334. //Output: Player: The player this profile belongs to
  335. //Use: Finds if this profile belongs to a specified player
  336. //=====================================================================
  337. public boolean isPlayer(Player player)
  338. {
  339. return player.getName().equals(playerName);
  340. }
  341. //=====================================================================
  342. //Function: isIgnored
  343. //Input: Player player: Checks if a player is ignored
  344. //Output: boolean: If they're ignored
  345. //Use: Finds if the specified player is in the ignore list
  346. //=====================================================================
  347. public boolean isIgnored(Player player){
  348. return ignoreList.contains(player.getName());
  349. }
  350. //=====================================================================
  351. //Function: addIgnore
  352. //Input: Player name: The player to ignore
  353. //Output: boolean: If the player was successfully ignored
  354. //Use: Ignores a player.
  355. //=====================================================================
  356. public boolean addIgnore(Player name)
  357. {
  358. if(!ignoreList.contains(name))
  359. {
  360. ignoreList.add(name.getName());
  361. save();
  362. return true;
  363. }
  364. return false;
  365. }
  366. //=====================================================================
  367. //Function: removeIgnore
  368. //Input: Player name: The player to unignore
  369. //Output: boolean: If the player was successfully unignored
  370. //Use: Stops ignoring a player.
  371. //=====================================================================
  372. public boolean removeIgnore(Player name)
  373. {
  374. if(ignoreList.contains(name.getName()))
  375. {
  376. ignoreList.remove(name.getName());
  377. save();
  378. return true;
  379. }
  380. return false;
  381. }
  382. //=====================================================================
  383. //Function: removeIgnore
  384. //Input: Player name: The player to unignore
  385. //Output: boolean: If the player was successfully unignored
  386. //Use: Stops ignoring a player.
  387. //=====================================================================
  388. public String[] listIgnore()
  389. {
  390. return ignoreList.toArray(new String[ignoreList.size()]);
  391. }
  392. //=====================================================================
  393. //Function: addAlias
  394. //Input: String command: The command to try to call
  395. // String[] args: The arguments for the command
  396. //Output: None
  397. //Use: Adds a command
  398. //=====================================================================
  399. public void addAlias(String name, String callCommand)
  400. {
  401. aliasList.registerAlias(name, callCommand);
  402. save();
  403. }
  404. //=====================================================================
  405. //Function: callAlias
  406. //Input: String command: The command to try to call
  407. // Player player: Checks if a player is ignored
  408. // String[] args: The arguments for the command
  409. //Output: int: Exit code
  410. //Use: Attempts to call a command
  411. //=====================================================================
  412. public int callAlias(String command, Player player, String[] args)
  413. {
  414. try
  415. {
  416. //Attemt to call the function
  417. return aliasList.call(command, player, args);
  418. }
  419. catch (Throwable e)
  420. {
  421. //The function wasn't found, returns fail
  422. return EXIT_FAIL;
  423. }
  424. }
  425. //=====================================================================
  426. //Function: setTag
  427. //Input: String newTag: The tag to set for the player
  428. //Output: None
  429. //Use: Sets a player tag
  430. //=====================================================================
  431. public void setTag(String newTag)
  432. {
  433. tag = newTag;
  434. save();
  435. }
  436. //=====================================================================
  437. //Function: setTpback
  438. //Input: None
  439. //Output: None
  440. //Use: Sets a player's tpback xyz coordinates
  441. //=====================================================================
  442. public void setTpback(double x, double y, double z)
  443. {
  444. //Coordinates
  445. x = tpx;
  446. y = tpy;
  447. z = tpz;
  448. }
  449. //=====================================================================
  450. //Function: getTpbx
  451. //Input: None
  452. //Output: Double: The player's tpback x coords
  453. //Use: Gets the x value of tpback
  454. //=====================================================================
  455. public double getTpx() { return tpx; }
  456. //=====================================================================
  457. //Function: getTpy
  458. //Input: None
  459. //Output: Double: The player's tpback x coords
  460. //Use: Gets the x value of tpback
  461. //=====================================================================
  462. public double getTpy() { return tpy; }
  463. //=====================================================================
  464. //Function: getTpz
  465. //Input: None
  466. //Output: Double: The player's tpback x coords
  467. //Use: Gets the x value of tpback
  468. //=====================================================================
  469. public double getTpz() { return tpz; }
  470. //=====================================================================
  471. //Function: getTag
  472. //Input: None
  473. //Output: String: The player tag
  474. //Use: Gets a player tag
  475. //=====================================================================
  476. public String getTag() { return tag; }
  477. //=====================================================================
  478. //Function: setNick
  479. //Input: String newTag: The nickname to set for the player
  480. //Output: None
  481. //Use: Sets a player nickname
  482. //=====================================================================
  483. public void setNick(String newNick)
  484. {
  485. nickName = newNick;
  486. save();
  487. }
  488. //=====================================================================
  489. //Function: getNick
  490. //Input: None
  491. //Output: String: The player nickname
  492. //Use: Gets a player nickname
  493. //=====================================================================
  494. public String getNick() { return nickName; }
  495. //=====================================================================
  496. //Function: setSuffix
  497. //Input: String newTag: The suffix to set for the player
  498. //Output: None
  499. //Use: Sets a player suffix
  500. //=====================================================================
  501. public void setSuffix(String newSuffix)
  502. {
  503. suffix = newSuffix;
  504. save();
  505. }
  506. //=====================================================================
  507. //Function: getSuffix
  508. //Input: None
  509. //Output: String: The player suffix
  510. //Use: Gets a player suffix
  511. //=====================================================================
  512. public String getSuffix() { return suffix; }
  513. //=====================================================================
  514. //Function: setColor
  515. //Input: String newTag: The color to set for the player
  516. //Output: None
  517. //Use: Sets a player color
  518. //=====================================================================
  519. public void setColor(String newColor)
  520. {
  521. defaultColor = newColor.charAt(0);
  522. save();
  523. }
  524. //=====================================================================
  525. //Function: getColor
  526. //Input: None
  527. //Output: String: The player color
  528. //Use: Gets a player color
  529. //=====================================================================
  530. public String getColor() {return vMinecraftChat.colorChange(defaultColor);}
  531. //=====================================================================
  532. //Function: setMessage
  533. //Input: String newName: The name of the player they last messaged
  534. // or recieved a message from.
  535. //Output: None
  536. //Use: Sets a player tag
  537. //=====================================================================
  538. public void setMessage(Player newName){ lastMessage = newName.getName(); }
  539. //=====================================================================
  540. //Function: getMessage
  541. //Input: None
  542. //Output: String: The player name
  543. //Use: Gets the name of the player they last messaged or recieved
  544. // a message from.
  545. //=====================================================================
  546. public Player getMessage()
  547. {
  548. if(lastMessage != null)
  549. return etc.getServer().matchPlayer(lastMessage);
  550. return null;
  551. }
  552. //=====================================================================
  553. //Function: isDead
  554. //Input: None
  555. //Output: boolean: If the player is dead or not
  556. //Use: Gets the player is dead or not.
  557. //=====================================================================
  558. public boolean isDead() {return dead;}
  559. //=====================================================================
  560. //Function: isDead
  561. //Input: boolean isded: if the player is dead or not.
  562. //Output: None
  563. //Use: Sets if the player is dead or not
  564. //=====================================================================
  565. public void isDead(boolean isded){dead = isded;}
  566. }
  567. }