I don't remove player's balance

Discussion in 'Spigot Plugin Development' started by RedLime, Feb 13, 2019.

  1. Code (Java):

    private static Economy econ = null;
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
        econ.withdrawPlayer(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()), customConfig.get().getInt(p + ".unlock.money"));
    }
    }
     
    I could not deduct the player's Balance,
    Bukkit has a "null" error. Why does this happen?
    I am also using the Vault API. I also confirmed that the '/vault-info' command correctly registers Economy.
     
  2. You're never setting `econ` to anything, it's always null.
     
  3. You can always use Bukkit.getpluginmanager instead of Main.getinstance.getserver.getpluginmanager

    Also, you set the variable Econ on the top as null and never re-set it.

    Could you show us your Main class?


    Sent from my iPhone using Tapatalk
     
  4. In my experience, I can not show all of the Main classes.However, getServer().getPluginManager().registerEvents(new SpawnBox(), this); Method in the onEnable of Main.
     
  5. Give the full stack trace of error.
     
  6. https://pastebin.com/1c4Uffwm
    SpawnBox.java:90 --> econ.withdrawPlayer(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()), customConfig.get().getInt(p + ".unlock.money"));
     
  7. Either customConfig, customConfig.get(), or customConfig.get().getInt (assuming it returns an Integer object) is null, check which one it is.
     
  8. You never define econ. You initialize it as null. Also, pass an instance of your main class through a constructor instead of abusing static.

    Player is a subclass of OfflinePlayer so no need to do Bukkit.getOfflinePlayer(player.getUniqueId())
     
  9. I have already checked it and checked that it is not null.
     
  10. So this stack trace has NOTHING to do with the code you submitted in the question. Error occur in PlayerInteractEvent in redlimerl.spigot.randombox.SpawnBox.CloseInv(SpawnBox.java:90). Line 90 of SpawnBox class. Search here,
     
  11. I wrote the example,
    econ.withdrawPlayer(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()),customConfig.get().getInt(p + ". unlock.money"));
    The line is correct.
     
  12. Share your code. You're just making this a lot harder for us to figure out what's actually wrong. You're here asking for help but refuse to provide us with what we need in order for us to help you out. Not quite smart.

    Let's start with your code:
    Code (Java):
    private static Economy econ = null;
    @EventHandler
    public void onJoin(PlayerJoinEvent e) {
    if (Main.getInstance().getServer().getPluginManager().getPlugin("Vault") != null && econ != null) {
        econ.withdrawPlayer(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()), customConfig.get().getInt(p + ".unlock.money"));
    }
    }
    Let's start with the simple stuff:
    Code (Java):
    econ.withdrawPlayer(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()), customConfig.get().getInt(p + ".unlock.money"))
    Code (Java):
    Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId()
    Why bother doing all that? Economy#withdrawPlayer also accepts a player as a param so you can simplify it to
    Code (Java):
    econ.withdrawPlayer(e.getPlayer()), customConfig.get().getInt(p + ".unlock.money"))
    Now what I believe the issue is, is customConfig.get().getInt(p + ".unlock.money")
    What's p exactly? Is it referring to a player perhaps? Since you're not providing your code, I can't really tell but I assume the path's missing. You have either made a typo or the path is actually not there.

    Vault provides you with a method to ensure that the plugin (vault) has been installed successfuly. That method returns a boolean. Use it, instead of doing these odd checks on your PJE.

    In addition, keep in mind Economy#withdraw may not succeed if the player has not enough money. Make sure they do have or check if the withdraw succeeded.
     
  13. It's impossible to help you here if you don't want to give the exact code with econ declaration. I can just recommend you to debug values of variables before this line like System.out.println(econ); System.out.println(Bukkit.getOfflinePlayer(e.getPlayer().getUniqueId())); etc
     
  14. The variable Econ is null. You must define it as you never do before you withdraw the players money.


    Sent from my iPhone using Tapatalk
     
  15. He "knows" what he's doing bro, its k
     
  16. Please understand that I do not know how to code well and I am very confused.
    (It is not long since I started Spigot.)
    p is a kind of integer id. Used as a data type in yaml of customconfig.
     
  17. Sorry, but looks like he not only can't solve this simple problem, but also can't ask normally how to solve it. But i just want to help
     
  18. If you really want all of the code, it can be hard to figure out because the code is so long, so I omit a lot. If you want all the code I will give you pastebin.
     
  19. We can't really help you out if you do not provide us with more code. And the contents of the config file as well. Share it in a pastebin/hastebin or whatever.
     

Share This Page