我在使用一个可运行的bukkit时遇到了一些麻烦。我试着让它起作用,但它只是把错误抛在我身上。这是我想要的
public class FlyE implements Listener {
@EventHandler
public void onPlayerMovement(PlayerMoveEvent e) {
Player p = e.getPlayer();
double y1 = p.getLocation().getY();
// wait 1 second
double y2 = p.getLocation().getY();
double yf = y1 - y2;
Bukkit.broadcastMessage(p + " Increase = " + yf);
}
}这段代码的目的是得到一个用户Y弦,等一下,再得到它,然后计算出增加的值。然而,无论我如何尝试使用BukkitRunnable,它只是使我困惑。我希望有人能带领我完成如何将下面的代码转换成一个可运行的Bukkit,它可以收集y1,等待20条蜱,然后收集y2。
发布于 2018-05-30 11:37:21
每次玩家移动时,都会调用player move事件。您只需要启动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坐标,并以一种高效的方式访问它们。但是,当存储的数据不再需要时(即玩家退出游戏),请记住移除存储的数据。
https://stackoverflow.com/questions/50564157
复制相似问题