Solved GUI not sensing click events

Discussion in 'Plugin Development' started by Xp10d3, Dec 20, 2019.

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

    Xp10d3

    Hello. I am having some trouble with my plugin sensing inventory click events. I've created a GUI that is customizable through a config (and copy and paste lol) yet when I try to sense it through an inventory click event, it does nothing. I put a console logger in it to see if it even registers, but it doesn't work. The GUI is customizable, but the click events aren't for some reason. Here is an example of the line I have:
    Code:
    if ((event.getCurrentItem().getType() == itemZero) && event.getRawSlot() < event.getInventory().getSize()) {
    
    Yes, I know it's a bunch of copy and paste. But it was the best idea I had :/
    Here is the config if it helps. It is not filled in, but when I tested it was. I get a null error (which I hope to fix) if it isn't filled in. This is also probably not the most updated version but I'm on a different computer at the moment so I can't get it.
    Thanks for the help!
    -Xp10d3

    EDIT: Bukkit won't let me post my code so I'm just posting the files xD Same thing, though; config isn't fully updated.
     

    Attached Files:

    • src.zip
      File size:
      7.6 KB
      Views:
      4
  2. Offline

    Strahan

    You can't just arbitrarily add things to the event constructor. It has to be a known constructor.

    PS and holy mother of Jesus that's a cluster-f of copy and paste heh. You mention it in your code, so you are aware that's bad design, yet you do it anyway? :) That could be seriously optimized quite easily.

    Any time you find yourself copy/pasting blocks of code with just minor changes, you need to stop and re-evaluate your process.

    Here is how I'd do it:
    Code:
    @EventHandler
    public void onItemClick(InventoryClickEvent event) {
        Player player = (Player) event.getWhoClicked();
        String itemName = event.getCurrentItem().getType().name();
       
        ConfigurationSection cfg = getConfig().getConfigurationSection("shop.items");
        if (cfg == null) return;
        if (!cfg.getKeys(false).contains(itemName)) return;
       
        int cost = cfg.getInt(itemName + ".cost");
        String command = cfg.getString(itemName + ".command");
        if (command == null) return;
       
        try {
            PreparedStatement statement = this.getConnection()
                    .prepareStatement("SELECT * FROM " + this.table + " WHERE UUID=?");
            statement.setString(1, player.getUniqueId().toString());
            ResultSet results = statement.executeQuery();
            results.next();
           
            int coins = results.getInt("GOLD");
            if (coins >= cost) {
                player.closeInventory();
                coins -= cost;
                getServer().dispatchCommand(getServer().getConsoleSender(), command);
            } else {
                player.closeInventory();
                player.sendMessage("You don't have enough coins!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    That relies on a config structured thus:
    Code:
    shops:
      items:
        BLUE_WOOL:
          command: time set 1000
          price: 50
        BLACK_WOOL:
          command: time set 13000
          price: 50
     
    Last edited: Dec 20, 2019
    Xp10d3 likes this.
  3. Offline

    Xp10d3

    Thank you! That was extremely helpful :)
     
    Last edited: Dec 25, 2019
Thread Status:
Not open for further replies.

Share This Page