In order to know if the player ran the command, you have to store the player's UUID somewhere. First of all you create a Set<UUID>
which temporarily stores all the Unique ID's of all players that have executed the command, so when you see a player stored in this set, you know they executed the command. A UUID
is a 36-character string that is unique for every player and is the same on every server. You make the Set
like this:
final Set<UUID> players = new HashSet<>();
Next you need to make your command. I would do that like this:
@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());
}
}
}
Now what you do next is listen for the PlayerInteractEvent
to see when a player clicks the book. If you see the player is in the Set
, you know they have executed the command. Here is how I would make that 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.
}
}
}
}
So what I've done is when the player executes the command, store them in a Set
. Next listen for the PlayerInteractEvent
. This is basicly a callback method called every time a player interacts. This could be when a player steps a pressureplate, when a player right or left clicks on a block or in the air etc.
In that PlayerInteractEvent
, I check if the player is stored in that Set
, if the player right clicked in the air or right clicked a block and check if the player has a book in their hand. If that's all correct, I remove the player from the Set
so they have to execute the command once again to perform the same action.
Also don't forget to register events and implement Listener
.
In case you wanna learn more about the Set
, the Javadocs can be found here.