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

    Code0

    Post your whole ShopMenuInv class
     
  2. Code0
    Okay
    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. @SuppressWarnings("null")
    80. @EventHandler
    81. public void onInventoryClick(InventoryClickEvent e) {
    82. if (!(e.getInventory().getName().equalsIgnoreCase(invShop.getName()))) return;
    83.  
    84. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Diamond")) {
    85. Player p = (Player) e.getWhoClicked();
    86.  
    87. if (p == null) {
    88. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "Something bad happened!");
    89. } else {
    90. if(econ.getBalance(p.getName()) >= 110) {
    91. EconomyResponse r = econ.withdrawPlayer(p.getName(), 110);
    92.  
    93. if (r.transactionSuccess()) {
    94. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.GREEN + "You bought 1 of Diamond for $110");
    95. p.getInventory().addItem(d);
    96.  
    97. } else {
    98. p.sendMessage(ChatColor.BLUE + "[" + ChatColor.GREEN + "MaxyMenu" + ChatColor.BLUE + "]" + ChatColor.RED + "You could not buy 1 of Diamond!");
    99. }
    100.  
    101. } else {
    102. //Do stuff
    103. }
    104. }
    105.  
    106. e.setCancelled(true);
    107. e.getWhoClicked().closeInventory();
    108.  
    109. }
    110.  
    111. }
    112. }
    113.  
     
  3. Offline

    Code0

    Code:java
    1. System.out.println(econ == null);
    2.  
    3. What Exactly Are You Trying To Do Here?
    Techno
     
Thread Status:
Not open for further replies.

Share This Page