首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ScheduledEvent Eventhandler从覆盖

ScheduledEvent Eventhandler从覆盖
EN

Stack Overflow用户
提问于 2016-04-09 21:42:07
回答 1查看 219关注 0票数 3

所以我想检查一下,当玩家在第一次执行命令后,手里拿着一本书时,他们会向右点击。我试着让Runnable作为定时器运行,并在调度程序中检查玩家是否在手中拿着一本书右键单击。Runnable迫使我重写“run”方法。

这就是我尝试过的:

代码语言:javascript
复制
@Override
public void onEnable() {

this.getServer().getScheduler().scheduleSyncRepeatingTask(this, new Runnable() {

    @Override
    public void run() {
        //Here I want to check if the player right clicked with a book in their hand.
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-12 19:07:46

为了知道玩家是否运行了命令,您必须将播放器的UUID存储在某个地方。首先,创建一个Set<UUID>,它临时存储执行命令的所有玩家的所有唯一ID,所以当您看到存储在这个集合中的播放器时,您知道他们执行了命令。UUID是一个36个字符的字符串,对每个播放器都是唯一的,在每个服务器上都是相同的。你把Set做成这样:

代码语言:javascript
复制
final Set<UUID> players = new HashSet<>();

接下来你需要下一个命令。我会这样做:

代码语言:javascript
复制
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cl, String[] args) {
    //Check if your command was executed
    if(cmd.getName().equalsIgnorecase("yourCommand")){
        //Check if the executor of the command is a player and not a commandblock or console
        if(sender instanceof Player){

            Player player = (Player) sender;

            //Add the player's unique ID to the set
            players.add(player.getUniqueId());
        } 
    }
}

现在,您接下来要做的是听一听PlayerInteractEvent,看看玩家何时点击这本书。如果您看到播放机在Set中,您就知道他们已经执行了命令。下面是我如何使EventHandler

代码语言:javascript
复制
@EventHandler
public void onInteract(PlayerInteractEvent event){
    //Check if the player right clicked.
    if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
        //Check if the Set contains this player
        if(players.contains(event.getPlayer().getUniqueId()){
            //Check if the player had an item in their hand
            if(event.getPlayer().getItemInHand().getType() == Material.BOOK){
                //Remove player from the set so they have to execute the command again before right clicking the book again
                players.remove(event.getPlayer().getUniqueId());
                //Here you can do whatever you want to do when the player executed the command and right clicks a book.
            }
        }
    }
}

所以我所做的是,当播放器执行命令时,将它们存储在一个Set中。接下来请收听PlayerInteractEvent。这基本上是一种每次玩家交互时调用的回调方法。这可能是当一个玩家踩着一个压力板,当一个玩家左右点击一个街区或空中等等。

在该PlayerInteractEvent中,我检查播放机是否存储在该Set中,播放机是否在空中单击或右击某个块,并检查播放机手中是否有一本书。如果这一切都是正确的,我将从Set中移除播放机,因此他们必须再次执行命令才能执行相同的操作。

另外,不要忘记注册事件并实现Listener

如果您想了解更多关于Set的信息,可以找到这里

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

https://stackoverflow.com/questions/36523067

复制
相关文章

相似问题

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