vMinecraftUsers.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. private String[] ignoreList = new String[]{""}; //For datafiles
  13. private String[] aliasList = new String[]{""}; //For datafiles
  14. public void loadUsers(){
  15. File theDir = new File("vminecraftusers.txt");
  16. if(!theDir.exists()){
  17. properties = new PropertiesFile("vminecraftusers.txt");
  18. FileWriter writer = null;
  19. try {
  20. writer = new FileWriter(location);
  21. writer.write("#Storage place for user information\r\n");
  22. writer.write("#username:nickname:suffix:tag:ignore,list,names:alias,commands,here\r\n");
  23. } catch (Exception e) {
  24. log.log(Level.SEVERE, "Exception while creating " + location, e);
  25. } finally {
  26. try {
  27. if (writer != null) {
  28. writer.close();
  29. }
  30. } catch (IOException e) {
  31. log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
  32. }
  33. }
  34. } else {
  35. properties = new PropertiesFile("vminecraftusers.txt");
  36. try {
  37. properties.load();
  38. } catch (IOException e) {
  39. log.log(Level.SEVERE, "Exception while loading vminecraftusers.txt", e);
  40. }
  41. }
  42. }
  43. public boolean doesPlayerExist(String player) {
  44. try {
  45. Scanner scanner = new Scanner(new File(location));
  46. while (scanner.hasNextLine()) {
  47. String line = scanner.nextLine();
  48. if (line.startsWith("#") || line.equals("") || line.startsWith("")) {
  49. continue;
  50. }
  51. String[] split = line.split(":");
  52. if (!split[0].equalsIgnoreCase(player)) {
  53. continue;
  54. }
  55. return true;
  56. }
  57. scanner.close();
  58. } catch (Exception e) {
  59. log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
  60. }
  61. return false;
  62. }
  63. public static void addUser(Player player){
  64. FileWriter writer = null;
  65. String location = "vminecraftusers.txt";
  66. String playerName = player.getName();
  67. if (!vMinecraftUsers.getInstance().doesPlayerExist(playerName)){ //Check to see if the player exists before writing
  68. try {
  69. BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
  70. bw.append(player.getName()+":::::\r");
  71. bw.newLine();
  72. bw.close();
  73. } catch (Exception e) {
  74. log.log(Level.SEVERE, "Exception while trying to add user with BufferedWriter to " + location, e);
  75. } finally {
  76. try {
  77. if (writer != null) {
  78. writer.close();
  79. }
  80. } catch (IOException e) {
  81. log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
  82. }
  83. }
  84. }
  85. }
  86. public static vMinecraftUsers getInstance() {
  87. if (instance == null) {
  88. instance = new vMinecraftUsers();
  89. }
  90. return instance;
  91. }
  92. public static void getRow(){
  93. }
  94. }
  95. //=====================================================================
  96. //Class: PlayerList
  97. //Use: Encapsulates the player list
  98. //Author: cerevisiae
  99. //=====================================================================
  100. class PlayerList
  101. {
  102. ArrayList<PlayerProfile> players;
  103. //=====================================================================
  104. //Function: PlayerList
  105. //Input: Player player: The player to create a profile object for
  106. //Output: none
  107. //Use: Initializes the ArrayList
  108. //=====================================================================
  109. public PlayerList() { players = new ArrayList<PlayerProfile>(); }
  110. //=====================================================================
  111. //Function: addPlayer
  112. //Input: Player player: The player to add
  113. //Output: None
  114. //Use: Add a profile of the specified player
  115. //=====================================================================
  116. public void addPlayer(Player player)
  117. {
  118. players.add(new PlayerProfile(player));
  119. }
  120. //=====================================================================
  121. //Function: removePlayer
  122. //Input: Player player: The player to remove
  123. //Output: None
  124. //Use: Remove the profile of the specified player
  125. //=====================================================================
  126. public void removePlayer(Player player)
  127. {
  128. players.remove(findProfile(player));
  129. }
  130. //=====================================================================
  131. //Function: findProfile
  132. //Input: Player player: The player to find's profile
  133. //Output: PlayerProfile: The profile of the specified player
  134. //Use: Get the profile for the specified player
  135. //=====================================================================
  136. private PlayerProfile findProfile(Player player)
  137. {
  138. for(PlayerProfile ply : players)
  139. {
  140. if(ply.getPlayer().equals(player))
  141. return ply;
  142. }
  143. return null;
  144. }
  145. //=====================================================================
  146. //Class: PlayerProfile
  147. //Use: Encapsulates all commands for player options
  148. //Author: cerevisiae
  149. //=====================================================================
  150. class PlayerProfile
  151. {
  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. //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 (!split[0].equalsIgnoreCase(name)) {
  186. continue;
  187. }
  188. nickName = (split[1].split(",").toString());
  189. if (split.length >= 4) {
  190. tag = (split[3]);
  191. }
  192. if (split.length >= 5) {
  193. //ignoreList = (split[4]);
  194. }
  195. if (split.length >= 6) {
  196. //aliasList
  197. }
  198. }
  199. scanner.close();
  200. } catch (Exception e) {
  201. log.log(Level.SEVERE, "Exception while reading " + location + " (Are you sure you formatted it correctly?)", e);
  202. }
  203. }
  204. //=====================================================================
  205. //Function: getPlayer
  206. //Input: None
  207. //Output: Player: The player this profile belongs to
  208. //Use: Finds if the specified player is in the ignore list
  209. //=====================================================================
  210. public Player getPlayer(){return playerName;}
  211. //=====================================================================
  212. //Function: isIgnored
  213. //Input: Player player: Checks if a player is ignored
  214. //Output: boolean: If they're ignored
  215. //Use: Finds if the specified player is in the ignore list
  216. //=====================================================================
  217. public boolean isIgnored(Player player){return ignoreList.contains(player);}
  218. //=====================================================================
  219. //Function: addIgnore
  220. //Input: Player name: The player to ignore
  221. //Output: None
  222. //Use: Ignores a player.
  223. //=====================================================================
  224. public void addIgnore(Player name)
  225. {
  226. if(!ignoreList.contains(name))
  227. ignoreList.add(name);
  228. }
  229. //=====================================================================
  230. //Function: removeIgnore
  231. //Input: Player name: The player to ignore
  232. //Output: None
  233. //Use: Ignores a player.
  234. //=====================================================================
  235. public void removeIgnore(Player name)
  236. {
  237. if(ignoreList.contains(name))
  238. ignoreList.remove(name);
  239. }
  240. //=====================================================================
  241. //Function: addAlias
  242. //Input: String command: The command to try to call
  243. // String[] args: The arguments for the command
  244. //Output: None
  245. //Use: Adds a command
  246. //=====================================================================
  247. public void addAlias(String name, String callCommand)
  248. {
  249. aliasList.registerAlias(name, callCommand);
  250. }
  251. //=====================================================================
  252. //Function: addAlias
  253. //Input: String command: The command to try to call
  254. // String[] args: The arguments for the command
  255. //Output: None
  256. //Use: Adds a command
  257. //=====================================================================
  258. public void addAlias(String name, String callCommand, String[] args)
  259. {
  260. aliasList.registerAlias(name, callCommand, args);
  261. }
  262. //=====================================================================
  263. //Function: callAlias
  264. //Input: String command: The command to try to call
  265. // Player player: Checks if a player is ignored
  266. // String[] args: The arguments for the command
  267. //Output: int: Exit code
  268. //Use: Attempts to call a command
  269. //=====================================================================
  270. public int callAlias(String command, Player player, String[] args)
  271. {
  272. try
  273. {
  274. //Attemt to call the function
  275. return aliasList.call(command, player, args);
  276. }
  277. catch (Throwable e)
  278. {
  279. //The function wasn't found, returns fail
  280. return EXIT_FAIL;
  281. }
  282. }
  283. //=====================================================================
  284. //Function: setTag
  285. //Input: String newTag: The tag to set for the player
  286. //Output: None
  287. //Use: Sets a player tag
  288. //=====================================================================
  289. public void setTag(String newTag){ tag = newTag; }
  290. //=====================================================================
  291. //Function: getTag
  292. //Input: None
  293. //Output: String: The player tag
  294. //Use: Gets a player tag
  295. //=====================================================================
  296. public String getTag() { return tag; }
  297. }
  298. }