2
0

vUsers.java 21 KB

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