Solved Why am I getting an error

Discussion in 'Spigot Plugin Development' started by Zebradude6047, Jun 11, 2021 at 7:22 PM.

  1. I am making a plugin that when you break a wheat block fully grown it replaces it and gives you money. It can do all of the player-side stuff, but it breaks when I try to give the money.


    Here is the code for the event
    Code (Text):
    package me.notdew.wheatmoney.events;

    import me.notdew.wheatmoney.WheatMoney;
    import net.milkbowl.vault.economy.Economy;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.data.Ageable;
    import org.bukkit.block.data.BlockData;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.BlockBreakEvent;
    import net.milkbowl.vault.economy.EconomyResponse;

    public class WheatBreakEvent implements Listener {
        Economy economy = WheatMoney.getEconomy();
        private static Economy econ = null;




        @EventHandler
        public void onWheatBreak(BlockBreakEvent e) {
            Player sender = e.getPlayer();
            Player player = e.getPlayer();
            BlockData blockdata = e.getBlock().getBlockData();
            if (blockdata instanceof Ageable) {
                if (e.getPlayer().getLocation().getWorld().getName().equals("world")) {
                    Ageable age = (Ageable) blockdata;

                    if (age.getAge() == age.getMaximumAge()) {

                        player.sendMessage("You broke fully planted wheat");
                        sender.sendMessage(String.format("You have %s", econ.format(econ.getBalance(player.getName()))));
                        EconomyResponse r = econ.depositPlayer(player, 100.00);
                        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));
                        }
                    }
                    if (age.getAge() != age.getMaximumAge()) {
                        player.sendMessage("You broke a non full planted wheat");
                    }


                    e.setCancelled(true);
                    Block block = e.getBlock();
                    ((Ageable) blockdata).setAge(0);
                    block.setType(Material.WHEAT, true);
                    block.setBlockData(blockdata);



                }
            }
        }
    }
     

    and for the main class. here it is


    Code (Java):
    package me.notdew.wheatmoney;

    import me.notdew.wheatmoney.events.WheatBreakEvent;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.EconomyResponse;
    import net.milkbowl.vault.permission.Permission;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.block.data.Ageable;
    import org.bukkit.block.data.BlockData;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.block.BlockBreakEvent;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.milkbowl.vault.economy.Economy;

    import java.util.logging.Logger;

    public class WheatMoney extends JavaPlugin {

        private static final Logger log = Logger.getLogger("Minecraft");
        private static Economy econ = null;
        private static Permission perms = null;
        private static Chat chat = null;

        @Override
        public void onDisable() {
            log.info(String.format("[%s] Disabled Version %s", getDescription().getName(), getDescription().getVersion()));
        }

        @Override
        public void onEnable() {
            if (!setupEconomy()) {
                log.severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            setupPermissions();
            setupChat();
        }

        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 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;
        }
        }
     
    Vault works when I use the /test-economy command but as I said not for the event even though it uses the same piece of code.

    Thanks!
     
  2. Oh yeah heres the error
    Code (Text):
    11:21:12] [Server thread/ERROR]: Error occurred while enabling WheatMoney v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: null
            at me.notdew.wheatmoney.WheatMoney.setupChat(WheatMoney.java:53) ~[?:?]
            at me.notdew.wheatmoney.WheatMoney.onEnable(WheatMoney.java:36) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:494) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:408) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:881) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.Bukkit.reload(Bukkit.java:651) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:763) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:748) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:387) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:356) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1008) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:847) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at java.lang.Thread.run(Thread.java:829) [?:?]
     
  3. The error message is there but the actual error is that it makes the event useless and doesn't work anymore. Once I place it to give the player money then none of the functions of that event work
     
  4. if you say that it gives you an error when we want to see that error as well
     
  5. I already posted the error message?
     
  6. SteelPhoenix

    Moderator

    Code (Java):
    Economy economy = WheatMoney.getEconomy();
    private static Economy econ = null;
    I wonder which one OP used...
     
    • Funny Funny x 2
  7. I used the private static one, I must have accidentally added the other. I removed both and tried both by itself and it's still broken. Thanks for the reply though
     
  8. What is the error message??? You never posted it
     
  9. Sorry it said it was awaitinf approval here it is:
    Code (Text):
    11:21:12] [Server thread/ERROR]: Error occurred while enabling WheatMoney v1.0-SNAPSHOT (Is it up to date?)
    java.lang.NullPointerException: null
            at me.notdew.wheatmoney.WheatMoney.setupChat(WheatMoney.java:53) ~[?:?]
            at me.notdew.wheatmoney.WheatMoney.onEnable(WheatMoney.java:36) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:494) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:408) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:881) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.Bukkit.reload(Bukkit.java:651) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:763) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:748) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:387) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:356) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1008) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:847) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot-1.16.5.jar:2991-Spigot-018b9a0-f3f3094]
            at java.lang.Thread.run(Thread.java:829) [?:?]
     
  10. I just checked back and idk what I did but it's all broken can someone please help.
     
  11. Code (Text):
    private boolean setupChat() {
            RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
            chat = rsp.getProvider();
            return chat != null;
        }
    Given that the error is being caused by this block of code, it might be wise to check whether or not 'getServer().getServicesManager().getRegistration(Chat.class);' is returning null.
     
  12. IT WORKED tysm