vMinecraftUsers.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. import java.util.Scanner;
  6. public class vMinecraftUsers {
  7. private static volatile vMinecraftUsers instance;
  8. protected static final Logger log = Logger.getLogger("Minecraft");
  9. String file = "vminecraftusers.txt";
  10. private PropertiesFile properties;
  11. String location = "vminecraftusers.txt";
  12. public static PlayerList players = new PlayerList();
  13. public void loadUsers(){
  14. File theDir = new File("vminecraftusers.txt");
  15. if(!theDir.exists()){
  16. properties = new PropertiesFile("vminecraftusers.txt");
  17. FileWriter writer = null;
  18. try {
  19. writer = new FileWriter(location);
  20. writer.write("#Storage place for user information\r\n");
  21. writer.write("#username:nickname:suffix:tag:ignore,list,names:alias,commands,here\r\n");
  22. } catch (Exception e) {
  23. log.log(Level.SEVERE, "Exception while creating " + location, e);
  24. } finally {
  25. try {
  26. if (writer != null) {
  27. writer.close();
  28. }
  29. } catch (IOException e) {
  30. log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
  31. }
  32. }
  33. } else {
  34. properties = new PropertiesFile("vminecraftusers.txt");
  35. try {
  36. properties.load();
  37. } catch (IOException e) {
  38. log.log(Level.SEVERE, "Exception while loading vminecraftusers.txt", e);
  39. }
  40. }
  41. }
  42. public boolean doesPlayerExist(String player) {
  43. try {
  44. Scanner scanner = new Scanner(new File(location));
  45. while (scanner.hasNextLine()) {
  46. String line = scanner.nextLine();
  47. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  48. continue;
  49. }
  50. String[] split = line.split(":");
  51. if (!split[0].equalsIgnoreCase(player)) {
  52. continue;
  53. }
  54. return true;
  55. }
  56. scanner.close();
  57. } catch (Exception e) {
  58. log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
  59. }
  60. return false;
  61. }
  62. //=====================================================================
  63. //Function: addUser
  64. //Input: Player player: The player to create a profile for
  65. //Output: none
  66. //Use: Creates the player profile
  67. //=====================================================================
  68. public static void addUser(Player player){
  69. players.addPlayer(player);
  70. }
  71. //=====================================================================
  72. //Function: getProfile
  73. //Input: Player player: The player to find the profile for
  74. //Output: PlayerList.PlayerProfile: The profile
  75. //Use: Gets the player profile
  76. //=====================================================================
  77. public static PlayerList.PlayerProfile getProfile(Player player){
  78. return players.findProfile(player);
  79. }
  80. public static vMinecraftUsers getInstance() {
  81. if (instance == null) {
  82. instance = new vMinecraftUsers();
  83. }
  84. return instance;
  85. }
  86. public static void getRow(){
  87. }
  88. }
  89. //=====================================================================
  90. //Class: PlayerList
  91. //Use: Encapsulates the player list
  92. //Author: cerevisiae
  93. //=====================================================================
  94. class PlayerList
  95. {
  96. protected static final Logger log = Logger.getLogger("Minecraft");
  97. ArrayList<PlayerProfile> players;
  98. //=====================================================================
  99. //Function: PlayerList
  100. //Input: Player player: The player to create a profile object for
  101. //Output: none
  102. //Use: Initializes the ArrayList
  103. //=====================================================================
  104. public PlayerList() { players = new ArrayList<PlayerProfile>(); }
  105. //=====================================================================
  106. //Function: addPlayer
  107. //Input: Player player: The player to add
  108. //Output: None
  109. //Use: Add a profile of the specified player
  110. //=====================================================================
  111. public void addPlayer(Player player)
  112. {
  113. players.add(new PlayerProfile(player));
  114. }
  115. //=====================================================================
  116. //Function: removePlayer
  117. //Input: Player player: The player to remove
  118. //Output: None
  119. //Use: Remove the profile of the specified player
  120. //=====================================================================
  121. public void removePlayer(Player player)
  122. {
  123. players.remove(findProfile(player));
  124. }
  125. //=====================================================================
  126. //Function: findProfile
  127. //Input: Player player: The player to find's profile
  128. //Output: PlayerProfile: The profile of the specified player
  129. //Use: Get the profile for the specified player
  130. //=====================================================================
  131. public PlayerProfile findProfile(Player player)
  132. {
  133. for(PlayerProfile ply : players)
  134. {
  135. if(ply.isPlayer(player))
  136. return ply;
  137. }
  138. return null;
  139. }
  140. //=====================================================================
  141. //Class: PlayerProfile
  142. //Use: Encapsulates all commands for player options
  143. //Author: cerevisiae
  144. //=====================================================================
  145. class PlayerProfile
  146. {
  147. protected final Logger log = Logger.getLogger("Minecraft");
  148. private String playerName,
  149. lastMessage,
  150. nickName,
  151. tag,
  152. suffix,
  153. defaultColor;
  154. private ArrayList<String> ignoreList;
  155. private commandList aliasList;
  156. static final int EXIT_FAIL = 0,
  157. EXIT_SUCCESS = 1,
  158. EXIT_CONTINUE = 2;
  159. //=====================================================================
  160. //Function: PlayerProfile
  161. //Input: Player player: The player to create a profile object for
  162. //Output: none
  163. //Use: Loads settings for the player or creates them if they don't
  164. // exist.
  165. //=====================================================================
  166. public PlayerProfile(Player player)
  167. {
  168. //Declare things
  169. playerName = player.getName();
  170. nickName = new String();
  171. tag = new String();
  172. suffix = new String();
  173. ignoreList = new ArrayList<String>();
  174. aliasList = new commandList();
  175. String location = "vminecraftusers.txt";
  176. //Try to apply what we can
  177. try {
  178. Scanner scanner = new Scanner(new File(location));
  179. while (scanner.hasNextLine()) {
  180. String line = scanner.nextLine();
  181. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  182. continue;
  183. }
  184. String[] split = line.split(":");
  185. //If the player name is equal to the name in the list
  186. if (split.length > 0 && split[0].equalsIgnoreCase(player.getName())) {
  187. //Get the tag from the 1st split
  188. if (split.length >= 2)
  189. nickName = split[1];
  190. //Get the tag from the 2nd split
  191. if (split.length >= 3)
  192. suffix = split[2];
  193. //Get the tag from the 3rd split
  194. if (split.length >= 4)
  195. tag = (split[3]);
  196. //Add all the ignored people to the player's ignore list
  197. if (split.length >= 5) {
  198. for(String name : split[4].split(","))
  199. ignoreList.add(name);
  200. }
  201. //Get the alias list, from the 5th split
  202. if (split.length >= 6) {
  203. //Loop through all the aliases
  204. for(String alias : split[5].split(","))
  205. {
  206. //Break apart the two parts of the alias
  207. String[] parts = alias.split("@");
  208. if(parts.length > 1)
  209. {
  210. //Register the alias to the player's aliasList
  211. aliasList.registerAlias(parts[0], parts[2]);
  212. }
  213. }
  214. }
  215. break;
  216. }
  217. }
  218. scanner.close();
  219. } catch (Exception e) {
  220. log.log(Level.SEVERE, "Exception while reading "
  221. + location + " (Are you sure you formatted it correctly?)", e);
  222. }
  223. save();
  224. }
  225. //=====================================================================
  226. // Function: save
  227. // Input: none
  228. // Output: None
  229. // Use: Writes current values of PlayerProfile to disk
  230. // Call this function to save current values
  231. //=====================================================================
  232. public void save(){
  233. try {
  234. String location = "vminecraftusers.txt";
  235. BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
  236. Scanner scanner = new Scanner(new File(location));
  237. while (scanner.hasNextLine()) {
  238. String line = scanner.nextLine();
  239. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  240. continue;
  241. }
  242. String[] split = line.split(":");
  243. if (!split[0].equalsIgnoreCase(playerName)) {
  244. continue;
  245. }
  246. String output =playerName + ":" + nickName + ":" + suffix + ":" + tag + ":";
  247. for(String player : ignoreList)
  248. output += player + ",";
  249. output += ":";
  250. bw.write(output);
  251. }
  252. scanner.close();
  253. } catch (Exception e) {
  254. String location = "vminecraftusers.txt";
  255. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  256. }
  257. }
  258. //=====================================================================
  259. //Function: isPlayer
  260. //Input: None
  261. //Output: Player: The player this profile belongs to
  262. //Use: Finds if this profile belongs to a specified player
  263. //=====================================================================
  264. public boolean isPlayer(Player player)
  265. {
  266. return player.getName().equals(playerName);
  267. }
  268. //=====================================================================
  269. //Function: isIgnored
  270. //Input: Player player: Checks if a player is ignored
  271. //Output: boolean: If they're ignored
  272. //Use: Finds if the specified player is in the ignore list
  273. //=====================================================================
  274. public boolean isIgnored(Player player){
  275. return ignoreList.contains(player.getName());
  276. }
  277. //=====================================================================
  278. //Function: addIgnore
  279. //Input: Player name: The player to ignore
  280. //Output: boolean: If the player was successfully ignored
  281. //Use: Ignores a player.
  282. //=====================================================================
  283. public boolean addIgnore(Player name)
  284. {
  285. if(!ignoreList.contains(name))
  286. {
  287. ignoreList.add(name.getName());
  288. save();
  289. return true;
  290. }
  291. return false;
  292. }
  293. //=====================================================================
  294. //Function: removeIgnore
  295. //Input: Player name: The player to unignore
  296. //Output: boolean: If the player was successfully unignored
  297. //Use: Stops ignoring a player.
  298. //=====================================================================
  299. public boolean removeIgnore(Player name)
  300. {
  301. if(ignoreList.contains(name.getName()))
  302. {
  303. ignoreList.remove(name.getName());
  304. save();
  305. return true;
  306. }
  307. return false;
  308. }
  309. //=====================================================================
  310. //Function: removeIgnore
  311. //Input: Player name: The player to unignore
  312. //Output: boolean: If the player was successfully unignored
  313. //Use: Stops ignoring a player.
  314. //=====================================================================
  315. public String[] listIgnore()
  316. {
  317. return ignoreList.toArray(new String[ignoreList.size()]);
  318. }
  319. //=====================================================================
  320. //Function: addAlias
  321. //Input: String command: The command to try to call
  322. // String[] args: The arguments for the command
  323. //Output: None
  324. //Use: Adds a command
  325. //=====================================================================
  326. public void addAlias(String name, String callCommand)
  327. {
  328. aliasList.registerAlias(name, callCommand);
  329. save();
  330. }
  331. //=====================================================================
  332. //Function: callAlias
  333. //Input: String command: The command to try to call
  334. // Player player: Checks if a player is ignored
  335. // String[] args: The arguments for the command
  336. //Output: int: Exit code
  337. //Use: Attempts to call a command
  338. //=====================================================================
  339. public int callAlias(String command, Player player, String[] args)
  340. {
  341. try
  342. {
  343. //Attemt to call the function
  344. return aliasList.call(command, player, args);
  345. }
  346. catch (Throwable e)
  347. {
  348. //The function wasn't found, returns fail
  349. return EXIT_FAIL;
  350. }
  351. }
  352. //=====================================================================
  353. //Function: setTag
  354. //Input: String newTag: The tag to set for the player
  355. //Output: None
  356. //Use: Sets a player tag
  357. //=====================================================================
  358. public void setTag(String newTag){ tag = newTag; }
  359. //=====================================================================
  360. //Function: getTag
  361. //Input: None
  362. //Output: String: The player tag
  363. //Use: Gets a player tag
  364. //=====================================================================
  365. public String getTag() { return tag; }
  366. //=====================================================================
  367. //Function: setNick
  368. //Input: String newTag: The nickname to set for the player
  369. //Output: None
  370. //Use: Sets a player nickname
  371. //=====================================================================
  372. public void setNick(String newNick){ nickName = newNick; }
  373. //=====================================================================
  374. //Function: getNick
  375. //Input: None
  376. //Output: String: The player nickname
  377. //Use: Gets a player nickname
  378. //=====================================================================
  379. public String getNick() { return nickName; }
  380. //=====================================================================
  381. //Function: setSuffix
  382. //Input: String newTag: The suffix to set for the player
  383. //Output: None
  384. //Use: Sets a player suffix
  385. //=====================================================================
  386. public void setSuffix(String newSuffix){ suffix = newSuffix; }
  387. //=====================================================================
  388. //Function: getSuffix
  389. //Input: None
  390. //Output: String: The player suffix
  391. //Use: Gets a player suffix
  392. //=====================================================================
  393. public String getSuffix() { return suffix; }
  394. //=====================================================================
  395. //Function: setColor
  396. //Input: String newTag: The color to set for the player
  397. //Output: None
  398. //Use: Sets a player color
  399. //=====================================================================
  400. public void setColor(String newColor){ defaultColor = newColor; }
  401. //=====================================================================
  402. //Function: getColor
  403. //Input: None
  404. //Output: String: The player color
  405. //Use: Gets a player color
  406. //=====================================================================
  407. public String getColor() { return defaultColor; }
  408. //=====================================================================
  409. //Function: setMessage
  410. //Input: String newName: The name of the player they last messaged
  411. // or recieved a message from.
  412. //Output: None
  413. //Use: Sets a player tag
  414. //=====================================================================
  415. public void setMessage(Player newName){ lastMessage = newName.getName(); }
  416. //=====================================================================
  417. //Function: getMessage
  418. //Input: None
  419. //Output: String: The player name
  420. //Use: Gets the name of the player they last messaged or recieved
  421. // a message from.
  422. //=====================================================================
  423. public Player getMessage()
  424. {
  425. if(lastMessage != null)
  426. return etc.getServer().matchPlayer(lastMessage);
  427. return null;
  428. }
  429. }
  430. }