ArrayIndexOutOfBounds Help

Discussion in 'Bukkit Help' started by boysnnoco, Feb 20, 2015.

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

    boysnnoco

    I've been making a gamemode, when I went to go test it, ArrayindexOutOfBounds was thrown in my Arena Class. I'm confused because I check to make sure there is atleast 4 in the arraylist before I do anything. But when I call on the class it throws that error.
    Problem is in createTeams I marked it out.
    Keeping the page small (open)


    Code for arena class:
    Code:
    package me.boysnnoco.MCWAR.Arena;
    
    import java.util.ArrayList;
    import java.util.UUID;
    
    import me.boysnnoco.MCWAR.Teams.Team;
    import me.boysnnoco.MCWAR.Utils.Log;
    import me.boysnnoco.MCWAR.Utils.Log.DebugLevel;
    import me.boysnnoco.MCWAR.Utils.MessageHelper;
    import me.boysnnoco.MCWAR.Utils.MessageHelper.MessageType;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.entity.Player;
    
    public class Arena {
    
        private ArrayList<Location> teamSpawns;
        private ArrayList<Location> teamCaptainSpawns;
        private ArrayList<UUID> ingamePlayers;
        private ArrayList<Team> teams;
        private Location preGameLoc;
        private String arenaName;
        private boolean hasStarted;
      
        public Arena(ArrayList<Location> teamSpawns, ArrayList<Location> teamCaptainSpawns, Location preGameLoc, String arenaName) {
    
            this.teamSpawns = teamSpawns;
            this.teamCaptainSpawns = teamCaptainSpawns;
            this.preGameLoc = preGameLoc;
            this.arenaName = arenaName;
          
            this.ingamePlayers = new ArrayList<UUID>();
            this.teams = new ArrayList<Team>();
            hasStarted = false;
          
            init();
        }
      
        private void init() {
          
          
            if(this.teamSpawns.size() != this.teamCaptainSpawns.size()) {
                Log.debug(DebugLevel.WARNING, arenaName + " cannot be enabled, Team Captains amounts do not match Team Spawns.");
                ArenaHelper.removeArena(this);
                return;
            }
          
            if(this.teamSpawns.size() < 4) {
                Log.debug(DebugLevel.WARNING, arenaName + " doesn't have enough spawns! (Minimun of 4)");
                ArenaHelper.removeArena(this);
                return;
            }
          
            if(this.arenaName == null) {
                Log.debug(DebugLevel.WARNING, "couldn't find a name?");
                ArenaHelper.removeArena(this);
                return;
            }
          
            if(this.preGameLoc == null) {
                Log.debug(DebugLevel.WARNING, arenaName + " cannot be enabled, No pregame location set.");
                ArenaHelper.removeArena(this);
                return;
            }
        }
      
        public boolean inArean(Player p) {
            return ingamePlayers.contains(p.getUniqueId());
        }
      
        public void addPlayer(Player p) {
            this.ingamePlayers.add(p.getUniqueId());
            p.teleport(this.preGameLoc);
            update();
        }
      
        public void update() {
            //TODO: Update anything IE Scoreboards etc
        }
    
        public void removePlayer(Player p) {
            if(!inArean(p))
                return;
          
            //TODO: Teleport pack to spawn
            this.ingamePlayers.remove(p.getUniqueId());
            Team t = getTeam(p);
            if(t != null && hasStarted)
                t.remove(p);
        }
      
        private Team getTeam(Player p) {
            for(Team t : teams)
                if(t.containsPlayer(p))
                    return t;
          
            return null;
        }
    
        public ArrayList<Location> getTeamSpawns() {
            return teamSpawns;
        }
    
        public ArrayList<Location> getTeamCaptainSpawns() {
            return teamCaptainSpawns;
        }
    
        public Location getPreGameLoc() {
            return preGameLoc;
        }
    
        public String getArenaName() {
            return arenaName;
        }
    
        public boolean hasStarted() {
            return hasStarted;
        }
    
        public void start() {
            sendToAll("The game has begun!");
            createTeams();
            teleport();
            hasStarted = true;
        }
    
        private void teleport() {
            for(Team t : teams)
                t.teleport();
        }
    
        private void createTeams() {      
          
            ArrayList<UUID> rt = new ArrayList<UUID>();
            ArrayList<UUID> bt = new ArrayList<UUID>();
            ArrayList<UUID> yt = new ArrayList<UUID>();
            ArrayList<UUID> gt = new ArrayList<UUID>();
          
            int index = 0;
      
            for(UUID id : this.ingamePlayers) {
                Player p = Bukkit.getPlayer(id);
                switch(index) {
                case 0:
                    rt.add(id);
                    MessageHelper.sendMessage(p, MessageType.INFO, "You have been placed on the Red team!");
                    break;
                case 1:
                    bt.add(id);
                    MessageHelper.sendMessage(p, MessageType.INFO, "You have been placed on the Blue team!");
                    break;
                case 2:
                    yt.add(id);
                    MessageHelper.sendMessage(p, MessageType.INFO, "You have been placed on the Yellow team!");
                    break;
                case 3:
                    MessageHelper.sendMessage(p, MessageType.INFO, "You have been placed on the Green team!");
                    break;
                }
                index++;
                if(index > 3) index = 0;
            }
          
            //Problem here, it says the teams are less than 4 even though I've checked to make sure there is for and up.
            Log.debug(DebugLevel.DEBUG, this.teamSpawns.size() + "");
            if(this.teamSpawns == null || this.teamSpawns.size() < 4)  {
                Log.debug(DebugLevel.DEBUG, "Team spawns is null or teamspawns is less than for?");
                return;
            }
            Location rtS = teamSpawns.get(0);
          
            if(rt.size() > 0)
                teams.add(new Team(rt, rtS, this));
          
            if(bt.size() > 0)
                teams.add(new Team(bt, teamSpawns.get(1), this));
    
            if(yt.size() > 0)
                teams.add(new Team(yt, teamSpawns.get(2), this));
          
            if(gt.size() > 0)
                teams.add(new Team(gt, teamSpawns.get(3), this));
          
          
        }
    
        private void sendToAll(String string) {
            for(UUID id : this.ingamePlayers) {
                Player p = Bukkit.getPlayer(id);
                MessageHelper.sendMessage(p, MessageType.INFO, string);
            }
        }
    
        public void end() {
            sendToAll("The game has been force ended!");
        }
    
        public void remove() {
            for(UUID id : ingamePlayers) {
                Player p = Bukkit.getPlayer(id);
                removePlayer(p);
            }
        }
       
      
      
    }
    

    Code for ArenaHelper
    Code:
    package me.boysnnoco.MCWAR.Arena;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    
    import me.boysnnoco.MCWAR.Main;
    import me.boysnnoco.MCWAR.Utils.Log;
    import me.boysnnoco.MCWAR.Utils.Log.DebugLevel;
    
    import org.bukkit.Bukkit;
    import org.bukkit.Location;
    import org.bukkit.World;
    import org.bukkit.entity.Player;
    
    public class ArenaHelper {
    
        private static ArrayList<Arena> arenas = new ArrayList<Arena>();
    
        public static void registerArenas() {
    
            // Load arenas from file
    
            File arena = new File(Main.instance.getDataFolder() + File.separator
                    + "Arenas");
            if (!arena.exists()) {
                Log.debug(DebugLevel.WARNING,
                        "Unable to find Arenas file generating new one...");
                try {
                    arena.createNewFile();
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.debug(DebugLevel.WARNING,
                            "Unable to generate Arenas file.. ");
                }
                return;
            }
    
            try {
    
                FileReader in = new FileReader(arena);
                BufferedReader br = new BufferedReader(in);
                String line;
                String name = null;
                ArrayList<Location> spawnLoc = new ArrayList<Location>();
                ArrayList<Location> spawnLocCap = new ArrayList<Location>();
                Location loc = null;
                World world = null;
    
                boolean capSpawn = false;
                boolean regSpawns = false;
                boolean preGameSpawns = false;
    
                while ((line = br.readLine()) != null) {
    
                    // Begining of Arena
                    if (line.startsWith("Arena_Name")) {
    
                        if (spawnLoc.size() > 0) {
                            Log.debug(DebugLevel.DEBUG, "Creating the arena "
                                    + name + "!");
                            Arena a = new Arena(spawnLoc, spawnLocCap, loc, name);
                            arenas.add(a);
                            spawnLoc.clear();
                            spawnLocCap.clear();
                            loc = null;
                            name = line.replace("Arena_Name ", "");
                            world = null;
                            capSpawn = false;
                            regSpawns = false;
                            preGameSpawns = false;
                            Log.debug(DebugLevel.INFO, name);
                            continue;
                        } else {
    
                            name = line.replace("Arena_Name ", "");
                            spawnLoc.clear();
                            spawnLocCap.clear();
                            loc = null;
                            name = line.replace("Arena_Name ", "");
                            world = null;
                            capSpawn = false;
                            regSpawns = false;
                            preGameSpawns = false;
                            continue;
                        }
                    }
    
                    if (line.startsWith("Game_World")) {
                        world = Bukkit.getWorld(line.replace("Game_World ", ""));
                        Log.debug(DebugLevel.DEBUG, "Searching for the world "
                                + line.replace("Game_World ", "") + " ....");
                        continue;
                    }
    
                    if (line.startsWith("Captain_Spawns")) {
                        capSpawn = true;
                        regSpawns = false;
                        preGameSpawns = false;
                        continue;
                    }
    
                    if (line.startsWith("Game_Spawns")) {
                        regSpawns = true;
                        capSpawn = false;
                        preGameSpawns = false;
                        continue;
                    }
    
                    if (line.startsWith("PreGame_Spawn")) {
                        regSpawns = false;
                        capSpawn = false;
                        preGameSpawns = true;
                        continue;
                    }
    
                    if (capSpawn) {
    
                        String[] args = line.split(":");
    
                        try {
    
                            if (args.length < 3) {
                                Log.debug(
                                        DebugLevel.SEVERE,
                                        line
                                                + " doesn't contain enough numbers. Syntax X:Y:Z");
                                continue;
                            }
    
                            int x = Integer.parseInt(args[0]);
                            int y = Integer.parseInt(args[1]);
                            int z = Integer.parseInt(args[2]);
    
                            spawnLocCap.add(new Location(world, x, y, z));
    
                        } catch (ArithmeticException e) {
                            e.printStackTrace();
                            Log.debug(DebugLevel.WARNING, line
                                    + " contains a non-number.");
                            continue;
                        }
                    }
    
                    if (regSpawns) {
    
                        String[] args = line.split(":");
    
                        try {
    
                            if (args.length < 3) {
                                Log.debug(
                                        DebugLevel.SEVERE,
                                        line
                                                + " doesn't contain enough numbers. Syntax X:Y:Z");
                                continue;
                            }
    
                            int x = Integer.parseInt(args[0]);
                            int y = Integer.parseInt(args[1]);
                            int z = Integer.parseInt(args[2]);
    
                            spawnLoc.add(new Location(world, x, y, z));
    
                        } catch (ArithmeticException e) {
                            e.printStackTrace();
                            Log.debug(DebugLevel.WARNING, line
                                    + " contains a non-number.");
                            continue;
                        }
    
                    }
    
                    if (preGameSpawns) {
                        String[] args = line.split(":");
    
                        try {
    
                            if (args.length < 3) {
                                Log.debug(DebugLevel.WARNING, line + " doesn't contain enough numbers. Syntax X:Y:Z");
                                continue;
                            }
    
                            int x = Integer.parseInt(args[0]);
                            int y = Integer.parseInt(args[1]);
                            int z = Integer.parseInt(args[2]);
    
                            loc = new Location(world, x, y, z);
    
                        } catch (ArithmeticException e) {
                            e.printStackTrace();
                            Log.debug(DebugLevel.WARNING, line
                                    + " contains a non-number.");
                            continue;
                        }
                    }
    
    
                }
              
    
                if (spawnLoc.size() > 0) {
                    Log.debug(DebugLevel.DEBUG, "Creating the arena " + name + "!");
                    Arena a = new Arena(spawnLoc, spawnLocCap, loc, name);
                    arenas.add(a);
                    spawnLoc.clear();
                    spawnLocCap.clear();
                    loc = null;
                    world = null;
                    capSpawn = false;
                    regSpawns = false;
                    preGameSpawns = false;
                  
                }
              
                br.close();
                in.close();
    
            } catch (Exception e) {
                e.printStackTrace();
    
                Log.debug(DebugLevel.SEVERE,
                        "Something happened while trying to load arenas.");
    
            }
    
        }
    
        public static Arena findArenaByName(String string) {
          
            for(Arena a : arenas) {
                if(a.getArenaName().equals(string)) {
                    Log.debug(DebugLevel.DEBUG, "Found " + a.getArenaName() + " is " + string);
                    return a;
                }
            }
            return null;
        }
    
        public static Arena findArenaByUser(Player p) {
          
            for(Arena a : arenas) {
                if(a.inArean(p)) {
                    return a;
                }
            }
            return null;
        }
      
        public static void removeArena(Arena arena) {
            if(!arenas.contains(arena))
                return;
            arenas.remove(arena);
            arena.remove();
            Log.debug(DebugLevel.INFO, "Removing the arena " + arena.getArenaName() + " from the arena pool!");
        }
    
        public static void tick() {
            for(Arena a : arenas) {
                a.update();
            }
        }
    
    }
    


    Any help would be appreciated!
     
    Last edited: Feb 20, 2015
Thread Status:
Not open for further replies.

Share This Page