Pickup Item to Increase Score

Discussion in 'Plugin Development' started by LemonBro3, Jan 22, 2013.

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

    LemonBro3

    I am trying to make it where if the player picks up the specified block, and only the specified block, then the score of the team they are on is increased by one. Here is the code:

    Code:
    @EventHandler
        public void onPlayerScore(PlayerPickupItemEvent event){
            Player player = event.getPlayer();
           
            if(event.getItem().getItemStack().getType() == Material.STONE){
               
                if(redTeam.containsKey(player)){
                    player.sendMessage("You have just scored!");
                    redScore++;
                }
                if(blueTeam.containsKey(player)){
                    player.sendMessage("You have just scored!");
                    blueScore++;
                }
            } else {
                event.setCancelled(true);
            }
        }
    As you can see, the plugin allows the pickup of stone, which is what I want. But the two if statements that attempt to increase the score, don't work. Any ideas?
     
  2. Offline

    polaris120990

    LemonBro3 Have you registered your event? Also, please post your whole source as the problem may not be contained within onPlayerScore.

    EDIT: Also do not use == use getType().equals(Material.STONE)
     
  3. Offline

    chasechocolate

    Using == is fine. Also, it is not a good idea to save the player's instance to a HashMap (e.g. don't use HashMap<Player>) use a HashMap<String> to save the player's name and in your listener check if the HashMap contains player.getName().
     
  4. Offline

    LemonBro3

    Is there a reason why not to use <Player>? Also, any idea on the problem with the scoring?
     
  5. Offline

    chasechocolate

    Storing the player's instance can cause memory leaks. I believe that ferrybig 's signature explains why not to do that.
     
  6. LemonBro3
    Storing player objects wont work between logins and cause memory leaks to occur because the Player object stay stored inside the set until the server is reloaded/restarted
    Player objects also have references to locations, what have references to worlds, world data can contains chunks and other things that can be cleanup when a world is removed, if your plugin is keepng those Player objects, it can finaly create out of memory statements because of those worlds its building up over time, not spoken about the entities inside those worlds
     
Thread Status:
Not open for further replies.

Share This Page