PageRenderTime 41ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/src/DataSource.java

https://github.com/mrsheen/Minecraft-Server-Mod
Java | 519 lines | 236 code | 62 blank | 221 comment | 39 complexity | c27cd2c327cbd779944eb762936beeaf 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. * @author James
  12. */
  13. public abstract class DataSource {
  14. protected static final Logger log = Logger.getLogger("Minecraft");
  15. protected List<Group> groups = new ArrayList<Group>();
  16. protected List<Kit> kits = new ArrayList<Kit>();
  17. protected List<Warp> homes = new ArrayList<Warp>();
  18. protected List<Warp> warps = new ArrayList<Warp>();
  19. protected List<Portal> portals = new ArrayList<Portal>();
  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. protected final Object portalLock = new Object();
  26. /**
  27. * Initializes the data source
  28. */
  29. abstract public void initialize();
  30. /**
  31. * Loads all groups
  32. */
  33. abstract public void loadGroups();
  34. /**
  35. * Loads all kits
  36. */
  37. abstract public void loadKits();
  38. /**
  39. * Loads all homes
  40. */
  41. abstract public void loadHomes();
  42. /**
  43. * Loads all warps
  44. */
  45. abstract public void loadWarps();
  46. /**
  47. * Loads all portals
  48. */
  49. abstract public void loadPortals();
  50. /**
  51. * Loads all items
  52. */
  53. abstract public void loadItems();
  54. /**
  55. * Loads the ban list
  56. */
  57. abstract public void loadBanList();
  58. /**
  59. * Adds user to the list
  60. * @param player
  61. */
  62. abstract public void addPlayer(Player player);
  63. /**
  64. * Modifies the provided user
  65. * @param player
  66. */
  67. abstract public void modifyPlayer(Player player);
  68. /**
  69. * Checks to see if the specified player exists
  70. * @param player
  71. * @return true if player exists
  72. */
  73. abstract public boolean doesPlayerExist(String player);
  74. /**
  75. * Returns specified user
  76. * @param name
  77. * @return user
  78. */
  79. abstract public Player getPlayer(String name);
  80. /**
  81. * Adds specified group to the list of groups
  82. * @param group
  83. */
  84. abstract public void addGroup(Group group);
  85. /**
  86. * Modifies group
  87. * @param group
  88. */
  89. abstract public void modifyGroup(Group group);
  90. /**
  91. * Returns specified group
  92. * @param name
  93. * @return group
  94. */
  95. public Group getGroup(String name) {
  96. synchronized (groupLock) {
  97. for (Group group : groups) {
  98. if (group.Name.equalsIgnoreCase(name)) {
  99. return group;
  100. }
  101. }
  102. }
  103. if (!name.equals("")) {
  104. log.log(Level.INFO, "Unable to find group '" + name + "'. Are you sure you have that group?");
  105. }
  106. return null;
  107. }
  108. /**
  109. * Returns the default group
  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. }
  119. }
  120. return null;
  121. }
  122. /**
  123. * Adds kit to list of kits
  124. * @param kit
  125. */
  126. abstract public void addKit(Kit kit);
  127. /**
  128. * Modifies kit
  129. * @param kit
  130. */
  131. abstract public void modifyKit(Kit kit);
  132. /**
  133. * Returns specified kit
  134. * @param name
  135. * @return kit
  136. */
  137. public Kit getKit(String name) {
  138. synchronized (kitLock) {
  139. for (Kit kit : kits) {
  140. if (kit.Name.equalsIgnoreCase(name)) {
  141. return kit;
  142. }
  143. }
  144. }
  145. return null;
  146. }
  147. /**
  148. * Returns true if there are any kits
  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. * @param player
  159. * @return string list of kits
  160. */
  161. public String getKitNames(Player player) {
  162. StringBuilder builder = new StringBuilder();
  163. builder.append(""); //incaseofnull
  164. synchronized (kitLock) {
  165. for (Kit kit : kits) {
  166. if (player.isInGroup(kit.Group) || kit.Group.equals("")) {
  167. builder.append(kit.Name).append(" ");
  168. }
  169. }
  170. }
  171. return builder.toString();
  172. }
  173. /**
  174. * Adds home to list of homes
  175. * @param home
  176. */
  177. abstract public void addHome(Warp home);
  178. /**
  179. * Modifies home
  180. * @param home
  181. */
  182. abstract public void changeHome(Warp home);
  183. /**
  184. * Returns specified home
  185. * @param name
  186. * @return home
  187. */
  188. public Warp getHome(String name) {
  189. synchronized (homeLock) {
  190. for (Warp home : homes) {
  191. if (home.Name.equalsIgnoreCase(name)) {
  192. return home;
  193. }
  194. }
  195. }
  196. return null;
  197. }
  198. /**
  199. * Adds warp to list of warps
  200. * @param warp
  201. */
  202. abstract public void addWarp(Warp warp);
  203. /**
  204. * Modifies warp
  205. * @param warp
  206. */
  207. abstract public void changeWarp(Warp warp);
  208. /**
  209. * Removes warp from list of warps
  210. * @param warp
  211. */
  212. abstract public void removeWarp(Warp warp);
  213. /**
  214. * Returns specified warp
  215. * @param name
  216. * @return warp
  217. */
  218. public Warp getWarp(String name) {
  219. synchronized (warpLock) {
  220. for (Warp warp : warps) {
  221. if (warp.Name.equalsIgnoreCase(name)) {
  222. return warp;
  223. }
  224. }
  225. }
  226. return null;
  227. }
  228. /**
  229. * Returns true if there are any warps
  230. * @return true if there are warps
  231. */
  232. public boolean hasWarps() {
  233. synchronized (warpLock) {
  234. return warps.size() > 0;
  235. }
  236. }
  237. /**
  238. * Returns a string containing all warp names the player has access to
  239. * @param player
  240. * @return string list of warps
  241. */
  242. public String getWarpNames(Player player) {
  243. StringBuilder builder = new StringBuilder();
  244. builder.append(""); //incaseofnull
  245. synchronized (warpLock) {
  246. for (Warp warp : warps) {
  247. if (player.isInGroup(warp.Group) || warp.Group.equals("")) {
  248. builder.append(warp.Name).append(" ");
  249. }
  250. }
  251. }
  252. return builder.toString();
  253. }
  254. /**
  255. * Adds portal to list of portals
  256. * @param portal
  257. */
  258. abstract public void addPortal(Portal portal);
  259. /**
  260. * Modifies portal
  261. * @param portal
  262. */
  263. abstract public void changePortal(Portal portal);
  264. /**
  265. * Removes portal from list of portals
  266. * @param portal
  267. */
  268. abstract public void removePortal(Portal portal);
  269. /**
  270. * Returns specified portal
  271. * @param name
  272. * @return portal
  273. */
  274. public Portal getPortal(String name) {
  275. synchronized (portalLock) {
  276. for (Portal portal : portals) {
  277. if (portal.Name.equalsIgnoreCase(name)) {
  278. return portal;
  279. }
  280. }
  281. }
  282. return null;
  283. }
  284. /**
  285. * Returns specified portal
  286. * @param ID
  287. * @return portal
  288. */
  289. public Portal getPortal(int ID) {
  290. synchronized (portalLock) {
  291. for (Portal portal : portals) {
  292. if (portal.ID == ID) {
  293. return portal;
  294. }
  295. }
  296. }
  297. return null;
  298. }
  299. /**
  300. * Returns specified portal
  301. * @param intX, intY, intZ
  302. * @return portal
  303. */
  304. public Portal getPortal(int x, int y, int z) {
  305. synchronized (portalLock) {
  306. for (Portal portal : portals) {
  307. if (portal.containsLoc(x,y,z) || portal.containsLoc(x,y-1,z)) { // If you creep to teh edge, you can be in a portal but above teh curtain block
  308. return portal;
  309. }
  310. }
  311. }
  312. return null;
  313. }
  314. /**
  315. * Returns specified portal
  316. * @param intX, intY, intZ
  317. * @return portal
  318. */
  319. public Portal getPortalByCurtain(int x, int y, int z) {
  320. synchronized (portalLock) {
  321. for (Portal portal : portals) {
  322. if (portal.containsCurtain(x,y,z)) {
  323. return portal;
  324. }
  325. }
  326. }
  327. return null;
  328. }
  329. /**
  330. * Returns a string containing all portal names
  331. * @return string list of portal
  332. */
  333. public String getPortalNames() {
  334. StringBuilder builder = new StringBuilder();
  335. builder.append(""); //incaseofnull
  336. synchronized (portalLock) {
  337. for (Portal portal : portals) {
  338. if (!portal.Name.equalsIgnoreCase("null")) {
  339. builder.append(portal.Name).append(" ");
  340. }
  341. }
  342. }
  343. return builder.toString();
  344. }
  345. /**
  346. * Returns item id corresponding to item name
  347. * @param name
  348. * @return item id
  349. */
  350. public int getItem(String name) {
  351. synchronized (itemLock) {
  352. if (items.containsKey(name)) {
  353. return items.get(name);
  354. }
  355. }
  356. return 0;
  357. }
  358. /**
  359. * Returns the name of the item corresponding to the ID
  360. * @param id id of item
  361. * @return name of item
  362. */
  363. public String getItem(int id) {
  364. synchronized (itemLock) {
  365. for (String name : items.keySet()) {
  366. if (items.get(name) == id) {
  367. return name;
  368. }
  369. }
  370. }
  371. return String.valueOf(id);
  372. }
  373. /**
  374. * Returns an unmodifiable map of items
  375. * @return unmodifiable map of items
  376. */
  377. public Map<String, Integer> getItems() {
  378. return Collections.unmodifiableMap(items);
  379. }
  380. /**
  381. * Adds player to whitelist
  382. * @param name
  383. */
  384. abstract public void addToWhitelist(String name);
  385. /**
  386. * Removes player from whitelist
  387. * @param name
  388. */
  389. abstract public void removeFromWhitelist(String name);
  390. /**
  391. * Returns true if whitelist is enabled
  392. * @return true if whitelist is enabled
  393. * @deprecated use etc.getInstance().isWhitelistEnabled() instead
  394. */
  395. @Deprecated public boolean hasWhitelist() {
  396. return etc.getInstance().isWhitelistEnabled();
  397. }
  398. /**
  399. * Returns true if the player is on the whitelist
  400. * @param user
  401. * @return true if player is on whitelist
  402. */
  403. abstract public boolean isUserOnWhitelist(String user);
  404. /**
  405. * Adds player to reservelist
  406. * @param name
  407. */
  408. abstract public void addToReserveList(String name);
  409. /**
  410. * Removes player from reservelist
  411. * @param name
  412. */
  413. abstract public void removeFromReserveList(String name);
  414. /**
  415. * Returns true if player is on reservelist
  416. * @param user
  417. * @return true if player is on reserve list
  418. */
  419. abstract public boolean isUserOnReserveList(String user);
  420. /**
  421. * Adds or modifies specified ban
  422. * @param ban Ban to add or modify
  423. */
  424. abstract public void modifyBan(Ban ban);
  425. /**
  426. * Checks to see if this player or IP is on the ban list
  427. * @param player Player name
  428. * @param ip IP Address
  429. * @return true if either name or IP is on the ban list
  430. */
  431. public boolean isOnBanList(String player, String ip) {
  432. synchronized (banLock) {
  433. for (Ban ban : bans) {
  434. if (ban.getName().equalsIgnoreCase(player) || ban.getIp().equalsIgnoreCase(ip)) {
  435. return true;
  436. }
  437. }
  438. }
  439. return false;
  440. }
  441. /**
  442. * Retrieves the ban details
  443. * @param player Player name
  444. * @param ip IP Address
  445. * @return the ban
  446. */
  447. public Ban getBan(String player, String ip) {
  448. synchronized (banLock) {
  449. for (Ban ban : bans) {
  450. if (ban.getName().equalsIgnoreCase(player) || ban.getIp().equalsIgnoreCase(ip)) {
  451. return ban;
  452. }
  453. }
  454. }
  455. return null;
  456. }
  457. }