[SPIGOT-229] itemFrame.setItem(null); no longer removes the item Created: 16/Dec/14  Updated: 20/Dec/14  Resolved: 20/Dec/14

Status: Resolved
Project: Spigot
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Major
Reporter: Weasel_Squeezer Assignee: Unassigned
Resolution: Fixed Votes: 1
Labels: 1.8, Craftbukkit, bug, spigot
Environment:

Irrelevant



 Description   

In 1.8, the ItemFrame entity class is bugged as there is no way to remove an item. previously, you had to set the item to null or air, and that would remove it, and after looking at the decompiled code, it seems like that has not changed (and it shouldn't have). So I have come to the conclusion that it is a bug. I have tried setting he item to null or air ( new ItemStack(Material.AIR); ), but the item stays in the ItemFrame and nothing happens (no stacktrace). I have tried removing many types of items and none were removed.
However, I am able to change the item in the ItemFrame to something else. This can be explained by looking at the CraftItemFrame class in craftbukkit.

CraftItemFrame.java
  public void setItem(ItemStack item)
  {
    if ((item == null) || (item.getTypeId() == 0))
    {
      getHandle().getDataWatcher().add(2, 5);
      getHandle().getDataWatcher().update(2);
    } 
    else
    {
      getHandle().setItem(CraftItemStack.asNMSCopy(item));
    }
  }


 Comments   
Comment by FearThe1337 [ 18/Dec/14 ]

CB PR 63 should fix this. Pending at this moment.

Comment by Weasel_Squeezer [ 16/Dec/14 ]

Sorry for the lack of a proper version. The Spigot version I am running is: git-Spigot-612de46-07c2162 (MC: 1.8) (Implementing API version 1.8-R0.1-SNAPSHOT)

Also, Here is a the code I used to test this bug:

Testing Code
@EventHandler
public void onItemFrameClick(PlayerInteractEntityEvent event) {
    if (event.getRightClicked() instanceof ItemFrame) {
        ItemFrame itemFrame = (ItemFrame) event.getRightClicked();
        if (!isEmptyItem(itemFrame.getItem())) {
            ItemStack heldItem = event.getPlayer().getItemInHand();
            // if player is not holding an item, remove the item from itemFrame. Otherwise replace the item
            if (isEmptyItem(heldItem)) {
                // will not work
                itemFrame.setItem(null);
                System.out.println("Item removed with null: " + isEmptyItem(itemFrame.getItem()));
                
                itemFrame.setItem(new ItemStack(Material.AIR));
                System.out.println("Item removed with air: " + isEmptyItem(itemFrame.getItem()));
            } else {
                // will work
                itemFrame.setItem(heldItem);
                System.out.println("Item added: " + (!isEmptyItem(itemFrame.getItem()) && itemFrame.getItem().equals(heldItem)));
            }
            event.setCancelled(true);
        }
    }
}

private boolean isEmptyItem(ItemStack item) {
    return item == null || item.getType() == Material.AIR;
}
Generated at Mon Apr 21 21:19:14 UTC 2025 using Jira 10.3.5#10030005-sha1:190c783f2bd6c69cd5accdb70f97e48812a78d14.