Solved if ItemFrame isEmpty bug

Discussion in 'Plugin Development' started by f3-AR, May 18, 2019.

  1. Offline

    f3-AR

    I am creating a Plugin to get free items whe you right click an Item Frame. Everything works but the if (iframe.isEmpty) part seems broken. I dont know why
    Code:
    public class Freeitem implements Listener{
        @EventHandler
        public void onFreeSign(PlayerInteractEntityEvent e) {
            if (e.getRightClicked() instanceof ItemFrame) {
                ItemFrame iframe = (ItemFrame) e.getRightClicked();
                Player p = e.getPlayer();
                if (iframe.isEmpty()) {
                    if (p.hasPermission("fccore.itemframe"))
                    p.sendMessage("§1[§aFearcraft§1] §aItemframe Created");
                } else {
                    Inventory inv = Bukkit.createInventory(null, 9, "§1Free Stuff");
                    inv.setItem(4, new ItemStack(iframe.getItem()));
                    p.openInventory(inv);
                    e.setCancelled(true);
                }
            }
        }
    }
    Can Someone help me?
     
  2. Online

    timtower Ninja on the waves Moderator

    @f3-AR What do you define as broken?
    Broken as it always returns true?
     
  3. Offline

    f3-AR

    yes
     
  4. Online

    timtower Ninja on the waves Moderator

    Use getItem instead and do a null check.
     
  5. Offline

    f3-AR

    still doesnt work:oops:
     
  6. Online

    timtower Ninja on the waves Moderator

    Post your new code.
    Print the material of the item.
     
  7. Offline

    f3-AR

    Code:
    package de.f3ar.fearcraftcore.events;
    
    import org.bukkit.Bukkit;
    import org.bukkit.entity.ItemFrame;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEntityEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    
    public class Freeitem implements Listener{
        @EventHandler
        public void onFreeSign(PlayerInteractEntityEvent e) {
            if (e.getRightClicked() instanceof ItemFrame) {
                ItemFrame iframe = (ItemFrame) e.getRightClicked();
                Player p = e.getPlayer();
                if (iframe.getItem() == null) {
                    if (p.hasPermission("fccore.itemframe"))
                    p.sendMessage("§1[§aFearcraft§1] §aItemframe Created");
                } else {
                    Inventory inv = Bukkit.createInventory(null, 9, "§1Free Stuff");
                    inv.setItem(4, new ItemStack(iframe.getItem()));
                    p.openInventory(inv);
                    e.setCancelled(true);
                }
            }
        }
    }
    
     
  8. Online

    timtower Ninja on the waves Moderator

    Did you print the material of the item?
     
  9. Offline

    f3-AR

    i dont know what that means. I've now made another Event where is use signs with a [Free] text. Thanks for the help anyways.
     
  10. Offline

    KarimAKL

    @f3-AR Printing the material name to the console.
    Code:Java
    1. System.out.println(material.name());
     

Share This Page