vMinecraftUsers.java 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.util.logging.Level;
  4. import java.util.logging.Logger;
  5. public class vMinecraftUsers {
  6. private static volatile vMinecraftUsers instance;
  7. protected static final Logger log = Logger.getLogger("Minecraft");
  8. String file = "vminecraftusers.txt";
  9. private PropertiesFile properties;
  10. String location = "vminecraftusers.txt";
  11. public void loadUsers(){
  12. File theDir = new File("vminecraftusers.txt");
  13. if(!theDir.exists()){
  14. properties = new PropertiesFile("vminecraftusers.txt");
  15. FileWriter writer = null;
  16. try {
  17. writer = new FileWriter(location);
  18. writer.write("#Storage place for user information\r\n");
  19. writer.write("#username:nickname:suffix:ignore,list,names:alias,commands,here\r\n");
  20. } catch (Exception e) {
  21. log.log(Level.SEVERE, "Exception while creating " + location, e);
  22. } finally {
  23. try {
  24. if (writer != null) {
  25. writer.close();
  26. }
  27. } catch (IOException e) {
  28. log.log(Level.SEVERE, "Exception while closing writer for " + location, e);
  29. }
  30. }
  31. } else {
  32. properties = new PropertiesFile("vminecraftusers.txt");
  33. try {
  34. properties.load();
  35. } catch (IOException e) {
  36. log.log(Level.SEVERE, "Exception while loading vminecraftusers.txt", e);
  37. }
  38. }
  39. }
  40. public static void addUser(Player player){
  41. FileWriter writer = null;
  42. String location = "vminecraftusers.txt";
  43. try {
  44. BufferedWriter bw = new BufferedWriter(new FileWriter(location, true));
  45. bw.append(player.getName()+"::::\r");
  46. bw.newLine();
  47. bw.close();
  48. } catch (Exception e) {
  49. log.log(Level.SEVERE, "Exception while trying to add user with BufferedWriter to " + location, e);
  50. } finally {
  51. try {
  52. if (writer != null) {
  53. writer.close();
  54. }
  55. } catch (IOException e) {
  56. log.log(Level.SEVERE, "Exception while closing BufferedWriter to " + location, e);
  57. }
  58. }
  59. }
  60. public static vMinecraftUsers getInstance() {
  61. if (instance == null) {
  62. instance = new vMinecraftUsers();
  63. }
  64. return instance;
  65. }
  66. public static void getRow(){
  67. }
  68. }
  69. //=====================================================================
  70. //Class: PlayerList
  71. //Use: Encapsulates the player list
  72. //Author: cerevisiae
  73. //=====================================================================
  74. class PlayerList
  75. {
  76. ArrayList<PlayerProfile> players;
  77. //=====================================================================
  78. //Function: PlayerList
  79. //Input: Player player: The player to create a profile object for
  80. //Output: none
  81. //Use: Initializes the ArrayList
  82. //=====================================================================
  83. public PlayerList() { players = new ArrayList<PlayerProfile>(); }
  84. //=====================================================================
  85. //Function: addPlayer
  86. //Input: Player player: The player to add
  87. //Output: None
  88. //Use: Add a profile of the specified player
  89. //=====================================================================
  90. public void addPlayer(Player player)
  91. {
  92. players.add(new PlayerProfile(player));
  93. }
  94. //=====================================================================
  95. //Function: removePlayer
  96. //Input: Player player: The player to remove
  97. //Output: None
  98. //Use: Remove the profile of the specified player
  99. //=====================================================================
  100. public void removePlayer(Player player)
  101. {
  102. players.remove(findProfile(player));
  103. }
  104. //=====================================================================
  105. //Function: findProfile
  106. //Input: Player player: The player to find's profile
  107. //Output: PlayerProfile: The profile of the specified player
  108. //Use: Get the profile for the specified player
  109. //=====================================================================
  110. private PlayerProfile findProfile(Player player)
  111. {
  112. for(PlayerProfile ply : players)
  113. {
  114. if(ply.getPlayer().equals(player))
  115. return ply;
  116. }
  117. return null;
  118. }
  119. //=====================================================================
  120. //Class: PlayerProfile
  121. //Use: Encapsulates all commands for player options
  122. //Author: cerevisiae
  123. //=====================================================================
  124. class PlayerProfile
  125. {
  126. private Player playerName;
  127. private String nickName;
  128. private String tag;
  129. private ArrayList<Player> ignoreList;
  130. private commandList aliasList;
  131. static final int EXIT_FAIL = 0,
  132. EXIT_SUCCESS = 1,
  133. EXIT_CONTINUE = 2;
  134. //=====================================================================
  135. //Function: PlayerProfile
  136. //Input: Player player: The player to create a profile object for
  137. //Output: none
  138. //Use: Loads settings for the player or creates them if they don't
  139. // exist.
  140. //=====================================================================
  141. public PlayerProfile(Player player)
  142. {
  143. ignoreList = new ArrayList<Player>();
  144. aliasList = new commandList();
  145. }
  146. //=====================================================================
  147. //Function: getPlayer
  148. //Input: None
  149. //Output: Player: The player this profile belongs to
  150. //Use: Finds if the specified player is in the ignore list
  151. //=====================================================================
  152. public Player getPlayer(){return playerName;}
  153. //=====================================================================
  154. //Function: isIgnored
  155. //Input: Player player: Checks if a player is ignored
  156. //Output: boolean: If they're ignored
  157. //Use: Finds if the specified player is in the ignore list
  158. //=====================================================================
  159. public boolean isIgnored(Player player){return ignoreList.contains(player);}
  160. //=====================================================================
  161. //Function: addIgnore
  162. //Input: Player name: The player to ignore
  163. //Output: None
  164. //Use: Ignores a player.
  165. //=====================================================================
  166. public void addIgnore(Player name)
  167. {
  168. if(!ignoreList.contains(name))
  169. ignoreList.add(name);
  170. }
  171. //=====================================================================
  172. //Function: removeIgnore
  173. //Input: Player name: The player to ignore
  174. //Output: None
  175. //Use: Ignores a player.
  176. //=====================================================================
  177. public void removeIgnore(Player name)
  178. {
  179. if(ignoreList.contains(name))
  180. ignoreList.remove(name);
  181. }
  182. //=====================================================================
  183. //Function: addAlias
  184. //Input: String command: The command to try to call
  185. // String[] args: The arguments for the command
  186. //Output: None
  187. //Use: Adds a command
  188. //=====================================================================
  189. public void addAlias(String name, String callCommand)
  190. {
  191. aliasList.registerAlias(name, callCommand);
  192. }
  193. //=====================================================================
  194. //Function: addAlias
  195. //Input: String command: The command to try to call
  196. // String[] args: The arguments for the command
  197. //Output: None
  198. //Use: Adds a command
  199. //=====================================================================
  200. public void addAlias(String name, String callCommand, String[] args)
  201. {
  202. aliasList.registerAlias(name, callCommand, args);
  203. }
  204. //=====================================================================
  205. //Function: callAlias
  206. //Input: String command: The command to try to call
  207. // Player player: Checks if a player is ignored
  208. // String[] args: The arguments for the command
  209. //Output: int: Exit code
  210. //Use: Attempts to call a command
  211. //=====================================================================
  212. public int callAlias(String command, Player player, String[] args)
  213. {
  214. try
  215. {
  216. //Attemt to call the function
  217. return aliasList.call(command, player, args);
  218. }
  219. catch (Throwable e)
  220. {
  221. //The function wasn't found, returns fail
  222. return EXIT_FAIL;
  223. }
  224. }
  225. //=====================================================================
  226. //Function: setTag
  227. //Input: String newTag: The tag to set for the player
  228. //Output: None
  229. //Use: Sets a player tag
  230. //=====================================================================
  231. public void setTag(String newTag){ tag = newTag; }
  232. //=====================================================================
  233. //Function: getTag
  234. //Input: None
  235. //Output: String: The player tag
  236. //Use: Gets a player tag
  237. //=====================================================================
  238. public String getTag() { return tag; }
  239. }
  240. }