CustomOreGen.java 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. @Override
  62. public void onEnable() {
  63. clogger = getServer().getConsoleSender();
  64. PluginManager pm = Bukkit.getPluginManager();
  65. pm.registerEvents(new Events(this), this);
  66. this.loadHook();
  67. Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
  68. // load the config.yml
  69. try {
  70. configHandler.loadConfig();
  71. } catch (IOException e) {
  72. // TODO Auto-generated catch block
  73. e.printStackTrace();
  74. }
  75. // some persisting saving stuff
  76. cachedOregenJsonConfig = new JSONConfig(new TypeToken<HashMap<UUID, Integer>>() {
  77. }.getType(), cachedOregenConfigs, this);
  78. cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.getObject();
  79. if (cachedOregenConfigs == null) {
  80. cachedOregenConfigs = new HashMap<UUID, Integer>();
  81. }
  82. disabledWorlds = getConfig().getStringList("disabled-worlds");
  83. // registering place holders
  84. if(Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null) {
  85. new NamePlaceholder(this).register();
  86. }
  87. new Metrics(this);
  88. }
  89. @Override
  90. public void onDisable() {
  91. cachedOregenJsonConfig.saveToDisk();
  92. }
  93. /**
  94. * Acquires the corresponding skyblock hook class
  95. */
  96. private void loadHook() {
  97. skyblockAPI = getHook();
  98. skyblockAPICached = new SkyblockAPICached(skyblockAPI);
  99. }
  100. public SkyblockAPIHook getHook() {
  101. NoClassDefFoundError loadException = null;
  102. SkyblockAPIHook skyblockAPI = null;
  103. for(HookInfo hookInfo : HookInfo.values()) {
  104. String pluginName = hookInfo.name().replace("Legacy", "");
  105. if(Bukkit.getServer().getPluginManager().isPluginEnabled(pluginName)) {
  106. try {
  107. try {
  108. skyblockAPI = (SkyblockAPIHook) hookInfo.getHookClass().newInstance();
  109. }catch(NoClassDefFoundError e) {
  110. loadException = e;
  111. continue;
  112. }
  113. sendConsole(String.format("&aUsing %s as SkyBlock-Plugin, hook class: %s", pluginName, hookInfo.getHookClass().getName()));
  114. break;
  115. } catch (InstantiationException | IllegalAccessException e) {
  116. e.printStackTrace();
  117. }
  118. }
  119. }
  120. if(skyblockAPI == null) {
  121. if(loadException != null)
  122. loadException.printStackTrace();
  123. sendConsole("§cYou are not using any supported skyblock plugin! Will use the vanilla range check hook instead.");
  124. skyblockAPI = new HookVanilla();
  125. }
  126. return skyblockAPI;
  127. }
  128. /**
  129. * @return all active skyblock worlds
  130. */
  131. public List<World> getActiveWorlds() {
  132. return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
  133. .collect(Collectors.toList());
  134. }
  135. /**
  136. * Returns the current island level based on the skyblock world and player.
  137. *
  138. * @param uuid UUID of the player to check
  139. * @param world the world of the island to check the level
  140. * @return player's island level
  141. */
  142. public int getLevel(UUID uuid, String world) {
  143. return this.getSkyblockAPICached().getIslandLevel(uuid, world);
  144. }
  145. /**
  146. * Gathers the owner of an island at a certain location
  147. *
  148. * @param loc Location to check
  149. * @return owner
  150. */
  151. public OfflinePlayer getOwner(Location loc) {
  152. if (this.getSkyblockAPICached().getIslandOwner(loc) == null) {
  153. return null;
  154. }
  155. Optional<UUID> uuid = this.getSkyblockAPICached().getIslandOwner(loc);
  156. if (!uuid.isPresent()) {
  157. return null;
  158. }
  159. OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
  160. return p;
  161. }
  162. public void reload() throws IOException {
  163. reloadConfig();
  164. configHandler.loadConfig();
  165. }
  166. /**
  167. * Acquires a generator config that applies for the given player,
  168. * The result depends on the permission or island level of the player.
  169. *
  170. * @param offlinePlayer the offline player (Usually created using the UUID)
  171. * @param world the skyblock world
  172. * @return the generator config
  173. */
  174. public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer offlinePlayer, String world) {
  175. GeneratorConfig gc = null;
  176. int id = 0;
  177. if (offlinePlayer == null) {
  178. gc = generatorConfigs.get(0);
  179. cacheOreGen(offlinePlayer.getUniqueId(), id);
  180. } else {
  181. int islandLevel = getLevel(offlinePlayer.getUniqueId(), world);
  182. if (offlinePlayer.isOnline()) {
  183. Player realP = offlinePlayer.getPlayer();
  184. if (this.getActiveWorlds().contains(realP.getWorld())) {
  185. for (GeneratorConfig gc2 : generatorConfigs) {
  186. if (gc2 == null) {
  187. continue;
  188. }
  189. if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
  190. && islandLevel >= gc2.unlock_islandLevel) {
  191. // continue
  192. gc = gc2;
  193. id++;
  194. }
  195. }
  196. }
  197. } else {
  198. gc = getCachedGeneratorConfig(offlinePlayer.getUniqueId());
  199. }
  200. }
  201. if (id > 0) {
  202. cacheOreGen(offlinePlayer.getUniqueId(), id - 1);
  203. }
  204. return gc;
  205. }
  206. /**
  207. * Returns all worlds in which the plugin is disabled in (configurable inside the config.yml)
  208. *
  209. * @return A list of world names as string
  210. */
  211. public List<String> getDisabledWorlds() {
  212. return disabledWorlds;
  213. }
  214. /**
  215. * Returns a cached generator config. Useful when an island owner left the server, but a player is still mining at a generator.
  216. *
  217. * @param uuid the owners UUID
  218. * @return the generator config
  219. */
  220. public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
  221. if (cachedOregenConfigs.containsKey(uuid)) {
  222. return generatorConfigs.get(cachedOregenConfigs.get(uuid));
  223. }
  224. return null;
  225. }
  226. /**
  227. * Writes an existing ore generator config to the cache
  228. *
  229. * @param uuid UUID of the owner
  230. * @param configID the ID of the generator config
  231. */
  232. public void cacheOreGen(UUID uuid, int configID) {
  233. cachedOregenConfigs.put(uuid, configID);
  234. }
  235. /**
  236. * Sends a formatted messages to the console, colors supported
  237. *
  238. * @param msg A string using either & or § for colors
  239. */
  240. public void sendConsole(String msg) {
  241. clogger.sendMessage(PREFIX + msg.replace("&", "§"));
  242. }
  243. public List<GeneratorConfig> getGeneratorConfigs() {
  244. return generatorConfigs;
  245. }
  246. public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
  247. this.generatorConfigs = generatorConfigs;
  248. }
  249. /**
  250. * @return the skyblockAPICached
  251. */
  252. public SkyblockAPICached getSkyblockAPICached() {
  253. return skyblockAPICached;
  254. }
  255. }