Advanced hashmaps help

Discussion in 'Spigot Plugin Development' started by Multimap, Apr 27, 2017.

  1. I know a fair bit about the bukkit api and java, i've been coding for awhile now and this is one of the first times in a while I've really tried to do something but I just can't figure it out.

    Basically what I am needed to do is store a hashmap like <Integer (for getting a specific inventory), List<Material, Integer (amount>

    I need it so i can stop a block and an amount of that block that is picked up to an id. Then I can loop through al the items from that id and add the items with the correct amount to a players inventory.

    But, I need it so you only have a certain amount you can store and its kind of like an inventory. So you can only store say 9 64 stacks for example. Then if you try to store anothe r item in there it would just go into your inventory or move onto the next inventory type thing. Also I need it so if you put 12 cobble in and then another 12 instead of taking up 2 of the nine spaces it would just condense into one slot of 24
  2. I am not quite sure if I got what you are trying to do, it is a little late so that might be why. But could you like tell me what your issue is?
  3. Im creating a backpack plugin. I have everything setup, giving players backpacks with unique ids etc.

    Im just wondering how to, when you mine a block, if all your backpacks in your inventory arent full, it fills them up before putting blocks in your inventory. But how do I save the type and amount of blocks that have gone into that backpack. You cant manually add or remove blocks from the backpack. The only way to get items out is to right click then all contents get out in you inventory. But Im just asking how to store these items and amount so I can do things with them.

    I know that @Inkzzz has worked on something similar for fadecloud.
  4. WAS


    It sounds like you are reinventing the wheel. You just need to create a inventory for each backpack. That way you have "inventories" bound to the backpacks in a players inventory, and it's easy to detect if that inventory is full or not, and add/remove items.
  5. Thank you, ill try it and see what i can come up with.
    • Like Like x 1
  6. You're looking for a Table data structure, see Guava Table class

    Table<UUID, Material, Integer> table;

    table.get(player.getUniqueId(), Material.SNOW);

    you can then get a collection of every material/number in the players row.

    rest of that logic is up to you.
    • Like Like x 1
  7. WAS


    You can also create a simulated table with maps, and hold any type of data. You'll need to suppress type casting warning in some instances.

    Code (Java):
    // A table object
    Map<UUID, Map<String, Object>> table = new HashMap<UUID, Map<String, Object>();

    // Table info
    Map<String, Object> tableInfo = new HashMap<String, Object>();

    // Add whatever info you want
    tableInfo.put("swordMaterial", Material.SNOW);
    tableInfo.put("swordAmount", item.getAmount());
    tableInfo.put("swordMeta", item.getItemMeta());
    tableInfo.put("swordDurability", item.getDurability());
    tableInfo.put("swordData", item.getData());
    tableInfo.put("swordMaxStack", item.getMaxStackSize());
    tableInfo.put("swordEnchantments", item.getEnchantments());

    // Add to "table"
    table.put(player.getUniqueId(), tableInfo);

    // Get table
    Map<String, Object> recTable = table.get(player.getUniqueId());

    // Get a object
    Material material = (Material) recTable.get("swordMaterial");
  8. never used tables before, if the inventory method doesnt work for me... ill look into tables
  9. What you just did is EXACTLY what a table does for you, plus many more useful features.
  10. WAS


    This wasn't an attack. It's another method, without using gc code. Also google Table class is a paired-key relationship with a value. Basic maps can be setup like this, but can be more primitive or more advanced depending on your usage. It's up to the end user. Simply an alternate method. When if you aren't using a bunch of other features, that's when it moves into the area of of bloating. Where Table is single column, you could have tons with a map.
    #10 WAS, Apr 29, 2017
    Last edited: Apr 29, 2017
  11. PQE


    Why don't you just make a new Object which has both a material and an Integer (count)?

    Because... Java does try real hard to support OOP and stuff..

    I'm pretty sure there's a much more elegant way to do all this, can you please explain exactly what you're trying to do?
    #11 PQE, Apr 29, 2017
    Last edited: Apr 29, 2017
    • Funny Funny x 1
  12. WAS


    Wouldn't you still be technically storing and looping anyway? Unless you are creating tons of instances for redundant reasons. In this case the slight slowdown for a larger map is a better trade off. But i still think just make an actual inventory is the real answer. Sorta what its there for.
    • Funny Funny x 1