Solved Storing names + value

Discussion in 'Plugin Development' started by bennie3211, Jun 10, 2013.

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

    bennie3211

    Hi bukkit, I have a problem. While I was making my pvpkits plugin, i wanted to add a value that contains a number off kills (killstreak). Before, i used an arraylist to store player names, but what is a better method to store player name + there killstreak?
     
  2. Offline

    ase34

    If you want to map name->integer, you can use a HashMap<String, Integer>:

    Code:
    Map<String, Integer> map = new HashMap<String,Integer>();
    Look in the javadocs for the methods, the most important are:
    - put
    - get
    - remove
     
  3. Offline

    bennie3211

    hmhmm, interesting :) I tried to make a new class that stores the kills, deahts and killsteak. Then i made in the main class a new method that returns the values. But how do i display them to the user?

    This is what i have:

    Main Class:

    Code:
    package me.dutch_kids.pvpkits;
     
    import java.util.HashMap;
     
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class PvPkitsMain extends JavaPlugin
    {
       
        public static HashMap<String, PvPkitsPlayerData> playerData = new HashMap<String, PvPkitsPlayerData>();
       
        public static PvPkitsPlayerData getPlayerData(Player player)
        {
            return playerData.get(player.getName());
        }
    }
    and the PvPkitsPlayerData class:

    Code:
    package me.dutch_kids.pvpkits;
     
    import org.bukkit.entity.Player;
     
    public class PvPkitsPlayerData
    {
     
        String holder;
        int kills;
        int deaths;
        int killstreak;
       
        public PvPkitsPlayerData(Player holder)
        {
            this.holder = holder.getName();
            kills = 0;
            deaths = 0;
            killstreak = 0;
        }
    }
    
     
  4. Offline

    AmShaegar

    You need getters in PvPkitsPlayerData.
    Code:
    public int getKills() {
      return kills;
    }
    And you can access it in main with:
    Code:
    PvPkitsPlayerData data = getPlayerData(palyer);
    player.sendMessage("You've got "+data.getKills()+" kills.");
     
  5. Offline

    bennie3211

    But i get the values already? Or am i doing that wrong?

    Edit so i have to replace
    public PvPkitsPlayerData(Player holder)
    {
    this.holder = holder.getName();
    kills = 0;
    deaths = 0;
    killstreak = 0;
    }

    with the int method?
     
  6. Offline

    AmShaegar

    I am sorry, I don't understand you. Your question was:
     
  7. Offline

    frozenpoptartmc

    you could just store it to a hashmap like ase34 suggested which I think would be the easiest way of doing this
     
  8. Offline

    bennie3211

    yes ik, but you told me that the PvPkitsPlayerData class needed getters So i just have to add them to the class or do they replace the
    Code:
    String holder;
        int kills;
        int deaths;
        int killstreak;
       
        public PvPkitsPlayerData(Player holder)
        {
            this.holder = holder.getName();
            kills = 0;
            deaths = 0;
            killstreak = 0;
        }
    ?
     
  9. Offline

    AmShaegar

    Add of course. How would you save the values otherwise? I recommend learning Java basics before creting a mod.
     
Thread Status:
Not open for further replies.

Share This Page