Setting both item and cursor to air not working

Discussion in 'Plugin Development' started by RandomHashTags, Sep 26, 2015.

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

    RandomHashTags

    I am trying to set both the cursor, and the item to air. When I do it, you can still get the item, the only that is suppose to be removed, and you can duplicate it.

    Code:
    package randomArmorEffects.Books;
    
    import org.bukkit.Material;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.ItemStack;
    
    public class ClickEvent implements Listener {
       
        @SuppressWarnings("deprecation")
        @EventHandler
        private void inventory(InventoryClickEvent event) throws InterruptedException {
           
            ItemStack air = new ItemStack(Material.AIR);
            ItemStack item = event.getCurrentItem().clone();
           
            if(event.getCursor().getType().name().endsWith("BOOK")) { // If item on cursor is a book
                System.out.println("Ends in BOOK");
                if(event.getCurrentItem().getType().name().endsWith("BOOTS")) {
                    System.out.println("ItemOnCursor ends with BOOTS");
                    if(event.getCursor().getType().name().endsWith("BOOK")) {
    // Removes the book, does not remove the item on cursor
                        event.setCurrentItem(air);
                        event.setCursor(air);
                        event.getInventory().addItem(item);
                        System.out.println("WORKED");
                    }
                }
            }
        }
    }
    
     
  2. Offline

    tcvs

    @RandomHashTags Could you elaborate what you mean "Set both cursor and item to air". I don't understand what you are referring to as the "cursor".
     
  3. Offline

    RandomHashTags

    The cursor is what the item is when you are holding it. I want to make it air, but I can't get it too work. SetCurrentItem is the item you are hovering over. I can get that to work, but not the cursor.
     
  4. Offline

    tcvs

    @RandomHashTags Oh. You mean the item that the user has picked up using their mouse.

    Have you tried setting the cursor item to null? Not sure if that would work. btw tahg me if you want me to respond faster in the future.
     
  5. Offline

    RandomHashTags

    Throws a NPE.
     
  6. Offline

    tcvs

    @RandomHashTags Not surprised... I will have to think about this one as I have never had to set the cursor before.
     
  7. Offline

    RandomHashTags

  8. Offline

    dznSpencer

    Here's a snip from one of my plugins.

    Also, Make sure to cancel your event.


    Code:
        @EventHandler
        public void onPlayerClickInInventoryEvent(InventoryClickEvent e) {
    
            HumanEntity p = e.getWhoClicked();
    
            if (e.getAction() == InventoryAction.SWAP_WITH_CURSOR) {
    
                if (e.getCursor().hasItemMeta()) {
                    if (e.getCursor().getType() == Material.NETHER_STAR) {
    
                        ItemStack current = e.getCurrentItem();
                        ItemMeta meta = e.getCurrentItem().getItemMeta();
    
                        List<String> lore = new ArrayList<>();
                        lore.add(e.getCursor().getItemMeta().getDisplayName());
                        meta.setLore(lore);
                        current.setItemMeta(meta);
                        e.setCancelled(true);
                        //What you're looking for
                        e.setCursor(new ItemStack(Material.AIR));
    
                    }
                }
    
            }
        }
     
  9. Offline

    RandomHashTags

  10. Offline

    dznSpencer

    Did you cancel the event? Did you copy & Paste my code?..

    This code snippet is for one of my plugins. It's for display. If you try it, You won't have much luck.
     
  11. Offline

    RandomHashTags

    @dznSpencerI copied and pasted the code, and changed it to my needs. The Swap doesn't even register...
     
  12. Offline

    Zombie_Striker

  13. Offline

    RandomHashTags

    Code:
        @SuppressWarnings("deprecation")
        @EventHandler
        private void inventory(InventoryClickEvent event){
            //ItemStack item = event.getCurrentItem().clone();
            ItemStack air = new ItemStack(Material.AIR);
           
            if(event.getAction() == InventoryAction.SWAP_WITH_CURSOR) {
                System.out.println("Ends in BOOK");
                if(event.getCurrentItem().getType().name().endsWith("BOOTS")) {
                    event.setCurrentItem(air);
                    event.setCursor(new ItemStack(Material.AIR));
                    System.out.println("ItemOnCursor ends with BOOTS");
                    if(event.getCursor().getType().name().endsWith("BOOK")) {
                        event.setCurrentItem(air);
                        event.setCursor(new ItemStack(Material.AIR));
                        event.setCancelled(true);
                        System.out.println("WORKED");
                    }
                }
            }
        }
     
  14. Offline

    Zombie_Striker

    Keep it consistent. Either create two new instances of air, or use the same field.

    What does the console print out? Is the event even getting triggered?
     
  15. Offline

    RandomHashTags

    Its not getting registered at all.
     
  16. Offline

    dznSpencer

    Did you register your events?

    Are your names for items colored? If they are you need to strip the colors when you check. Also make sure capitalization is correct.
     
  17. Offline

    RandomHashTags

    I registered everything. The event itself doesn't work.
     
  18. Offline

    Zombie_Striker

    @RandomHashTags
    Can we see the whole class and the class that is registering the event.
     
  19. Offline

    RandomHashTags

    Code:
    package randomArmorEffects.Books;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Random;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.FireworkEffect;
    import org.bukkit.Material;
    import org.bukkit.entity.Firework;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryAction;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.FireworkMeta;
    import org.bukkit.inventory.meta.ItemMeta;
    
    public class Books implements Listener {
    
    // My other code is not going to be shown here.
       
        @SuppressWarnings("deprecation")
        @EventHandler // Doesn't even register/work
        private void inventory(InventoryClickEvent event){
            //ItemStack item = event.getCurrentItem().clone();
            ItemStack air = new ItemStack(Material.AIR);
           
            if(event.getAction() == InventoryAction.SWAP_WITH_CURSOR) { 
                System.out.println("Ends in BOOK"); // Doesn't even work
                if(event.getCurrentItem().getType().name().endsWith("BOOTS")) {
                    event.setCurrentItem(air);
                    event.setCursor(new ItemStack(Material.AIR));
                    System.out.println("ItemOnCursor ends with BOOTS");
                    if(event.getCursor().getType().name().endsWith("BOOK")) {
                        event.setCurrentItem(air);
                        event.setCursor(new ItemStack(Material.AIR));
                        event.setCancelled(true);
                        System.out.println("WORKED");
                    }
                }
            }
        }
    }
     
  20. Offline

    dznSpencer

    @RandomHashTags System.out.println does not log to the console in bukkit.

    Can we see the ItemStack of the "BOOTS"?
     
  21. Offline

    RandomHashTags

    @dznSpencer The System.println() does work for bukkit. I do it all the time. Their is no ItemStack for "BOOTS". Read the code .-.
     
  22. Offline

    RandomHashTags

Thread Status:
Not open for further replies.

Share This Page