Solved How to make players unable to change the GUI?

Discussion in 'Plugin Development' started by Ogami Shiro, Feb 10, 2023.

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

    Ogami Shiro

    Spigot 1.16.
    I know several ways, but I'm not sure if it's right.
    My English may be bad.

    GUI:
    Code:
        public static Inventory getGUI (String playerName) {
            Inventory mAGUI = Bukkit.createInventory(null, 9, confHandler.getBS("GUI.name"));
    
            ItemStack glass = new ItemStack(Material.getMaterial(confHandler.getBS("GUI.glass")));
            mAGUI.setItem(0, glass);
            mAGUI.setItem(8, glass);
    
            for (String section:confHandler.getKA("GUI.sections")) {
                ItemStack sectionItem = new ItemStack(Material.getMaterial(confHandler.getBS("GUI.sections."+section+".material")));
                ItemMeta sectionItemMeta = sectionItem.getItemMeta();
                sectionItemMeta.setDisplayName(confHandler.getBS("GUI.sections."+section+".name"));
                sectionItemMeta.setLore(confHandler.getBSA("GUI.sections."+section+".lore", playerName));
                sectionItem.setItemMeta(sectionItemMeta);
                mAGUI.addItem(sectionItem);
            }
    
            return mAGUI;
        }
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Ogami Shiro That is code for making the gui, not being unable to change it.
     
  3. Offline

    Ogami Shiro

    I need to make this GUI immutable.

    Is it correct to use the following method?

    GUI:
    Code:
        public static InventoryHolder inventoryHolder;
    
        public static Inventory getGUI (String playerName) {
            inventoryHolder = new InventoryHolder() {
                @Override
                public Inventory getInventory() {
                    return null;
                }
            };
    
            Inventory mAGUI = Bukkit.createInventory(inventoryHolder, 9, confHandler.getBS("GUI.name"));
    Event:
    Code:
       @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getHolder()==inventoryHolder) {
                event.setCancelled(true);
            }
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Feb 11, 2023
  4. Offline

    timtower Administrator Administrator Moderator

    @Ogami Shiro Have you tried that method? Or did you only write it?
     
  5. Offline

    Ogami Shiro

    Yes, I've tried it. It works. But maybe there is a shorter and more correct way.
     
  6. Offline

    mehboss

    No, you are correct -- Utilizing the InventoryClickEvent to cancel clicks. It is the "shortest" and "more correct way"! :)
     
  7. Offline

    Ogami Shiro

    Is that sarcasm? If so, can you suggest your own option to prohibit GUI changes?
     
  8. Offline

    mehboss

    Not sarcasm, sorry.
     
Thread Status:
Not open for further replies.

Share This Page