Solved Problems with Vault API

Discussion in 'Plugin Development' started by Scrapnix, Sep 3, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Scrapnix

    Hey there,
    I made a plugin which allows players to perform a command while rightclicking on a NPC about another plugin. I used the Vault API for economy and for example if you click on an item something will be bought and money will be withdrawed. The problem is, that something wents wrong with the Vault API and the console puts out this error:
    Code:
    [07:13:22 ERROR]: Could not pass event InventoryClickEvent to NPCCommands v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:1630) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:31) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInWindowClick.a(SourceFile:9) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_131]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_131]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Thread.java:748) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
            at com.scrap.main.Main.onInventoryClick(Main.java:413) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_131]
            at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_131]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_131]
            at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_131]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot-1.8.8.jar:git-Spigot-db6de12-18fbb24]
            ... 15 more
    This is the error line of code:
    Code:
    if (econ.getBalance(player.getName()) > 10) {
    
    My initiation method (I think there is the error):
    Code:
        private boolean setupEconomy() {
            if (getServer().getPluginManager().getPlugin("Vault") == null) {
                return false;
            }
            RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
            if (rsp == null) {
                return false;
            }
            econ = rsp.getProvider();
            return econ != null;
        }
      
        private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }
      
        private boolean setupPermissions() {
            RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
            perms = rsp.getProvider();
            return perms != null;
        }
      
        public static Economy getEcononomy() {
            return econ;
        }
      
        public static Permission getPermissions() {
            return perms;
        }
      
        public static Chat getChat() {
            return chat;
        }
          
    And my onEnable method:
    Code:
        private static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;
    
        public void onEnable() {
            Bukkit.getPluginManager().registerEvents(this, this);
            worlds.add("utopialife");
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
            setupChat();
        }
    I hope someone can help me :)
     
  2. Offline

    Zombie_Striker

    @Scrapnix
    Try null checking the values:
    1. Is econ null? If so, you will have to check if the setupEcon method is working correctly.
    2. Is the player null? if so, make sure it is not null.
    3. Does the player have a balance. Although doing so should return -1, it may be that that is throwing the NPE.
     
  3. Offline

    Horsey

    @Zombie_Striker If the method did throw a NPE, the first class in the stacktrace would be somewhere in vault/the economy plugin

    @Scrapnix You're setupEcon method is correct, maybe you don't have an economy plugin installed? Vault isn't a stand-alone economy plugin, it requires an external one like iConomy.
     
    MasterDoctor likes this.
  4. Offline

    Scrapnix

    Hey there, I found the problem. Smth. was wrong in my onEnable() method. I don't know why, but after deleting this method it worked! Thx :)

    //closed
     
Thread Status:
Not open for further replies.

Share This Page