Solved Need Help With Arguments

Discussion in 'Plugin Development' started by TheArabianCoder, Sep 15, 2015.

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

    TheArabianCoder

    The problem is that when I do /fly, I get and internal error, but if I do /fly player then it works fine. This problem is in some of my commands, but not all. Here is my code:
    Code:
    package me.tag.personal;
    
    import java.util.ArrayList;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Fly implements CommandExecutor {
    
        ArrayList<String> fly = new ArrayList<String>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    
            Player p = (Player) sender;
            Player t = Bukkit.getServer().getPlayer(args[0]);
    
            if (cmd.getName().equalsIgnoreCase("fly")) {
    
    
                if (args.length == 0) {
    
                    if (fly.contains(p.getName())) {
    
                        p.setAllowFlight(false);
                        p.sendMessage(ChatColor.GREEN + "You are now unable to fly!");
                        return true;
    
                    }
    
                    fly.add(p.getName());
                    p.sendMessage(ChatColor.GREEN + "You are now able to fly!");
                    return true;
                }
                fly.add(p.getName());
                p.setAllowFlight(true);
                p.sendMessage(ChatColor.GREEN + "Fly is now Enabled!");
                return true;
            }
            if (t == null) {
                p.sendMessage(ChatColor.RED + "Player " + args[0] + " could not be found!");
                return true;
            } else if (fly.contains(t.getName())) {
               
    
                    fly.remove(t.getName());
                    t.setAllowFlight(false);
                    p.sendMessage(ChatColor.GREEN + "Fly for " + t.getName() + " is now Disabled!");
                    t.sendMessage(ChatColor.GREEN + "Fly is now Disabled!");
                    return true;
                }
                fly.add(t.getName());
                t.setAllowFlight(true);
                p.sendMessage(ChatColor.GREEN + "Fly for " + t.getName() + " is now Enabled!");
                t.sendMessage(ChatColor.GREEN + "Fly is now Enabled!");
                return true;
            }
        }
     
  2. Offline

    SuperSniper

    @TheArabianCoder You need to check the args.length

    Code:
    // When the don't put args:
    if(args.length == 0) {
    // Do Stuff
    
    // When you check for player args:
    if(args.length == 1) {
    // Do Stuff
    
     
  3. Offline

    RoboticPlayer

    Check before casting!
    Code:
    if (sender instanceof Player) {
        //Code
    }
    You don't want the console to be able to execute this command either, as it is setting flight.
     
  4. Offline

    Wingzzz

    I would opt for the negative check instead and return. This allows for the rest of your code to not have to be inside so many if-statements making it easier to read. Only do that if you need one particular functionality though.
     
    Last edited: Sep 15, 2015
  5. Offline

    TheArabianCoder

    They didn't work :(
     
  6. Offline

    SyTeck

    @TheArabianCoder, you are getting an player from your argument before actually checking if your argument exists.

    Code:
    Player t = Bukkit.getServer().getPlayer(args[0]);
    //is basically when there's no argument
    Player t = Bukkit.getServer().getPlayer(null);
    //which obviously throws null exception
     
  7. Offline

    TheArabianCoder

Thread Status:
Not open for further replies.

Share This Page