vMinecraftUsers.java 14 KB

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