Solved Arguments and delay between sendmessage.

Discussion in 'Plugin Development' started by MrAndeos, Oct 11, 2017.

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

    MrAndeos

    Hello.
    I have two problems, first one: different action if the argument is set, and if it is not set, how to do this?

    Code:
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            Player player = (Player)sender;
            if(cmd.getName().equalsIgnoreCase("chat")) {
                if (args.length == 0) {
                    if(sender.hasPermission("addons.chat.help")) {
                        player.sendMessage(ChatColor.RED + "Usage: /chat [off/on/clear] [reason]");
                    } else {
                        player.sendMessage(ChatColor.RED + "You don't have permission to this command!");
                    }
                    return false;
                }
                if(args[0].equalsIgnoreCase("clear") || args[0].equalsIgnoreCase("c")) {
                    if(sender.hasPermission("addons.chat.clear")) {
                        if (args.length > 0) {                   // When arg 1 is set
                            for(int i=0; i < 100; i ++) {
                                Bukkit.broadcastMessage(" ");
                            }
                            Bukkit.broadcastMessage(ChatColor.RED + "The chat has been cleared, reason:" + args[1] + "!");
                        } else {                    // When arg 1 is not set
                            for(int i=0; i < 100; i ++) {
                                Bukkit.broadcastMessage(" ");
                            }
                            Bukkit.broadcastMessage(ChatColor.RED + "The chat has been cleared!");
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "You don't have permission to this command!");
                    }
                }
            }
            return false;
        }
    }
    Second: How to make delay between sendmessage? I mean something like this:

    Code:
    player.sendMessage(ChatColor.RED + " message 1");
    // 5 second delay
    player.sendMessage(ChatColor.RED + " message 2");
    // 1 second delay
    player.sendMessage(ChatColor.RED + " message 3");
    // 1 second delay
    // rest of the code
    Sorry for any mistakes, English is not my native language.
     
  2. Offline

    Side8StarLite

    @MrAndeos
    First problem: You're getting an "ArrayIndexOutOfBoundsException" from this line
    Code:
    Bukkit.broadcastMessage(ChatColor.RED + "The chat has been cleared, reason:" + args[1] + "!");
    You checked args.length > 0, when you should do args.length > 1.

    Second problem: You should use BukkitRunnables.
     
    MrAndeos likes this.
  3. Offline

    MrAndeos

    OK, thanks, I get both things working. I have one more question: I created new ShapedRecipe in class B, and now I need to register this recipe in Main class, how to do this?
     
  4. Offline

    Side8StarLite

    @MrAndeos
    Get an instance of the main class through the class B's constructor, and do Plugin#getServer().addRecipe(recipe)
     
    MrAndeos likes this.
Thread Status:
Not open for further replies.

Share This Page