Solved Having trouble with InventoryClickEvent Cancelling

Discussion in 'Plugin Development' started by EngineersBox, Jan 16, 2018.

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

    EngineersBox

    So I'm working on a plugin for a block palette to show different biome specific block textures, and ive run aground!
    I'm trying to create an inventory that has a few options to go to sub-inventories. However at the moment i cant figure out how to incorporate setCancelled(true); so that the items cannot be moved inside the inventory

    I've had a look around at other posts and bits of info and i still cant quite get my head around it. Any help would be much appreciated.

    Here is the Main.java:

    PHP:
    package me.engineersbox.menuinv;
    import java.util.ArrayList;
    import java.util.List;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.ChatColor;
    public class 
    Main extends JavaPlugin implements Listener {
     
     
      public static 
    Inventory BiomePalette;
     
     
      public 
    void onEnable() {
      
      
    this.getLogger().info("BlockPalette Enabled");
      
      
    getCommand("blockpalette").setExecutor(new Commands());
      
      
    BiomePalette Bukkit.createInventory(null9"Biome Block Palette");
      
    getServer().getPluginManager().registerEvents(thisthis);
      
      
    ItemStack leaves = new ItemStack(Material.LEAVES);
      
    ItemMeta im leaves.getItemMeta();
      
    im.setDisplayName(ChatColor.GREEN "");
      List<
    StringloreList = new ArrayList<String>();
      
    loreList.add(ChatColor.GREEN "Forest");
      
    loreList.add(ChatColor.YELLOW "Biome Palette");
      
    im.setLore(loreList);
      
    leaves.setItemMeta(im);
      
      
    ItemStack stone = new ItemStack(Material.SANDSTONE);
      
    ItemMeta im1 stone.getItemMeta();
      
    im1.setDisplayName(ChatColor.RED "");
      List<
    StringloreList1 = new ArrayList<String>();
      
    loreList1.add(ChatColor.RED "Desert");
      
    loreList1.add(ChatColor.YELLOW "Biome Palette");
      
    im1.setLore(loreList1);
      
    stone.setItemMeta(im1);
      
      
    BiomePalette.addItem(leavesstone);
      }
     
     
      @
    SuppressWarnings("deprecation")
      @
    EventHandler
      
    public void onPlayerUse(PlayerInteractEvent event){
      
    Player p event.getPlayer();
      
      if(
    p.getItemInHand().getType() == Material.BLAZE_POWDER){
      
    event.getPlayer().openInventory(BiomePalette);
      }
      if(
    p.getInventory().equals(BiomePalette)) {
      
    event.setCancelled(true);
      }
      }
    }
     
    Last edited: Jan 16, 2018
  2. Offline

    RunsWithShovels

    @EngineersBox The event that you are cancelling in your code is the playerinteractevent. You need to make another event to listener for inventoryclicks and cancel that.
     
  3. Offline

    EngineersBox

    Ah ok, I’ll give that a go.
     
  4. Offline

    EngineersBox

    @RunsWithShovels i still cant get it to work, neither the main class nor the command class seem to work

    this is the code I'm using for the Main.java:
    PHP:
    @EventHandler
        
    public void onClick(InventoryClickEvent event) {
            
    event.setCancelled(true);
           
        }
    This is the code for Cmd.java:
    PHP:
    @EventHandler
      
    public void onClick(InventoryClickEvent event) {
       
       
    event.setCancelled(true);
       
       
    Player player = (Playerevent.getWhoClicked(); // The player that clicked the item
       
    ItemStack clicked event.getCurrentItem(); // The item that was clicked
       
    Inventory inventory event.getInventory(); // The inventory that was clicked in
       
       
       
    if (inventory.getName().equals(bp.getName())) { // The inventory is our custom Inventory
       
       
    if (clicked.getType() == Material.GRASS) { // The item that the player clicked it dirt
       
       
    event.setCancelled(true); // Make it so the dirt is back in its original spot
       
    player.closeInventory(); // Closes there inventory
       
    player.getInventory().addItem(new ItemStack(Material.GRASS1)); // Adds dirt
       
    player.closeInventory();
       }
       }
      }
     
  5. Offline

    Machine Maker

    Are you registering the event?
     
  6. Offline

    Colinus999

    If an Event had already been cancelled (like in your Main.java), the following Events will not fire. Use @EventHandler(ignoreCancelled = true) in your Cmd.java.

    Also, add debug messages.
     
Thread Status:
Not open for further replies.

Share This Page