1.16.x Vault Hook doesn't work.

Discussion in 'Spigot Plugin Development' started by xEmirr, Sep 15, 2020.

  1. Code (Java):
        Economy econ = null;
        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;
        }
       
        public Economy getEco() {
            return econ;
        }
    this is in my main class and i call setupEconomy on enable

    Code (Java):
    Double playerBal = plugin.getEco().getBalance(p);
    this is in another class and gives java.lang.NullPointerException error.

    I can't understand why.
     
  2. Did you call your setupEconomy method? Do you have an economy plugin on the server? Is plugin initialized from wherever you're calling the method?
     
  3. yes, yes and yes
     
  4. Show us the rest of the code and the entire stack trace.
     
  5. show code in getBalance
     
  6. Get balance is vault apis own method it has nothing to do with my code
     
  7. Grafnus

    Grafnus Previously Lord_Grafnus

    If your are using 1.16.3 you have a problem. Vault /somehow) does not support this version. I have found an other recompied Version from an other developer https://www.spigotmc.org/resources/...sentials-economy-mysql-sqlite-much-more.3742/.
    He has a version created for this plugin above. It works fine on 1.16.3 while the offical version does not. In the first few paragraphs there should be a link to the recomplied version to download.

    But be aware that this version is not designed to work properly with other eco systems then CMI

    ~G
     
    • Optimistic Optimistic x 1
  8. I am not using 1.16.3 still at 1.16.2
     
  9. Grafnus

    Grafnus Previously Lord_Grafnus

    Actually I never used 1.16.2. I jumped from 1.16.1 to 1.16.3. I dont know if vault works there. You could try the version I described.
     

  10. Main Class:
    Code (Java):

    import java.io.File;
    import java.io.IOException;

    import org.bukkit.Bukkit;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;

    public class Main extends JavaPlugin{
     
        private static Economy econ = null;
     
        SetupRankupPrices rankupPriceClass;
        public static Main mainClass;
     
        public void instanceClasses() {
            rankupPriceClass = new SetupRankupPrices();
            mainClass = this;
        }
       
        //Level of each player
        private File lFile;
        private YamlConfiguration lYaml;

        @Override
        public void onEnable() {
            instanceClasses();
            setupEconomy();
            setupChat();
         
            final FileConfiguration config = this.getConfig();
            setConfig(config);
            config.options().copyDefaults(true);
            saveConfig();
         
            Bukkit.getPluginManager().registerEvents(new onJoin(this), this);
            Bukkit.getPluginManager().registerEvents(new onChat(this), this);
         
            getCommand("rankup").setExecutor(new RankupCommand(this));
         
            try {
                initiateFiles();
            } catch (IOException e) {
                e.printStackTrace();
            }
         
            rankupPriceClass.SetupPrices();
        }
     
        public void setConfig(FileConfiguration conf) {
            conf.addDefault("numberOfLevels", 100);
            conf.addDefault("firstLevelPrice", 1000.0);
        }
       
       
        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;
        }
       

        public static Chat chat = null;
     
        private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }

        public YamlConfiguration getLYaml() { return lYaml; }
        public File getLFile() { return lFile; }
     
        public void initiateFiles() throws IOException {
            lFile = new File(Bukkit.getServer().getPluginManager().getPlugin("MineneticLevels").getDataFolder(), "levels.yml");
            if (!lFile.exists()) {
                lFile.createNewFile();
            }
         
            lYaml = YamlConfiguration.loadConfiguration(lFile);
        }
     
        public static Economy getEco() {
            return econ;
        }
    }
     
    The class where i try to access the eco:
    Code (Java):
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class RankupCommand implements CommandExecutor {

        Main plugin;
        public RankupCommand(Main instance) {
            plugin = instance;
        }
       
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                Double playerBal = Main.getEco().getBalance(p);
                int playerLevel = Integer.valueOf(plugin.getLYaml().getString(p.getName()));
                if (playerLevel != plugin.getConfig().getInt("numberOfLevels")) {
                    Double price = SetupRankupPrices.rankupPrices.get(playerLevel + 1);
                    if (playerBal >= price) {
                        Main.getEco().withdrawPlayer(p, price);
                        plugin.getLYaml().set(p.getName(), String.valueOf(playerLevel + 1));
                    } else {
                       
                    }
                } else {
                   
                }
            }
            return false;
        }

    }
    Console when i run /rankup:
    Code (Text):
    16.09 09:42:43 [Server] Server thread/INFO xEmir issued server command: /rankup
    16.09 09:42:43 [Server] Server thread/ERROR null
    16.09 09:42:43 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'rankup' in plugin MineneticLevels v1
    16.09 09:42:43 [Server] INFO     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchCommand(CraftServer.java:756) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.PlayerConnection.handleCommand(PlayerConnection.java:1697) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1540) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     at java.lang.Thread.run(Thread.java:748) [?:1.8.0_211]
    16.09 09:42:43 [Server] INFO Caused by: java.lang.NullPointerException
    16.09 09:42:43 [Server] INFO     at com.emiv.mineneticlevels.RankupCommand.onCommand(RankupCommand.java:19) ~[?:?]
    16.09 09:42:43 [Server] INFO     at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[custom.jar:git-Spigot-379750e-43c7ff9]
    16.09 09:42:43 [Server] INFO     ... 18 more
     
  11. I use vault in another plugin i really don't know whats wrong here
     
  12. Grafnus

    Grafnus Previously Lord_Grafnus

    Which line generates the error? If I count to 19 I dont get the eco line :eek:
     
  13. Double playerBal = Main.getEco().getBalance(p);
     
  14. Grafnus

    Grafnus Previously Lord_Grafnus

    Can you do /vaultinfo (I dont remember the command right now) and have a look, if there is a econ plugin that hooks into vault?
     
  15. there is one which i coded my self it works just fine
     
  16. Vault won't automatically support your plugin, you will have to register it as a service yourself. Have you done that?
     
  17. yes. i use it just like any other economy plugin its hooked to vault
     
  18. Is your plugin enabling after Vault? Check for the response of setupEconomy, for whether Vault was not loaded, or the service was not found.
     
  19. I would suggest that you re-visit the Vault API and take note of the section Implementing Vault
    Your 'onEnable' provides
    Code (Text):
            setupEconomy();
            setupChat();
    But does not take any action upon these events.


    Sorry I missed out on reading part of the code.

    Does the plugin.yml 'depend: vault' ?
     
    #19 Goldentoenail, Sep 17, 2020
    Last edited: Sep 18, 2020
  20. it doesn't but setupChat works? i use vault chat without any errors