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.
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.
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.
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.
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");
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.
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. }
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.
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/
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.