Class PlayerCommandEvent

All Implemented Interfaces:
Cancellable

public final class PlayerCommandEvent extends PlayerEvent implements Cancellable
Called when a player enters a command into chat. A command is identified by a preceding slash '/'. For instance, if the player enters "hello" into chat, it's considered a regular chat message (and the according PlayerChatEvent is triggered). But if the player enters "/hello", it's considered a command (and this event is triggered instead).
Example:
1@EventMethod
2public void onPlayerCommandEvent(PlayerCommandEvent evt) {
3 //Split the command by whitespaces
4 String[] cmd = evt.getCommand().split(" ");
5
6 //Get the player who executed the command
7 Player player = evt.getPlayer();
8
9 //If player enters "/heal", fully heal him
10 if (cmd[0].equalsIgnoreCase("/heal")) {
11 player.setHealth(100);
12 player.setHunger(100);
13 player.setThirst(100);
14 player.setBrokenBones(false);
15 player.setBleeding(false);
16 }
17
18 //If player enters "/kill", kill him
19 else if (cmd[0].equalsIgnoreCase("/kill")) {
20 player.kill();
21 }
22
23 //If player enters "/teleport", teleport him to another player
24 else if (cmd[0].equalsIgnoreCase("/teleport")) {
25 //Check if player provided the target player name as parameter
26 if (cmd.length >= 2) {
27 //Find target player
28 Player targetPlayer = Server.getPlayerByName(cmd[1]);
29
30 //If player exists, teleport our player
31 if (targetPlayer != null) {
32 //Just set the position to teleport him
33 player.setPosition(targetPlayer.getPosition);
34 }
35 //Else if player is null, he was not found
36 else {
37 player.sendTextMessage("<color=red>Player " + cmd[1] + " not found!<color>");
38 }
39 }
40 else {
41 //Otherwise tell player to provide a name
42 player.sendTextMessage("Please provide a player name: '/teleport <playername>'");
43 }
44 }
45}
  • Method Details

    • getCommand

      public String getCommand()
      Gets the command the player has entered (typically this contains a preceding slash).
      Returns:
      the command.
    • isCancelled

      public boolean isCancelled()
      Description copied from interface: Cancellable
      Determines if the event is cancelled. If an event is cancelled, it will no longer be executed, but other plugins will still receive the event.

      Please note: If the event is threaded, cancellation has no effect, i.e the event will still be executed.
      Specified by:
      isCancelled in interface Cancellable
      Returns:
      true if the event is cancelled, or false if not.
    • setCancelled

      public void setCancelled(boolean cancel)
      Description copied from interface: Cancellable
      Cancels this event. This means it will no longer be executed, but other plugins will still receive the event.

      Specified by:
      setCancelled in interface Cancellable
      Parameters:
      cancel - set to true if you want to cancel this event.
      Example: Cancel "PlayerEnterAreaEvent", i.e prevent player from entering an area
      1//Listener class
      2public class PlayerListener implements Listener{
      3 @EventMethod
      4 public void onEnterArea(PlayerEnterAreaEvent evt){
      5 //Now the player will not be able to enter the area, i.e.
      6 //he will be teleported back to his old position (outside the area)
      7 evt.setCancelled(true);
      8 }
      9}