Hashmap + Integer[]: Increase an single integer of it?

Discussion in 'Plugin Development' started by recon88, Nov 30, 2012.

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

    recon88

    How can I increase an single Integer in a hashmap with Integer[] by 1?

    Code:java
    1.  
    2.  
    3. public void raiseCount(String playername, Integer itemtype //Is that correct?)
    4. {
    5. Integer[] count = database.get(playername);
    6. if (count == null) {
    7. database.put(playername, new Integer[7]);
    8. } else {
    9. // Raise the given itemtype here (+1). Got 7 Itemtypes.
    10. }
    11. }
    12.  
     
  2. Offline

    bergerkiller

    recon88
    I think you can just use int itemtype and int[] count. Instead of using HashMap<String, Integer[]> you could use HashMap<String, int[]> instead. I believe int[] arrays are a class type as well, and thus can be represented as such in a hashmap.
    PHP:
    public void raiseCount(String playernameint itemtype)
    {
        
    int[] count database.get(playername);
        if (
    count == null) {
            
    count = new int[7]; // Put in new array
            
    database.put(playernamecount);
        }
        
    // Raise the given itemtype here (+1). Got 7 Itemtypes.
        // The count array instance is the same as in the map
        // Changing it here will automatically change it in the map
        // To get the index, you might need an itemtype -> index conversion function
        
    count[getIndex(itemType)]++;
    }
     
    private final 
    Material[] materials = new Material[] {Material.WOODMaterial.COALMaterial.IRON_OREMaterial.STICKMaterial.DIAMONDMaterial.STONEMaterial.GOLD_BLOCK};
    public 
    int getIndex(int itemType) {
        for (
    int i 0materials.lengthi++) {
            if (
    materials[i].getId() == itemType) {
                return 
    i;
            }
        }
        return -
    1// This will cause an error!
    }
     
  3. Offline

    recon88

    Wait. Where is the database.put? Don't I need that?

    Code:
    To get the index, you might need an itemtype -> index conversion function
    This is where I'm stuck.
     
  4. Offline

    bergerkiller

    recon88
    Edited my post with the added function.
     
  5. Offline

    recon88

    Code:java
    1. public void raiseCount(String playername, Integer itemtype) {
    2. Integer[] count = database.get(playername);
    3. if (count == null) {
    4. count = new Integer[8];
    5. database.put(playername, count);
    6. } else
    7. count[getIndex(itemtype)]++;
    8. database.put(playername, count);
    9. }
    10.  


    "getIndex(itemtype)" gives me an NPE if I pass an ItemID. Am I doing it wrong?

    Code:java
    1.  
    2. private final Material[] materials = new Material[] {Material.STONE, Material.COAL_ORE, Material.IRON_ORE, Material.GOLD_ORE, Material.DIAMOND_ORE, Material.EMERALD_ORE, Material.REDSTONE_ORE, Material.LAPIS_ORE};
    3. public int getIndex(int itemType) {
    4. for (int i = 0; i < materials.length; i++) {
    5. if (materials.getId() == itemType) {
    6. return i;
    7. }
    8. }
    9. return -1;
    10. }
    11.  


    Code:java
    1. @EventHandler
    2. public void onBlockBrake(BlockBreakEvent event) {
    3. Block blocktype = event.getBlock();
    4. String playername = event.getPlayer().getName().toLowerCase();
    5.  
    6. if (blocktype.getType().equals(Material.STONE)) {
    7. System.out.println("STONE");
    8. plugin.raiseCount(playername, blocktype.getTypeId());
    9. }


    Nevermind.

    Replaced
    Code:
    count = new Integer[8];
    with
    Code:
    count = new Integer[] { 0, 0, 0, 0, 0, 0, 0 };
    for new players.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page