Tracking items dropped to the ground

47 views Asked by At

I'm making a spigot plugin that makes dropped item explode when it touched the ground and the version I'm working on is Minecraft 1.20.2(paper server) with spigot 1.20.2 API.

The problem is that when I drop a item, it just keeps falling and nothing happened even after the item touches the ground.

I have found out that when I tried to get the location of the item, its value remains on the location I dropped it. This is my code now.

    @EventHandler
    public void onItemDrop(PlayerDropItemEvent e) {

        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() {
            public void run() {
                if (e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equals(ItemControl.unstable_tnt.getItemMeta().getDisplayName())) {
                    Location location = e.getItemDrop().getLocation();
                    while (location.add(0,-1,0).getBlock().getType() == Material.AIR) {
                        location = e.getItemDrop().getLocation();
                        System.out.println(location.add(0,-1,0));
                        System.out.println(location.add(0,-1,0).getBlock().getType());
                    }
                    System.out.println("explode");
                    World world = location.getWorld();
                    for (int i=0;i<2;i++) {
                        TNTPrimed tnt = (TNTPrimed) world.spawnEntity(location, EntityType.PRIMED_TNT);
                        tnt.setFuseTicks(0);
                    }
                }
            }
        });
    }
}

I would like to know how to solve it, thanks.

1

There are 1 answers

1
Quitrin Dev On

You need to set time for your Runnable.

@EventHandler
public void onItemDrop(PlayerDropItemEvent e) {

    long ticks = 5 * 20; // five seconds to ticks

    Bukkit.getScheduler().scheduleSyncDelayedTask(plugin,new Runnable() {
        public void run() {
            if (e.getItemDrop().getItemStack().getItemMeta().getDisplayName().equals(ItemControl.unstable_tnt.getItemMeta().getDisplayName())) {
                Location location = e.getItemDrop().getLocation();
                while (location.add(0,-1,0).getBlock().getType() == Material.AIR) {
                    location = e.getItemDrop().getLocation();
                    System.out.println(location.add(0,-1,0));
                    System.out.println(location.add(0,-1,0).getBlock().getType());
                }
                System.out.println("explode");
                World world = location.getWorld();
                for (int i=0;i<2;i++) {
                    TNTPrimed tnt = (TNTPrimed) world.spawnEntity(location, EntityType.PRIMED_TNT);
                    tnt.setFuseTicks(0);
                }
            }
        }
    }, ticks);
}

Or just delete scheduleSyncDelayedTask.