Solved What the hell is going on

Discussion in 'Spigot Plugin Development' started by SwagiWagi, Mar 8, 2018.

Thread Status:
Not open for further replies.
  1. I've been trying to figure this out for ages:
    What The Hell:
    Code (Text):
    package com.swagiwagi.handlers;

    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    import com.swagiwagi.arena.ArenaManager;
    import com.swagiwagi.kits.KitsManager;
    import com.swagiwagi.lobby.LobbyManager;
    import com.swagiwagi.teams.TeamsHandler;

    public class CommandHandler implements CommandExecutor {

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (sender instanceof Player) {
                Player p = (Player) sender;
                if (label.equalsIgnoreCase("teamdeathmatch")) {
                    if (args.length == 1) {
                        System.out.println("1");
                        if (args[0].equalsIgnoreCase("join")) {
                            if (p.hasPermission("teamdeathmatch.commands.join")) {
                                if (LobbyManager.getManager().hasStarted == true) {
                                    p.sendMessage(ChatColor.RED + "The game has already started!");
                                } else if (LobbyManager.getManager().hasStarted == false) {
                                    LobbyManager.getManager().lobbyAdd(p);
                                }
                            } else {
                                p.sendMessage(ChatColor.RED + "You don't have permission to use this command!");
                            }
                        } else if (args[0].equalsIgnoreCase("setlobby")) {
                            if (p.hasPermission("teamdeathmatch.commands.setlobby")) {
                                LobbyManager.getManager().setLobby(p);
                                p.sendMessage(ChatColor.GREEN + "Lobby has been succesfully set.");
                            }
                        } else if (args[0].equalsIgnoreCase("setarenateamred")) {
                            ArenaManager.getManager().setTeamRed(p);
                        } else if (args[0].equalsIgnoreCase("setarenateamblue")) {
                            ArenaManager.getManager().setTeamBlue(p);
                        } else if (args[0].equalsIgnoreCase("leave")) {
                            if (LobbyManager.lobbyPlayers.contains(p)) {
                                LobbyManager.getManager().lobbyRemove(p);
                                p.sendMessage(ChatColor.GREEN + "You have left the lobby!");
                            } else if (ArenaManager.matchPlayers.contains(p)) {
                                ArenaManager.getManager().removePlayer(p);
                                p.sendMessage(ChatColor.GREEN + "You have left the lobby!");
                            } else {
                                System.out.println("Error + LOBBYREMOVE||ARENAREMOVE");
                            }
                        } else if (args[0].equalsIgnoreCase("teamblue") || args[0].equalsIgnoreCase("teamred")) {
                            LobbyManager.getManager();
                            if (LobbyManager.lobbyPlayers.contains(p)) {
                                if (args[0].equalsIgnoreCase("teamblue")) {
                                    TeamsHandler.getManager().teamBlue(p);
                                }
                                if (args[0].equalsIgnoreCase("teamred")) {
                                    TeamsHandler.getManager().teamRed(p);
                                }
                            } else {
                                p.sendMessage(ChatColor.RED + "You are not in a lobby!");
                            }
                        } else {
                            p.sendMessage(ChatColor.AQUA + "Please use the following syntex: " + ChatColor.BLUE
                                    + "/teamdeathmatch join" + ChatColor.AQUA + "!");
                        }
                    } else if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("kit")) {
                            if (LobbyManager.lobbyPlayers.contains(p) || ArenaManager.matchPlayers.contains(p)) {
                                if (KitsManager.getManager().getKitNull(args[2]) == false) {
                                    KitsManager.getManager().getKit(args[2], p);
                                }

                                for (int i = 0; i < args.length; i++) {
                                    System.out.println(i);
                                    System.out.println(args[i]);
                                }
                            } else {
                                p.sendMessage(ChatColor.RED + "Kit does not exists.");
                            }
                        } else {
                            p.sendMessage(ChatColor.RED + "You are not in a lobby or arena!");
                        }
                    } else {
                        p.sendMessage(ChatColor.AQUA + "Please use the following syntex: " + ChatColor.BLUE
                                + "/teamdeathmatch join" + ChatColor.AQUA + "!");
                    }
                } else {
                    p.sendMessage(ChatColor.AQUA + "Please use the following syntex: " + ChatColor.BLUE
                            + "/teamdeathmatch join" + ChatColor.AQUA + "!");
                }
            } else {
                System.out.println("You have to be a player to use this command!");
            }
            return false;
        }
    }
    Somehow:
    Console:
    Code (Text):
    [22:32:51 INFO]: SwagiWagi issued server command: /teamdeathmatch join
    [22:32:51 INFO]: 1
    [22:32:51 INFO]: SwagiWagi issued server command: /teamdeathmatch kit warrior
    [22:32:51 INFO]: 0
    [22:32:51 INFO]: kit
    [22:32:51 INFO]: 1
    [22:32:51 INFO]: warrior
                                                         
    The problem is with the args if you didn't notice.
     
  2. A bit more information would be helpful. I can't see the problem here.
     
  3. When I type /teamdeathmatch kit warrior, it is considered with three arguments (0, 1, 2), when I type /teamdeathmatch join, it is considered with two arguments(0, 1), look at the args.length check, but when I try to print the args[1], IndexOutOfBounds, same with kit warrior.
     
  4. you know lists start at zero right? args.length == 1 means there is only one value so the only possible index is args[0] (the first value)


    adding on,
    /teamdeathmatch join
    this has ONE arugment which is ‘join’ which appears at the index 0 as arrays and lists always start at 0
     
    • Funny Funny x 1
  5. You're trying to get args[2] from an array of length 2.

    Length 2... args[0], args[1]. That's the whole array. Args[2] doesn't exist. Basic array skills. ;)

    Code (Java):
    if (args.length == 2) {
                        if (args[0].equalsIgnoreCase("kit")) {
                            if (LobbyManager.lobbyPlayers.contains(p) || ArenaManager.matchPlayers.contains(p)) {
                                if (KitsManager.getManager().getKitNull(args[2]) == false) {
                                    KitsManager.getManager().getKit(args[2], p);
                                }
     
    • Funny Funny x 1
  6. Yup fixed that, I was working on this for like two days :(
     
Thread Status:
Not open for further replies.

Share This Page