Solved exception java.lang.IllegalArgumentException: n must be positive

Discussion in 'Plugin Development' started by BajanAmerican, Mar 3, 2013.

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

    BajanAmerican

    Hello, I was wondering if someone could help me. I made two array lists,
    Code:
    static ArrayList<Player> red = new ArrayList<Player>();
        static ArrayList<Player> blue = new ArrayList<Player>();
    and I want to split them up into two teams, but my code keeps generating the error in the title.
    Code:
    public static void SplitTeams() {
            World world = Bukkit.getWorld("world");
            Random rnd = new Random();
            int size = blue.size();
            int random = rnd.nextInt(size);
            Player player = blue.get(random);
            Location BlueSpawn = new Location(world, 1399, 68, -417);
            Location RedSpawn = new Location(null, 1311, 140, -460);
            if (rnd.nextInt(100) > 50) {
                red.add(player);
                player.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
            } else {
                blue.add(player);
                player.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
                if (red.size() > blue.size()) {
                    blue.add(player);
                } else if (red.size() < blue.size()) {
                    red.add(player);
                } else {
                    Integer team = new Random().nextInt(2);
                    if (team == 1) {
                        red.add(player);
                    } else {
                        blue.add(player);
                    }
                }
            }
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (blue.contains(p)) {
                    p.teleport(BlueSpawn);
                }
                if (red.contains(p)) {
                    p.teleport(RedSpawn);
                }
            }
        }
    If anyone could help me, that would be greatly appreciated. Thanks!
     
  2. Offline

    minoneer

    Please post the whole error log. That will make it much easier to help you.

    What I noticed in your code: you are setting the world for the SpawnLocation of the red team to "null", which is a really bad idea. Also, you somehow add the player 3 or 4 times to a team, resulting in those lists having players multiple times and/or on both teams.
     
  3. Offline

    BajanAmerican

    This is the whole error log:
    I did not notice the null for the red team (facepalm), and what do you recommend for the players in teams? The only reason I did that was to have it run through multiple times to make sure that the teams are even. Thanks!
     
  4. Offline

    minoneer

    Oh, you are working with tasks. Could you please post you whole class, not just this one method?
     
  5. Offline

    BajanAmerican

    Code:
    public class CloudWars extends JavaPlugin implements Listener {
        public final Logger logger = Logger.getLogger("Minecraft");
        public static CloudWars plugin;
        public static int gameLoop = 0;
        public static int timeInSeconds;
        public static boolean canStart;
       
        static ArrayList<Player> red = new ArrayList<Player>();
        static ArrayList<Player> blue = new ArrayList<Player>();
       
       
        @Override
        public void onDisable() {
            getServer().getScheduler().cancelTask(gameLoop);
            this.logger.info("DISABLED!");
        }
     
        @Override
        public void onEnable() {
            CanStart(Bukkit.getOnlinePlayers().length);
            timeInSeconds = 120;
            gameLoop = getServer().getScheduler().scheduleSyncRepeatingTask(this,
                    new GameLoop(), 20l, 20l);
            PluginDescriptionFile pdfFile = this.getDescription();
            logger.info("[" + pdfFile.getName() + "] v" + pdfFile.getVersion()
                    + " Has Been Enabled!");
        }
     
        public void CanStart(int OnlinePlayers){
              if(Bukkit.getOnlinePlayers().length >= 3){
                canStart = true;
                this.logger.info("Player Update: Greater then 4");
              } else {
                canStart = false;
                this.logger.info("Player Update: Less then 4");
                }
            }
        public static void SplitTeams() {
            World world = Bukkit.getWorld("world");
            Random rnd = new Random();
            int size = blue.size();
            int random = rnd.nextInt(size);
            Player player = blue.get(random);
            Location BlueSpawn = new Location(world, 1399, 68, -417);
            Location RedSpawn = new Location(world, 1311, 140, -460);
            if (rnd.nextInt(100) > 50) {
                red.add(player);
                player.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
            } else {
                blue.add(player);
                player.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
                        + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
                if (red.size() > blue.size()) {
                    blue.add(player);
                } else if (red.size() < blue.size()) {
                    red.add(player);
                } else {
                    Integer team = new Random().nextInt(2);
                    if (team == 1) {
                        red.add(player);
                    } else {
                        blue.add(player);
                    }
                }
            }
            for (Player p : Bukkit.getOnlinePlayers()) {
                if (blue.contains(p)) {
                    p.teleport(BlueSpawn);
                }
                if (red.contains(p)) {
                    p.teleport(RedSpawn);
                }
            }
        }
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            CanStart(Bukkit.getOnlinePlayers().length);
        }
     
        @EventHandler
        public void onPlayerLeave(PlayerQuitEvent event) {
            CanStart(Bukkit.getOnlinePlayers().length - 1);
        }
     
        public boolean onCommand(CommandSender sender, Command cmd,
                String commandLabel, final String[] args) {
            return false;
        }
    }
     
    *NEW CLASS*
     
    public class GameLoop extends CloudWars implements Runnable {
     
        public static int gameTime;
     
        public void run() {
            if (CloudWars.timeInSeconds >= 60 && CloudWars.timeInSeconds <= 120
                    && CloudWars.timeInSeconds % 60 == 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 30 && CloudWars.timeInSeconds % 15 == 0
                    && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(true);
            }
            if (CloudWars.timeInSeconds <= 10 && CloudWars.timeInSeconds > 0) {
                broadcastTimeUntilStart(false);
                note();
            }
     
            if (CloudWars.timeInSeconds == 0) {
                if(canStart) {
                    Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE
                            + "[Cloud Wars] " + ChatColor.RED
                            + "THE GAME HAS BEGUN!");
                    CloudWars.SplitTeams();
                    getServer().getScheduler().scheduleSyncRepeatingTask(this,
                            new PlayLoop(), 20l, 20l);
                    gameTime = 1200;
                    getServer().getScheduler().cancelTask(gameLoop);
                } else {
                    Bukkit.broadcastMessage(ChatColor.YELLOW
                            + "Not Enough Players! Resetting Timer!");
                    resetNote();
                    CloudWars.timeInSeconds = 121;
                }
            }
            if (CloudWars.timeInSeconds > 0) {
                CloudWars.timeInSeconds--;
            }
            if (timeInSeconds > 120) {
                broadcastTimeUntilStart(false);
            }
        }
     
        private void note() {
            for (Player player : Bukkit.getOnlinePlayers()) {
                player.playSound(player.getLocation(), Sound.NOTE_PIANO, 15, 2);
            }
        }
     
        private void resetNote() {
            for (final Player player : Bukkit.getOnlinePlayers()) {
                final Location l = player.getLocation();
                new Thread(new Runnable() {
     
                    public void run() {
                        try {
                            for (int i = 0; i < 4; i++) {
                                player.playEffect(l, Effect.CLICK1, 1);
                                Thread.sleep(450);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        }
     
        private void broadcastTimeUntilStart(boolean extraArgs) {
            if (extraArgs) {
                Bukkit.broadcastMessage(ChatColor.GOLD + "FABIAN IS GAY!");
            }
            if (CloudWars.timeInSeconds % 60 == 0 && CloudWars.timeInSeconds >= 60) {
                Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "[CloudWars] "
                        + ChatColor.GOLD + CloudWars.timeInSeconds
                        + ChatColor.GREEN + " Seconds Until Start!");
            } else if (CloudWars.timeInSeconds < 30) {
                Bukkit.broadcastMessage(ChatColor.LIGHT_PURPLE + "[CloudWars] "
                        + ChatColor.GOLD + CloudWars.timeInSeconds
                        + ChatColor.GREEN + " Seconds Until The Game Starts!");
            }
        }
    }
    
    So what do you think?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  6. Offline

    minoneer

    Still reading your code ;)

    ok, the problem here is if the blue list is empty, the "int size = blue.size();" will be "0". The "Random.nextInt(Number)" only takes positive numers, meaning "0" will cause the exception you see above.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 31, 2016
  7. Offline

    BajanAmerican

    That's what my original thought was. But now I have no idea how to troubleshoot it :(
     
  8. Offline

    minoneer

    well, if you are starting a game, you probably want to make sure at least a certain ammount (2? 4?) of players are starting. Just don't start untill at least that ammount has joined, then you'll never have an empty list.
     
  9. Offline

    BajanAmerican

    Yeah I had 4 players in my server and it did that. That's why I have the "public void CanStart" there. It still generates an empty list. I played around with that and it wouldn't start the game and when I reversed it, it would show the error you see there AND the message "The game has begun" will just keep spamming and not stop. :/
     
  10. Offline

    minoneer

    Are always all player on the server going to join the game? or is there some way to say hey I wanna join or not
     
  11. Offline

    BajanAmerican

    No, all players must join the game
     
  12. Offline

    minoneer

    ok, that makes things a lot easier.

    Code:java
    1.  
    2. public static void SplitTeams() {
    3. World world = Bukkit.getWorld("world");
    4. Random rnd = new Random();
    5.  
    6. Location BlueSpawn = new Location(world, 1399, 68, -417);
    7. Location RedSpawn = new Location(world, 1311, 140, -460);
    8.  
    9. blue = new ArrayList<Player>();
    10. red = new ArrayList<Player>();
    11. Player[] player = Bukkit.getServer().getOnlinePlayers();
    12. int size = players.length;
    13.  
    14. if (size < 4) {
    15. Bukkit.getServer().broadcastMessage("Not enough player online");
    16. return;
    17. } else {
    18. int i = 0;
    19. for(Player p : player) {
    20.  
    21. if (i%2 == 0) {
    22. red.add(p);
    23. p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
    24. + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
    25. p.teleport(RedSpawn);
    26. } else {
    27. blue.add(p);
    28. p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
    29. + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
    30. p.teleport(BlueSpawn);
    31. }
    32. }
    33. }
    34. }



    I would use something like this.

    (Note: I wrote this code from memory in this editor. There might be some typos.)
     
  13. Offline

    BajanAmerican

    Alright so I ran the plugin a few times and it works perfectly expect for one thing, no one is on the blue team. Any suggestions? Thanks man, I really appreciate it!
     
  14. Offline

    minoneer

    oh, yea I forgot to increase the index.

    Code:java
    1.  
    2. public static void SplitTeams() {
    3. World world = Bukkit.getWorld("world");
    4. Random rnd = new Random();
    5.  
    6. Location BlueSpawn = new Location(world, 1399, 68, -417);
    7. Location RedSpawn = new Location(world, 1311, 140, -460);
    8.  
    9. blue = new ArrayList<Player>();
    10. red = new ArrayList<Player>();
    11. Player[] player = Bukkit.getServer().getOnlinePlayers();
    12. int size = players.length;
    13.  
    14. if (size < 4) {
    15. Bukkit.getServer().broadcastMessage("Not enough player online");
    16. return;
    17. } else {
    18. int i = 0;
    19. for(Player p : player) {
    20.  
    21. if (i%2 == 0) {
    22. red.add(p);
    23. p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
    24. + ChatColor.RED + "YOU ARE ON THE RED TEAM!");
    25. p.teleport(RedSpawn);
    26. } else {
    27. blue.add(p);
    28. p.sendMessage(ChatColor.LIGHT_PURPLE + "[Cloud Wars] "
    29. + ChatColor.BLUE + "YOU ARE ON THE BLUE TEAM!");
    30. p.teleport(BlueSpawn);
    31. }
    32. i++;
    33. }
    34. }
    35. }
     
Thread Status:
Not open for further replies.

Share This Page