Solved Remove drop item after some time?

Discussion in 'Plugin Development' started by Christian210x, Apr 12, 2017.

Thread Status:
Not open for further replies.
  1. Offline

    Christian210x

    Title describes what i want.

    Code:
                if(event.getEntity().hasMetadata("Bio")){
                    final Location location = (Location) snowball.getLocation();
                    final World world = location.getWorld();
                    ItemStack item = Guns.biorifle_bullet;
                    world.dropItemNaturally(location, item);
                    final BukkitScheduler scheduler = Bukkit.getScheduler();
                    final int task = scheduler.scheduleSyncRepeatingTask(CraftInvasion.getPlugin(), new Runnable() {
                        @Override
                        public void run() {
                            for (Entity x : world.getNearbyEntities(location, 0.5, 0.5, 0.5)) {
                                if (x instanceof Zombie) {
                                    ((Zombie) x).damage(1);
                                }
                            }
                        }
                    }, 0, 0);
    
                    new BukkitRunnable() {
                        int count = 5;
    
                        @Override
                        public void run() {
                            count--;
                            if (count == 0) {
                                Bukkit.getScheduler().cancelTask(task);
                             //remove item here
                            }
                        }
                    }.runTaskTimer(CraftInvasion.getPlugin(), 0L, 20L);
     
  2. Offline

    Zombie_Striker

    @Christian210x
    What you got should work, but you should have a delayed task instead of a repeating task. Instead of doing counts, just increase the delay of the task.
     
  3. Offline

    Christian210x

    well i cant remove the item.
    item.remove is not available.
    @Zombie_Striker
     
  4. Offline

    Zombie_Striker

    @Christian210x
    That is because you are not storing the Item instance, and only referring to the Itemstack instance. Store the actual Item that is returned from the World.dropItem method, and call the remove method on that object.
     
  5. Offline

    Ragnarok_

    I'm not sure if you're using an PlayerDropItemEvent or not. But if you are, I think you could get the drop, make an instanceof, like Zombie_Striker said, and after some time, remove the item, if it hasn't been picked-up already. So, anyway, I'll list off the stuff
    1. PlayerDropItemEvent
    2. Create a dropped item instance
    3. Use the dropped item instance in the method you've created inside the DelayedTask
    4. Create a DelayedTask with selected time (Remember 20 ticks is 1 second)

    Or, you could use an ItemSpawnEvent and just do the same method above if you want to remove all items dropped after a specific amount of time. You could do the same for the dropped item instance, just with getting it as entity.

    @Zombie_Striker
    You wouldn't need to get the item from Bukkit.dropItem method. I think there's a method in the event itself, like e.getDroppedItem() (Edit, Ops, that is if he's using a player instance and not counting it for all objects, which is probably what he's doing)
     
    Last edited: Apr 12, 2017
  6. Offline

    Christian210x

    @Zombie_Striker & @Rangarok_
    i'm using a projectile hit event to spawn the slimeballs. im making a gun...
    and would i store it in a... ArrayList???

    nvm i used:

    Code:
                    final Location location = (Location) snowball.getLocation();
                    final World world = location.getWorld();
                    final ItemStack item = Guns.biorifle_bullet;
                    final Item it = world.dropItem(location, item);
                  
                    //to remove item
                    it.remove();
    


    Ok i got it to work i wasn't thinking straight. SOLVED
     
    Last edited: Apr 13, 2017
Thread Status:
Not open for further replies.

Share This Page