Material.'nothing'?

Discussion in 'Spigot Plugin Development' started by iClicks, Nov 10, 2018.

  1. Hi,
    i'm trying to make a kit system but i don't know what nothing is.
    I tried Material.AIR but that dont work.
    Can someone help me please? :)
     
  2. It really depends on what you are trying to achieve? What did you try and didn't work with Material.AIR? Null is usually considered nothing in ItemStacks.
     
  3. If you mean an ItemStack which is a free space in inventory, then you should use Material.AIR. That should work. Can you explain a bit more about your situation? Why isn't Material.AIR working for your?
     
  4. No, you do not. A free space in an inventory should be null, whether you're setting it or getting what is in that space.
     
  5. What have you tried? Show us your full code.
     
  6. Well, I was used to that too. But one time, I was just coding another plugin as usual, I was using null and it threw errors. After doing the same thing but using an ItemStack with Material.AIR, everything was fine. Since then I always used Material.AIR.

    Edit: Alright I have to correct myself. When getting items from the inventory, empty slots actually are null. But when setting items, null throws an error. So you have to get the ItemStack in the slot you want and set its Material to Air.
     
  7. Settings a ItemStack to air doesn't work anymore. I think you need to use inventory.setItem(slot, null); or something like that.
     
  8. That's exactly what did not work for me ^^ But anyways, it doesn't matter now, right? It should work with one of the methods, so thread solved I guess.

    Edit: What does not work is setting the Material to null ofc, maybe you did that by mistake?
     
  9. boi what?? he’s asking about materials not some code concept to give him...
     

  10. @iClicks Haven't really tried it but I think you're not allowed to create an itemstack with a null material value (or any value, can't remember for sure).
    Code (Java):
    final ItemStack nullItem = new ItemStack(null);
    The above for example would throw some sort of exception (can't remember which one or whether it would, you may test it out yourselves).
    Whereas:
    Code (Java):
    final Inventory inv = ...;
    inv.setItem(0, null);
    Would remove any item in the first slot of any inventory. I've tried this one and had no issues whatsoever.
     
    • Agree Agree x 1
  11. I know you said you already tried with Material.AIR, but I want to be sure:
    Have you tried inventory.setItem(slot, new ItemStack(Material.AIR)); ?
     
  12. boii..


    everyone, you just the item in the slot to null. that is all there is to it.
     
    • Agree Agree x 1
  13. inventory.setItem(slot, new ItemStack(Material.AIR)); should work,
    inventory.getItem(slot).setType(Material.AIR); should work,
    inventory.setItem(slot, new ItemStack(null)); should not work and
    inventory.getItem(slot).setType(null); should not work. But
    inventory.setItem(slot, null); should also work.
     
  14. If I remember correctly, an error would be thrown if you attempted to set a slot as air. I'd be great if someone verified this on the latest version.
     
  15. Can you give a little more detail about the function where you are trying to do this?

    Everyone so far is assuming that it is an Inventory Slot, are they right do do so?
    Or, perhaps you are trying to create a Custom Recipe, or something else?
     
    • Like Like x 1
  16. Setting a slot as air works fine for me. And I'm coding in 1.13.2.
     
    • Agree Agree x 1
  17. 2008Choco

    Junior Mod

    Guys, come on. Bukkit and CraftBukkit are open sourced
    Inventory#setItem(int, ItemStack):
    Code (Java):
    public void setItem(int index, ItemStack item) {
        getInventory().setItem(index, CraftItemStack.asNMSCopy(item));
    }
    CraftItemStack.asNMSCopy(ItemStack):
    Code (Java):
    public static net.minecraft.server.ItemStack asNMSCopy(ItemStack original) {
        if (original instanceof CraftItemStack) {
            CraftItemStack stack = (CraftItemStack) original;
            return stack.handle == null ? net.minecraft.server.ItemStack.a : stack.handle.cloneItemStack();
        }
        if (original == null || original.getType() == Material.AIR) { // If it's null, or air, Minecraft's AIR constant will be used
            return net.minecraft.server.ItemStack.a;
        }

        // etc. not important
    }
    In the end, whether you use an ItemStack of type AIR or null, both will set air to the inventory. I, personally, would set null instead because you then don't have to create a new instance of ItemStack every time you want to remove an item, it uses a Minecraft constant anyways. Unnecessary instance allocation. Use null.
     
    • Agree Agree x 2
    • Informative Informative x 1

Share This Page