vMinecraftUsers.java 18 KB

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