Override 的 ScheduledEvent 事件处理程序
ScheduledEvent Eventhandler from Override
所以我想检查玩家在第一次执行命令后何时右击手里拿着一本书。我尝试将 Runnable
运行 作为计时器,并在该调度程序中检查玩家是否右击手中的书。 Runnable 迫使我重写 'run' 方法。
这是我试过的:
@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.
}
}
为了知道玩家是否运行命令,你必须在某处存储玩家的UUID。首先,您创建一个 Set<UUID>
临时存储所有已执行该命令的玩家的所有唯一 ID,因此当您看到该集合中存储的玩家时,您就知道他们执行了该命令。 UUID
是一个 36 个字符的字符串,对于每个玩家都是唯一的,并且在每个服务器上都相同。你使 Set
像这样:
final Set<UUID> players = new HashSet<>();
接下来您需要发出命令。我会这样做:
@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
:
@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
的更多信息,可以找到 Javadocs here。
所以我想检查玩家在第一次执行命令后何时右击手里拿着一本书。我尝试将 Runnable
运行 作为计时器,并在该调度程序中检查玩家是否右击手中的书。 Runnable 迫使我重写 'run' 方法。
这是我试过的:
@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.
}
}
为了知道玩家是否运行命令,你必须在某处存储玩家的UUID。首先,您创建一个 Set<UUID>
临时存储所有已执行该命令的玩家的所有唯一 ID,因此当您看到该集合中存储的玩家时,您就知道他们执行了该命令。 UUID
是一个 36 个字符的字符串,对于每个玩家都是唯一的,并且在每个服务器上都相同。你使 Set
像这样:
final Set<UUID> players = new HashSet<>();
接下来您需要发出命令。我会这样做:
@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
:
@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
的更多信息,可以找到 Javadocs here。