Using the VaultAPI to take money when an event is fired

Discussion in 'Plugin Development' started by Techno, Mar 17, 2014.

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

    PogoStick29

    First of all, that's invalid. It should be if(econ.getBalance(p.getName()) >= 110) {

    If that doesn't solve the problem, either p or econ is null. If this is in an event or command, we can safely say that p is not null. In that case, econ is null. Where is it econ being instantiated?
     
  2. Okay, thanks! There is a new error..

    Code:
    Code:java
    1.  
    2. @EventHandler
    3. public void onInventoryClick(InventoryClickEvent e) {
    4. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    5.  
    6. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    7. Player p = (Player) e.getWhoClicked();
    8.  
    9. if(econ.getBalance(p.getName()) >= 110) {
    10. EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    11.  
    12. if (r.transactionSuccess()) {
    13. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    14. p.getInventory().addItem(d);
    15.  
    16. } else {
    17. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    18. }
    19.  
    20. } else {
    21. //Do stuff
    22. }
    23.  
    24. e.setCancelled(true);
    25. e.getWhoClicked().closeInventory();
    26.  
    27. }
    28.  
    29. }
    30.  


    Stack:
    Code:
    Could not pass event InventoryClickEvent to MaxyMenu v1.0
    org.bukkit.event.EventException
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:427) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1351) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    Caused by: java.lang.NoClassDefFoundError: net/milkbowl/vault/economy/Economy
    at noah.plugins.maxymenu.ShopMenuInv.onInventoryClick(ShopMenuInv.java:84) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    ... 13 more
    Caused by: java.lang.ClassNotFoundException: net.milkbowl.vault.economy.Economy
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0]
    at java.net.URLClassLoader$1.run(Unknown Source) ~[?:1.7.0]
    at java.security.AccessController.doPrivileged(Native Method) ~[?:1.7.0]
    at java.net.URLClassLoader.findClass(Unknown Source) ~[?:1.7.0]
    at org.bukkit.plugin.java.PluginClassLoader.findClass0(PluginClassLoader.java:80) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:53) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0]
    at java.lang.ClassLoader.loadClass(Unknown Source) ~[?:1.7.0]
    at noah.plugins.maxymenu.ShopMenuInv.onInventoryClick(ShopMenuInv.java:84) ~[?:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0]
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0]
    at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0]
    at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:425) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.2-b2974jnks]
    ... 13 more
    [09:40:55] [Server thread/INFO]: Bill4788 issued server command: /rl
    
     
  3. Offline

    PogoStick29

    Caused by: java.lang.ClassNotFoundException: net.milkbowl.vault.economy.Economy
    It's obvious that you don't have Vault installed. Are you sure you watched my video on reading stack traces? This error seems pretty obvious.
     
  4. Offline

    PogoStick29

  5. Offline

    mazentheamazin

    Techno
    I think PogoStick29 wants you to call setupEconomy() in the constructor of ShopMenuInv
     
    PogoStick29 likes this.
  6. mazentheamazin
    So like:
    Code:
    public ShopMenuInv(Plugin p) {
    invShop = Bukkit.getServer().createInventory(null, 9, "Maxy Shop");
     
    d = createItem(ChatColor.BLUE + "Diamond");
     
    invShop.setItem(4, d);
     
    Bukkit.getServer().getPluginManager().registerEvents(this, p);
     
    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;
       }
     
    }
    
    mazentheamazin
    Look at the stack though: http://pastebin.com/HRx2BbL4

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  7. Offline

    PogoStick29

    Calling a method is not the same as defining it. You've called methods in that code so you should know that setupEconomy() goes outside of the constructor and you call setupEconomy() inside.
     
  8. PogoStick29
    I know I actually set setupEconomy() outside of the constructor, so what will I do now?
     
  9. Offline

    PogoStick29

    Are you sure it's being set up? Do me a favor and add this code before your use of the econ variable.

    Code:java
    1. System.out.println(econ == null);


    If that prints true, you aren't setting it up.
     
  10. PogoStick29
    Error on println:
    Code:java
    1.  
    2. Syntax error on token "println", = expected after this token
    3.  


    Can anyone help?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    PogoStick29

    Did you copy the exact line I wrote letter for letter?
     
  12. PogoStick29
    Yes, Code (ShopMenuInv.java):
    Code:java
    1.  
    2. package noah.plugins.maxymenu;
    3.  
    4. import java.util.Arrays;
    5.  
    6. import net.milkbowl.vault.economy.Economy;
    7. import net.milkbowl.vault.economy.EconomyResponse;
    8.  
    9. import org.bukkit.Bukkit;
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.Material;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.Listener;
    15. import org.bukkit.event.inventory.InventoryClickEvent;
    16. import org.bukkit.inventory.Inventory;
    17. import org.bukkit.inventory.ItemStack;
    18. import org.bukkit.inventory.meta.ItemMeta;
    19. import org.bukkit.plugin.Plugin;
    20. import org.bukkit.plugin.RegisteredServiceProvider;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23.  
    24. public class ShopMenuInv extends JavaPlugin implements Listener {
    25.  
    26. private Inventory invShop;
    27. private ItemStack d;
    28.  
    29. public static Economy econ = null;
    30.  
    31. public void onEnable() {
    32. if (!setupEconomy() ) {
    33. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    34. Bukkit.getServer().getPluginManager().disablePlugin(this);
    35. return;
    36. }
    37.  
    38. System.out.println(econ == null);
    39.  
    40. }
    41.  
    42. public ShopMenuInv(Plugin p) {
    43. invShop = Bukkit.getServer().createInventory(null, 9, "Maxy Shop");
    44.  
    45. d = createItem(ChatColor.BLUE + "Diamond");
    46.  
    47. invShop.setItem(4, d);
    48.  
    49. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    50. }
    51.  
    52. private boolean setupEconomy() {
    53. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    54. return false;
    55. }
    56. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    57. if (rsp == null) {
    58. return false;
    59. }
    60. econ = rsp.getProvider();
    61. return econ != null;
    62. }
    63.  
    64. private ItemStack createItem(String name) {
    65. ItemStack i = new ItemStack(Material.DIAMOND);
    66. ItemMeta im = i.getItemMeta();
    67.  
    68. im.setDisplayName(name);
    69. im.setLore(Arrays.asList(ChatColor.GREEN + "Buy " + ChatColor.BLUE + "DIAMOND", "" + ChatColor.GREEN + " Price: $110" ));
    70. i.setItemMeta(im);
    71.  
    72. return i;
    73. }
    74.  
    75. public void show(Player p) {
    76. p.openInventory(invShop);
    77. }
    78.  
    79. @EventHandler
    80. public void onInventoryClick(InventoryClickEvent e) {
    81. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    82.  
    83. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    84. Player p = (Player) e.getWhoClicked();
    85.  
    86. if(econ.getBalance(p.getName()) >= 110) {
    87. EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    88.  
    89. if (r.transactionSuccess()) {
    90. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    91. p.getInventory().addItem(d);
    92.  
    93. } else {
    94. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    95. }
    96.  
    97. } else {
    98. //Do stuff
    99. }
    100.  
    101. e.setCancelled(true);
    102. e.getWhoClicked().closeInventory();
    103.  
    104. }
    105.  
    106. }
    107. }
    108.  
     
  13. *bump* *bump*
     
  14. *bump* *bump**bump* *bump*
     
  15. Offline

    iPoke111

    Techno
    Sorry if I'm going to get your hopes up by posting something that isn't to do with your code, but:

    You're bumping a ton more than is polite, I think more people than I would appreciate it if you didn't put it right back at the top of page one every time it gets to page two...
     
  16. iPoke111
    I know, but I want to get this finished!
     
  17. Offline

    iPoke111

    Techno
    I had a thread sitting on here from Christmas and I let it wait three months before bumping it... That's probably too long but still.

    Also, you seem to be getting a lot of errors in your code. It might make more sense to go back and relearn some techniques because if you need someone to solve all of your problems, it won't help you later.
     
    PogoStick29 likes this.
  18. *bump*

    (Last one for a week!)
     
  19. *BUMP PLEASE SOMEONE REPLY!*
     
  20. Offline

    xTigerRebornx

    Techno Can I see all the classes you have so far, and an updated stacktrace?
     
  21. Offline

    Code0

    Seriously noone saw the problem yet? ...

    Techno You have to check if your player (p) (that right clicked) is online.

    Code:java
    1. if(p == null){
    2. //CODE
    3. }else{
    4. //WHAT YOU WANT TO DO (CODE)
    5. }
     
  22. Code0

    I changed it to:
    Code:java
    1.  
    2. @SuppressWarnings("null")
    3. @EventHandler
    4. public void onInventoryClick(InventoryClickEvent e) {
    5. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    6.  
    7. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    8. Player p = (Player) e.getWhoClicked();
    9.  
    10. if (p == null) {
    11. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "Something bad happened!");
    12. } else {
    13. if(econ.getBalance(p.getName()) >= 110) {
    14. EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    15.  
    16. if (r.transactionSuccess()) {
    17. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    18. p.getInventory().addItem(d);
    19.  
    20. } else {
    21. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    22. }
    23.  
    24. } else {
    25. //Do stuff
    26. }
    27. }
    28.  
    29. e.setCancelled(true);
    30. e.getWhoClicked().closeInventory();
    31.  
    32. }
    33.  
    34. }
    35.  
     
  23. Offline

    Code0

    Correct! That should work.

    Techno
     
Thread Status:
Not open for further replies.

Share This Page