Problem with registering separate args

Discussion in 'Plugin Development' started by CheifKeef, Sep 10, 2015.

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

    CheifKeef

    Ok so I'm making it so when they do /help it does the first thing but when they do /help 2 it displays another body of text, but when i do it in game it runs both and not separately

    heres my code:
    Code:
    package me.RalphLauren.Liquid;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.Sound;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Liquid extends JavaPlugin {
        public Logger logger = Logger.getLogger("Minecraft");
        public static Liquid plugin;
       
        @Override
        public void onDisable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + pdfFile.getVersion() + " has been disabled!");
    }
        @Override
        public void onEnable() {
            PluginDescriptionFile pdfFile = this.getDescription();
            this.logger.info(pdfFile.getName() + pdfFile.getVersion() + " has been enabled!");
        }
       
        public boolean onCommand(CommandSender sender, Command cmd, String label, String args[]) {
            Player player = (Player) sender;
            if(cmd.getName().equalsIgnoreCase("help")){
                player.sendMessage(ChatColor.DARK_PURPLE + "§m----------------------------------------------------");
                player.sendMessage(ChatColor.YELLOW + "                              [+]" + ChatColor.RED + " Help " + ChatColor.YELLOW + "[+]");
                player.sendMessage(ChatColor.GRAY + "This server uses the liquid plugin created by:" + ChatColor.AQUA + " RalphLauren");
                player.sendMessage(ChatColor.GREEN + "- To get started do /kit {kitname}");
                player.sendMessage(ChatColor.GREEN + "- To view kits do /kits");
                player.sendMessage(ChatColor.GREEN + "- To view more help pages do /help 2");
                player.sendMessage(ChatColor.DARK_PURPLE + "§m----------------------------------------------------");
                player.playSound(player.getLocation(), Sound.FIREWORK_BLAST, 2F, 1F);
            }
            if(cmd.getName().equalsIgnoreCase("help")) {
                if(args.length == 1) {
                    if(args[0].equalsIgnoreCase("2")){
                        player.sendMessage(ChatColor.DARK_PURPLE + "§m----------------------------------------------------");
                        player.sendMessage(ChatColor.YELLOW + "                             [+]" + ChatColor.RED + " Help 2 " + ChatColor.YELLOW + "[+]");
                        player.sendMessage(ChatColor.GREEN + "- Blah");
                        player.sendMessage(ChatColor.GREEN + "- Blah");
                        player.sendMessage(ChatColor.GREEN + "- Blah");
                        player.sendMessage(ChatColor.DARK_PURPLE + "§m----------------------------------------------------");
                        player.playSound(player.getLocation(), Sound.AMBIENCE_THUNDER, 2F, 1F);
                        return true;
                    }
                }
            }
            if(cmd.getName().equalsIgnoreCase("kits")){
                player.sendMessage(ChatColor.GREEN + "Kits: " + ChatColor.YELLOW + "FishingRod, Swordsman, PvP");
            }
            if(cmd.getName().equalsIgnoreCase("kit")){
                if(args.length == 1) {
                    if(args[0].equalsIgnoreCase("fishingrod")){
                        ItemStack fishingrod = new ItemStack(Material.FISHING_ROD);
                        ItemMeta fritem = fishingrod.getItemMeta();
                        List<String> FRIlore = new ArrayList<String>();
                        FRIlore.add(ChatColor.RED + "Fishing Rod");
                        fritem.setLore(FRIlore);
                        fritem.setDisplayName(ChatColor.AQUA + "Fishing rod");
                        fishingrod.setItemMeta(fritem);
                        player.setItemInHand(fishingrod);
                        player.sendMessage(ChatColor.RED + "You were given a Fishing Rod.");
                        player.playSound(player.getLocation(), Sound.CHICKEN_EGG_POP, 2F, 1F);
                    }
                }
            }
            return false;
        }
    }
     
  2. Offline

    oceantheskatr

    Check if args.length is equal to zero for the first help page. Also don't forget to return true at the end of it, too. (After the firework blast line)
     
  3. Offline

    DoggyCode™

    Also, check for a illegal number exception to check if the argument is actually a integer, and check if the argument comtains commas/dots or not, because /help 1.2 won't work. If you don't do that you will have a lot of internal errors in your console, which we want to avoid.
     
    oceantheskatr likes this.
  4. Offline

    RoboticPlayer

    Check before casting! You are assuming the sender is a player, but never actually checking, meaning that it will throw an error when sent from the console.
    Player player = (Player) sender;
    Should be
    if (sender instanceof Player) {
    Player player = (Player) sender;
    //Code
    }
     
Thread Status:
Not open for further replies.

Share This Page