Search in sources :

Example 1 with WorldGuardInterface

use of me.wiefferink.areashop.interfaces.WorldGuardInterface in project AreaShop by NLthijs48.

the class AreaShop method onEnable.

/**
 * Called on start or reload of the server.
 */
public void onEnable() {
    AreaShop.instance = this;
    Do.init(this);
    managers = new HashSet<>();
    boolean error = false;
    // Check if WorldGuard is present
    String wgVersion = null;
    String rawVersion = null;
    int major = 0;
    int minor = 0;
    int fixes = 0;
    Integer build = null;
    Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
    if (plugin == null || !(plugin instanceof WorldGuardPlugin) || !plugin.isEnabled()) {
        error("WorldGuard plugin is not present or has not loaded correctly");
        error = true;
    } else {
        worldGuard = (WorldGuardPlugin) plugin;
        // Get correct WorldGuardInterface (handles things that changed version to version)
        try {
            rawVersion = worldGuard.getDescription().getVersion();
            if (rawVersion.contains("-SNAPSHOT;")) {
                String buildNumber = rawVersion.substring(rawVersion.indexOf("-SNAPSHOT;") + 10, rawVersion.length());
                if (buildNumber.contains("-")) {
                    buildNumber = buildNumber.substring(0, buildNumber.indexOf("-"));
                    try {
                        build = Integer.parseInt(buildNumber);
                    } catch (NumberFormatException e) {
                        warn("Could not correctly parse the build of WorldGuard, raw version: " + rawVersion + ", buildNumber: " + buildNumber);
                    }
                }
            }
            // Clear stuff from the version string that is not a number
            String[] versionParts = rawVersion.split("\\.");
            for (int i = 0; i < versionParts.length; i++) {
                Pattern pattern = Pattern.compile("^\\d+");
                Matcher matcher = pattern.matcher(versionParts[i]);
                if (matcher.find()) {
                    versionParts[i] = matcher.group();
                }
            }
            // Find major, minor and fix numbers
            try {
                if (versionParts.length > 0) {
                    major = Integer.parseInt(versionParts[0]);
                }
                if (versionParts.length > 1) {
                    minor = Integer.parseInt(versionParts[1]);
                }
                if (versionParts.length > 2) {
                    fixes = Integer.parseInt(versionParts[2]);
                }
            } catch (NumberFormatException e) {
                warn("Something went wrong while parsing WorldGuard version number: " + rawVersion);
            }
            // Determine correct implementation to use
            if (worldGuard.getDescription().getVersion().startsWith("5.")) {
                wgVersion = "5";
            } else if (major == 6 && minor == 1 && fixes < 3) {
                wgVersion = "6";
            } else {
                if (build != null && build == 1672) {
                    error = true;
                    error("Build 1672 of WorldGuard is broken, update to a later build or a stable version!");
                } else if (build != null && build < 1672) {
                    wgVersion = "6";
                } else {
                    wgVersion = "6_1_3";
                }
            }
        } catch (Exception e) {
            // If version detection fails, at least try to load the latest version
            wgVersion = "6_1_3";
        }
        // Load chosen implementation
        try {
            final Class<?> clazz = Class.forName("me.wiefferink.areashop.handlers.WorldGuardHandler" + wgVersion);
            // Check if we have a NMSHandler class at that location.
            if (WorldGuardInterface.class.isAssignableFrom(clazz)) {
                // Make sure it actually implements WorldGuardInterface
                // Set our handler
                worldGuardInterface = (WorldGuardInterface) clazz.getConstructor(AreaShopInterface.class).newInstance(this);
            }
        } catch (final Exception e) {
            error("Could not load the handler for WorldGuard (tried to load " + wgVersion + "), report this problem to the author:" + ExceptionUtils.getStackTrace(e));
            error = true;
            wgVersion = null;
        }
    }
    // Check if WorldEdit is present
    String weVersion = null;
    plugin = getServer().getPluginManager().getPlugin("WorldEdit");
    if (plugin == null || !(plugin instanceof WorldEditPlugin) || !plugin.isEnabled()) {
        error("WorldEdit plugin is not present or has not loaded correctly");
        error = true;
    } else {
        worldEdit = (WorldEditPlugin) plugin;
        // Get correct WorldEditInterface (handles things that changed version to version)
        if (worldEdit.getDescription().getVersion().startsWith("5.")) {
            weVersion = "5";
        } else {
            weVersion = "6";
        }
        try {
            final Class<?> clazz = Class.forName("me.wiefferink.areashop.handlers.WorldEditHandler" + weVersion);
            // Check if we have a NMSHandler class at that location.
            if (WorldEditInterface.class.isAssignableFrom(clazz)) {
                // Make sure it actually implements WorldEditInterface
                // Set our handler
                worldEditInterface = (WorldEditInterface) clazz.getConstructor(AreaShopInterface.class).newInstance(this);
            }
        } catch (final Exception e) {
            error("Could not load the handler for WorldEdit (tried to load " + weVersion + "), report this problem to the author: " + ExceptionUtils.getStackTrace(e));
            error = true;
            weVersion = null;
        }
    }
    // Check if Vault is present
    if (getServer().getPluginManager().getPlugin("Vault") == null) {
        error("Vault plugin is not present or has not loaded correctly");
        error = true;
    }
    // Load all data from files and check versions
    fileManager = new FileManager();
    managers.add(fileManager);
    error = error | !fileManager.loadFiles(false);
    // Print loaded version of WG and WE in debug
    if (wgVersion != null) {
        AreaShop.debug("Loaded WorldGuardHandler" + wgVersion + " (raw version: " + rawVersion + ", major:" + major + ", minor:" + minor + ", fixes:" + fixes + ", build:" + build + ")");
    }
    if (weVersion != null) {
        AreaShop.debug("Loaded WorldEditHandler" + weVersion);
    }
    setupLanguageManager();
    if (error) {
        error("The plugin has not started, fix the errors listed above");
    } else {
        featureManager = new FeatureManager();
        managers.add(featureManager);
        // Register the event listeners
        getServer().getPluginManager().registerEvents(new PlayerLoginLogoutListener(this), this);
        setupTasks();
        // Startup the CommandManager (registers itself for the command)
        commandManager = new CommandManager();
        managers.add(commandManager);
        // Create a signLinkerManager
        signLinkerManager = new SignLinkerManager();
        managers.add(signLinkerManager);
        // Enable Metrics if config allows it
        if (getConfig().getBoolean("sendStats")) {
            Analytics.start();
        }
        // Register dynamic permission (things declared in config)
        registerDynamicPermissions();
        // Don't initialize the updatechecker if disabled in the config
        if (getConfig().getBoolean("checkForUpdates")) {
            githubUpdateCheck = new GithubUpdateCheck(AreaShop.getInstance(), "NLThijs48", "AreaShop").withVersionComparator((latestVersion, currentVersion) -> !cleanVersion(latestVersion).equals(cleanVersion(currentVersion))).checkUpdate((result) -> {
                AreaShop.debug("Update check result:", result);
                if (!result.hasUpdate()) {
                    return;
                }
                AreaShop.info("Update from AreaShop V" + cleanVersion(result.getCurrentVersion()) + " to AreaShop V" + cleanVersion(result.getLatestVersion()) + " available, get the latest version at https://www.spigotmc.org/resources/areashop.2991/");
                for (Player player : Utils.getOnlinePlayers()) {
                    notifyUpdate(player);
                }
            });
        }
    }
}
Also used : HandlerList(org.bukkit.event.HandlerList) Plugin(org.bukkit.plugin.Plugin) StringUtils(org.apache.commons.lang.StringUtils) Message(me.wiefferink.interactivemessenger.processing.Message) ConfigurationSection(org.bukkit.configuration.ConfigurationSection) CommandManager(me.wiefferink.areashop.managers.CommandManager) AreaShopInterface(me.wiefferink.areashop.interfaces.AreaShopInterface) Player(org.bukkit.entity.Player) HashSet(java.util.HashSet) PlayerLoginLogoutListener(me.wiefferink.areashop.listeners.PlayerLoginLogoutListener) Matcher(java.util.regex.Matcher) Analytics(me.wiefferink.areashop.tools.Analytics) SignLinkerManager(me.wiefferink.areashop.managers.SignLinkerManager) Nullable(javax.annotation.Nullable) Bukkit(org.bukkit.Bukkit) GithubUpdateCheck(me.wiefferink.areashop.tools.GithubUpdateCheck) CommandSender(org.bukkit.command.CommandSender) WorldEditInterface(me.wiefferink.areashop.interfaces.WorldEditInterface) Economy(net.milkbowl.vault.economy.Economy) Do(me.wiefferink.bukkitdo.Do) WorldGuardInterface(me.wiefferink.areashop.interfaces.WorldGuardInterface) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) Set(java.util.Set) FileManager(me.wiefferink.areashop.managers.FileManager) Utils(me.wiefferink.areashop.tools.Utils) OfflinePlayer(org.bukkit.OfflinePlayer) Permission(org.bukkit.permissions.Permission) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) WorldEditPlugin(com.sk89q.worldedit.bukkit.WorldEditPlugin) List(java.util.List) FeatureManager(me.wiefferink.areashop.managers.FeatureManager) YamlConfiguration(org.bukkit.configuration.file.YamlConfiguration) Pattern(java.util.regex.Pattern) LanguageManager(me.wiefferink.interactivemessenger.source.LanguageManager) Manager(me.wiefferink.areashop.managers.Manager) RegisteredServiceProvider(org.bukkit.plugin.RegisteredServiceProvider) Pattern(java.util.regex.Pattern) Player(org.bukkit.entity.Player) OfflinePlayer(org.bukkit.OfflinePlayer) Matcher(java.util.regex.Matcher) FileManager(me.wiefferink.areashop.managers.FileManager) PlayerLoginLogoutListener(me.wiefferink.areashop.listeners.PlayerLoginLogoutListener) WorldEditPlugin(com.sk89q.worldedit.bukkit.WorldEditPlugin) CommandManager(me.wiefferink.areashop.managers.CommandManager) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) SignLinkerManager(me.wiefferink.areashop.managers.SignLinkerManager) GithubUpdateCheck(me.wiefferink.areashop.tools.GithubUpdateCheck) FeatureManager(me.wiefferink.areashop.managers.FeatureManager) Plugin(org.bukkit.plugin.Plugin) WorldGuardPlugin(com.sk89q.worldguard.bukkit.WorldGuardPlugin) JavaPlugin(org.bukkit.plugin.java.JavaPlugin) WorldEditPlugin(com.sk89q.worldedit.bukkit.WorldEditPlugin) AreaShopInterface(me.wiefferink.areashop.interfaces.AreaShopInterface)

Aggregations

WorldEditPlugin (com.sk89q.worldedit.bukkit.WorldEditPlugin)1 WorldGuardPlugin (com.sk89q.worldguard.bukkit.WorldGuardPlugin)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 Nullable (javax.annotation.Nullable)1 AreaShopInterface (me.wiefferink.areashop.interfaces.AreaShopInterface)1 WorldEditInterface (me.wiefferink.areashop.interfaces.WorldEditInterface)1 WorldGuardInterface (me.wiefferink.areashop.interfaces.WorldGuardInterface)1 PlayerLoginLogoutListener (me.wiefferink.areashop.listeners.PlayerLoginLogoutListener)1 CommandManager (me.wiefferink.areashop.managers.CommandManager)1 FeatureManager (me.wiefferink.areashop.managers.FeatureManager)1 FileManager (me.wiefferink.areashop.managers.FileManager)1 Manager (me.wiefferink.areashop.managers.Manager)1 SignLinkerManager (me.wiefferink.areashop.managers.SignLinkerManager)1 Analytics (me.wiefferink.areashop.tools.Analytics)1 GithubUpdateCheck (me.wiefferink.areashop.tools.GithubUpdateCheck)1 Utils (me.wiefferink.areashop.tools.Utils)1