vMinecraftUsers.java 18 KB

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