Main.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. package de.Linus122.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.HashMap;
  8. import java.util.HashSet;
  9. import java.util.List;
  10. import java.util.Set;
  11. import java.util.UUID;
  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. public class Main extends JavaPlugin {
  23. public static List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
  24. public static List<String> disabledWorlds = new ArrayList<String>();
  25. public static ConsoleCommandSender clogger;
  26. private static HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
  27. private static JSONConfig cachedOregenJsonConfig;
  28. public static String activeInWorldName = "";
  29. public void onEnable() {
  30. clogger = getServer().getConsoleSender();
  31. PluginManager pm = Bukkit.getPluginManager();
  32. pm.registerEvents(new Events(), this);
  33. Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
  34. try{
  35. loadConfig();
  36. }catch(IOException e) {
  37. e.printStackTrace();
  38. }
  39. cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() { }.getType(), this);
  40. cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.get();
  41. if(cachedOregenConfigs == null){
  42. cachedOregenConfigs = new HashMap<UUID, Integer>();
  43. }
  44. disabledWorlds = getConfig().getStringList("disabled-worlds");
  45. if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
  46. activeInWorldName = com.wasteofplastic.askyblock.ASkyBlock.getIslandWorld().getName();
  47. clogger.sendMessage("§6[CustomOreGen] §aUsing ASkyBlock as SkyBlock-Plugin");
  48. }else if(Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
  49. activeInWorldName = com.wasteofplastic.acidisland.ASkyBlock.getIslandWorld().getName();
  50. clogger.sendMessage("§6[CustomOreGen] §aUsing AcidIsland as SkyBlock-Plugin");
  51. }else if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
  52. us.talabrek.ultimateskyblock.api.uSkyBlockAPI api = (us.talabrek.ultimateskyblock.api.uSkyBlockAPI) Bukkit.getPluginManager().getPlugin("uSkyBlock");
  53. api.getConfig().getString("options.general.worldName");
  54. activeInWorldName = api.getConfig().getString("options.general.worldName");
  55. clogger.sendMessage("§6[CustomOreGen] §aUsing uSkyBlock as SkyBlock-Plugin");
  56. }
  57. new Metrics(this);
  58. }
  59. public void onDisable() {
  60. cachedOregenJsonConfig.saveToDisk(cachedOregenConfigs);
  61. }
  62. public static World getActiveWorld(){
  63. return Bukkit.getWorld(activeInWorldName);
  64. }
  65. public static int getLevel(UUID uuid) {
  66. if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
  67. return com.wasteofplastic.askyblock.ASkyBlockAPI.getInstance().getIslandLevel(uuid);
  68. }
  69. if(Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
  70. return com.wasteofplastic.acidisland.ASkyBlockAPI.getInstance().getIslandLevel(uuid);
  71. }
  72. if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
  73. if(Bukkit.getPlayer(uuid) != null){
  74. Player p = Bukkit.getPlayer(uuid);
  75. return (int) Math.floor(us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getIslandLevel(p));
  76. }
  77. // Note: The API for getIslandInfo seems to be broken
  78. return (int) Math.floor(us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getIslandInfo(us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getPlayerInfo(uuid)).getLevel());
  79. }
  80. return 0;
  81. }
  82. public static OfflinePlayer getOwner(Location loc) {
  83. Set<Location> set = new HashSet<Location>();
  84. set.add(loc);
  85. UUID uuid = null;
  86. if(Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
  87. uuid = com.wasteofplastic.askyblock.ASkyBlockAPI.getInstance()
  88. .getOwner(com.wasteofplastic.askyblock.ASkyBlockAPI.getInstance().locationIsOnIsland(set, loc));
  89. }else if(Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
  90. uuid = com.wasteofplastic.acidisland.ASkyBlockAPI.getInstance()
  91. .getOwner(com.wasteofplastic.acidisland.ASkyBlockAPI.getInstance().locationIsOnIsland(set, loc));
  92. }else if(Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
  93. String player = us.talabrek.ultimateskyblock.uSkyBlock.getInstance().getIslandInfo(loc).getLeader();
  94. if((Bukkit.getPlayer(player) != null) && (Bukkit.getPlayer(player).getUniqueId() != null)) {
  95. uuid = Bukkit.getOfflinePlayer(player).getUniqueId();
  96. }
  97. }
  98. OfflinePlayer p = Bukkit.getOfflinePlayer(uuid);
  99. return p;
  100. }
  101. public void reload() throws IOException {
  102. reloadConfig();
  103. loadConfig();
  104. }
  105. public void loadConfig() throws IOException {
  106. // Writing default config to data directory
  107. File cfg = new File("plugins/CustomOreGen/config.yml");
  108. File dir = new File("plugins/CustomOreGen/");
  109. if(!dir.exists()) dir.mkdirs();
  110. if(!cfg.exists()){
  111. FileOutputStream writer = new FileOutputStream(new File(getDataFolder() + "/config.yml"));
  112. InputStream out = this.getClassLoader().getResourceAsStream("config.yml");
  113. byte[] linebuffer = new byte[4096];
  114. int lineLength = 0;
  115. while((lineLength = out.read(linebuffer)) > 0)
  116. {
  117. writer.write(linebuffer, 0, lineLength);
  118. }
  119. writer.close();
  120. }
  121. this.reloadConfig();
  122. generatorConfigs = new ArrayList<GeneratorConfig>();
  123. for(String key : this.getConfig().getConfigurationSection("generators").getKeys(false)){
  124. GeneratorConfig gc = new GeneratorConfig();
  125. gc.permission = this.getConfig().getString("generators." + key + ".permission");
  126. gc.unlock_islandLevel = this.getConfig().getInt("generators." + key + ".unlock_islandLevel");
  127. for(String raw : this.getConfig().getStringList("generators." + key + ".blocks")){
  128. try{
  129. if(!raw.contains("!")){
  130. String material = raw.split(":")[0];
  131. double percent = Double.parseDouble(raw.split(":")[1]);
  132. gc.itemList.add(new GeneratorItem(material, (byte) 0, percent));
  133. }else{
  134. String material = raw.split("!")[0];
  135. double percent = Double.parseDouble(raw.split(":")[1]);
  136. int damage = Integer.parseInt(raw.split("!")[1].split(":")[0]);
  137. gc.itemList.add(new GeneratorItem(material, (byte) damage, percent));
  138. }
  139. }catch(Exception e){
  140. e.printStackTrace();
  141. }
  142. }
  143. generatorConfigs.add(gc);
  144. }
  145. //this.saveConfig();
  146. clogger.sendMessage("§6[CustomOreGen] §aLoaded §c" + generatorConfigs.size() + " §agenerators");
  147. }
  148. public static GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p){
  149. GeneratorConfig gc = null;
  150. int id = 0;
  151. if (p == null) {
  152. gc = Main.generatorConfigs.get(0);
  153. cacheOreGen(p.getUniqueId(), id);
  154. } else {
  155. int islandLevel = Main.getLevel(p.getUniqueId());
  156. if(p.isOnline()){
  157. Player realP = p.getPlayer();
  158. if (activeInWorldName.equals(
  159. realP.getWorld().getName())) {
  160. for (GeneratorConfig gc2 : Main.generatorConfigs) {
  161. if (gc2 == null) {
  162. continue;
  163. }
  164. if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0) && islandLevel >= gc2.unlock_islandLevel) {
  165. // Weiter
  166. gc = gc2;
  167. id++;
  168. }
  169. }
  170. }
  171. }else{
  172. gc = getCachedGeneratorConfig(p.getUniqueId());
  173. }
  174. }
  175. if(id > 0){
  176. cacheOreGen(p.getUniqueId(), id - 1);
  177. }
  178. return gc;
  179. }
  180. public static GeneratorConfig getCachedGeneratorConfig(UUID uuid){
  181. if(cachedOregenConfigs.containsKey(uuid)){
  182. return Main.generatorConfigs.get(cachedOregenConfigs.get(uuid));
  183. }
  184. return null;
  185. }
  186. public static void cacheOreGen(UUID uuid, int configID){
  187. cachedOregenConfigs.put(uuid, configID);
  188. }
  189. }