Solved setTicksLived not working??

Discussion in 'Spigot Plugin Development' started by Rixterz, May 13, 2017.

  1. Hi,

    I would have thought that this code would allow an item to live for 3 seconds before it despawns, however the item still lives for 6000 ticks.

    Code (Text):
    Item item = LocationUtils.end.dropItem(loc.clone().add(new Vector(randX - 0.5, 0.5, randZ - 0.5)), newStack);
    item.setTicksLived(6000 - (3 * 20));
     
  2. I had the same problem with endermite, changing it's living ticks didn't prevent it from despawning and seems to do nothing
     
  3. It's not supposed to despawn the entity.
    [​IMG]
     
    • Agree Agree x 1
  4. Forgive my if I am wrong, but does that method even set how long they live for? In the Javadocs it says "Sets the amount of ticks this entity has lived for. This is the equivalent to "age" in entities. May not be less than one tick."
     
  5. I think it's a grammatical thing;

    at the time of setting its ticks lived, that defines how long the entity has lived for up to that point.

    So surely, setting the ticks lived to 5000 will mean it only lives for 1000 ticks until it despawns.
     
    #5 Rixterz, May 13, 2017
    Last edited: May 13, 2017
  6. That seems to make sense now, I was unaware that items dropped live for 6,000 game ticks.
     
  7. The weird thing is, when I set the ticks lived to 3 seconds before 5 minutes and check the ticks lived 4 seconds later, it says 6020

    Items are supposed to despawn at 6000
     
  8. So I was looking through the code that actually removes the item after the item-despawn-rate has passed, and I couldn't find any reference to "ticksLived," which is what is set when Entity#setTicksLived(int value) is called. However, what I did find was a check against the private "age" field to remove the item, as well I didn't see the age field being set to anything with ticksLived. I finally got the despawn time to work after directly setting the age field using reflection.
     
  9. Ooh ok, could you please post the reflection code?
     
  10. Code (Java):

    Item item = player.getWorld().dropItem(player.getLocation(), new ItemStack(Material.DIAMOND, 1));

    try
    {
        Field itemField = item.getClass().getDeclaredField("item");
        Field ageField;
        Object entityItem;

        itemField.setAccessible(true);
        entityItem = itemField.get(item);

        ageField = entityItem.getClass().getDeclaredField("age");
        ageField.setAccessible(true);
        ageField.set(entityItem, 6000 - (3 * 20));
    }
    catch (NoSuchFieldException | IllegalAccessException e)
    {
        e.printStackTrace();
    }
     
     
    • Winner Winner x 3
  11. It works! Thank you! <3

    + I filed a bug report
     
    #11 Rixterz, May 13, 2017
    Last edited: May 13, 2017
    • Agree Agree x 1