Solved PlayerCommandPreprocessEvent

Discussion in 'Plugin Development' started by mickedplay, Mar 24, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    mickedplay

    Hello everybody.
    How let I a player perform a specific command? If I use the following code, every command is disabled. But I want to perform a specific command.
    So Player A can only perform /xyz. Every other is disabled. Hope you understand it.
    Code:java
    1. @EventHandler
    2. public void onCmdPreprocess(PlayerCommandPreprocessEvent e)
    3. {
    4. Player p = e.getPlayer();
    5. Command command = Bukkit.getServer().getPluginCommand(e.getMessage().toString().replace("/", ""));
    6. if(CaptureTheFlag.playersInGame.contains(p))
    7. {
    8. if(command != null) return;
    9. e.setCancelled(true);
    10. p.sendMessage(CaptureTheFlag.prefix + "ยง4Deactivated.");
    11. }
    12. }
     
  2. Offline

    Opacification

    mickedplay

    Simple way of doing it, make it permissions-based.

    If a player has a permission for a command and if the player is in-game, allow him to do it, or else just send that same message.

    Code:java
    1. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    2.  
    3. Player player = (Player) sender;
    4.  
    5. if(CaptureTheFlag.playersInGame.contains(player)) {
    6.  
    7. if(commandLabel.equalsIgnoreCase("thecommand") {
    8.  
    9. if(player.hasPermission("thepermission") {
    10.  
    11. //allow players to use the command.
    12.  
    13. } else {
    14.  
    15. //don't allow players.
    16.  


    I see what you're trying to do, this would work perfectly for it; as well as easier to manage.
     
  3. Offline

    mickedplay

    Opacification I won't that players (also op's) can use /gamemode, /tp or /reload while they're ingame.
     
  4. Offline

    Opacification

    mickedplay

    So you can do:

    Code:java
    1. if(player.hasPermission("permission") || (player.isOp)) {
    2. //allow command.
    3.  
    4. } else {
    5.  
    6. //disallow command
    7.  
    8.  
     
  5. Offline

    mickedplay

    Opacification No, I want to disable EVERY command (without one) for EVERY player. Op's and normal player.
     
  6. Offline

    Opacification

    mickedplay

    Then just use the PlayerCommandPrepocessEvent and set cancelled to the event when a player uses a command.
     
  7. Offline

    mickedplay

  8. Offline

    foldagerdk

    This should cancel all commands. Not tested

    Code:
    @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
        e.setCancelled(true);
    }
    Alternatively,
    Code:
    @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
        if(!e.isCancelled) {
            e.setCancelled(true);
        }
    }
    Edit; after re-reading:
    Code:
    @EventHandler
        public void PlayerCommand(PlayerCommandPreprocessEvent e) {
        if(!e.isCancelled) {
            if(!e.getMessage.equalsIgnoreCase("/whatever")) {
                e.setCancelled(true);
            }
        }
    }
     
  9. Offline

    mickedplay

    foldagerdk If I cancel the full event, nothing happens if I perform a command. Thats right.

    Example:
    Player A can use every command on the server.
    If player A perform /ctf join, everything is ok.
    But after player A performed /ctf join, every command is disabled for him. Only /ctf leave can be used (to leave the game). -> /gamemode disabled, /rl disabled.... etc....

    ##EDIT: Works! Thanks foldagerdk
     
  10. Offline

    foldagerdk

    No problem :)
     
  11. Offline

    Jade

    Marked as solved.
     
Thread Status:
Not open for further replies.

Share This Page