Creating infinite unpickable item

Discussion in 'Spigot Plugin Development' started by YunusGG, Jun 15, 2018.

  1. Hey!
    How can I crate an item that infinite and unpickable?
    I coded this but it doesn't work.
    Code (Java):
    tem item =  p.getWorld().dropItem(loc);
    item.setCustomNameVisible(true);
    item.setCustomName("I'm an item");
    item.setPickupDelay(999999999);
    Help pls.
     
    #1 YunusGG, Jun 15, 2018
    Last edited: Jun 15, 2018
  2. TheJavaHacker

    Supporter

    Tell us where you messed up, we're not gods. We don't know why it's not working either, because you haven't shown us the error nor the whole class.
     
    #2 TheJavaHacker, Jun 15, 2018
    Last edited by a moderator: Jun 15, 2018
  3. I don't see a problem with the way you're doing it. If it still doesn't work, you could try a different way by cancelling the PlayerPickupItemEvent, you'll probably want to make only some items not pickupable, I'd recommend using metadata for that.
    I do something similar right here, you'd have to change it up a bit so it doesn't verify if it's the right player who picks up the item and instead just always cancel the event when the item has some special metadata.
     
  4. There is no error. I just ask you how can I create an infinite item. U arrogant..
     
  5. TheJavaHacker

    Supporter

    I'm not arrogant. You can't expect someone to be able to help with "It broken, help fix". I need to know details to be able to fix it. I'm not a God and nor am I psychic.
     
  6. But How can I check item? My item's name is custom like that:
    Code (Java):
    Item item =  p.getWorld().dropItem(new Location(p.getWorld(), p.getLocation().getBlockX(), p.getLocation().getBlockY()+1, p.getLocation().getBlockZ()), i);
    item.setCustomNameVisible(true);
    item.setCustomName("§6" + gold.getAmount() + " §7 adet§6" + gold.getType() + " §8|| §6§l" + price + " golditem");
     
  7. Look at the gist I linked, it contains similar code I programmed some time ago as an example with a comment about the tiny details you'd have to change.
     
  8. Code (Java):
    Item entityItem = /* Your Item entity */null;
    entityItem.setPickupDelay(Short.MAX_VALUE); //Unpickable until death
    try { // Some reflection magic (nms.EntityItem:65)
        final EntityItem handle = (EntityItem) ((CraftItem) entityItem).getHandle();
        final Field ageField = handle.getClass().getField("age");
        ageField.setAccessible(true);
        ageField.set(handle, Short.MIN_VALUE); //Undying until picking up
    } catch (NoSuchFieldException | IllegalAccessException ignore) {
        //Maybe it will fail, I did not checked it.
    }
     
  9. It is definitely possible to stop items from being picked up and despawning without NMS code which should usually be avoided if not needed. I would advise people against using the solution @Dereku posted as it is a really bad habit to use NMS code for simple things like this. A tutorial on NMS and it's side effects can be found here.
     
  10. BananaPuncher714

    Supporter

    You can do this by editing the Item entity's NBT tags. They can be found here: https://minecraft.gamepedia.com/Item_(entity)/ED
    The best option for you would be to set:
    • "Age" to -32768s
    • "Invulnerable" to 1b
    • "PickupDelay" to 32767s
    and if you want to item to have no gravity, you can set the "NoGravity" tag to 1b. This should effectively create an "infinite" item. :)

    You can edit them with some NMS, or my 1 class NBT Editor: https://www.spigotmc.org/threads/nbt-tag-editing-reflection-custom-items-and-mobs-updated.269621/ ;)
     
    • Like Like x 1
  11. Ok, let's register another listener for cancelling ItemEntity deaspawn/pickup event every tick, because bukkit api doesn't have a method for setting "age". And yes, that solution is not pretty good, because we need to import 2 nms classes.
     
  12. Lowkey the smoothest advertising right here

    I’d also recommend his editor, I use it, it’s amazing.
     
    • Like Like x 1