Solved Hashmap if checker not cooperating.

Discussion in 'Plugin Help/Development/Requests' started by Scorpionvssub, Aug 11, 2015.

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


    Im working on a Scoreboard but its gonna require either a hashmap or a file such as a config file so...thought i'd go with hashmaps, might learn me a bit more...

    private HashMap<String, Integer> kills;
    private HashMap<String, Integer> deaths;
    The hashmap setup should speak for itself and will store the amount of kills/deaths a player has.

    Since i dont wanna create a new hashmap everytime as it will lose the amount it already had stored i thought...

    Player p = e.getPlayer();
    if (!(kills.containsKey(p.getName()))) {
    would work...but apperantly it always returns nullpointer and i got no clue why.
  2. Offline

    I Al Istannen

    @Scorpionvssub Have you instantiated the HashMap? using "= new HashMap<>()"? If you do that in the constructor (or the onEnable(), depends on your class setup) or just after what you wrote, it should only be created once. So it wouldn't loose anything, as long as you don't create another instance of the class containing the HashMaps. You would need to save it between restarts though.

    And if this is stored for a longer amount of time, UUIDs could be useful. Players can change their name.
  3. Offline


    Last edited: Aug 13, 2015
  4. Offline

    I Al Istannen

    @Scorpionvssub You create a new HashMap every time a player JOINS. So it would delete the previous one.
    What you should do is creating the HashMap in the constructor or directly after the declaration. So line 21 would become "private Map<String, Integer> kills = new HashMap<String, Integer>();".
    By doing so a new a new HashMap is only created when the class is created. That means, as long as you don't restart / reload the server, it won't get resetted. If you want to make it persistent, you will have to write it to a file. You could use YAMLConfigurations which make the saving / loading pretty easy.
  5. Offline


    which is 1 thing i never tried X) i know how configs work but as far as creating a completely new file idea
  6. Offline


    @Scorpionvssub You can just use the default config file, that's what I do (#lazy, I know). Just set config values that you want to save onDisable(), then load them onEnable().
  7. Offline


    yea i got that working now just the problem is Lets say i want others to use it(not really the plan right now but) in instance of others. they may wanna change...the prefix the top of the scoreboard name you name it. It saves it loads it stores it...well does everything almost right, ...almost. within the default config it has:

    prefix: <a prefix here>
    Userdata: []

    As soon as it saves it goes like this:

    Settings: {}
    <someones uuid here>
    kills: <amount>
    deaths: <amount>

    Every user gets added to it everytime upon server shutting down or leaving the server.

    Since i made the kitpvp plugin use both sign feature and GUI feature with multiple gui's the signs use the prefix from the config. else it says [kitpvp] in orange good haha
  8. Offline


    @Scorpionvssub since you're setting stuff in the config then saving it, I think you need to set the prefix again. Pretty sure that's how I would fix overwrite problems.
  9. Offline


    Got it to work indeed that way :p

    Im looking through possible Effects for arrow trails but im trying to find the enchant effect, since bukkit uses diff methods to retrieve something or calls things different, anyone know the name of the Enchantment Effect? which u get from bookcases/enchantment tables

    Found itnvm
Thread Status:
Not open for further replies.

Share This Page