CustomOreGen.java 7.2 KB

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