Solved Error when using Economy

Discussion in 'Plugin Development' started by YJJcoolcool, Apr 11, 2020.

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

    YJJcoolcool

    Hi, I'm making a plugin and there is one part which deducts money from a player's account. But when I use the withdrawPlayer or getBalance function, the plugin doesn't work and just throws an error.

    Here is the main class:
    Code:
    package yjjcity.plugins.Cashiers;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.Economy;
    
    import net.md_5.bungee.api.ChatColor;
    import yjjcity.plugins.Cashiers.Events.EventsClass;
    
    public class Cashiers extends JavaPlugin {
       
        @Override
        public void onEnable() {
            setupEconomy();
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.GREEN+" has been ENABLED.\n\n");
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
        }
       
        public static Economy economy = null;
        
        private boolean setupEconomy(){
    
            RegisteredServiceProvider<Economy> economyProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.economy.Economy.class);
    
            if(economyProvider != null){
                economy = economyProvider.getProvider();
            }
    
            return (economy != null);
    
        }
       
        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.RED+" has been DISABLED.\n\n");
        }
    }
    
    Here is the part of the Events class code which involves the Economy:
    Code:
    else if (line3.equalsIgnoreCase("Click to Pay")) {
                            double amount = Double.parseDouble(line2.substring(3));
                            player.sendMessage(ChatColor.GREEN+"Amount: YC$"+amount);
                            player.sendMessage(ChatColor.GREEN+"You have YC$"+economy.getBalance(player)+" in your account.");
                            EconomyResponse r = economy.withdrawPlayer(player, amount);
                            player.sendMessage(ChatColor.GREEN+"Deducted YC$"+amount+" from your account.");
                            if (r.transactionSuccess()) {
                                player.sendMessage(ChatColor.GREEN+line2+" has been deducted from your account.");
                            } else {
                                player.sendMessage(ChatColor.RED+"An error occured. Do you have enough money in your account?");
    And here is the error that pops up in the console:
    Code:
    [15:36:50 ERROR]: Could not pass event PlayerInteractEvent to YJJCityPlugins-Cashiers v1.0
    
    java.lang.NullPointerException: null
            at yjjcity.plugins.Cashiers.Events.EventsClass.onInteract(EventsClass.java:97) ~[?:?]
            at com.destroystokyo.paper.event.executor.asm.generated.GeneratedEventExecutor333.execute(Unknown Source) ~[?:?]
            at org.bukkit.plugin.EventExecutor.lambda$create$1(EventExecutor.java:69) ~[patched_1.15.2.jar:git-Paper-168]
            at co.aikar.timings.TimedEventExecutor.execute(TimedEventExecutor.java:80) ~[patched_1.15.2.jar:git-Paper-168]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:70) ~[patched_1.15.2.jar:git-Paper-168]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:607) ~[patched_1.15.2.jar:git-Paper-168]
            at org.bukkit.craftbukkit.v1_15_R1.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:463) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.PlayerInteractManager.a(PlayerInteractManager.java:448) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.PlayerConnection.a(PlayerConnection.java:1378) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:27) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.PacketPlayInUseItem.a(PacketPlayInUseItem.java:5) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.PlayerConnectionUtils.lambda$ensureMainThread$0(PlayerConnectionUtils.java:23) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.TickTask.run(SourceFile:18) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeTask(IAsyncTaskHandler.java:136) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.executeNext(IAsyncTaskHandler.java:109) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.MinecraftServer.ba(MinecraftServer.java:1053) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.MinecraftServer.executeNext(MinecraftServer.java:1046) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.IAsyncTaskHandler.awaitTasks(IAsyncTaskHandler.java:119) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.MinecraftServer.sleepForTick(MinecraftServer.java:1016) ~[patched_1.15.2.jar:git-Paper-168]
            at net.minecraft.server.v1_15_R1.MinecraftServer.run(MinecraftServer.java:939) ~[patched_1.15.2.jar:git-Paper-168]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
     
  2. Offline

    timtower Administrator Administrator Moderator

  3. Offline

    YJJcoolcool

    So I put a
    Code:
    if (economy == null) {
        setupEconomy();
    }
    in onEnable()?
     
  4. Offline

    timtower Administrator Administrator Moderator

    @YJJcoolcool No, before you start using it.
    You don't check the result of setupEconomy()
     
  5. Offline

    YJJcoolcool

    Ok I copied the Economy setup from the VaultApi repo. But all the examples I see elsewhere show Economy being used in the Main class. How do I "import" it into the Events class?

    Below is the main class:
    Code:
    package yjjcity.plugins.Cashiers;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import net.md_5.bungee.api.ChatColor;
    import yjjcity.plugins.Cashiers.Events.EventsClass;
    
    public class Cashiers extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        private static Economy econ = null;
        private static Chat chat = null;
       
        @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;
            }
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.GREEN+" has been ENABLED.\n\n");
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
            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;
        }
       
        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.RED+" has been DISABLED.\n\n");
        }
       
        public static Economy getEconomy() {
            return econ;
        }
       
        public static Chat getChat() {
            return chat;
        }
    }
    
     
  6. Offline

    timtower Administrator Administrator Moderator

    @YJJcoolcool You make a getter for it.
    And then you give the EventsClass an instance to the main class.
     
  7. Offline

    YJJcoolcool

    Sorry, I don't know how to do this. Do you know how?
     
  8. Offline

    timtower Administrator Administrator Moderator

  9. Offline

    YJJcoolcool

    Sorry, I am completely lost. Do I replace private static Economy econ with public static Economy econ? And what should I do after that?
     
  10. Offline

    timtower Administrator Administrator Moderator

    No, you remove the static all together.
    You move the field to the main class (and the setupEconomy method)
    Then you add a getter for econ.
     
  11. Offline

    YJJcoolcool

    Is this what I'm supposed to do?
    Here is the main class:
    Code:
    package yjjcity.plugins.Cashiers;
    
    import java.util.logging.Logger;
    
    import org.bukkit.Bukkit;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    import net.milkbowl.vault.chat.Chat;
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    import net.md_5.bungee.api.ChatColor;
    import yjjcity.plugins.Cashiers.Events.EventsClass;
    
    public class Cashiers extends JavaPlugin {
        private static final Logger log = Logger.getLogger("Minecraft");
        public static Economy econ = null;
        private static Chat chat = null;
       
        @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;
            }
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.GREEN+" has been ENABLED.\n\n");
            getServer().getPluginManager().registerEvents(new EventsClass(), this);
            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;
        }
       
        @Override
        public void onDisable() {
            getServer().getConsoleSender().sendMessage(ChatColor.YELLOW+"\n\nYJJ City Plugins - Cashiers"+ChatColor.RED+" has been DISABLED.\n\n");
        }
       
        public static Economy getEconomy() {
            return econ;
        }
       
        public static Chat getChat() {
            return chat;
        }
    }
    
    And in the Events class I added this
    Code:
    public class EventsClass implements Listener {
        private static Economy econ = null;
        public static Economy getEcon(){
            return econ;
        }
    ...
                            EconomyResponse r = econ.withdrawPlayer(player, amount);
                            player.sendMessage(ChatColor.GREEN+"Deducted YC$"+amount+" from your account.");
    
     
  12. Offline

    timtower Administrator Administrator Moderator

    @YJJcoolcool And where do you set the economy now?
    Should only have 1 econ variable
     
  13. Offline

    YJJcoolcool

    So I want to set the economy in the Events class, so I should remove the
    Code:
    public static Economy econ = null;
    in the Main class? But if so then the Main class would have an error since setupEconomy() uses econ.
     
  14. Offline

    caderapee

    @YJJcoolcool No create it in your main class, and when you instancie your event class, pass an instance of ur main class in the parameter of the constructor, then in your event class, you will able to access to variable into your mainclass
     
  15. Offline

    YJJcoolcool

    Sorry, I'm a bit confused.
    But what I did was removed the getEcon() function in the Events class.
    Just to verify, nothing needs to be changed in the Main class as it is right now right?
    What do I do in the Event's class?

    I put a Economy econ = new Economy in the Events class but it doesn't work.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
  16. Offline

    timtower Administrator Administrator Moderator

    @YJJcoolcool in the events class you do main.getEcon()
    When you make a getting for econ in the main class.
    And you pass the main instance along to the event.
     
  17. Offline

    YJJcoolcool

    I put Economy econ = new main.getEcon() in the Events class but it gave an error which said
    main cannot be resolved to a type
     
  18. Offline

    timtower Administrator Administrator Moderator

    You can do new there, you need to pass the main instance along using a constructor.

    Does your event class have a constructor?
     
  19. Offline

    YJJcoolcool

    Code:
    public class EventsClass implements Listener {
          private Plugin plugin = Cashiers.getPlugin(Cashiers.class);
          @EventHandler
          public void onInteract(PlayerInteractEvent event) {
                Economy econ = new main.getEcon();
    Is this a constructor?
     
  20. Offline

    timtower Administrator Administrator Moderator

    @YJJcoolcool No, that is a static getter.
    And in your case it would be plugin.getEcon()
     
  21. Offline

    YJJcoolcool

    plugin.getEcon() didn't work but I changed it to Cashiers.getEcon() (name of the main class) and it worked.
    Thank you so much for helping, really appreciate your patience!!
     
Thread Status:
Not open for further replies.

Share This Page