vMinecraftUsers.java 20 KB

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