Solved InventoryClickEvent

Discussion in 'Plugin Development' started by Albkad, Feb 25, 2017.

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

    Albkad

    So The Plugin Is Giving Me An error With The Inventory ClickEvent.
    Console: https://gyazo.com/1baa13bf14fba279699ff4b98e1e0c93
    Code:
    package me.albkad.netherbuy;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    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.material.Chest;
    import org.bukkit.plugin.RegisteredServiceProvider;
    import org.bukkit.plugin.java.JavaPlugin;
    
    import net.milkbowl.vault.economy.Economy;
    import net.milkbowl.vault.economy.EconomyResponse;
    
    
    public class Main extends JavaPlugin implements Listener {
        public static Economy econ = null;
        String Command1 = getConfig().getString("Item1." + "Command");
       
        public void onEnable() {
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
            getConfig().options().copyDefaults(true);
            saveConfig();
            if (!setupEconomy() ) {
                getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
                getServer().getPluginManager().disablePlugin(this);
                return;
            }
            Menu();
    
        }
       
        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 boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
            if (cmd.getName().equalsIgnoreCase("ingbuy") && sender instanceof Player) {
                Player p = (Player) sender;
                if (p.getPlayer() == null) {
                    return true;
                }
               
               
            //    if (inv == null) {
                //    return true;
            //    }
                p.getPlayer().openInventory(inv);
            }
           
           
           
            return true;
        }
        private ItemStack y;
        private ItemStack e;
        private ItemStack c;
        private Inventory inv;
       
       
        public void Menu() {
            inv = Bukkit.getServer().createInventory(null, 54, "BuyIngame");
           
           
                c = createItem1(getConfig().getString("Item1." + ".ItemName"));
                y = createItem3(getConfig().getString("Item3." + ".ItemName"));
                e = createItem2(getConfig().getString("Item2." + ".ItemName"));
                inv.setItem(getConfig().getInt("Item2." + ".ItemSlot"), y);
                inv.setItem(getConfig().getInt("Item3." + ".ItemSlot"), e);
                inv.setItem(getConfig().getInt("Item1." + ".ItemSlot"), c);
        }
       
        private ItemStack createItem1(String name) {
            ItemStack i = new Chest().toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item2." + ".Cost")));
            i.setItemMeta(im);
            return i;
           
        }
        private ItemStack createItem2(String name) {
            ItemStack i = new Chest().toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item2." + ".Cost")));
            i.setItemMeta(im);
            return i;
           
        }
        private ItemStack createItem3(String name) {
            ItemStack i = new Chest().toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item3." + ".Cost")));
            i.setItemMeta(im);
            return i;
           
        }
       
       
           
       
       
       
       
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
           
            if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
            if (e.getCurrentItem() == null) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item1." + ".ItemName"))) {
                e.setCancelled(true);
                Player p = (Player) e.getWhoClicked();
                EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item1." + ".Cost"));
                if (r.transactionSuccess()) {
                    if(Bukkit.getServer() == null) return;
                    if(Bukkit.getServer().getConsoleSender() == null) return;
                    if (getConfig().getString("Item1." + ".Command") == null) {
                        return;
                    }
                    Command1 = Command1.replaceAll("%Player%", p.getName());
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), getConfig().getString(Command1));
                    p.closeInventory();
                } else {
                    p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
                    p.closeInventory();
                }
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item2." + ".ItemName"))) {
                e.setCancelled(true);
                Player p = (Player) e.getWhoClicked();
                EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item2." + ".Cost"));
                if (r.transactionSuccess()) {
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), getConfig().getString("Item2." + ".Command"));
                    p.closeInventory();
                } else {
                    p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
                    p.closeInventory();
                }
            }
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item3." + ".ItemName"))) {
                e.setCancelled(true);
                Player p = (Player) e.getWhoClicked();
                EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item3." + ".Cost"));
                if (r.transactionSuccess()) {
                    Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "give " + p.getName() + " gold_ingot 1");
                    p.closeInventory();
                   
                } else {
                    p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
                    p.closeInventory();
                }
            }
        }
    // Bukkit.getServer().getConsoleSender(), getConfig().getString("Item3." + ".Command"
    }
    
     
  2. Online

    timtower Moderator Moderator

    @Albkad Set the value of Command1 in the onEnable, not before it.
     
  3. Offline

    PQE

    Why are you using one class to hold all this information? Make a main class, a command class and a listener class.

    Also why don't you check if Command1 == null instead of getting the same value again, seems redundant.
    [​IMG]
    And I'm pretty sure this is your mistake, what is "Item1..Command" in your config, is it an actual thing?? Same issue when you register your value at the top.

    Also not sure if this is necessary for .replace method, but maybe check if the string contains "%Player%". Please use multiple classes, I can't stress this enough.
     
  4. Offline

    Albkad

    @timtower @PQE The Thing Is The Problem Has Nothing To Do With The Config And Command1. The Problem Is When You Click The Item In The Gui. I Need A Way To Stop. CommandLine Cannot Be Null.
     
  5. Online

    timtower Moderator Moderator

    @Albkad Did you make the change that I told you to do?
     
  6. Offline

    PQE

    Literally all of your grabbing values from config will return null, you need to fix them all. Like what the hell is this:
    Code:
    getConfig().getString("Item1." + ".ItemName")
    Unless you have a "Item1..ItemName" key in your config it will return null for all of them, so none of your code will ever be reached. Why are you even joining "Strings" with the +, unless you have assigned a certain thing a string value there is no need.
     
  7. Offline

    Albkad

    It isnt the config that is bugging up it was a problem Before i even created the config @PQE @timtower
    And The "Item1." + ".Command" Goes To
    Item1:
    Command: (Command)
     
    Last edited: Feb 25, 2017
  8. Offline

    PQE

    No it doesn't, "Item1.Command" gets that.

    I'm convinced your whole issue is because of the config returning a null value.
     
  9. Online

    timtower Moderator Moderator

    @Albkad So you get Command1, before the configs exists so it will be null.
    Then you try to use it in a command which throws an error that it can't be null.
    Make the change, post the result.
     
  10. Offline

    PQE

    @timtower
    Doesn't onEnable get called before any other code is read?
     
  11. Online

    timtower Moderator Moderator

  12. Offline

    Albkad

    @timtower @PQE The Thing You DOnt UnderStand Is That I Have Tested It WithOut Command1 And Config And It Still Doesnt Work. It Seems Like You Guys Dont Know Anything Ur Talking About. And "Item1." + ".Command" Also Returns The Command. (It Is Tested And I Know It Works). So Please Stop Talking About Command1 And Config Because That Is Irrelevant In This Case.
     
  13. Online

    timtower Moderator Moderator

    @Albkad Then post your code again but with the line marked where this error occurs.
     
  14. Offline

    Albkad

    Here Is The Code Without The Command1 In Use.
    Console: https://gyazo.com/d3bc34d644748e405dba23a0db32f490



    Code:java
    1. package me.albkad.netherbuy;
    2.  
    3. import java.util.Arrays;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    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.material.Chest;
    17. import org.bukkit.plugin.RegisteredServiceProvider;
    18. import org.bukkit.plugin.java.JavaPlugin;
    19.  
    20. import net.milkbowl.vault.economy.Economy;
    21. import net.milkbowl.vault.economy.EconomyResponse;
    22.  
    23.  
    24. public class Main extends JavaPlugin implements Listener {
    25. public static Economy econ = null;
    26. // String Command1 = getConfig().getString("Item1." + ".Command");
    27.  
    28. public void onEnable() {
    29. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    30. getConfig().options().copyDefaults(true);
    31. saveConfig();
    32. if (!setupEconomy() ) {
    33. getLogger().severe(String.format("[%s] - Disabled due to no Vault dependency found!", getDescription().getName()));
    34. getServer().getPluginManager().disablePlugin(this);
    35. return;
    36. }
    37. Menu();
    38.  
    39. }
    40.  
    41. private boolean setupEconomy() {
    42. if (getServer().getPluginManager().getPlugin("Vault") == null) {
    43. return false;
    44. }
    45. RegisteredServiceProvider<Economy> rsp = getServer().getServicesManager().getRegistration(Economy.class);
    46. if (rsp == null) {
    47. return false;
    48. }
    49. econ = rsp.getProvider();
    50. return econ != null;
    51. }
    52.  
    53.  
    54.  
    55.  
    56. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    57. if (cmd.getName().equalsIgnoreCase("ingbuy") && sender instanceof Player) {
    58. Player p = (Player) sender;
    59. if (p.getPlayer() == null) {
    60. return true;
    61. }
    62.  
    63.  
    64. // if (inv == null) {
    65. // return true;
    66. // }
    67. p.getPlayer().openInventory(inv);
    68. }
    69.  
    70.  
    71.  
    72. return true;
    73. }
    74. private ItemStack y;
    75. private ItemStack e;
    76. private ItemStack c;
    77. private Inventory inv;
    78.  
    79.  
    80. public void Menu() {
    81. inv = Bukkit.getServer().createInventory(null, 54, "BuyIngame");
    82.  
    83.  
    84. c = createItem1(getConfig().getString("Item1." + ".ItemName"));
    85. y = createItem3(getConfig().getString("Item3." + ".ItemName"));
    86. e = createItem2(getConfig().getString("Item2." + ".ItemName"));
    87. inv.setItem(getConfig().getInt("Item2." + ".ItemSlot"), y);
    88. inv.setItem(getConfig().getInt("Item3." + ".ItemSlot"), e);
    89. inv.setItem(getConfig().getInt("Item1." + ".ItemSlot"), c);
    90. }
    91.  
    92. private ItemStack createItem1(String name) {
    93. ItemStack i = new Chest().toItemStack(1);
    94. ItemMeta im = i.getItemMeta();
    95. im.setDisplayName(name);
    96. im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item2." + ".Cost")));
    97. i.setItemMeta(im);
    98. return i;
    99.  
    100. }
    101. private ItemStack createItem2(String name) {
    102. ItemStack i = new Chest().toItemStack(1);
    103. ItemMeta im = i.getItemMeta();
    104. im.setDisplayName(name);
    105. im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item2." + ".Cost")));
    106. i.setItemMeta(im);
    107. return i;
    108.  
    109. }
    110. private ItemStack createItem3(String name) {
    111. ItemStack i = new Chest().toItemStack(1);
    112. ItemMeta im = i.getItemMeta();
    113. im.setDisplayName(name);
    114. im.setLore(Arrays.asList("Cost $" + getConfig().getInt("Item3." + ".Cost")));
    115. i.setItemMeta(im);
    116. return i;
    117.  
    118. }
    119.  
    120.  
    121.  
    122.  
    123.  
    124.  
    125.  
    126.  
    127. @EventHandler
    128. public void onInventoryClick(InventoryClickEvent e) {
    129.  
    130. if (!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
    131. if (e.getCurrentItem() == null) return;
    132. if (e.getCurrentItem().getItemMeta() == null) return;
    133. if (e.getCurrentItem().getItemMeta().getDisplayName() == null) return;
    134. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item1." + ".ItemName"))) {
    135. e.setCancelled(true);
    136. Player p = (Player) e.getWhoClicked();
    137. EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item1." + ".Cost"));
    138. if (r.transactionSuccess()) {
    139. if(Bukkit.getServer() == null) return;
    140. if(Bukkit.getServer().getConsoleSender() == null) return;
    141. // if (getConfig().getString(Command1) == null) {
    142. // return;
    143. // }
    144. //Command1 = Command1.replaceAll("%Player%", p.getName());
    145. // Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), getConfig().getString(Command1));
    146. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "give " + p.getName() + " gold_ingot 1");
    147. p.closeInventory();
    148. } else {
    149. p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
    150. p.closeInventory();
    151. }
    152. }
    153.  
    154. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item2." + ".ItemName"))) {
    155. e.setCancelled(true);
    156. Player p = (Player) e.getWhoClicked();
    157. EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item2." + ".Cost"));
    158. if (r.transactionSuccess()) {
    159. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), getConfig().getString("Item2." + ".Command"));
    160. p.closeInventory();
    161. } else {
    162. p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
    163. p.closeInventory();
    164. }
    165. }
    166. if (e.getCurrentItem().getItemMeta().getDisplayName().contains(getConfig().getString("Item3." + ".ItemName"))) {
    167. e.setCancelled(true);
    168. Player p = (Player) e.getWhoClicked();
    169. EconomyResponse r = econ.withdrawPlayer(p, getConfig().getInt("Item3." + ".Cost"));
    170. if (r.transactionSuccess()) {
    171. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), "give " + p.getName() + " gold_ingot 1");
    172. p.closeInventory();
    173.  
    174. } else {
    175. p.sendMessage(ChatColor.RED + "ERROR: Player Dont Have Enough Money!");
    176. p.closeInventory();
    177. }
    178. }
    179. }
    180. // Bukkit.getServer().getConsoleSender(), getConfig().getString("Item3." + ".Command"
    181. }
    182.  
     
    Last edited by a moderator: Feb 25, 2017
  15. Online

    timtower Moderator Moderator

    @Albkad Could you print this value:
    getConfig().getString("Item2." + ".Command")
     
  16. Offline

    Albkad

    @timtower OMG I Found The Problem :/
    I Forgot To Return :/
     
    Last edited: Feb 25, 2017
Thread Status:
Not open for further replies.

Share This Page