CustomOreGen.java 7.2 KB

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