Solved Getting Errors when trying to add arguments to my code.

Discussion in 'Plugin Development' started by McJoshuaGaming, Jan 28, 2020.

Thread Status:
Not open for further replies.
  1. Trying to add an argument to my code and its just giving me an error.

    Code:
    
    package me.SkyJoshua.CustomUHC.commands;
    import org.bukkit.Bukkit;
    import org.bukkit.GameMode;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import me.SkyJoshua.CustomUHC.Main;
    public class Test implements CommandExecutor{
    
    private Main plugin;
    
    public Test(Main plugin) {
    this.plugin = plugin;
    
    plugin.getCommand("gmc").setExecutor(this);
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    if (!(sender instanceof Player)) {
    sender.sendMessage("Only players may execute this command!");
    return true;
    }
    
    if (sender instanceof Player) {
    Player p = (Player) sender;
    
    if (label.equalsIgnoreCase("gmc")) {
    if (args.length == 1) {
    Player t = Bukkit.getPlayer(args[1]);
    t.setGameMode(GameMode.CREATIVE);
    } else {
    
    p.sendMessage("Usage: /gmc <player>");
    }
    }
    }
    
    return false;
    }
    }
    
    Console Error (open)
     
  2. Offline

    cdnyassuo34

    Ok, in java , a List start at 0 (ex: [0,1,2,3,4,5,6,etc...] and here you are trying to get the argument at the position 1, and as you can see here: [0,1,2,3,4,5,6,etc...] 1 is at the second position ... you just need to get the argument 0
    with args[0] instead of args[1] here :
    Player t = Bukkit.getPlayer(args[1]); == > Player t = Bukkit.getPlayer(args[0]);

    sorry if my english is not good, I am french ^^
    hope it helped you ^^
     
  3. Offline

    Strahan

    Yep. Also you don't need to do the sender instanceof Player check the second time; you already checked if they weren't and returned. So if the code reaches that point, they must be a Player so checking is redundant. You also should validate that the player you get is legit. If they made a typo or the player isn't on, t will be null and will crash the plugin when you attempt to set a gamemode on it.

    Also you're better off checking cmd.getName() instead of label. I'd also recommend you continue with the negative check/return as you did with the sender Player check. Keeps it cleaner IMO. You may want to consider some sort of status return to the command issuer to let them know it was done. So like I'd do:
    Code:
    if (cmd.getName().equalsIgnoreCase("gmc")) {
      if (args.length == 0) {
        p.sendMessage("Usage: /gmc <player>");
        return true;
      }
    
      Player t = Bukkit.getPlayer(args[1]);
      if (t == null) {
        p.sendMessage("Invalid player!");
        return true;
      }
    
      t.setGameMode(GameMode.CREATIVE);
      p.sendMessage(args[1] + " is now in creative mode.");
    }
     
  4. Ty both of you, I got it working now. TY again :)
     
Thread Status:
Not open for further replies.

Share This Page