vMinecraftUsers.java 11 KB

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