1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Despawning items 3 seconds after they were thrown by player

Discussion in 'Development' started by Levi, Oct 19, 2020.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    PHP:
        /**
         * @param ItemSpawnEvent $event
         */
        
    public
        function 
    itemSpawn ItemSpawnEvent $event )
        {
            
    $entity $event->getEntity();
            
    $this->plugin->getScheduler()->scheduleDelayedTask(new ClosureTask(function (?int $currentTick) use($entity): void{
                 
    $entity->flagForDespawn();
             }), 
    20);
        }
    I despawn items 3 seconds after they were thrown by a player by using a delayed task but what if multiple players throw a lot of items? It'd create a lot of delayed tasks and would probably cause lag as well. How should approach this instead?
     
  2. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    I dont think running a lot of task that only runs one line of code would do any harm, the items probably would cause more lag than the tasks
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Isn't there a way to set its lifespan before getting despawned?
     
  4. wolfdale

    wolfdale Zombie Pigman

    Messages:
    535
    GitHub:
    diamond-gold
    Technically you can extend ItemEntity, change the part where it checks for the time to despawn and register with override
     
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.