CustomOreGen.java 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package xyz.spaceio.customoregen;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.HashMap;
  9. import java.util.HashSet;
  10. import java.util.List;
  11. import java.util.Optional;
  12. import java.util.Set;
  13. import java.util.UUID;
  14. import java.util.stream.Collectors;
  15. import org.bukkit.Bukkit;
  16. import org.bukkit.Location;
  17. import org.bukkit.Material;
  18. import org.bukkit.OfflinePlayer;
  19. import org.bukkit.World;
  20. import org.bukkit.command.ConsoleCommandSender;
  21. import org.bukkit.entity.Player;
  22. import org.bukkit.plugin.PluginManager;
  23. import org.bukkit.plugin.java.JavaPlugin;
  24. import com.google.gson.reflect.TypeToken;
  25. import de.Linus122.SpaceIOMetrics.Metrics;
  26. import xyz.spaceio.config.ConfigHandler;
  27. import xyz.spaceio.config.JSONConfig;
  28. import xyz.spaceio.hooks.HookASkyBlock;
  29. import xyz.spaceio.hooks.HookAcidIsland;
  30. import xyz.spaceio.hooks.HookBentoBox;
  31. import xyz.spaceio.hooks.HookPlotSquared;
  32. import xyz.spaceio.hooks.HookSkyblockEarth;
  33. import xyz.spaceio.hooks.SkyblockAPIHook;
  34. import xyz.spaceio.hooks.HookuSkyBlock;
  35. public class CustomOreGen extends JavaPlugin {
  36. /*
  37. * Configurations for all generators (defined in the config.yml)
  38. */
  39. private List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
  40. /*
  41. * Disabled world blacklist
  42. */
  43. private List<String> disabledWorlds = new ArrayList<String>();
  44. public List<GeneratorConfig> getGeneratorConfigs() {
  45. return generatorConfigs;
  46. }
  47. public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
  48. this.generatorConfigs = generatorConfigs;
  49. }
  50. /*
  51. * Our logger
  52. */
  53. private ConsoleCommandSender clogger;
  54. /*
  55. * Cache for GeneratorConfig ID's for each player
  56. */
  57. private HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
  58. private JSONConfig cachedOregenJsonConfig;
  59. /*
  60. * API Hook for the corresponding SkyBlock plugin
  61. */
  62. private SkyblockAPIHook skyblockAPI;
  63. /*
  64. * Object that handles the loading process of the config.yml file
  65. */
  66. private ConfigHandler configHandler = new ConfigHandler(this, "plugins/CustomOreGen/config.yml");;
  67. /*
  68. * Prefix for the clogger
  69. */
  70. private final String PREFIX = "§6[CustomOreGen] ";
  71. @Override
  72. public void onEnable() {
  73. clogger = getServer().getConsoleSender();
  74. PluginManager pm = Bukkit.getPluginManager();
  75. pm.registerEvents(new Events(this), this);
  76. this.loadHook();
  77. Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
  78. try {
  79. configHandler.loadConfig();
  80. } catch (IOException e) {
  81. // TODO Auto-generated catch block
  82. e.printStackTrace();
  83. }
  84. cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() {
  85. }.getType(), this);
  86. cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.get();
  87. if (cachedOregenConfigs == null) {
  88. cachedOregenConfigs = new HashMap<UUID, Integer>();
  89. }
  90. disabledWorlds = getConfig().getStringList("disabled-worlds");
  91. new Metrics(this);
  92. }
  93. /**
  94. * creates a new api hook instance for the used skyblock plugin
  95. */
  96. private void loadHook() {
  97. if (Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
  98. skyblockAPI = new HookASkyBlock();
  99. sendConsole("&aUsing ASkyBlock as SkyBlock-Plugin");
  100. } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
  101. skyblockAPI = new HookAcidIsland();
  102. sendConsole("&aUsing AcidIsland as SkyBlock-Plugin");
  103. } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
  104. skyblockAPI = new HookuSkyBlock();
  105. sendConsole("&aUsing uSkyBlock as SkyBlock-Plugin");
  106. } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("BentoBox")) {
  107. skyblockAPI = new HookBentoBox();
  108. sendConsole("&aUsing BentoBox as SkyBlock-Plugin");
  109. } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("SkyBlock")) {
  110. skyblockAPI = new HookSkyblockEarth();
  111. sendConsole("&aUsing SkyblockEarth as SkyBlock-Plugin");
  112. } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlotSquared")) {
  113. skyblockAPI = new HookPlotSquared();
  114. sendConsole("&aUsing PlotSquared as SkyBlock-Plugin");
  115. }
  116. }
  117. @Override
  118. public void onDisable() {
  119. cachedOregenJsonConfig.saveToDisk(cachedOregenConfigs);
  120. }
  121. public List<World> getActiveWorlds() {
  122. return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
  123. .collect(Collectors.toList());
  124. }
  125. public int getLevel(UUID uuid, String world) {
  126. return skyblockAPI.getIslandLevel(uuid, world);
  127. }
  128. public OfflinePlayer getOwner(Location loc) {
  129. if (skyblockAPI.getIslandOwner(loc) == null) {
  130. return null;
  131. }
  132. Optional<UUID> uuid = skyblockAPI.getIslandOwner(loc);
  133. if (!uuid.isPresent()) {
  134. return null;
  135. }
  136. OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
  137. return p;
  138. }
  139. public void reload() throws IOException {
  140. reloadConfig();
  141. configHandler.loadConfig();
  142. }
  143. public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p, String world) {
  144. GeneratorConfig gc = null;
  145. int id = 0;
  146. if (p == null) {
  147. gc = generatorConfigs.get(0);
  148. cacheOreGen(p.getUniqueId(), id);
  149. } else {
  150. int islandLevel = getLevel(p.getUniqueId(), world);
  151. if (p.isOnline()) {
  152. Player realP = p.getPlayer();
  153. if (this.getActiveWorlds().contains(realP.getWorld())) {
  154. for (GeneratorConfig gc2 : generatorConfigs) {
  155. if (gc2 == null) {
  156. continue;
  157. }
  158. if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
  159. && islandLevel >= gc2.unlock_islandLevel) {
  160. // continue
  161. gc = gc2;
  162. id++;
  163. }
  164. }
  165. }
  166. } else {
  167. gc = getCachedGeneratorConfig(p.getUniqueId());
  168. }
  169. }
  170. if (id > 0) {
  171. cacheOreGen(p.getUniqueId(), id - 1);
  172. }
  173. return gc;
  174. }
  175. public List<String> getDisabledWorlds() {
  176. return disabledWorlds;
  177. }
  178. public void setDisabledWorlds(List<String> disabledWorlds) {
  179. this.disabledWorlds = disabledWorlds;
  180. }
  181. public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
  182. if (cachedOregenConfigs.containsKey(uuid)) {
  183. return generatorConfigs.get(cachedOregenConfigs.get(uuid));
  184. }
  185. return null;
  186. }
  187. public void cacheOreGen(UUID uuid, int configID) {
  188. cachedOregenConfigs.put(uuid, configID);
  189. }
  190. public void sendConsole(String msg) {
  191. clogger.sendMessage(PREFIX + msg.replace("&", "§"));
  192. }
  193. }