所以我一直在开发一款新的迷你游戏,它对计时器非常忙碌。这基本上是一个快节奏的跑酷游戏,但问题是,当我的计时器工作时,他们会同时影响所有的在线玩家。我怎么把计时器限制在玩家身上?我在上面读了一些,我看到很多解决方案都是将播放器名和任务ID存储在HashMap中,但是我不知道从那一点开始应该去哪里。小小的指导将不胜感激!
发布于 2014-05-13 23:54:49
您可以为每个播放器创建一个单独的定时器,然后将ID存储在HashMap中。
public Map<String, Integer> taskID = new HashMap<String, Integer>();
//call this to schedule the task
public void scheduleRepeatingTask(final Player p, long ticks){
final int tid = plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable(){
public void run(){
//do you want here
}
},ticks, ticks); //schedule task with the ticks specified in the arguments
taskID.put(p.getName(), tid); //put the player in a hashmap
}
//call this to end the task
public void endTask(Player p){
if(taskID.containsKey(p.getName()){
int tid = taskID.get(p.getName()); //get the ID from the hashmap
plugin.getServer().getScheduler().cancelTask(tid); //cancel the task
taskID.remove(p.getName()); //remove the player from the hashmap
}
}发布于 2014-08-10 21:03:12
不要为每个玩家单独设置一个任务,就像上面提到的(我没有足够的声誉来评论),让一个任务在HashMap中减少一个计数器,并且当计数器达到零时,从HashMap中删除UUID
为每个播放器编写单独的任务是没有意义的,它增加了CPU负载,因为它必须处理大量线程而不是一个线程。
https://stackoverflow.com/questions/23642957
复制相似问题