Storing a number using the exp bar?

Discussion in 'Plugin Development' started by xXRobbie21Xx, Jul 27, 2014.

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

    xXRobbie21Xx

    Ok guys i recently made a plugin that gave a player a buff once reaching a certain level. And due to the fact that i need to make a killstreak plugin, i was wondering if it would be okay to store a number using there exp bar. In other words, when ever a player kills someone i add one level to there bar and and of course when they die, the levels are removed.

    However would it be cool to base all perks, messages, etc, off a playerlevelchange event? So i check for when there level goes up (given when they kill someone) and based off that number i can either announce the killstreak/give them a buff.

    I understand im making a fool out of myself but, do you guys have any input about this?
     
  2. Offline

    Deleted user

    If your goal is visual display to the player, then I suppose. Technically if you used the XP Level to store an int (as long as no other plugin is altering the level and no exp can be received via mobs, players, ores, or whatever else) it will remain persistent.

    Although I would highly suggest using a HashMap to store data.
    A HashMap can be created, like so..
    Code:
    public static HashMap<UUID, Integer> killStreak = new HashMap<UUID, Integer>();
    
    HashMaps can be used to store/retrieve keys and values, like so..
    Code:
    -- to add a player into the data --
    killSteak.put(player.getUniqueId(), 1);
    -- to increase a player's kills by 1 you would use --
    // be sure to check the player is in the data before doing this
    killSteak.put(player.getUniqueId(), killSteak.get(player.getUniqueId()) + 1);
    -- To check to see if a player is in the data --
    if (killStreak.containsKey(player.getUniqueId())) {
    // yes they're here!
    } else {
    //no they're not, add them?
    }
    
    Keep in mind that to retain this data you must (serialize if applicable - not in this case!) save it.

    When storing data I personally prefer to use and ObjectOutputStream (totally easier imo and allows for a larger range, and maybe more secure, of data storage) however I find that a lot of people like using yaml (it also seems to be the most basic, or common, method of data storage around here) To save the data via yml you could do something like this:
    Code:
    -- the stock config.yml could be use for this system but you can also create a custom yml --
    File dataFile = new File(plugin.getDataFolder(), "data.yml");
     
    if (!dataFile .exists()) {
    dataFile .getParentFile().mkdirs();
    // yay we created another yml
    }
    -- to loads the custom yml file --
    File dataFile = new File(plugin.getDataFolder(), "data.yml");
    YamlConfiguration data = YamlConfiguration.loadConfiguration(dataFile );
    -- storing data within the custom yml --
    ConfigurationSection keys = dataFile.createSection("keys");
    for (UUID playerId : killStreak.ketSet()) {
    keys.set(String.valueOf(playerId), killStreak.get(playerId));
    }
    -- loading data within the custom yml --
    ConfigurationSection keys = dataFile.getConfigurationSection("keys");
    for (String uid: keys.getKeys(false)) {
    UUID playerId = UUID.fromString(playerId);
    int kills = keys.getInt(uid);
    killStreak.put(playerId, kills);
    }
    
    Now Ik you might be thinking this is a lot of work, or what's the point? Well here's your damn point, like I had previously mentioned: if visual display is your desired result..

    Using this form of data storage, does not only allow you store pretty much whatever you want, but it can also be displayed various ways: Comprehensive GUI Links and Tutorials Thread for your Plugins
     
    xXRobbie21Xx likes this.
  3. Offline

    xXRobbie21Xx

    Eballer48 Oh wow, dude thank you so much for writing that! I previously had no clue about how to work with hashmaps, and thanks to you i can get this done legitimately XD. I really appreciate the time you put into this, i really learned allot! Sorry for the late reply, (i feel terrible for that) i was just too excited to get to work.
     
  4. Offline

    Deleted user

    Not a problem! Glad to help.
     
Thread Status:
Not open for further replies.

Share This Page