CustomOreGen.java 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package xyz.spaceio.customoregen;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Optional;
  8. import java.util.UUID;
  9. import java.util.stream.Collectors;
  10. import org.bukkit.Bukkit;
  11. import org.bukkit.Location;
  12. import org.bukkit.OfflinePlayer;
  13. import org.bukkit.World;
  14. import org.bukkit.command.ConsoleCommandSender;
  15. import org.bukkit.entity.Player;
  16. import org.bukkit.plugin.PluginManager;
  17. import org.bukkit.plugin.java.JavaPlugin;
  18. import com.google.gson.reflect.TypeToken;
  19. import de.Linus122.SpaceIOMetrics.Metrics;
  20. import xyz.spaceio.configutils.ConfigHandler;
  21. import xyz.spaceio.configutils.JSONConfig;
  22. import xyz.spaceio.hooks.HookInfo;
  23. import xyz.spaceio.hooks.HookVanilla;
  24. import xyz.spaceio.hooks.SkyblockAPICached;
  25. import xyz.spaceio.hooks.SkyblockAPIHook;
  26. import xyz.spaceio.misc.NamePlaceholder;
  27. public class CustomOreGen extends JavaPlugin {
  28. /*
  29. * Configurations for all generators (defined in the config.yml)
  30. */
  31. private List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
  32. /*
  33. * Disabled worlds blacklist
  34. */
  35. private List<String> disabledWorlds = new ArrayList<String>();
  36. /*
  37. * Our logger
  38. */
  39. private ConsoleCommandSender clogger;
  40. /*
  41. * Cache for GeneratorConfig ID's for each player
  42. */
  43. private HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
  44. private JSONConfig cachedOregenJsonConfig;
  45. /*
  46. * API Hook for the corresponding SkyBlock plugin
  47. */
  48. private SkyblockAPIHook skyblockAPI;
  49. /*
  50. * API Hook but cached
  51. */
  52. private SkyblockAPICached skyblockAPICached;
  53. /*
  54. * Object that handles the loading process of the config.yml file
  55. */
  56. private ConfigHandler configHandler = new ConfigHandler(this, "plugins/CustomOreGen/config.yml");;
  57. /*
  58. * Prefix for the clogger
  59. */
  60. private final String PREFIX = "§6[CustomOreGen] ";
  61. /*
  62. * Main event class
  63. */
  64. private Events events;
  65. @Override
  66. public void onEnable() {
  67. clogger = getServer().getConsoleSender();
  68. PluginManager pm = Bukkit.getPluginManager();
  69. this.loadHook();
  70. Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
  71. // load the config.yml
  72. try {
  73. configHandler.loadConfig();
  74. } catch (IOException e) {
  75. // TODO Auto-generated catch block
  76. e.printStackTrace();
  77. }
  78. events = new Events(this);
  79. pm.registerEvents(events, this);
  80. // some persisting saving stuff
  81. cachedOregenJsonConfig = new JSONConfig(new TypeToken<HashMap<UUID, Integer>>() {
  82. }.getType(), cachedOregenConfigs, this);
  83. cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.getObject();
  84. if (cachedOregenConfigs == null) {
  85. cachedOregenConfigs = new HashMap<UUID, Integer>();
  86. }
  87. disabledWorlds = getConfig().getStringList("disabled-worlds");
  88. // registering place holders
  89. if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
  90. new NamePlaceholder(this).register();
  91. }
  92. new Metrics(this);
  93. }
  94. @Override
  95. public void onDisable() {
  96. cachedOregenJsonConfig.saveToDisk();
  97. }
  98. /**
  99. * Acquires the corresponding skyblock hook class
  100. */
  101. private void loadHook() {
  102. skyblockAPI = getHook();
  103. skyblockAPICached = new SkyblockAPICached(skyblockAPI);
  104. }
  105. public SkyblockAPIHook getHook() {
  106. NoClassDefFoundError loadException = null;
  107. SkyblockAPIHook skyblockAPI = null;
  108. for(HookInfo hookInfo : HookInfo.values()) {
  109. String pluginName = hookInfo.name().replace("Legacy", "");
  110. if(Bukkit.getServer().getPluginManager().isPluginEnabled(pluginName)) {
  111. try {
  112. try {
  113. skyblockAPI = (SkyblockAPIHook) hookInfo.getHookClass().newInstance();
  114. }catch(NoClassDefFoundError e) {
  115. loadException = e;
  116. continue;
  117. }
  118. sendConsole(String.format("&aUsing %s as SkyBlock-Plugin, hook class: %s", pluginName, hookInfo.getHookClass().getName()));
  119. break;
  120. } catch (InstantiationException | IllegalAccessException e) {
  121. e.printStackTrace();
  122. }
  123. }
  124. }
  125. if(skyblockAPI == null) {
  126. if(loadException != null)
  127. loadException.printStackTrace();
  128. sendConsole("§cYou are not using any supported skyblock plugin! Will use the vanilla range check hook instead.");
  129. skyblockAPI = new HookVanilla();
  130. }
  131. return skyblockAPI;
  132. }
  133. /**
  134. * @return all active skyblock worlds
  135. */
  136. public List<World> getActiveWorlds() {
  137. return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
  138. .collect(Collectors.toList());
  139. }
  140. /**
  141. * Returns the current island level based on the skyblock world and player.
  142. *
  143. * @param uuid UUID of the player to check
  144. * @param world the world of the island to check the level
  145. * @return player's island level
  146. */
  147. public int getLevel(UUID uuid, String world) {
  148. return this.getSkyblockAPICached().getIslandLevel(uuid, world);
  149. }
  150. /**
  151. * Gathers the owner of an island at a certain location
  152. *
  153. * @param loc Location to check
  154. * @return owner
  155. */
  156. public OfflinePlayer getOwner(Location loc) {
  157. if (this.getSkyblockAPICached().getIslandOwner(loc) == null) {
  158. return null;
  159. }
  160. Optional<UUID> uuid = this.getSkyblockAPICached().getIslandOwner(loc);
  161. if (!uuid.isPresent()) {
  162. return null;
  163. }
  164. OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
  165. return p;
  166. }
  167. /**
  168. * Gets the skyblock hook name in use
  169. *
  170. * @return The name of the hook class
  171. */
  172. public String getHookName() {
  173. return this.skyblockAPI.getClass().getName();
  174. }
  175. public void reload() throws IOException {
  176. reloadConfig();
  177. configHandler.loadConfig();
  178. events.load();
  179. }
  180. /**
  181. * Acquires a generator config that applies for the given player,
  182. * The result depends on the permission or island level of the player.
  183. *
  184. * @param offlinePlayer the offline player (Usually created using the UUID)
  185. * @param world the skyblock world
  186. * @return the generator config
  187. */
  188. public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer offlinePlayer, String world) {
  189. GeneratorConfig gc = null;
  190. int id = 0;
  191. if (offlinePlayer == null) {
  192. gc = generatorConfigs.get(0);
  193. cacheOreGen(offlinePlayer.getUniqueId(), id);
  194. } else {
  195. int islandLevel = getLevel(offlinePlayer.getUniqueId(), world);
  196. if (offlinePlayer.isOnline()) {
  197. Player realP = offlinePlayer.getPlayer();
  198. if (this.getActiveWorlds().contains(realP.getWorld())) {
  199. for (GeneratorConfig gc2 : generatorConfigs) {
  200. if (gc2 == null) {
  201. continue;
  202. }
  203. if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
  204. && islandLevel >= gc2.unlock_islandLevel) {
  205. // continue
  206. gc = gc2;
  207. id++;
  208. }
  209. }
  210. }
  211. } else {
  212. gc = getCachedGeneratorConfig(offlinePlayer.getUniqueId());
  213. }
  214. }
  215. if (id > 0) {
  216. cacheOreGen(offlinePlayer.getUniqueId(), id - 1);
  217. }
  218. return gc;
  219. }
  220. /**
  221. * Returns all worlds in which the plugin is disabled in (configurable inside the config.yml)
  222. *
  223. * @return A list of world names as string
  224. */
  225. public List<String> getDisabledWorlds() {
  226. return disabledWorlds;
  227. }
  228. /**
  229. * Returns a cached generator config. Useful when an island owner left the server, but a player is still mining at a generator.
  230. *
  231. * @param uuid the owners UUID
  232. * @return the generator config
  233. */
  234. public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
  235. if (cachedOregenConfigs.containsKey(uuid)) {
  236. return generatorConfigs.get(cachedOregenConfigs.get(uuid));
  237. }
  238. return null;
  239. }
  240. /**
  241. * Writes an existing ore generator config to the cache
  242. *
  243. * @param uuid UUID of the owner
  244. * @param configID the ID of the generator config
  245. */
  246. public void cacheOreGen(UUID uuid, int configID) {
  247. cachedOregenConfigs.put(uuid, configID);
  248. }
  249. /**
  250. * Sends a formatted messages to the console, colors supported
  251. *
  252. * @param msg A string using either & or § for colors
  253. */
  254. public void sendConsole(String msg) {
  255. clogger.sendMessage(PREFIX + msg.replace("&", "§"));
  256. }
  257. public List<GeneratorConfig> getGeneratorConfigs() {
  258. return generatorConfigs;
  259. }
  260. public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
  261. this.generatorConfigs = generatorConfigs;
  262. }
  263. /**
  264. * @return the skyblockAPICached
  265. */
  266. public SkyblockAPICached getSkyblockAPICached() {
  267. return skyblockAPICached;
  268. }
  269. }