Bukkit等待/延迟帮助(Bukkit Runnable)

时间:2018-05-28 10:19:50

标签: java bukkit

我在使用bukkit runnable时遇到了一些麻烦。我试图让它发挥作用,但它只会给我带来错误。这就是我想要的

myshader.fragmentShader = myfragmentshader; //string

此代码旨在让用户Y坐标,等待一秒,再次获取,然后计算出增加。但是,无论我如何尝试使用BukkitRunnable,它都让我感到困惑。我希望有人能指导我如何将下面的内容转换为收集y1,等待20个滴答,然后收集y2的Bukkit Runnable。

1 个答案:

答案 0 :(得分:1)

每次玩家移动时都会调用玩家移动事件。您只需要启动一次Bukkit调度程序,然后它就会连续运行。我不确定你想如何选择你的播放器,所以这可能不是你想要实现的,而是在将它放入onEnable()方法后启动调度程序。

public class MyPlugin extends JavaPlugin implements Listener {

    private HashMap<String, Integer> lastY = new HashMap<>(); //Stores the last y location for an arbitrary number of users. The map key (String) is the user's name and the value (Integer) is the user's last Y coord

    @Override
    public void onEnable(){
        //Start the timer asynchronously because it doesn't need to run on the main thread and the time will also be more accurate
        Bukkit.getScheduler().runTaskTimerAsynchronously(this, new Runnable() {

            @Override
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()) { //Loop through all the players on the server
                    int y = player.getLocation().getBlockX();
                    player.sendMessage("Increase = " + (y - lastY.getOrDefault(player.getName(), 0))); //Display the increase in height using the stored value or 0 if none exists
                    lastY.put(player.getName(), y); //Replace their previous y coordinate with the new one
                }
            }

        }, 0, 20L);
    }

    @EventHandler
    public void onPlayerQuit(PlayerQuitEvent e){
        lastY.remove(e.getPlayer().getName()); //Remove stored data for player
    }

}

HashMap允许您存储服务器上所有玩家的y坐标,并以有效的方式访问它们。但是,请记住在不再需要时删除存储的数据(即玩家退出游戏)