java 检查延迟后块是否仍在世界中

x759pob2  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(88)

我想检查放置的块在短暂延迟后是否仍然存在并执行命令
我已经尝试过睡眠,但它只是给了块,在那里之前的延迟

public class BlockListener implements Listener {
    @EventHandler
    public void check(BlockPlaceEvent e)
    {          
      Material blockType = e.getBlockPlaced().getType();
      int locationX = e.getBlockPlaced().getLocation().getBlockX();
      int locationY = e.getBlockPlaced().getLocation().getBlockY();
      int locationZ = e.getBlockPlaced().getLocation().getBlockZ();
      String locationXb = String.valueOf(locationX);
      String locationYb = String.valueOf(locationY);
      String locationZb = String.valueOf(locationZ);
      World world = e.getPlayer().getWorld();          
      if ((blockType == Material.PLAYER_HEAD) || (blockType == Material.PLAYER_WALL_HEAD))
        {
          try
          {
            Thread.sleep(1000);
          }
          catch (InterruptedException ex)
          {
            
          }
          Material block = world.getBlockAt(locationX, locationY, locationZ).getType();
          if((block == Material.PLAYER_WALL_HEAD) || (block == Material.PLAYER_HEAD)){
             //empty
          }
          else {
            Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
          }
        }
      }
qltillow

qltillow1#

你应该Never在spigot上使用Thread.sleep方法。它会停止所有服务器。
您需要使用Scheduler(Tutorial- Javadoc:BukkitScheduler):

@EventHandler
public void check(BlockPlaceEvent e) {
  Material blockType = e.getBlockPlaced().getType();
  int locationX = e.getBlockPlaced().getLocation().getBlockX();
  int locationY = e.getBlockPlaced().getLocation().getBlockY();
  int locationZ = e.getBlockPlaced().getLocation().getBlockZ();
  String locationXb = String.valueOf(locationX);
  String locationYb = String.valueOf(locationY);
  String locationZb = String.valueOf(locationZ);
  World world = e.getPlayer().getWorld();
  if ((blockType == Material.PLAYER_HEAD) || (blockType == Material.PLAYER_WALL_HEAD)) {
    Bukkit.getScheduler().runTaskLater(MyPlugin.getInstance(), () -> {
      Material block = world.getBlockAt(locationX, locationY, locationZ).getType();
      if ((block == Material.PLAYER_WALL_HEAD) || (block == Material.PLAYER_HEAD)) {
        //empty
      } else {
        Bukkit.dispatchCommand(Bukkit.getConsoleSender(), command);
      }
    }, 20);
  }
}

其中:

  • MyPlugin.getInstance()用于获取JavaPlugin的示例,也称为plugin变量。
  • () -> {}是将运行的内容
  • 20是服务器应该等待的时间(以滴答为单位)。20滴答= 1秒。

相关问题