CustomOreGen.java 8.8 KB

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