Minecraft Bukkit - Detect if player is running command from a sign

144 Views Asked by At

I am trying to write a command that is supposed to teleport a player if (a) the player is running it by clicking a sign and (b) other conditions are met. This is what I have so far for my CommandExecutor onCommand function:

public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage("ERROR: You must be a player to use this command.");
            return true;
        }
        if (sender.hasPermission(permission) && /* Other conditions */) { // permission is given as an argument to the command
            ((Player) sender).teleport(new Location(world, destX, destY, destZ, destYw, destPt)); // These variables are all determined based on the arguments to the command.
            return true;
        }
        return true;
}

This allows any player to teleport anywhere by typing /<name of my custom command> and using a permission that they have, such as group.default (for LuckPerms). I don't want players to be able to use my command to do this.

In contrast, on an unmodded Minecraft server, if you give yourself a sign using

/give @s minecraft:oak_sign{BlockEntityTag:{front_text:{messages:['{"text":"","clickEvent":{"action":"run_command","value":"tp @s 0 100 0"}}','{"text":"MySign"}','{"text":"Select to teleport.","italic":true,"color":"white"}','{"text":""}']},is_waxed:1b}}

then anyone (i.e., people without op) can click the sign to teleport to (0, 100, 0), but they cannot run /tp 0 100 0 or /tp <anything> in the chat.

How do I accomplish this behavior with my custom Bukkit command? Is there a way to detect whether a player is running a command by right-clicking a sign (and then return without teleporting if they are not), or is there an easier way to do this?

1

There are 1 best solutions below

1
KoCode On

With this sign

/give @s minecraft:oak_sign{BlockEntityTag:{front_text:{messages:['{"text":"","clickEvent":{"action":"run_command","value":"tp @s 0 100 0"}}','{"text":"MySign"}','{"text":"Select to teleport.","italic":true,"color":"white"}','{"text":""}']},is_waxed:1b}}

The server executes the command /tp @s 0 100 0, thats why the players can click the sign even tho they dont have the permisson to run the /tp command.

I think the best way to do this is to listen to the PlayerInteractEvent in a Bukkit Listener instead of a command.

This is how to teleport the Player when they interact with a sign:

@EventHandler
public void OnInteract(PlayerInteractEvent event)
{
   if(event.getClickedBlock().getState() instanceof Sign)
   {
       //When the player clicked on a Sign

        event.getPlayer().teleport(........);
    }
}

I hope this helped you!