Solved What do I need to do to complete the code?

Discussion in 'Spigot Plugin Development' started by Wilsoon, Jan 14, 2020.

Thread Status:
Not open for further replies.
  1. Code (Text):

    [18:46:55 ERROR]: Error occurred while enabling WERegionTeleport v1.0 (Is it up to date?)
    java.lang.NullPointerException
            at com.gmail.calorious.wergteleport.Main.onEnable(Main.java:16) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.s(MinecraftServer.java:414) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.k(MinecraftServer.java:378) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.a(MinecraftServer.java:333) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.init(DedicatedServer.java:263) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:525) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_231]

    Code:
    Code (Java):

    package com.gmail.calorious.wergteleport;

    import org.bukkit.plugin.java.JavaPlugin;

    import org.bukkit.ChatColor;

    import org.bukkit.plugin.PluginDescriptionFile;

    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
       
        @Override
       public void onEnable() {
       getServer().getPluginManager().registerEvents(new EventHandling(), this);
       getLogger().info(ChatColor.GOLD + "Hooking to WorldEdit...");
       if(getServer().getPluginManager().getPlugin("WorldEdit").isEnabled() == true) {
           getLogger().info(ChatColor.GREEN + "Sucessfully hooked to Worldedit!");
       }
       else if(getServer().getPluginManager().getPlugin("WorldEdit").isEnabled() == false) {
               getLogger().info(ChatColor.RED + "Failed to hook to Worldedit!");
               getLogger().info(ChatColor.RED + "This plugin requires Worldedit to function.");
               getLogger().severe(ChatColor.DARK_RED + "STOPPING PLUGIN: " + getDescription().getName());
               getLogger().severe(ChatColor.DARK_RED + "Reason: Dependent Plugin not found.");
               getLogger().severe(ChatColor.DARK_RED + "Required Plugins: " + getDescription().getDepend());
               getPluginLoader().disablePlugin(this);
           }
    }
    }
     
     
  2. WorldEdit is either
    a) Not installed
    or
    b) You have not defined WorldEdit as a dependency in your plugin.yml.
     
  3. SteelPhoenix

    Moderator

    PluginManager#getPlugin() can return null (which is why it is annotated with @Nullable) The JavaDoc tells you this.
     
  4. I didn't install it in because I was planning to test the getLogger#severe if Worldedit isn't installed. However it just returns that
     
  5. Where would I put it in my code though..
     
  6. Code (Java):
    if(getServer().getPluginManager().getPlugin("WorldEdit") == null) {
           // stuff here
       }
     
  7. Hmm
    (I added the new onDisable() part)
    Code (Java):
    package com.gmail.calorious.wergteleport;

    import org.bukkit.plugin.java.JavaPlugin;

    import org.bukkit.ChatColor;

    import org.bukkit.plugin.PluginDescriptionFile;

    @SuppressWarnings("unused")
    public class Main extends JavaPlugin {
       
        @Override
       public void onEnable() {
       getServer().getPluginManager().registerEvents(new EventHandling(), this);
       getLogger().info(ChatColor.GOLD + "Hooking to WorldEdit...");
       if(getServer().getPluginManager().getPlugin("WorldEdit") == null) {
           getLogger().info(ChatColor.GREEN + "Sucessfully hooked to Worldedit!");
       }
       else if(getServer().getPluginManager().getPlugin("WorldEdit") == null) {
               getLogger().info(ChatColor.RED + "Failed to hook to Worldedit!");
               getLogger().info(ChatColor.RED + "This plugin requires Worldedit to function.");
               getLogger().severe(ChatColor.DARK_RED + "STOPPING PLUGIN: " + getDescription().getName());
               getLogger().severe(ChatColor.DARK_RED + "Reason: Dependent Plugin not found.");
               getLogger().severe(ChatColor.DARK_RED + "Required Plugins: " + getDescription().getDepend());
               getPluginLoader().disablePlugin(this);
           }
        }
        @Override
        public void onDisable() {
            getLogger().info(ChatColor.DARK_RED + "[WERegionTeleport Disabled]");
            getLogger().info(ChatColor.RED + " Plugin has disabled successfully!");
        }
    }
    The plugin doesn't seem to be able to scan for WorldEdit anymore, it always goes for the first one which is "Successfully Hooked!", even though WorldEdit is not installed.
     
  8. You implemented the code incorrectly. Do a check to see if world edit is null, if it is, says failed to hook.

    Code (Java):
    if(getServer().getPluginManager().getPlugin("WorldEdit") == null) {
           // World Edit is NULL, just like the if statement says.
       }
       else {
          //World Edit is NOT NULL, Please learn if statements and how they work.
        }
     
    • Agree Agree x 1
  9. You checked twice if WorldEdit is null. basically what you did is:
    Code (Text):
    if (WorldEdit == null) {
        log("successfully hooked into WorldEdit!")
    } else if (WorldEdit == null) {
        log("FatalError bla bla")
    }
    simply remove the second if-condition

    Edit: too slow...
     
  10. I actually do know how they work, just that I dont use it as much cuz I just started Java and would usually want to use it in like an "exact" way so instead of just

    Code (Text):
    if(WorldEdit = null) {

    } else {

    }

     
    I would wanna use
    Code (Text):
    if(getServer().getPluginManager().getPlugin("WorldEdit") = Found) {} else (getServer().getPluginManager().getPlugin("WorldEdit") = Not Found) {}
     
  11. Correct
     
  12. It works now, thank you everyone for your help!
     
  13. Close Thread.
     
Thread Status:
Not open for further replies.