Solved How to take money from player's balance

Discussion in 'Spigot Plugin Development' started by Kompye, Sep 11, 2019.

  1. Hello. I am working on a plugin which implies paying for doing some things in an inventory and I can't figure out how to take money from the player. I am using Vault and what I did so far is add this to the onEnable method
    Code (Java):
        public Economy economy = null;

           public boolean setupEconomy()
            {
                RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
                if (economyProvider != null) {
                    economy = economyProvider.getProvider();
                }

                return (economy != null);
            }
    and adding this to when I want the money to be taken, but it gives the error "Cannot make a static reference to the non-static field Main.economy". I am a beginner in plugin development and I've probably made a Java mistake.
    Code (Java):
    Main.economy.withdrawPlayer(p, 100.0F);
     
  2. You have to add the static keyword to your economy variable in your main class.
    The first line of your code should then look like that:
    Code (Java):
    public static Economy economy = null;
    and you will be fine.
     
  3. SteelPhoenix

    Junior Mod

    Pay attention to static abuse and encapsulation.
     
    • Agree Agree x 1
  4. Hello / Salutare prietene,

    It's not the most "correct" way to do it, yes, but you still can do it, it will work.
    Else you'll have to instantiate your listener with your main class. Search on google "dependency injection java".
     
    • Funny Funny x 1
  5. joehot200

    Supporter

    It is not wrong, in that it works. However, it is bad practise. That said, it will absolutely work if you just make the variable static.

    The common way to do it is to have an access to your class (for example public static Main mainClass), and then reference that from elsewhere (mainClass.economy.withdrawlMoneyz()).

    Your class instance variable doesn't necessarily need to be static either.
     
  6. Doesn't work when I make it static, that happens when clicking the item
    Code (Text):
    11.09 17:18:40 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:2310) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:545) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:18:40 [Server] INFO at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor254.execute(Unknown Source) ~[?:?]
    11.09 17:18:40 [Server] INFO at net.etheral.fantastichealth.listeners.MenuHandler.onMenuClick(MenuHandler.java:31) ~[?:?]
    11.09 17:18:40 [Server] INFO java.lang.NullPointerException: null
    11.09 17:18:40 [Server] ERROR Could not pass event InventoryClickEvent to FantasticHealth v1.0
     
  7. That doesn't work either. It just says this error when clicking it
    Code (Text):
    11.09 17:22:38 [Server] INFO at java.lang.Thread.run(Thread.java:748) [?:1.8.0_222]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.run(MinecraftServer.java:923) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.sleepForTick(MinecraftServer.java:990) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.executeNext(MinecraftServer.java:1006) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.MinecraftServer.aX(MinecraftServer.java:1013) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.TickTask.run(SourceFile:18) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:10) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.PacketPlayInWindowClick.a(SourceFile:33) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at net.minecraft.server.v1_14_R1.PlayerConnection.a(PlayerConnection.java:2310) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:545) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.14.4.jar:git-Paper-175]
    11.09 17:22:38 [Server] INFO at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor322.execute(Unknown Source) ~[?:?]
    11.09 17:22:38 [Server] INFO at net.etheral.fantastichealth.listeners.MenuHandler.onMenuClick(MenuHandler.java:31) ~[?:?]
    11.09 17:22:38 [Server] INFO java.lang.NullPointerException: null
    11.09 17:22:38 [Server] ERROR Could not pass event InventoryClickEvent to FantasticHealth v1.0
     
  8. joehot200

    Supporter

    What's at line 31 of MenuHandler?
     
  9. It is mainClass.economy.withdrawPlayer(p, 100.0F);
     
  10. joehot200

    Supporter

    So, either mainClass, economy, or p are null.

    You should do some checks - system.out.println(p != null), etc.
     
  11. "public Economy economy = null;" in main class. What should I set it to? As I said, I am just getting into these things and still learning :)
    Sorry but I can't understand those fancy words
     
  12. The economy should no longer be null if there's one on the server after setupEconomy has been called. Are you calling setupEconomy (e.g. in your onEnable)?
     
  13. This is my onEnable
    Code (Java):
        @Override
        public void onEnable() {
           
           
           
            System.out.println("FantasticHealth has been enabled.");
           
            getCommand("fhealth").setExecutor(new fhealthcommand());
            getServer().getPluginManager().registerEvents(new MenuHandler(), this);
           
        }
       
        public Economy economy = null;

           public boolean setupEconomy()
            {
                RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
                if (economyProvider != null) {
                    economy = economyProvider.getProvider();
                }

                return (economy != null);
            }

    }
     
  14. Your setupEconomy is not being called, add a call to setupEconomy() at the bottom of the method (after you register the MenuHandler)
     
  15. Did it, but I am getting the same error.

    EDIT: Do I have to use Essentials for this to work?
     
  16. This is potentially due to the plugin enable order - you may need to add an additional PluginEnableListener that calls your setupEconomy again, since the service wasn't registered until the plugin providing it was enabled.
     
  17. Same error.
    Code (Java):
    package net.etheral.fantastichealth.listeners;

    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;

    import net.etheral.fantastichealth.Main;

    public class PluginEnablingListener implements Listener{

        public Main mainClass;
       
        @EventHandler
        public void onPluginEnable(PluginEnablingListener pel) {
            mainClass.setupEconomy();
        }

    }
     
     
  18. Have you registered this listener? Is there an economy plugin on the server? Anything with Vault support.
     
  19. I followed this tutorial and fixed it :D

    Thank you guys for looking into it!