vMinecraftUsers.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. ArrayList<PlayerList> players = new ArrayList<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. public static void addUser(Player player){
  63. FileWriter writer = null;
  64. String location = "vminecraftusers.txt";
  65. String playerName = player.getName();
  66. if (!vMinecraftUsers.getInstance().doesPlayerExist(playerName)){ //Check to see if the player exists before writing
  67. try {
  68. BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
  69. bw.append(player.getName()+":::::\r");
  70. bw.newLine();
  71. bw.close();
  72. } catch (Exception e) {
  73. log.log(Level.SEVERE, "Exception while trying to add user with BufferedWriter to " + location, e);
  74. } finally {
  75. try {
  76. if (writer != null) {
  77. writer.close();
  78. }
  79. } catch (IOException e) {
  80. log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
  81. }
  82. }
  83. }
  84. }
  85. public static vMinecraftUsers getInstance() {
  86. if (instance == null) {
  87. instance = new vMinecraftUsers();
  88. }
  89. return instance;
  90. }
  91. public static void getRow(){
  92. }
  93. }
  94. //=====================================================================
  95. //Class: PlayerList
  96. //Use: Encapsulates the player list
  97. //Author: cerevisiae
  98. //=====================================================================
  99. class PlayerList
  100. {
  101. ArrayList<PlayerProfile> players;
  102. //=====================================================================
  103. //Function: PlayerList
  104. //Input: Player player: The player to create a profile object for
  105. //Output: none
  106. //Use: Initializes the ArrayList
  107. //=====================================================================
  108. public PlayerList() { players = new ArrayList<PlayerProfile>(); }
  109. //=====================================================================
  110. //Function: addPlayer
  111. //Input: Player player: The player to add
  112. //Output: None
  113. //Use: Add a profile of the specified player
  114. //=====================================================================
  115. public void addPlayer(Player player)
  116. {
  117. players.add(new PlayerProfile(player));
  118. }
  119. //=====================================================================
  120. //Function: removePlayer
  121. //Input: Player player: The player to remove
  122. //Output: None
  123. //Use: Remove the profile of the specified player
  124. //=====================================================================
  125. public void removePlayer(Player player)
  126. {
  127. players.remove(findProfile(player));
  128. }
  129. //=====================================================================
  130. //Function: findProfile
  131. //Input: Player player: The player to find's profile
  132. //Output: PlayerProfile: The profile of the specified player
  133. //Use: Get the profile for the specified player
  134. //=====================================================================
  135. private PlayerProfile findProfile(Player player)
  136. {
  137. for(PlayerProfile ply : players)
  138. {
  139. if(ply.getPlayer().equals(player))
  140. return ply;
  141. }
  142. return null;
  143. }
  144. //=====================================================================
  145. //Class: PlayerProfile
  146. //Use: Encapsulates all commands for player options
  147. //Author: cerevisiae
  148. //=====================================================================
  149. class PlayerProfile
  150. {
  151. protected final Logger log = Logger.getLogger("Minecraft");
  152. private Player playerName;
  153. private String nickName;
  154. private String tag;
  155. private String suffix;
  156. private ArrayList<Player> ignoreList;
  157. private commandList aliasList;
  158. static final int EXIT_FAIL = 0,
  159. EXIT_SUCCESS = 1,
  160. EXIT_CONTINUE = 2;
  161. //=====================================================================
  162. //Function: PlayerProfile
  163. //Input: Player player: The player to create a profile object for
  164. //Output: none
  165. //Use: Loads settings for the player or creates them if they don't
  166. // exist.
  167. //=====================================================================
  168. public PlayerProfile(Player player)
  169. {
  170. //Declare things
  171. ignoreList = new ArrayList<Player>();
  172. aliasList = new commandList();
  173. nickName = new String();
  174. tag = new String();
  175. suffix = new String();
  176. String location = "vminecraftusers.txt";
  177. //Try to apply what we can
  178. try {
  179. Scanner scanner = new Scanner(new File(location));
  180. while (scanner.hasNextLine()) {
  181. String line = scanner.nextLine();
  182. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  183. continue;
  184. }
  185. String[] split = line.split(":");
  186. if (!split[0].equalsIgnoreCase(player.getName())) {
  187. continue;
  188. }
  189. nickName = (split[1].split(",").toString());
  190. if (split.length >= 4) {
  191. tag = (split[3]);
  192. }
  193. //Add all the ignored people to the player's ignore list
  194. if (split.length >= 5) {
  195. for(String name : split[4].split(","))
  196. ignoreList.add(etc.getServer().getPlayer(name));
  197. }
  198. if (split.length >= 6) {
  199. //Loop through all the aliases
  200. for(String alias : split[5].split(","))
  201. {
  202. //Break apart the two parts of the alias
  203. String[] parts = alias.split("@");
  204. if(parts.length > 1)
  205. {
  206. //Get the arguments for the alias if there are any
  207. String[] command = parts[1].split(" ");
  208. String[] args = null;
  209. if(command.length > 1)
  210. System.arraycopy(command, 1, args, 0, command.length - 2);
  211. //Register the alias to the player's aliasList
  212. aliasList.registerAlias(parts[0], command[0], args);
  213. }
  214. }
  215. }
  216. }
  217. scanner.close();
  218. } catch (Exception e) {
  219. log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
  220. }
  221. }
  222. //=====================================================================
  223. // Function: save
  224. // Input: none
  225. // Output: None
  226. // Use: Writes current values of PlayerProfile to disk
  227. // Call this function to save current values
  228. //=====================================================================
  229. public void save(){
  230. try {
  231. String location = "vminecraftusers.txt";
  232. BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
  233. Scanner scanner = new Scanner(new File(location));
  234. while (scanner.hasNextLine()) {
  235. String line = scanner.nextLine();
  236. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  237. continue;
  238. }
  239. String[] split = line.split(":");
  240. if (!split[0].equalsIgnoreCase(playerName.toString())) {
  241. continue;
  242. }
  243. bw.write(playerName + ":" + nickName + ":" + suffix + ":" + tag + ":" + ignoreList + ":" + aliasList);
  244. }
  245. scanner.close();
  246. } catch (Exception e) {
  247. String location = "vminecraftusers.txt";
  248. log.log(Level.SEVERE, "Exception while writing to " + location + " (Are you sure you formatted it correctly?)", e);
  249. }
  250. }
  251. //=====================================================================
  252. //Function: getPlayer
  253. //Input: None
  254. //Output: Player: The player this profile belongs to
  255. //Use: Finds if the specified player is in the ignore list
  256. //=====================================================================
  257. public Player getPlayer(){return playerName;}
  258. //=====================================================================
  259. //Function: isIgnored
  260. //Input: Player player: Checks if a player is ignored
  261. //Output: boolean: If they're ignored
  262. //Use: Finds if the specified player is in the ignore list
  263. //=====================================================================
  264. public boolean isIgnored(Player player){return ignoreList.contains(player);}
  265. //=====================================================================
  266. //Function: addIgnore
  267. //Input: Player name: The player to ignore
  268. //Output: None
  269. //Use: Ignores a player.
  270. //=====================================================================
  271. public void addIgnore(Player name)
  272. {
  273. if(!ignoreList.contains(name))
  274. ignoreList.add(name);
  275. }
  276. //=====================================================================
  277. //Function: removeIgnore
  278. //Input: Player name: The player to ignore
  279. //Output: None
  280. //Use: Ignores a player.
  281. //=====================================================================
  282. public void removeIgnore(Player name)
  283. {
  284. if(ignoreList.contains(name))
  285. ignoreList.remove(name);
  286. }
  287. //=====================================================================
  288. //Function: addAlias
  289. //Input: String command: The command to try to call
  290. // String[] args: The arguments for the command
  291. //Output: None
  292. //Use: Adds a command
  293. //=====================================================================
  294. public void addAlias(String name, String callCommand)
  295. {
  296. aliasList.registerAlias(name, callCommand);
  297. }
  298. //=====================================================================
  299. //Function: addAlias
  300. //Input: String command: The command to try to call
  301. // String[] args: The arguments for the command
  302. //Output: None
  303. //Use: Adds a command
  304. //=====================================================================
  305. public void addAlias(String name, String callCommand, String[] args)
  306. {
  307. aliasList.registerAlias(name, callCommand, args);
  308. }
  309. //=====================================================================
  310. //Function: callAlias
  311. //Input: String command: The command to try to call
  312. // Player player: Checks if a player is ignored
  313. // String[] args: The arguments for the command
  314. //Output: int: Exit code
  315. //Use: Attempts to call a command
  316. //=====================================================================
  317. public int callAlias(String command, Player player, String[] args)
  318. {
  319. try
  320. {
  321. //Attemt to call the function
  322. return aliasList.call(command, player, args);
  323. }
  324. catch (Throwable e)
  325. {
  326. //The function wasn't found, returns fail
  327. return EXIT_FAIL;
  328. }
  329. }
  330. //=====================================================================
  331. //Function: setTag
  332. //Input: String newTag: The tag to set for the player
  333. //Output: None
  334. //Use: Sets a player tag
  335. //=====================================================================
  336. public void setTag(String newTag){ tag = newTag; }
  337. //=====================================================================
  338. //Function: getTag
  339. //Input: None
  340. //Output: String: The player tag
  341. //Use: Gets a player tag
  342. //=====================================================================
  343. public String getTag() { return tag; }
  344. }
  345. }