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

    Techno

    As the title says, I want to know how to take money for the player when an event is fired..
    I am using a InventoryMenu like a shop, so.. Player clicks on the item they want to buy, it takes money.. How do I do that?

    MaxyMenu.java (MAIN):
    Code:java
    1.  
    2. package noah.plugins.maxymenu;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.block.Action;
    9. import org.bukkit.event.player.PlayerInteractEvent;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class MaxyMenu extends JavaPlugin implements Listener {
    13.  
    14. private Menu menu;
    15. private ShopMenu shopmenu;
    16.  
    17. public void onEnable() {
    18. menu = new Menu(this);
    19. shopmenu = new ShopMenu(this);
    20.  
    21. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    22.  
    23. }
    24.  
    25. @EventHandler
    26. public void onPlayerInteract(PlayerInteractEvent e) {
    27. if (e.getPlayer().getItemInHand().getType() == Material.COAL) {
    28. if (e.getPlayer().hasPermission("MaxyMenu.gmmenu")) {
    29. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    30. menu.show(e.getPlayer());
    31. }
    32. }
    33. }
    34.  
    35. @EventHandler
    36. public void onPlayerInteract1(PlayerInteractEvent e) {
    37. if (e.getPlayer().getItemInHand().getType() == Material.APPLE) {
    38. if (e.getPlayer().hasPermission("MaxyMenu.buyitem")) {
    39. if (!(e.getAction() == Action.RIGHT_CLICK_AIR)) return;
    40. shopmenu.show(e.getPlayer());
    41. }
    42. }
    43. }
    44.  
    45.  
    46.  
    47. }
    48.  


    ShopMenu.java (WHERE I WANT TO TAKE MONEY):
    Code:java
    1.  
    2. package noah.plugins.maxymenu;
    3.  
    4. import java.util.Arrays;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. import org.bukkit.plugin.Plugin;
    17.  
    18. public class ShopMenu implements Listener {
    19.  
    20. private Inventory invShop;
    21. private ItemStack d;
    22.  
    23. public ShopMenu(Plugin p) {
    24. invShop = Bukkit.getServer().createInventory(null, 9, "Maxy Shop");
    25.  
    26. d = createItem(ChatColor.BLUE + "Diamond");
    27.  
    28. invShop.setItem(4, d);
    29.  
    30. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    31.  
    32. }
    33.  
    34. private ItemStack createItem(String name) {
    35. ItemStack i = new ItemStack(Material.DIAMOND);
    36. ItemMeta im = i.getItemMeta();
    37.  
    38. im.setDisplayName(name);
    39. im.setLore(Arrays.asList(ChatColor.GREEN + "Buy " + ChatColor.BLUE + "DIAMOND" + ChatColor.GREEN + "Price: $110" ));
    40. i.setItemMeta(im);
    41.  
    42. return i;
    43. }
    44.  
    45. public void show(Player p) {
    46. p.openInventory(invShop);
    47. }
    48.  
    49. @EventHandler
    50. public void onInventoryClick(InventoryClickEvent e) {
    51. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    52.  
    53. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    54. e.setCancelled(true);
    55. e.getWhoClicked().getInventory().addItem(d);
    56. e.getWhoClicked().closeInventory();
    57. }
    58.  
    59. }
    60.  
    61. }
    62.  
     
  2. Offline

    KaiPol

    First you would have to set up Vault's economy and put it in the onEnable, then it's something like economyname.withdrawPlayer(player, amount)

    I haven't used Vault in a while but it should work.
     
  3. Offline

    Techno

  4. Offline

    KaiPol

    Techno Probably the one where you take the money.
     
  5. Offline

    bramhaag

    PogoStick29 likes this.
  6. Offline

    Techno

    KaiPol
    Okay

    bramhaag
    Yeah, I know.. I've seen it

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

    ImPhantom

  8. Offline

    Techno

    New Problem, error on Class ShopMenu:
    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.  
    13. import org.bukkit.entity.Player;
    14. import org.bukkit.event.EventHandler;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.inventory.InventoryClickEvent;
    17. import org.bukkit.inventory.Inventory;
    18. import org.bukkit.inventory.ItemStack;
    19. import org.bukkit.inventory.meta.ItemMeta;
    20. import org.bukkit.plugin.Plugin;
    21. import org.bukkit.plugin.RegisteredServiceProvider;
    22.  
    23. public class ShopMenu implements Listener, Plugin {
    24.  
    25. private Inventory invShop;
    26. private ItemStack d;
    27.  
    28. public static Economy econ = null;
    29.  
    30. public void onEnable() {
    31. if (!setupEconomy() ) {
    32. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    33. Bukkit.getServer().getPluginManager().disablePlugin(this);
    34. return;
    35. }
    36. }
    37.  
    38. private boolean setupEconomy() {
    39. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    40. return false;
    41. }
    42. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    43. if (rsp == null) {
    44. return false;
    45. }
    46. econ = rsp.getProvider();
    47. return econ != null;
    48. }
    49.  
    50.  
    51. public ShopMenu(Plugin p) {
    52. invShop = Bukkit.getServer().createInventory(null, 9, "Maxy Shop");
    53.  
    54. d = createItem(ChatColor.BLUE + "Diamond");
    55.  
    56. invShop.setItem(4, d);
    57.  
    58. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    59.  
    60. }
    61.  
    62. private ItemStack createItem(String name) {
    63. ItemStack i = new ItemStack(Material.DIAMOND);
    64. ItemMeta im = i.getItemMeta();
    65.  
    66. im.setDisplayName(name);
    67. im.setLore(Arrays.asList(ChatColor.GREEN + "Buy " + ChatColor.BLUE + "DIAMOND" + ChatColor.GREEN + " Price: $110" ));
    68. i.setItemMeta(im);
    69.  
    70. return i;
    71. }
    72.  
    73. public void show(Player p) {
    74. p.openInventory(invShop);
    75. }
    76.  
    77. @EventHandler
    78. public void onInventoryClick(InventoryClickEvent e) {
    79. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    80.  
    81. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    82. Player p = (Player) e.getWhoClicked();
    83. EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    84.  
    85. if (r.transactionSuccess()) {
    86. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    87. p.getInventory().addItem(d);
    88.  
    89. } else {
    90. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    91. }
    92.  
    93. e.setCancelled(true);
    94. e.getWhoClicked().closeInventory();
    95.  
    96. }
    97.  
    98. }
    99. }
    100.  


    Error:
    [​IMG]

    < BUMP >

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

    PogoStick29

    You don't implement Plugin, you extend JavaPlugin. Makes me wonder how you got so far not knowing that, unless you just made a mistake...
     
  10. Offline

    Techno

    PogoStick29
    Oh my god! You replied, dude I love your videos! Thanks so much

    PogoStick29
    New error.. Stack-trace:
    Code:
    [Server thread/ERROR]: 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.NullPointerException
    at noah.plugins.maxymenu.ShopMenuInv.onInventoryClick(ShopMenuInv.java:83) ~[?:?]
    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
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  11. Offline

    bramhaag

    Techno
    Seems like you have a NPE. If the code above is still correct, it is this line:
    EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    I don't use VaultAPI very often. But, maybe 110 has to be a double and not a integer? Like 110.0 or 110D and not 110. If that is not working, you should ask our Java expert PogoStick29
     
  12. Offline

    Techno

    bramhaag
    Yeah, still not working.. I set 11o to 110.0, Its not working :(

    PogoStick29
    Can you help?
     
  13. Offline

    bramhaag

    Techno
    Try using this:

    if(plugin.economy.getBalance(p.getName()) >= 110) {
    econ.withdrawPlayer(p.getName(), 110);
    //do stuff
    }
    else
    {
    //Do stuff
    }

    Instead of:

    EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);

    if (r.transactionSuccess()) {
    p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    p.getInventory().addItem(d);

    } else {
    p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    }
     
  14. Offline

    Techno

  15. Offline

    bramhaag

    Techno
    Whoops! It seems like i didn't edit it. This code should work:
    Code:java
    1. if(econ.getBalance(p.getName(), >= 110) {
    2. econ.withdrawPlayer(p.getName(), 110);
    3. //do stuff
    4. }
    5. else
    6. {
    7. //Do stuff
    8. }
     
  16. Offline

    Techno

    bramhaag
    Theres an error on: p.getName() >= 110

    I removed the , at: p.getName() >= 110
     
  17. Offline

    Barinade

    Just create your own economy plugin. How hard is it to add and subtract in Java?
     
    drtshock likes this.
  18. Offline

    Techno

    Barinade
    I just wanna do it this way..
     
  19. Offline

    mazentheamazin

    Techno
    Please provide line 83 of the class ShopMenuInv
     
  20. Offline

    MordorKing78

    Can u help me with nthis? i am working in multiple classes with itemstacks.. But i just can take it out and it gives me errors
     
  21. Offline

    Techno

    mazentheamazin
    Whole class:
    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.  
    39. private boolean setupEconomy() {
    40. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    41. return false;
    42. }
    43. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    44. if (rsp == null) {
    45. return false;
    46. }
    47. econ = rsp.getProvider();
    48. return econ != null;
    49. }
    50.  
    51.  
    52. public ShopMenuInv(Plugin p) {
    53. invShop = Bukkit.getServer().createInventory(null, 9, "Maxy Shop");
    54.  
    55. d = createItem(ChatColor.BLUE + "Diamond");
    56.  
    57. invShop.setItem(4, d);
    58.  
    59. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    60.  
    61. }
    62.  
    63. private ItemStack createItem(String name) {
    64. ItemStack i = new ItemStack(Material.DIAMOND);
    65. ItemMeta im = i.getItemMeta();
    66.  
    67. im.setDisplayName(name);
    68. im.setLore(Arrays.asList(ChatColor.GREEN + "Buy " + ChatColor.BLUE + "DIAMOND", "" + ChatColor.GREEN + " Price: $110" ));
    69. i.setItemMeta(im);
    70.  
    71. return i;
    72. }
    73.  
    74. public void show(Player p) {
    75. p.openInventory(invShop);
    76. }
    77.  
    78. @EventHandler
    79. public void onInventoryClick(InventoryClickEvent e) {
    80. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    81.  
    82. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    83. Player p = (Player) e.getWhoClicked();
    84.  
    85. if(econ.getBalance(p.getName() >= 110) {
    86. econ.withdrawPlayer(p.getName(), 110);
    87. //do stuff
    88. }
    89. else
    90. {
    91. //Do stuff
    92. }
    93.  
    94. if (r.transactionSuccess()) {
    95. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    96. p.getInventory().addItem(d);
    97.  
    98. } else {
    99. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    100. }
    101.  
    102. e.setCancelled(true);
    103. e.getWhoClicked().closeInventory();
    104.  
    105. }
    106.  
    107. }
    108. }
    109.  
     
  22. Offline

    Techno

    *bump*
     
  23. Offline

    PogoStick29

    Thanks, and sorry if I came off as rude ;)

    Is that error still unsolved? If so, it's an NPE at ShopMenuInv.java at line 83
     
  24. Offline

    Techno

  25. Offline

    PogoStick29

    What exactly is on line 83? Also, I just made a video yesterday about reading stack traces:

     
  26. Offline

    Techno

    PogoStick29
    Yeah, I watched it :)
    Full class of ShopMenuInv:
    Code:
    package noah.plugins.maxymenu;
     
    import java.util.Arrays;
     
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
     
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
     
     
    public class ShopMenuInv extends JavaPlugin implements Listener {
     
    private Inventory invShop;
    private ItemStack d;
     
        public static Economy econ = null;
     
    public void onEnable() {
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                Bukkit.getServer().getPluginManager().disablePlugin(this);
                return;
            }
        }
     
        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;
        }
     
     
    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 ItemStack createItem(String name) {
    ItemStack i = new ItemStack(Material.DIAMOND);
    ItemMeta im = i.getItemMeta();
     
    im.setDisplayName(name); 
    im.setLore(Arrays.asList(ChatColor.GREEN + "Buy " + ChatColor.BLUE + "DIAMOND", "" + ChatColor.GREEN + " Price: $110" ));
    i.setItemMeta(im);
     
    return i;
    }
     
    public void show(Player p) {
    p.openInventory(invShop);
    }
     
    @EventHandler
    public void onInventoryClick(InventoryClickEvent e) {
    if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
     
    if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    Player p = (Player) e.getWhoClicked();
     
    if(econ.getBalance(p.getName() >= 110)) {
    econ.withdrawPlayer(p.getName(), 110);
    //do stuff
    }
    else
    {
    //Do stuff
    }
     
    if (r.transactionSuccess()) {
    p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    p.getInventory().addItem(d);
     
    } else {
    p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    }
     
    e.setCancelled(true);
    e.getWhoClicked().closeInventory();
     
    }
     
    }
    }
    
     
  27. Offline

    PogoStick29

    All I need is that one line.
     
  28. Offline

    Techno

    PogoStick29
    Oh, sorry

    *bump*

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

    PogoStick29

    If you want anyone to help you, you need to post that line I asked for.
     
  30. Offline

    Techno

    PogoStick29
    Okay..
    Code:java
    1.  
    2. if(econ.getBalance(p.getName() >= 110)) {
    3.  
     
Thread Status:
Not open for further replies.

Share This Page