Solved Inventory problems in my plugin

Discussion in 'Plugin Development' started by Kody_Jay, Oct 30, 2016.

Thread Status:
Not open for further replies.
  1. Okay, so basically, I have made a plugin that will have a range of blocks in there, that when clicked would do something to the player in game. This is accessed by an op or someone with the permission node by typing /adminpanel

    But, I have made it so they cannot pick up, throw or move items around in the GUI, but that stops people from doing these things at all. So they can't do it, even if they are not in the GUI, may someone help me?

    Here is the class that blocks the items from being moved, they are at the bottom of the class:

    Code:
    package me.kody_jay.inventorymenu;
    
    import java.util.Arrays;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerDropItemEvent;
    import org.bukkit.event.player.PlayerPickupItemEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;
    
    public class Menu implements Listener {
    
        private Inventory inv;
        private ItemStack c, s, a;
     
        public Menu(Plugin p) {
            inv = Bukkit.getServer().createInventory(null, 9, ChatColor.DARK_RED + "Control Panel");
         
            c = createItem(DyeColor.LIME, ChatColor.GREEN + "Creative");
            s = createItem(DyeColor.RED, ChatColor.RED + "Survival");
            a = createItem(DyeColor.YELLOW, ChatColor.YELLOW + "Adventure");
         
            inv.setItem(2, c);
            inv.setItem(4, s);
            inv.setItem(6, a);
         
            Bukkit.getServer().getPluginManager().registerEvents(this, p);
        }
     
        private ItemStack createItem(DyeColor dc, String name) {
            ItemStack i = new Wool(dc).toItemStack(1);
            ItemMeta im = i.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList("Set your gamemode", "to " + ChatColor.ITALIC + name.toLowerCase() + " mode."));
            i.setItemMeta(im);
            return i;
        }
     
        public void show(Player p) {
            p.openInventory(inv);
        }
     
        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!(e.getInventory().equals(inv))) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Creative")) {
                e.getWhoClicked().setGameMode(GameMode.CREATIVE);
                e.getWhoClicked().closeInventory();
                return;
            }
         
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Survival")) {
                e.getWhoClicked().setGameMode(GameMode.SURVIVAL);
                e.getWhoClicked().closeInventory();
                return;
            }
         
            if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Adventure")) {
                e.getWhoClicked().setGameMode(GameMode.ADVENTURE);
                e.getWhoClicked().closeInventory();
                return;
            }
         
        }
    
    
        @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e){
            e.setCancelled(true);
        }
        @EventHandler
        public void onPickup(PlayerPickupItemEvent e){
            e.setCancelled(true);
        }
        @EventHandler
        public void onPlayerDropItem(PlayerDropItemEvent e){
            e.setCancelled(true);
        }
    }
    Here is my main class:

    Code:
    package me.kody_jay.inventorymenu;
    
    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.plugin.java.JavaPlugin;
    
    public class MenuInv extends JavaPlugin implements Listener {
    
        private Menu menu;
     
        public void onEnable() {
            menu = new Menu(this);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     
        @EventHandler
        public boolean onCommand(CommandSender sender, Command cmd, String Label, String[] args) {
        if(cmd.getName().equalsIgnoreCase("adminpanel")) {
            Player p = (Player) sender;
                if (!p.hasPermission("adminpanel.panel")) {
                    p.sendMessage(ChatColor.RED + "No permission!");
                } else {
                    menu.show(p.getPlayer());
                }
             }
            return true;
        }
     
    }
    Here is my plugin.yml:

    Code:
    name: InventoryMenu
    version: 1.0
    main: me.kody_jay.inventorymenu.MenuInv
    description: MenuInv
    
    commands:
        adminpanel:
            description: Displays the control panel.
            permission-node: adminpanel.panel
            usage: /<command>
    Thanks :)
     
    Last edited: Nov 2, 2016
  2. Offline

    Whoneedspacee

    Code:
     @EventHandler
        public void onInventoryClickEvent(InventoryClickEvent e){
            e.setCancelled(true);
        }
    
    Of course that's going to effect every inventory, check through the event if the inventory is the inventory that you created, then cancel it.
     
  3. Offline

    RenditionsRule

    Also, don't cast to a Player if you haven't check who is running the command. If the console or a commandblock runs that command, it's going to throw an error.
     
  4. There is my code, i use that to cancel InventoryClickEvent:
    Code:
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if (ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Name of your menu")) {
                  event.setCancelled(true);
            }
        }
    But you can make variable for your gui's name
     
Thread Status:
Not open for further replies.

Share This Page