onPlayerCommandPreprocess clarificaitons

Discussion in 'Plugin Development' started by madcap, Mar 9, 2011.

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

    madcap

    I need a few clarifications on onPlayerCommandPreprocess. I thought I could use this to re-write a command before it's processed by the server.

    Consider this code in the main class:


    Code:
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvent(Event.Type.PLAYER_COMMAND_PREPROCESS, playerListener, Priority.Lowest, this);
    
    And this in the player listener:


    Code:
    @Override
    public void onPlayerCommandPreprocess(PlayerChatEvent event){
    
        // when someone uses /help command, change it to /me is stupid!
    
        // split the command
        String[] args = event.getMessage().split(" ");
    
        if(args.length>0 && args[0].compareToIgnoreCase("/help")==0){
    
            event.setMessage("/me is stupid!");
            System.out.println("lol he just made a fool out of himself");
    
        }
    
    }
    Here is the actual source and jar file for you to test:
    <source removed>

    So I thought this would cause a player's command of /help to be replaced with the /me. But this doesn't happen. Instead the /help command is executed.

    So what is the point of event.setMessage if it doesn't actually replace the message?

    And there seems to be a disconnect between the way commands are normally processed and the onPlayerCommandPreprocess since the latter only works on player commands but not commands that come from the console or other possible sources. It's also possible that onPlayerCommandPreprocess isn't even running on a command at all, it's possible the player just sent a chat message.


    Is there a way to transform commands before they hit the server?
     
  2. Offline

    madcap

    Just thought I'd give this one more chance before submitting it as a bukkit bug. Anyone have any insight on this?
     
  3. Offline

    madcap

    Just in case anyone ever searches and finds this, the correct way to do it is:
    Code:
    @Override
    public void onPlayerCommandPreprocess(PlayerChatEvent event){
    
        // when someone uses /help command, change it to /me is stupid!
    
        // split the command
        String[] args = event.getMessage().split(" ");
    
        if(args.length>0 && args[0].compareToIgnoreCase("/help")==0){
    
            // this doesn't work like it should
            //event.setMessage("/me is stupid!");
    
            // use this instead:
            event.setCancelled(true);
            event.getPlayer().chat("/me is stupid!");
    
            System.out.println("lol he just made a fool out of himself");
    
        }
    
    }
     
  4. Offline

    repeat

    UP

    You changed command. A me need change arguments.

    setMessage don't work
    This need me for decoding broken codepage (convert codepage russian text to utf8).
    Code:
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
    // debug
    System.out.print("1: " + event.getMessage());
    System.out.print("2: " + fixFromGame(event.getMessage()));
    //
    event.setMessage(fixFromGame(event.getMessage()));
        } 
    nothing happening
     
  5. Offline

    repeat

    i try fix this problem something like this (now test this solution)
    Code:
    ...
        private String fix = "";
    
        public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event){
            if (event.isCancelled()) return;
    
    
        // Fixer for don't working event.setMessage()
        Player player = event.getPlayer();
        if (fix.equalsIgnoreCase(event.getMessage())) {
            fix = "";
        } else {
            // Convert command to correct codepage and cancel original command
            String message = fixFromGame(event.getMessage());
            fix = message;
            player.chat(message); //send command with correct codepage
            event.setCancelled(true);
        }
    } 
     
  6. Offline

    jackks

    thanks. it helped.
     
Thread Status:
Not open for further replies.

Share This Page