Solved Deleting an item from a container

Discussion in 'Plugin Development' started by Catoklysm, Apr 22, 2025.

  1. Offline

    Catoklysm

    I tried to delete an item from a container but I could not figure out how to do it and my attempt did not work.

    This is what I attempted to use and the code is definetely being fired
    Code:
    container.getInventory().setItem(13, null);
    container.update();

    Here is the full code of which the other parts work with no issue:
    Code:
                        if (block.getState() instanceof Container container) {
    
                                container.getInventory().setItem(13, new ItemStack(Material.AIR)); //also tried null instead of ItemStack
                                container.setCustomName(null);
                                container.update(true, true);     
                         
                                PersistentDataContainer dataContainer = container.getPersistentDataContainer();
                                dataContainer.remove(new NamespacedKey(Cataklysm.getInstance(), "owner_uuid"));
                                dataContainer.remove(new NamespacedKey(Cataklysm.getInstance(), "shop_id"));
                                container.update(true, true);
                         
                                config.set("shop." + shopID, null);
                                data.save();
                         
                            player.sendMessage("§cYour shop has been deleted.");
                            player.closeInventory();
                                }
                            }

    I have also tried using this:
    Code:
    Inventory inv = container.getInventory();
    ItemStack[] contents = inv.getContents();
    contents[13] = null;
    inv.setContents(contents); 

    and updating the block state like this:
    Code:
    block.getState().update(true, true);
     
    Last edited: Apr 23, 2025
  2. Offline

    KarimAKL

    @Catoklysm I assume you could determine that "the code is definitely being fired" because the data container and the configuration file are updated along with the player receiving the message.
    What server software and version are you running?
     
  3. Offline

    Catoklysm

    Exactly, the other things are happening just not the item being removed or even replaced when I try that instead.

    I am using Paper Version 1.21.4-190-main@5538d24 and I also tried Paper Version 1.21.5-30-main@767868d

    Edit: I moved the function to delete the item into the InventoryCloseEvent and got it working that way
     
    Last edited: May 12, 2025

Share This Page