vMinecraftUsers.java 17 KB

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