首页
学习
活动
专区
圈层
工具
发布

Bukkit _
EN

Stack Overflow用户
提问于 2014-05-13 22:55:51
回答 2查看 2.9K关注 0票数 2

所以我一直在开发一款新的迷你游戏,它对计时器非常忙碌。这基本上是一个快节奏的跑酷游戏,但问题是,当我的计时器工作时,他们会同时影响所有的在线玩家。我怎么把计时器限制在玩家身上?我在上面读了一些,我看到很多解决方案都是将播放器名和任务ID存储在HashMap中,但是我不知道从那一点开始应该去哪里。小小的指导将不胜感激!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-05-13 23:54:49

您可以为每个播放器创建一个单独的定时器,然后将ID存储在HashMap中。

代码语言:javascript
复制
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
  }
}
票数 1
EN

Stack Overflow用户

发布于 2014-08-10 21:03:12

不要为每个玩家单独设置一个任务,就像上面提到的(我没有足够的声誉来评论),让一个任务在HashMap中减少一个计数器,并且当计数器达到零时,从HashMap中删除UUID

为每个播放器编写单独的任务是没有意义的,它增加了CPU负载,因为它必须处理大量线程而不是一个线程。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23642957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档