How To Filter Items That Go Into An Inventory?

Discussion in 'Plugin Development' started by KuroRyuuNA, Nov 25, 2014.

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

    KuroRyuuNA

    Hello there!

    We are coding a Bukkit Plugin that will allow players to store items in Bookshelves! While we got that part working... we need to be able to filter the items that go into the chest since ONLY books and records will be allowed to be inserted.

    Here is my code so far:

    Code:
    package blueywaka.technagemc.TechnoShelf;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class technoshelf extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable(){
            getLogger().info("TechnoShelf has been enabled! :) ");
        }
       
        @Override
        public void onDisable(){
            getLogger().info("Technoshelf has been disabled! :( ");
           
        }
       
        @EventHandler
        public static void onPlayerInteract(PlayerInteractEvent event){
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (event.getClickedBlock().getType() == Material.BOOKSHELF ){
                   
                   
                    Inventory myInventory = Bukkit.createInventory(null, 27, "Bookshelf");
                   
                   
                                       
                    }
       
               
                   
                }
                   
                }
            }
    
    Question(s):

    What is the event to cancel all items EXCEPT books and records to be inserted into the bookshelf inventory?
     
  2. Offline

    Skionz

    KuroRyuuNA Use the inventory click event, check if the user is clicking on a material that isn't aloud, cancel the event.
     
  3. Offline

    xTrollxDudex

    KuroRyuuNA
    You haven't registered your events yet
     
  4. Offline

    KuroRyuuNA

    Thanks, Skionz!

    Skionz

    What would I do to check the item that was clicked?

    Code:
    package blueywaka.technagemc.TechnoShelf;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class technoshelf extends JavaPlugin implements Listener{
     
        @Override
        public void onEnable(){
            getLogger().info("TechnoShelf has been enabled! :) ");
        }
     
        @Override
        public void onDisable(){
            getLogger().info("Technoshelf has been disabled! :( ");
         
        }
     
        @EventHandler
        public static void onPlayerInteract(PlayerInteractEvent event){
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (event.getClickedBlock().getType() == Material.BOOKSHELF ){
                 
                 
                    Inventory myInventory = Bukkit.createInventory(null, 27, "Bookshelf");
                 
                 
                                     
                    }
     
             
                 
                }
                 
                }
     
        @EventHandler
        public static void onInventoryClickEvent (InventoryClickEvent event){
            if (){
             
            }
        }
            }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
    Skionz likes this.
  5. Offline

    xTrollxDudex

  6. Offline

    KuroRyuuNA

    Thanks for your help, but when I do that:

    Code:
    package blueywaka.technagemc.TechnoShelf;
     
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.block.Block;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.event.Event;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class technoshelf extends JavaPlugin implements Listener{
       
        @Override
        public void onEnable(){
            getLogger().info("TechnoShelf has been enabled! :) ");
        }
       
        @Override
        public void onDisable(){
            getLogger().info("Technoshelf has been disabled! :( ");
           
        }
       
        @EventHandler
        public static void onPlayerInteract(PlayerInteractEvent event){
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK){
                if (event.getClickedBlock().getType() == Material.BOOKSHELF ){
                   
                   
                    Inventory myInventory = Bukkit.createInventory(null, 27, "Bookshelf");
                   
                   
                                       
                    }
       
               
                   
                }
                   
                }
       
        @EventHandler
        public static void onInventoryClickEvent (InventoryClickEvent event){
            if (event.getClickedItem() == Material.BOOK || Material.RECORD_3){
               
            }
        }
            }
    
    I get an error under getClickedItem():
    The method getClickedItem() is undefined for the type InventoryClickEvent
     
  7. Offline

    Skionz

    KuroRyuuNA Why are your events static? Your getting an error because there is no "getClickedItem()" in the InventoryClickEvent class
     
    SuperOriginal likes this.
  8. Offline

    KuroRyuuNA

    Skionz My mistake! I am still learning Bukkit.

    How do I check if they are a Book or Records then?
     
  9. Offline

    Skionz

    KuroRyuuNA
    PHP:
    if(yourItemStack.getType() == Material.BEDROCK) {
        
    //the ItemStack yourItemStack is bedrock
    }
     
  10. Offline

    KuroRyuuNA

    Skionz

    Thanks so much for dealing with my newbiness haha, you have been a big help :)
     
    Skionz likes this.
  11. Offline

    Dragonphase

    Be sure to learn Java and the principles of Object Oriented Programming too, preferably before learning how to use an API like Bukkit.

    See my post here.
     
Thread Status:
Not open for further replies.

Share This Page