123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231 |
- package xyz.spaceio.customoregen;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Optional;
- import java.util.Set;
- import java.util.UUID;
- import java.util.stream.Collectors;
- import org.bukkit.Bukkit;
- import org.bukkit.Location;
- import org.bukkit.Material;
- import org.bukkit.OfflinePlayer;
- import org.bukkit.World;
- import org.bukkit.command.ConsoleCommandSender;
- import org.bukkit.entity.Player;
- import org.bukkit.plugin.PluginManager;
- import org.bukkit.plugin.java.JavaPlugin;
- import com.google.gson.reflect.TypeToken;
- import de.Linus122.SpaceIOMetrics.Metrics;
- import xyz.spaceio.config.ConfigHandler;
- import xyz.spaceio.config.JSONConfig;
- import xyz.spaceio.hooks.HookASkyBlock;
- import xyz.spaceio.hooks.HookAcidIsland;
- import xyz.spaceio.hooks.HookBentoBox;
- import xyz.spaceio.hooks.HookPlotSquared;
- import xyz.spaceio.hooks.HookSkyblockEarth;
- import xyz.spaceio.hooks.SkyblockAPIHook;
- import xyz.spaceio.hooks.HookuSkyBlock;
- public class CustomOreGen extends JavaPlugin {
- /*
- * Configurations for all generators (defined in the config.yml)
- */
- private List<GeneratorConfig> generatorConfigs = new ArrayList<GeneratorConfig>();
- /*
- * Disabled world blacklist
- */
- private List<String> disabledWorlds = new ArrayList<String>();
- public List<GeneratorConfig> getGeneratorConfigs() {
- return generatorConfigs;
- }
- public void setGeneratorConfigs(List<GeneratorConfig> generatorConfigs) {
- this.generatorConfigs = generatorConfigs;
- }
- /*
- * Our logger
- */
- private ConsoleCommandSender clogger;
- /*
- * Cache for GeneratorConfig ID's for each player
- */
- private HashMap<UUID, Integer> cachedOregenConfigs = new HashMap<UUID, Integer>();
- private JSONConfig cachedOregenJsonConfig;
- /*
- * API Hook for the corresponding SkyBlock plugin
- */
- private SkyblockAPIHook skyblockAPI;
- /*
- * Object that handles the loading process of the config.yml file
- */
- private ConfigHandler configHandler = new ConfigHandler(this, "plugins/CustomOreGen/config.yml");;
- /*
- * Prefix for the clogger
- */
- private final String PREFIX = "§6[CustomOreGen] ";
- @Override
- public void onEnable() {
- clogger = getServer().getConsoleSender();
- PluginManager pm = Bukkit.getPluginManager();
- pm.registerEvents(new Events(this), this);
- this.loadHook();
- Bukkit.getPluginCommand("customoregen").setExecutor(new Cmd(this));
- try {
- configHandler.loadConfig();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- cachedOregenJsonConfig = new JSONConfig(cachedOregenConfigs, new TypeToken<HashMap<UUID, Integer>>() {
- }.getType(), this);
- cachedOregenConfigs = (HashMap<UUID, Integer>) cachedOregenJsonConfig.get();
- if (cachedOregenConfigs == null) {
- cachedOregenConfigs = new HashMap<UUID, Integer>();
- }
- disabledWorlds = getConfig().getStringList("disabled-worlds");
- new Metrics(this);
- }
- /**
- * creates a new api hook instance for the used skyblock plugin
- */
- private void loadHook() {
- if (Bukkit.getServer().getPluginManager().isPluginEnabled("ASkyBlock")) {
- skyblockAPI = new HookASkyBlock();
- sendConsole("&aUsing ASkyBlock as SkyBlock-Plugin");
- } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("AcidIsland")) {
- skyblockAPI = new HookAcidIsland();
- sendConsole("&aUsing AcidIsland as SkyBlock-Plugin");
- } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("uSkyBlock")) {
- skyblockAPI = new HookuSkyBlock();
- sendConsole("&aUsing uSkyBlock as SkyBlock-Plugin");
- } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("BentoBox")) {
- skyblockAPI = new HookBentoBox();
- sendConsole("&aUsing BentoBox as SkyBlock-Plugin");
- } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("SkyBlock")) {
- skyblockAPI = new HookSkyblockEarth();
- sendConsole("&aUsing SkyblockEarth as SkyBlock-Plugin");
- } else if (Bukkit.getServer().getPluginManager().isPluginEnabled("PlotSquared")) {
- skyblockAPI = new HookPlotSquared();
- sendConsole("&aUsing PlotSquared as SkyBlock-Plugin");
- }
- }
- @Override
- public void onDisable() {
- cachedOregenJsonConfig.saveToDisk(cachedOregenConfigs);
- }
- public List<World> getActiveWorlds() {
- return Arrays.stream(skyblockAPI.getSkyBlockWorldNames()).map(v -> Bukkit.getWorld(v))
- .collect(Collectors.toList());
- }
- public int getLevel(UUID uuid, String world) {
- return skyblockAPI.getIslandLevel(uuid, world);
- }
- public OfflinePlayer getOwner(Location loc) {
- if (skyblockAPI.getIslandOwner(loc) == null) {
- return null;
- }
- Optional<UUID> uuid = skyblockAPI.getIslandOwner(loc);
- if (!uuid.isPresent()) {
- return null;
- }
- OfflinePlayer p = Bukkit.getOfflinePlayer(uuid.get());
- return p;
- }
- public void reload() throws IOException {
- reloadConfig();
- configHandler.loadConfig();
- }
- public GeneratorConfig getGeneratorConfigForPlayer(OfflinePlayer p, String world) {
- GeneratorConfig gc = null;
- int id = 0;
- if (p == null) {
- gc = generatorConfigs.get(0);
- cacheOreGen(p.getUniqueId(), id);
- } else {
- int islandLevel = getLevel(p.getUniqueId(), world);
- if (p.isOnline()) {
- Player realP = p.getPlayer();
- if (this.getActiveWorlds().contains(realP.getWorld())) {
- for (GeneratorConfig gc2 : generatorConfigs) {
- if (gc2 == null) {
- continue;
- }
- if ((realP.hasPermission(gc2.permission) || gc2.permission.length() == 0)
- && islandLevel >= gc2.unlock_islandLevel) {
- // continue
- gc = gc2;
- id++;
- }
- }
- }
- } else {
- gc = getCachedGeneratorConfig(p.getUniqueId());
- }
- }
- if (id > 0) {
- cacheOreGen(p.getUniqueId(), id - 1);
- }
- return gc;
- }
- public List<String> getDisabledWorlds() {
- return disabledWorlds;
- }
- public void setDisabledWorlds(List<String> disabledWorlds) {
- this.disabledWorlds = disabledWorlds;
- }
- public GeneratorConfig getCachedGeneratorConfig(UUID uuid) {
- if (cachedOregenConfigs.containsKey(uuid)) {
- return generatorConfigs.get(cachedOregenConfigs.get(uuid));
- }
- return null;
- }
- public void cacheOreGen(UUID uuid, int configID) {
- cachedOregenConfigs.put(uuid, configID);
- }
- public void sendConsole(String msg) {
- clogger.sendMessage(PREFIX + msg.replace("&", "§"));
- }
- }
|