PageRenderTime 867ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/src/DataSource.java

http://github.com/traitor/Minecraft-Server-Mod
Java | 432 lines | 157 code | 51 blank | 224 comment | 27 complexity | 67c0b5d0785f52bbb2287f9a43141bdc MD5 | raw file
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import java.util.Map;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import net.minecraft.server.MinecraftServer;
  9. /**
  10. * DataSource.java - Abstract class for implementing new data sources.
  11. *
  12. * @author James
  13. */
  14. public abstract class DataSource {
  15. protected static final Logger log = Logger.getLogger("Minecraft");
  16. protected List<Group> groups = new ArrayList<Group>();
  17. protected List<Kit> kits = new ArrayList<Kit>();
  18. protected List<Warp> homes = new ArrayList<Warp>();
  19. protected List<Warp> warps = new ArrayList<Warp>();
  20. protected List<Ban> bans = new ArrayList<Ban>();
  21. protected Map<String, Integer> items = new HashMap<String, Integer>();
  22. protected MinecraftServer server;
  23. protected final Object groupLock = new Object(), kitLock = new Object(), banLock = new Object();
  24. protected final Object homeLock = new Object(), warpLock = new Object(), itemLock = new Object();
  25. /**
  26. * Initializes the data source
  27. */
  28. abstract public void initialize();
  29. /**
  30. * Loads all groups
  31. */
  32. abstract public void loadGroups();
  33. /**
  34. * Loads all kits
  35. */
  36. abstract public void loadKits();
  37. /**
  38. * Loads all homes
  39. */
  40. abstract public void loadHomes();
  41. /**
  42. * Loads all warps
  43. */
  44. abstract public void loadWarps();
  45. /**
  46. * Loads all items
  47. */
  48. abstract public void loadItems();
  49. /**
  50. * Loads the ban list
  51. */
  52. abstract public void loadBanList();
  53. /**
  54. * Adds user to the list
  55. *
  56. * @param player
  57. */
  58. abstract public void addPlayer(Player player);
  59. /**
  60. * Modifies the provided user
  61. *
  62. * @param player
  63. */
  64. abstract public void modifyPlayer(Player player);
  65. /**
  66. * Checks to see if the specified player exists
  67. *
  68. * @param player
  69. * @return true if player exists
  70. */
  71. abstract public boolean doesPlayerExist(String player);
  72. /**
  73. * Returns specified user
  74. *
  75. * @param name
  76. * @return user
  77. */
  78. abstract public Player getPlayer(String name);
  79. /**
  80. * Adds specified group to the list of groups
  81. *
  82. * @param group
  83. */
  84. abstract public void addGroup(Group group);
  85. /**
  86. * Modifies group
  87. *
  88. * @param group
  89. */
  90. abstract public void modifyGroup(Group group);
  91. /**
  92. * Returns specified group
  93. *
  94. * @param name
  95. * @return group
  96. */
  97. public Group getGroup(String name) {
  98. synchronized (groupLock) {
  99. for (Group group : groups)
  100. if (group.Name.equalsIgnoreCase(name))
  101. return group;
  102. }
  103. if (!name.equals(""))
  104. log.log(Level.INFO, "Unable to find group '" + name + "'. Are you sure you have that group?");
  105. return null;
  106. }
  107. /**
  108. * Returns the default group
  109. *
  110. * @return default group
  111. */
  112. public Group getDefaultGroup() {
  113. synchronized (groupLock) {
  114. for (Group group : groups)
  115. if (group.DefaultGroup)
  116. return group;
  117. }
  118. return null;
  119. }
  120. /**
  121. * Adds kit to list of kits
  122. *
  123. * @param kit
  124. */
  125. abstract public void addKit(Kit kit);
  126. /**
  127. * Modifies kit
  128. *
  129. * @param kit
  130. */
  131. abstract public void modifyKit(Kit kit);
  132. /**
  133. * Returns specified kit
  134. *
  135. * @param name
  136. * @return kit
  137. */
  138. public Kit getKit(String name) {
  139. synchronized (kitLock) {
  140. for (Kit kit : kits)
  141. if (kit.Name.equalsIgnoreCase(name))
  142. return kit;
  143. }
  144. return null;
  145. }
  146. /**
  147. * Returns true if there are any kits
  148. *
  149. * @return true if there are kits
  150. */
  151. public boolean hasKits() {
  152. synchronized (kitLock) {
  153. return kits.size() > 0;
  154. }
  155. }
  156. /**
  157. * Returns a list of all kits names separated by commas
  158. *
  159. * @param player
  160. * @return string list of kits
  161. */
  162. public String getKitNames(Player player) {
  163. StringBuilder builder = new StringBuilder();
  164. builder.append(""); // incaseofnull
  165. synchronized (kitLock) {
  166. for (Kit kit : kits)
  167. if (player.isInGroup(kit.Group) || kit.Group.equals(""))
  168. builder.append(kit.Name).append(" ");
  169. }
  170. return builder.toString();
  171. }
  172. /**
  173. * Adds home to list of homes
  174. *
  175. * @param home
  176. */
  177. abstract public void addHome(Warp home);
  178. /**
  179. * Modifies home
  180. *
  181. * @param home
  182. */
  183. abstract public void changeHome(Warp home);
  184. /**
  185. * Returns specified home
  186. *
  187. * @param name
  188. * @return home
  189. */
  190. public Warp getHome(String name) {
  191. synchronized (homeLock) {
  192. for (Warp home : homes)
  193. if (home.Name.equalsIgnoreCase(name))
  194. return home;
  195. }
  196. return null;
  197. }
  198. /**
  199. * Adds warp to list of warps
  200. *
  201. * @param warp
  202. */
  203. abstract public void addWarp(Warp warp);
  204. /**
  205. * Modifies warp
  206. *
  207. * @param warp
  208. */
  209. abstract public void changeWarp(Warp warp);
  210. /**
  211. * Removes warp from list of warps
  212. *
  213. * @param warp
  214. */
  215. abstract public void removeWarp(Warp warp);
  216. /**
  217. * Returns specified warp
  218. *
  219. * @param name
  220. * @return warp
  221. */
  222. public Warp getWarp(String name) {
  223. synchronized (warpLock) {
  224. for (Warp warp : warps)
  225. if (warp.Name.equalsIgnoreCase(name))
  226. return warp;
  227. }
  228. return null;
  229. }
  230. /**
  231. * Returns true if there are any warps
  232. *
  233. * @return true if there are warps
  234. */
  235. public boolean hasWarps() {
  236. synchronized (warpLock) {
  237. return warps.size() > 0;
  238. }
  239. }
  240. /**
  241. * Returns a string containing all warp names the player has access to
  242. *
  243. * @param player
  244. * @return string list of warps
  245. */
  246. public String getWarpNames(Player player) {
  247. StringBuilder builder = new StringBuilder();
  248. builder.append(""); // incaseofnull
  249. synchronized (warpLock) {
  250. for (Warp warp : warps)
  251. if (player.isInGroup(warp.Group) || warp.Group.equals(""))
  252. builder.append(warp.Name).append(" ");
  253. }
  254. return builder.toString();
  255. }
  256. /**
  257. * Returns item id corresponding to item name
  258. *
  259. * @param name
  260. * @return item id
  261. */
  262. public int getItem(String name) {
  263. synchronized (itemLock) {
  264. if (items.containsKey(name))
  265. return items.get(name);
  266. }
  267. return 0;
  268. }
  269. /**
  270. * Returns the name of the item corresponding to the ID
  271. *
  272. * @param id
  273. * id of item
  274. * @return name of item
  275. */
  276. public String getItem(int id) {
  277. synchronized (itemLock) {
  278. for (String name : items.keySet())
  279. if (items.get(name) == id)
  280. return name;
  281. }
  282. return String.valueOf(id);
  283. }
  284. /**
  285. * Returns an unmodifiable map of items
  286. *
  287. * @return unmodifiable map of items
  288. */
  289. public Map<String, Integer> getItems() {
  290. return Collections.unmodifiableMap(items);
  291. }
  292. /**
  293. * Adds player to whitelist
  294. *
  295. * @param name
  296. */
  297. abstract public void addToWhitelist(String name);
  298. /**
  299. * Removes player from whitelist
  300. *
  301. * @param name
  302. */
  303. abstract public void removeFromWhitelist(String name);
  304. /**
  305. * Returns true if whitelist is enabled
  306. *
  307. * @return true if whitelist is enabled
  308. * @deprecated use etc.getInstance().isWhitelistEnabled() instead
  309. */
  310. @Deprecated
  311. public boolean hasWhitelist() {
  312. return etc.getInstance().isWhitelistEnabled();
  313. }
  314. /**
  315. * Returns true if the player is on the whitelist
  316. *
  317. * @param user
  318. * @return true if player is on whitelist
  319. */
  320. abstract public boolean isUserOnWhitelist(String user);
  321. /**
  322. * Adds or modifies specified ban
  323. *
  324. * @param ban
  325. * Ban to add or modify
  326. */
  327. abstract public void modifyBan(Ban ban);
  328. /**
  329. * Checks to see if this player or IP is on the ban list
  330. *
  331. * @param player
  332. * Player name
  333. * @param ip
  334. * IP Address
  335. * @return true if either name or IP is on the ban list
  336. */
  337. public boolean isOnBanList(String player, String ip) {
  338. synchronized (banLock) {
  339. for (Ban ban : bans)
  340. if (ban.getName().equalsIgnoreCase(player) || ban.getIp().equalsIgnoreCase(ip))
  341. return true;
  342. }
  343. return false;
  344. }
  345. /**
  346. * Retrieves the ban details
  347. *
  348. * @param player
  349. * Player name
  350. * @param ip
  351. * IP Address
  352. * @return the ban
  353. */
  354. public Ban getBan(String player, String ip) {
  355. synchronized (banLock) {
  356. for (Ban ban : bans)
  357. if (ban.getName().equalsIgnoreCase(player) || ban.getIp().equalsIgnoreCase(ip))
  358. return ban;
  359. }
  360. return null;
  361. }
  362. /**
  363. * Adds player to reservelist
  364. *
  365. * @param name
  366. */
  367. abstract public void addToReserveList(String name);
  368. /**
  369. * Removes player from reservelist
  370. *
  371. * @param name
  372. */
  373. abstract public void removeFromReserveList(String name);
  374. /**
  375. * Returns true if player is on reservelist
  376. *
  377. * @param user
  378. * @return true if player is on reserve list
  379. */
  380. abstract public boolean isUserOnReserveList(String user);
  381. }