我正在制作一个spigot插件,当掉落的物品接触地面时会爆炸,我正在开发的版本是带有spigot 1.20.2 API的Minecraft 1.20.2(纸质服务器)。
问题是,当我掉落一个物品时,它会一直下落,即使物品接触地面后也没有任何反应。
我发现,当我尝试获取该项目的位置时,它的值仍保留在我放下它的位置。 这是我现在的代码。
@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);
}
}
}
});
}
}
我想知道如何解决,谢谢。
您需要为您的
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);
}
或者直接删除
scheduleSyncDelayedTask
。