Solved Error hooking into Vault

Discussion in 'Spigot Plugin Development' started by Stellrow, Jul 17, 2018.

  1. So im trying to hook into vault to use its economy in my plugin but i get an error when loading
    The plugin itself works everything is working but the economy part

    Here is what im using at the end of the event:
    Code (Text):
    economy.depositPlayer(killer, 10);
    Code (Text):
    [10:08:53] [Server thread/ERROR]: Error occurred while enabling Slaughter v0.1 (Is it up to date?)
    java.lang.NullPointerException
        at main.Main.setupChat(Main.java:61) ~[?:?]
        at main.Main.onEnable(Main.java:40) ~[?:?]
        at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:321) ~[spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:340) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:405) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.loadPlugin(CraftServer.java:357) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.enablePlugins(CraftServer.java:317) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.reload(CraftServer.java:741) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.Bukkit.reload(Bukkit.java:535) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:25) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchServerCommand(CraftServer.java:627) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.aO(DedicatedServer.java:412) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:375) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-21fe707-e1ebe52]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_161]
    Here is the main where vault is integrated
    Code (Text):
    package main;


    import java.util.logging.Logger;

    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;

    import net.md_5.bungee.api.ChatColor;

    public class Main extends JavaPlugin implements Listener {
     
        private static final Logger log = Logger.getLogger("Minecraft");
        private static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;
     
        public void onEnable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "INFO: " + ChatColor.WHITE + "Slaughter a fost activat");
            Bukkit.getPluginManager().registerEvents(new DeathEvent(), this);
            Bukkit.getPluginManager().registerEvents(new OnSpawnEvent(), this);
            Bukkit.getPluginManager().registerEvents(new OnDeathEvent(), this);
         
            if (!setupEconomy() ) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
            setupChat();
         
        }
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.GREEN + "INFO: " + ChatColor.WHITE + "Slaughter a fost dezactivat");
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }
        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;
        }
     
        @SuppressWarnings("deprecation")
        public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args) {
            if(!(sender instanceof Player)) {
                log.info("Only players are supported for this Example Plugin, but you should not do this!!!");
                return true;
            }
         
            Player player = (Player) sender;
         
            if(command.getLabel().equals("test-economy")) {
                // Lets give the player 1.05 currency (note that SOME economic plugins require rounding!)
                sender.sendMessage(String.format("You have %s", econ.format(econ.getBalance(player.getName()))));
                EconomyResponse r = econ.depositPlayer(player, 1.05);
                if(r.transactionSuccess()) {
                    sender.sendMessage(String.format("You were given %s and now have %s", econ.format(r.amount), econ.format(r.balance)));
                } else {
                    sender.sendMessage(String.format("An error occured: %s", r.errorMessage));
                }
                return true;
            } else if(command.getLabel().equals("test-permission")) {
                // Lets test if user has the node "example.plugin.awesome" to determine if they are awesome or just suck
                if(perms.has(player, "example.plugin.awesome")) {
                    sender.sendMessage("You are awesome!");
                } else {
                    sender.sendMessage("You suck!");
                }
                return true;
            } else {
                return false;
            }
        }
     
        public static Economy getEconomy() {
            return econ;
        }
     
        public static Permission getPermissions() {
            return perms;
        }
     
        public static Chat getChat() {
            return chat;
        }

    }
     
     
    #1 Stellrow, Jul 17, 2018
    Last edited: Jul 17, 2018