Need Help Figuring Out How To Do This

Discussion in 'Plugin Development' started by DeadlyScone, Dec 3, 2011.

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

    DeadlyScone

    First off i am trying to get the items from a players death, which i have done easy enough, but i also want to store the items in a single "set" that can be read from a hashmap by a key value of the players name.
    I have tried to do this but i can't seem to find the right method for the hashmap <String, ?????>

    So my question is that what should i do to achieve this?, or should i not even use a HashMap?
    Please let me know, i am highly confused. and BTW the data needs to be stored alongside with the Players name.
    Thanks :)
     
  2. Offline

    nisovin

    You could use a map of arrays:

    HashMap<String, ItemStack[]>

    Or perhaps a map of sets:

    HashMap<String, HashSet<ItemStack>>
     
  3. Offline

    coldandtired

    You can do this:
    Code:
    Map<String, PlayerInventory> items = new HashMap<String,  PlayerInventory >();
     items.put(p.getName(), p.getInventory());
    
    use
    Code:
    event.getDrops().clear();
    in the EntityDeathEvent if you don't want the player's stuff to drop on the ground. (Obviously check that's it a player first).

    Restoring the items is a bit more difficult as there is no setInventory() (for some reason :( ) so you'll have to build the player's backpack manually if it's important all the items go back in the same place.
     
  4. Offline

    DeadlyScone

    th
    thanks, ill try this and let you know :)

    How do you dissect the PlayerInventory?
    like say separate each item into a usable ItemStack so its ready for addItem

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 21, 2016
  5. Offline

    wwsean08

    actually what would probably be better for rebuilding the inventory is using the setContents. if you saved the whole inventory then you can simply do (assuming you already have a handle on the player):
    Code:
    player.getInventory().setContents(items.get(player.getName()).getContents());
    I believe that will work in restoring their inventory back to normal (the only thing it may not do is the armor, i forget).
     
  6. Offline

    DeadlyScone

    well, when i tried this, i got a nullpointerexception, but if i do remember when the player dies, you can still retrieve the inventory and get the contents, the only problem is that nullpointerexception :/
     
Thread Status:
Not open for further replies.

Share This Page