Need help hashmap

Discussion in 'Plugin Development' started by ImRayz66, May 24, 2015.

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

    ImRayz66

    I want it so when a player joins a hashmap will be created, storing them as the key and an int as the value.
    I am making a deathstreaks plugin so when a player dies the hashmap int is increased by 1 and when they get a kill it is set to 0. If you have a better idea also say pls! :) ty
     
  2. Offline

    mine-care

    You mean the player is added to the map, not it is cerated.
    The structure:
    1. A hashmap variable of <String,Integer> datatypes
    2. On death: Check if the map contains player's name, if it does, set the value to itself + 1 else add player's name with value 1
    3. On disable of your plugin either serialise the map or using a loop save its contents to a Yaml file.

    Notice: I mentioned Player's Name, Thats good as long as it is for temp storage if you are going for step 3 (saving of data) prefer to use player's UUID instead.
     
  3. ^^ What mine-care said is good, and yeah I'd use UUIDs if storing, but player names if not storing to a file. Anyway, in addition to what he said, if you're using Java 8, step 2 is a lot easier.

    This is how you'd do it in Java 8 (example):
    Code:
    public Map<String, Integer> playerDeaths = new HashMap<>(); // Create the Map.
    
    In the PlayerDeathEvent:
    Code:
    playerDeaths.put(event.getEntity().getName(), playerDeaths.getOrDefault(event.getEntity().getName(), 0) + 1); // Put/replace the player in the Map's death streak with 1 + their current streak. If they aren't in the Map, use "0" as the default death streak.
    
    Also in the PlayerDeathEvent, check if the entity's killer is not null, if so, remove the killer's name from the Map.

    If not using Java 8, you would most likely do:
    Code:
    if (playerDeaths.containsKey(event.getEntity().getName())) playerDeaths.put(event.getEntity().getName(), playerDeaths.get(event.getEntity().getName()) + 1); // If they are in the Map, get their current death streak and add one to it, putting it back into the Map.
    else playerDeaths.put(event.getEntity().getName(), 1); // If they aren't in the Map, put them in the Map with a death streak of 1.
    
     
  4. Offline

    ImRayz66

    thanks guys!
     
  5. Will be better if he unload the player on quit, if you have a big server(even if not) it will end with thousands of player before a stop.
     
  6. I knew that as I was typing the code, I just didn't put it in there because I wanted him to realise that himself in the future, but oh well =)
     
Thread Status:
Not open for further replies.

Share This Page