Custom Shop Help

Discussion in 'Plugin Development' started by CONTREKEE, Aug 24, 2014.

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

    CONTREKEE

    What am I doing wrong in this InventoryClickEvent? Doesn't seem to be working. I have vault installed and all the proper economy code, and I also have another command that works with the economy. Please help. Thank you!


    Code:
        @EventHandler
        public void BuyShop(InventoryClickEvent e) {
           
            Player player = (Player) e.getWhoClicked();
           
            abuy = Bukkit.getServer().createInventory(null, 18, ChatColor.DARK_AQUA + underline + "Buy Shop" + ChatColor.GREEN + bold + " A");
           
            if(e.getCurrentItem() == null) {
                return;
            }
            if(e.getInventory().equals(abuy)) {
                e.setCancelled(true);
                if(e.getCurrentItem().equals(AStoneBuy)) {
                    @SuppressWarnings("deprecation")
                    EconomyResponse r = econ.withdrawPlayer(player.getName(), 20);
                    if(r.transactionSuccess()) {
                    player.getInventory().addItem(new ItemStack(Material.STONE));
                    return;
                }else {
                    player.sendMessage(ChatColor.RED + "Not enough money!");
                    return;
                }
                }
            }
       
    }
     
  2. Offline

    unon1100

    Did you register your event and implement Listener?
     
  3. Offline

    CONTREKEE

    Yes, I did both of those.

    Here is all the code


    Code:java
    1. import java.util.ArrayList;
    2.  
    3. import net.milkbowl.vault.economy.Economy;
    4. import net.milkbowl.vault.economy.EconomyResponse;
    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.event.player.PlayerInteractEvent;
    14. import org.bukkit.inventory.Inventory;
    15. import org.bukkit.inventory.ItemStack;
    16. import org.bukkit.inventory.meta.ItemMeta;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. public class MSUP extends JavaPlugin implements Listener {
    21.  
    22.  
    23. public static Inventory abuy;
    24. public static Inventory asell;
    25.  
    26.  
    27. final String italic = ChatColor.ITALIC + "";
    28. final String bold = ChatColor.BOLD + "";
    29. final String magic = ChatColor.MAGIC + "";
    30. final String strike = ChatColor.STRIKETHROUGH + "";
    31. final String underline = ChatColor.UNDERLINE + "";
    32.  
    33. ItemStack AStoneSell = new ItemStack(Material.STONE, 1);{
    34. ItemMeta AStonesellmeta = AStoneSell.getItemMeta();
    35. ArrayList<String> cc = new ArrayList<String>();
    36. AStonesellmeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Stone");
    37. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "10");
    38. AStonesellmeta.setLore(cc);
    39. AStoneSell.setItemMeta(AStonesellmeta);
    40. }
    41.  
    42. ItemStack AStoneBuy = new ItemStack(Material.STONE, 1);{
    43. ItemMeta AStoneBuymeta = AStoneBuy.getItemMeta();
    44. ArrayList<String> cc = new ArrayList<String>();
    45. AStoneBuymeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Stone");
    46. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "20");
    47. AStoneBuymeta.setLore(cc);
    48. AStoneBuy.setItemMeta(AStoneBuymeta);
    49. }
    50.  
    51. ItemStack ACobblestoneSell = new ItemStack(Material.COBBLESTONE, 1);{
    52. ItemMeta ACobblestoneSellmeta = ACobblestoneSell.getItemMeta();
    53. ArrayList<String> cc = new ArrayList<String>();
    54. ACobblestoneSellmeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Cobblestone");
    55. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "5");
    56. ACobblestoneSellmeta.setLore(cc);
    57. ACobblestoneSell.setItemMeta(ACobblestoneSellmeta);
    58. }
    59.  
    60. ItemStack ACobblestoneBuy = new ItemStack(Material.COBBLESTONE, 1);{
    61. ItemMeta ACobblestoneBuymeta = ACobblestoneBuy.getItemMeta();
    62. ArrayList<String> cc = new ArrayList<String>();
    63. ACobblestoneBuymeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Cobblestone");
    64. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "15");
    65. ACobblestoneBuymeta.setLore(cc);
    66. ACobblestoneBuy.setItemMeta(ACobblestoneBuymeta);
    67. }
    68.  
    69. ItemStack ACoalBuy = new ItemStack(Material.COAL, 1);{
    70. ItemMeta ACoalBuymeta = ACoalBuy.getItemMeta();
    71. ArrayList<String> cc = new ArrayList<String>();
    72. ACoalBuymeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Coal");
    73. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "30");
    74. ACoalBuymeta.setLore(cc);
    75. ACoalBuy.setItemMeta(ACoalBuymeta);
    76. }
    77.  
    78. ItemStack ACoalSell = new ItemStack(Material.COAL, 1);{
    79. ItemMeta ACoalSellmeta = ACoalSell.getItemMeta();
    80. ArrayList<String> cc = new ArrayList<String>();
    81. ACoalSellmeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Coal");
    82. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "15");
    83. ACoalSellmeta.setLore(cc);
    84. ACoalSell.setItemMeta(ACoalSellmeta);
    85. }
    86.  
    87. ItemStack AWoodSell = new ItemStack(Material.LOG, 1);{
    88. ItemMeta AWoodSellmeta = AWoodSell.getItemMeta();
    89. ArrayList<String> cc = new ArrayList<String>();
    90. AWoodSellmeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Log");
    91. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "10");
    92. AWoodSellmeta.setLore(cc);
    93. AWoodSell.setItemMeta(AWoodSellmeta);
    94. }
    95.  
    96. ItemStack AWoodBuy = new ItemStack(Material.LOG, 1);{
    97. ItemMeta AWoodBuymeta = AWoodBuy.getItemMeta();
    98. ArrayList<String> cc = new ArrayList<String>();
    99. AWoodBuymeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Log");
    100. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "20");
    101. AWoodBuymeta.setLore(cc);
    102. AWoodBuy.setItemMeta(AWoodBuymeta);
    103. }
    104.  
    105. ItemStack ABeef = new ItemStack(Material.COOKED_BEEF, 1);{
    106. ItemMeta ABeefmeta = ABeef.getItemMeta();
    107. ArrayList<String> cc = new ArrayList<String>();
    108. ABeefmeta.setDisplayName(ChatColor.DARK_GRAY + underline + "Cooked Beef");
    109. cc.add(ChatColor.GREEN + italic + "$ " + ChatColor.GRAY + "15");
    110. ABeefmeta.setLore(cc);
    111. ABeef.setItemMeta(ABeefmeta);
    112. }
    113.  
    114.  
    115. public static Economy econ = null;
    116.  
    117. public void onEnable() {
    118. Bukkit.getPluginManager().registerEvents(this, this);
    119.  
    120. if (!setupEconomy() ) {
    121. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    122. getServer().getPluginManager().disablePlugin(this);
    123. return;
    124. }
    125. }
    126.  
    127. private boolean setupEconomy() {
    128. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    129. return false;
    130. }
    131. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    132. if (rsp == null) {
    133. return false;
    134. }
    135. econ = rsp.getProvider();
    136. return econ != null;
    137. }
    138.  
    139. @EventHandler
    140. public void BuyShop(InventoryClickEvent e) {
    141.  
    142. Player player = (Player) e.getWhoClicked();
    143.  
    144. abuy = Bukkit.getServer().createInventory(null, 18, ChatColor.DARK_AQUA + underline + "Buy Shop" + ChatColor.GREEN + bold + " A");
    145.  
    146. if(e.getCurrentItem() == null) {
    147. return;
    148. }
    149. if(e.getInventory().equals(abuy)) {
    150. e.setCancelled(true);
    151. if(e.getCurrentItem().equals(AStoneBuy)) {
    152. @SuppressWarnings("deprecation")
    153. EconomyResponse r = econ.withdrawPlayer(player.getName(), 20);
    154. if(r.transactionSuccess()) {
    155. player.getInventory().addItem(new ItemStack(Material.STONE));
    156. return;
    157. }else {
    158. player.sendMessage(ChatColor.RED + "Not enough money!");
    159. return;
    160. }
    161. }
    162. }
    163.  
    164. }


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

    ReggieMOL

    CONTREKEE

    What do you mean by not working? As in nothing at all is happening or there is an error. Send some stacktraces?
     
Thread Status:
Not open for further replies.

Share This Page