[SOLVED] Wierd event exception

Discussion in 'Plugin Development' started by LucasEmanuel, Apr 22, 2012.

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

    LucasEmanuel

    Hi, im developing a plugin for my Hunger Games server that keeps track of alive and dead players.

    Im getting alot of wierd event exceptions as soon as i call a method i have created.

    Im listening for player respawns, then calling a method that inputs "false" to a hashmap if he dies, then calls a method that checks if the game is over. Its when its checking if its over that it fails.

    Code:

    This is the hashmap:
    Code:
    private static HashMap<String, Boolean> playerlist = new HashMap<String, Boolean>();
    Its designed like - playerlist<playername, boolean>
    boolean is true if he is alive, and false if he is dead

    This is my listener:
    Code:
    @EventHandler(priority = EventPriority.MONITOR)
    public void onPlayerRespawn(PlayerRespawnEvent event) {
        Player player = event.getPlayer();
     
        VironHungerGames.setDead(player.getName());
    }
    Method that alters the hashmap:
    Code:
    public static void setDead(String player) {
        playerlist.put(player, false);
        checkIfGameOver();
    }
    Method that checks if the game is over:
    Code:
    public static void checkIfGameOver() {
        Player[] onlineplayers = server.getOnlinePlayers();
     
        int aliveplayers = 0;
        int winner = 0;
     
        for(int i = 0 ; i < playerlist.size() ; i++) {
            if(playerlist.get(onlineplayers[i].getName())) {
                aliveplayers++;
            }
        }
     
        if(aliveplayers > 1) {
            server.dispatchCommand(server.getConsoleSender(), "broadcast " + aliveplayers + "/" + playerlist.size() + " still alive.");
        }
        else if(aliveplayers == 1) {
            for(int i = 0 ; i < playerlist.size() ; i++) {
                if(playerlist.get(onlineplayers[i].getName()) == true) {
                    winner = i;
                }
            }
            server.dispatchCommand(server.getConsoleSender(), "broadcast GAME OVER! " + onlineplayers[winner].getName() + " is the winner!");
        }
        else {
            server.dispatchCommand(server.getConsoleSender(), "broadcast Something went wrong!");
        }
    }
    If i dont call that last method the plugin is working fine, so the problem is there somewhere but i just cant find it.

    Exception message:
     
  2. Offline

    Njol

    Which is line 46?
     
  3. Offline

    LucasEmanuel

    You must mean line 47:
    Code:
    if(playerlist.get(onlineplayers[i].getName())) {
    in:
    Code:
    for(int i = 0 ; i < playerlist.size() ; i++) {
        if(playerlist.get(onlineplayers[i].getName())) {
            aliveplayers++;
        }
    }
     
  4. Offline

    DotEfekts

    How are you initialising onlineplayers[]

    EDIT: I'm blind. Just saw in the post.

    Rather than using playerlist.size() try using onlineplayers.length.

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

    LucasEmanuel

    I seem to have fixed the issue, switching from a for-loop to a for-each-loop it seems to be working :)

    Code:
    for(Player player : onlineplayers) {
        if(playerlist.get(player.getName())) {
            aliveplayers++;
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page