vMinecraftUsers.java 16 KB

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