Development Assistance Giving Money To Players Issue

Discussion in 'Plugin Help/Development/Requests' started by RockKetcher, Jun 15, 2015.

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

    RockKetcher

    I've been trying to make a command to allow players to sell a Nether Star for $100.

    I've been getting Java.lang.NullPointerExceptions :(

    Here is my class:
    Code:
    package me.mcwarcraftdev.SellConquest;
    
    import java.util.logging.Logger;
    
    import net.md_5.bungee.api.ChatColor;
    import net.milkbowl.vault.economy.Economy;
    
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class SellConquest extends JavaPlugin {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static SellConquest plugin;
        public static Economy econ = null;
       
        @Override
        public void onDisable() {
            this.logger.info("[SellConquest] Has Been Disabled!");
        }
       
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info("[SellConquest] Version " + pdfFile.getVersion() + " Has Been Enabled!");
        }
       
        private boolean setupEconomy() {
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
            if (economyProvider != null) {
                econ = economyProvider.getProvider();
            }
    
            return (econ != null);
        }
       
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (cmd.getName().equalsIgnoreCase("basic")) { // If the player typed /sellconquestpoint then do the following...
                setupEconomy();
                Player player = (Player) sender;
                if (player.getItemInHand().getType().equals(Material.NETHER_STAR)) {
                    int handAmount = player.getItemInHand().getAmount();
                    int giveAmount = handAmount - 1;
                    if (giveAmount == 0) {
                        SellConquest.econ.depositPlayer(player.getName(), 100);
                        player.sendMessage(ChatColor.GREEN + "Transaction success!");
                        player.setItemInHand(new ItemStack(0, giveAmount));   
                    }
                    else {
                        SellConquest.econ.depositPlayer(player.getName(), 100);
                        player.sendMessage(ChatColor.GREEN + "Transaction success!");
                        player.setItemInHand(new ItemStack(0, giveAmount));
                    }
                }
               
                else {
                player.sendMessage(ChatColor.RED + "You must be holding a conquest point!");
                }
            } //If this has happened the function will return true.
                // If this hasn't happened the value of false will be returned.
            return false;
        }
    }
    Here is the error I'm getting when executing the command in game:
    Code:
    [04:56:23 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'basic' in plugin SellConquest v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at org.bukkit.craftbukkit.v1_8_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.PlayerConnection.handleCommand(PlayerConnection.java:1139) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.PlayerConnection.a(PlayerConnection.java:974) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) [?:1.7.0_65]
            at java.util.concurrent.FutureTask.run(FutureTask.java:262) [?:1.7.0_65]
            at net.minecraft.server.v1_8_R2.SystemUtils.a(SourceFile:60) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.A(MinecraftServer.java:712) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.DedicatedServer.A(DedicatedServer.java:368) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.z(MinecraftServer.java:651) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at net.minecraft.server.v1_8_R2.MinecraftServer.run(MinecraftServer.java:554) [spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            at java.lang.Thread.run(Thread.java:745) [?:1.7.0_65]
    Caused by: java.lang.NullPointerException
            at me.mcwarcraftdev.SellConquest.SellConquest.onCommand(SellConquest.java:60) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.3.jar:git-Spigot-2ec6f06-7722428]
            ... 15 more
     
  2. Online

    timtower Administrator Administrator Moderator

    Moved to Bukkit alternatives
     
  3. Offline

    Boomer

    plugin is null, you dont initialize it anywhere (onEnable)
     
  4. Offline

    RockKetcher

    How would I do that?
     
  5. Offline

    Boomer

    Thats not the issue in this case though - I spotted something early just before leaving for work, but then realized I typed it wrong, so I changed my comment but thats not the problem.

    However, its very apparent you're using an example code and trying to make it work, given the use of the ancient logging style and logging info to the server on enable, and the fact that you're processing a command "/basic" not actually the command you note in your comments. However, i see you are executing "/basic" during testing this, so thats okay.


    Caused by: java.lang.NullPointerException
    at me.mcwarcraftdev.SellConquest.SellConquest.onCommand(SellConquest.java:60) ~[?:?]

    So we look at line 60 to find out what might be null at that point... and see nothing -- its a bracket closing only. So that line referenced there might be off from what you pasted... Please make sure to post the full class code with the corresponding stacktrace error, so that the line number referenced in the error can be directly examined.

    Meanwhile, lets look at potential errors from top-down:


    FIRST == you want to run the setup-economy code during the initialization, not each time the command is run.

    Player player = (Player) sender; << You do this without checking if the sender IS a player. Which is fine if its your own plugin, and you are 100% sure that the command will never be executed in the console or via a command block, but if used via the console, crash-time.

    if (player.getItemInHand().getType().equals(Material.NETHER_STAR)) {

    Okay, this one can fail bigtime
    If the player doesn't have anything in his hand, its null. You can't getType() of a null thing, which throws an error. So you should make sure the player.getItemInHand is not null, and if it is, cancel out of everything before progressing.

    SellConquest.econ.depositPlayer(player.getName(), 100);

    This assumes that your econ variable is set to something non-null. You aren't even checking with your initialization code to see if it found an economy system. Thats why you should move the call to set it up into the onEnable, and then do something with the return from that function to see if its setup or not...

    Your mathematics for handAmount and giveAmount, and how to act on them, dont really make much sense either, or at least what you plan to test with them (given that you have an if/else that does the same thing with no difference)
    But I'll chalk that one up to "I just need to get one thing working first, then i can modify it"
     
  6. Offline

    RockKetcher

    It turns out the code worked perfectly although it may have been a little sketchy.

    FIX: I thought Vault came with its own Economy system. I just threw essentials in and everything worked perfectly.

    Thanks Boomer for the tips!
     
Thread Status:
Not open for further replies.

Share This Page