OnCommand doesn't work.

Discussion in 'Plugin Development' started by streedie, Jan 19, 2011.

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

    streedie

    Hello all.

    I'm creating a plugin atm, and I've got 1 error left:

    OnCommand doesn't work here:

    Code:
    public void onCommand(Player p, String[] cmd) {
    
                if (cmd[0].equals("/red")) {
                    p.broadcastMessage(Colors.Red+"This text is red!");
                return true;
                }
                return false;
                }
    
            }
    Please help!

    ~Streedie
     
  2. Offline

    apexearth

    Code:
        @Override
        public void onPlayerCommand(PlayerChatEvent event) {
            String[] split = event.getMessage().substring(1).trim().split(" ");
    
        }
    Try this instead?
     
  3. Offline

    feverdream

    That syntax looks malformed.

    You should use .equalsIgnoreCase not .equals

    You are using the wrong signature for that function call.

    Look in the ScrapBukkit code for the correct way. Or look in my git at the Murder plugin.
     
  4. Offline

    thegleek

    Code:
    @Override
    public void onPlayerCommand(PlayerChatEvent event) {
        Player player = event.getPlayer();
        String[] sCommand = event.getMessage().split(" ");
    
        if ((!event.isCancelled()) && sCommand[0].equalsIgnoreCase("/yourcommand")) {
            player.sendMessage(ChatColor.YELLOW + "Message back to player here");
            event.setCancelled(true);
        }
    }
    Of course that can differ in many ways... Sending a message when someone uses a command via broadcast should be thought about - many users could care less what another user is doing... A lot of factors need to be considered for using broadcast in my opinion.

    Also-- Colors.{Color} has been replaced with ChatColor.{COLOR}
     
  5. Offline

    streedie

    @Apexearth, we're to put command and text need to be shown?

    Edit: NVM

    EditEdit: It needs to be broadcasted. And how to use colors?

    EditEditEdit: This is getting weird XD

    Still some error:
    Code:
    void is an invalid type for the variable onPlayerCommand
     
  6. Offline

    DerpinLlama

    onPlayerCommand should not return anything, are you still trying to make it return something?
     
  7. Offline

    exec

    Like feverdream said, take a look at the ScrapBukkit Code to see how to do this, and refrain from using onPlayerCommand, as it's kinda outdated.

    https://github.com/Bukkit/ScrapBukk.../com/dinnerbone/bukkit/scrap/ScrapBukkit.java
    https://github.com/Bukkit/ScrapBukkit/blob/master/src/main/resources/plugin.yml

    Code:
    public boolean onCommand(Player player, Command command, String commandLabel, String[] args) {
        if (command.getName().equalsIgnoreCase("message")) {
            getServer().broadcastMessage("Your Message in " + ChatColor.RED + "red");
            return true;
        }
        return false;
    }
     
Thread Status:
Not open for further replies.

Share This Page