Solved WorldGuard SoftDepend

Discussion in 'Spigot Plugin Development' started by OkDexter12, Jan 24, 2020.

  1. I'm making a plugin that checks if worldguard has a flag set to deny in a region. It works, but I want it to be able to work without worldguard loaded as well so I set worldguard to soft depend. However, when I remove worldguard from my plugins on starting my server, I get this error:

    Code (Text):
    [21:13:59] [Server thread/ERROR]: Error occurred while enabling VanillaAmplified v1.0 (Is it up to date?)
    java.lang.NoClassDefFoundError: com/sk89q/worldguard/protection/flags/Flag
            at vanillaamplified.main.VanillaMain.onEnable(VanillaMain.java:79) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:352) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:416) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugin(CraftServer.java:461) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at org.bukkit.craftbukkit.v1_14_R1.CraftServer.enablePlugins(CraftServer.java:375) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at net.minecraft.server.v1_14_R1.MinecraftServer.a(MinecraftServer.java:439) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at net.minecraft.server.v1_14_R1.DedicatedServer.init(DedicatedServer.java:258) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:764) [spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_191]
    Caused by: java.lang.ClassNotFoundException: com.sk89q.worldguard.protection.flags.Flag
            at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.8.0_191]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:135) ~[spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:81) ~[spigot-1.14.3-R0.1-SNAPSHOT.jar:git-Spigot-d05d3c1-7ffb2a2]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_191]
            at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.8.0_191]
            ... 10 more

    This is my code:

    Code (Text):
    public static boolean noFlag(Location l, String flag) {
            if (main.getServer().getPluginManager().getPlugin("WorldGuard") != null) {
                com.sk89q.worldguard.protection.flags.StateFlag f = null;
                if(flag.equals("PVP")) {
                    f = com.sk89q.worldguard.protection.flags.Flags.PVP;
                }
                else if(flag.equals("MOB_DAMAGE")) {
                    f = com.sk89q.worldguard.protection.flags.Flags.MOB_DAMAGE;
                }
                com.sk89q.worldedit.util.Location loc = com.sk89q.worldedit.bukkit.BukkitAdapter.adapt(l);
                com.sk89q.worldguard.protection.regions.RegionContainer container = com.sk89q.worldguard.WorldGuard.getInstance().getPlatform().getRegionContainer();
                com.sk89q.worldguard.protection.regions.RegionQuery query = container.createQuery();
                com.sk89q.worldguard.protection.ApplicableRegionSet set = query.getApplicableRegions(loc);
                for(com.sk89q.worldguard.protection.regions.ProtectedRegion region : set) {
                    if(region.getFlag(f) == com.sk89q.worldguard.protection.flags.StateFlag.State.DENY) {
                        return false;
                    }
                }
            }
            return true;
        }
    And this is line 79 where it says the error happens:

    Code (Text):
    this.getServer().getPluginManager().registerEvents(new CombatMain(), this);
    Does soft depend not work how I think it does or am I making some other mistake in my plugin? Thanks for any help.
     
  2. you're calling worldguard methods without worldguard...

    they way you're suppose to design this is you initialize a class dedicated to worldguard if worldguard exists. you check if it exists but because of how class loading works, it loads those classes even if it has a chance of being run or not.
     
    • Like Like x 1
  3. Ah I see, thanks didn't know that.