CustomOreGen.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. for(HookInfo hookInfo : HookInfo.values()) {
  98. String pluginName = hookInfo.name();
  99. if(Bukkit.getServer().getPluginManager().isPluginEnabled(pluginName)) {
  100. sendConsole(String.format("&aUsing %s as SkyBlock-Plugin", pluginName));
  101. try {
  102. skyblockAPI = (SkyblockAPIHook) hookInfo.getHookClass().newInstance();
  103. break;
  104. } catch (InstantiationException | IllegalAccessException e) {
  105. // TODO Auto-generated catch block
  106. e.printStackTrace();
  107. }
  108. }
  109. }
  110. if(skyblockAPI == null) {
  111. sendConsole("§cYou are not using any supported skyblock plugin! Will use the vanilla range check hook instead.");
  112. skyblockAPI = new HookVanilla();
  113. }
  114. skyblockAPICached = new SkyblockAPICached(skyblockAPI);
  115. }
  116. /**
  117. * @return all active skyblock worlds
  118. */
  119. public List<World> getActiveWorlds() {
  120. return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
  121. .collect(Collectors.toList());
  122. }
  123. /**
  124. * Returns the current island level based on the skyblock world and player.
  125. *
  126. * @param uuid UUID of the player to check
  127. * @param world the world of the island to check the level
  128. * @return player's island level
  129. */
  130. public int getLevel(UUID uuid, String world) {
  131. return this.getSkyblockAPICached().getIslandLevel(uuid, world);
  132. }
  133. /**
  134. * Gathers the owner of an island at a certain location
  135. *
  136. * @param loc Location to check
  137. * @return owner
  138. */
  139. public OfflinePlayer getOwner(Location loc) {
  140. if (this.getSkyblockAPICached().getIslandOwner(loc) == null) {
  141. return null;
  142. }
  143. Optional<UUID> uuid = this.getSkyblockAPICached().getIslandOwner(loc);
  144. if (!uuid.isPresent()) {
  145. return null;
  146. }
  147. OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
  148. return p;
  149. }
  150. public void reload() throws IOException {
  151. reloadConfig();
  152. configHandler.loadConfig();
  153. }
  154. /**
  155. * Acquires a generator config that applies for the given player,
  156. * The result depends on the permission or island level of the player.
  157. *
  158. * @param offlinePlayer the offline player (Usually created using the UUID)
  159. * @param world the skyblock world
  160. * @return the generator config
  161. */
  162. public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer offlinePlayer, String world) {
  163. GeneratorConfig gc = null;
  164. int id = 0;
  165. if (offlinePlayer == null) {
  166. gc = generatorConfigs.get(0);
  167. cacheOreGen(offlinePlayer.getUniqueId(), id);
  168. } else {
  169. int islandLevel = getLevel(offlinePlayer.getUniqueId(), world);
  170. if (offlinePlayer.isOnline()) {
  171. Player realP = offlinePlayer.getPlayer();
  172. if (this.getActiveWorlds().contains(realP.getWorld())) {
  173. for (GeneratorConfig gc2 : generatorConfigs) {
  174. if (gc2 == null) {
  175. continue;
  176. }
  177. if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
  178. && islandLevel >= gc2.unlock_islandLevel) {
  179. // continue
  180. gc = gc2;
  181. id++;
  182. }
  183. }
  184. }
  185. } else {
  186. gc = getCachedGeneratorConfig(offlinePlayer.getUniqueId());
  187. }
  188. }
  189. if (id > 0) {
  190. cacheOreGen(offlinePlayer.getUniqueId(), id - 1);
  191. }
  192. return gc;
  193. }
  194. /**
  195. * Returns all worlds in which the plugin is disabled in (configurable inside the config.yml)
  196. *
  197. * @return A list of world names as string
  198. */
  199. public List<String> getDisabledWorlds() {
  200. return disabledWorlds;
  201. }
  202. /**
  203. * Returns a cached generator config. Useful when an island owner left the server, but a player is still mining at a generator.
  204. *
  205. * @param uuid the owners UUID
  206. * @return the generator config
  207. */
  208. public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
  209. if (cachedOregenConfigs.containsKey(uuid)) {
  210. return generatorConfigs.get(cachedOregenConfigs.get(uuid));
  211. }
  212. return null;
  213. }
  214. /**
  215. * Writes an existing ore generator config to the cache
  216. *
  217. * @param uuid UUID of the owner
  218. * @param configID the ID of the generator config
  219. */
  220. public void cacheOreGen(UUID uuid, int configID) {
  221. cachedOregenConfigs.put(uuid, configID);
  222. }
  223. /**
  224. * Sends a formatted messages to the console, colors supported
  225. *
  226. * @param msg A string using either & or § for colors
  227. */
  228. public void sendConsole(String msg) {
  229. clogger.sendMessage(PREFIX + msg.replace("&", "§"));
  230. }
  231. public List<GeneratorConfig> getGeneratorConfigs() {
  232. return generatorConfigs;
  233. }
  234. public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
  235. this.generatorConfigs = generatorConfigs;
  236. }
  237. /**
  238. * @return the skyblockAPICached
  239. */
  240. public SkyblockAPICached getSkyblockAPICached() {
  241. return skyblockAPICached;
  242. }
  243. }