java - Store player's inventory -


how can store player's inventory, clear it, , return stored version?

you use hashmap store contents of of player's inventories , armor

map<uuid, itemstack[]> items = new hashmap<uuid, itemstack[]>(); map<uuid, itemstack[]> armor = new hashmap<uuid, itemstack[]>(); 

you store player's inventory contents in hashmap using

uuid uuid = player.getuniqueid();  itemstack[] contents = player.getinventory().getcontents(); itemstack[] armorcontents = player.getinventory().getarmorcontents();  items.put(uuid, contents); armor.put(uuid, armorcontents); 

and clear player's inventory , armor doing

player.getinventory().clear();  player.getinventory().sethelmet(null); player.getinventory().setchestplate(null); player.getinventory().setleggings(null); player.getinventory().setboots(null); 

to restore inventory , amor, use

uuid uuid = player.getuniqueid();  itemstack[] contents = items.get(uuid); itemstack[] armorcontents = armor.get(uuid);  player.getinventory().setcontents(contents); player.getinventory().setarmorcontents(armorcontents); 

so, have code looks this

map<uuid, itemstack[]> items = new hashmap<uuid, itemstack[]>(); map<uuid, itemstack[]> armor = new hashmap<uuid, itemstack[]>();  public void storeandclearinventory(player player){     uuid uuid = player.getuniqueid();      itemstack[] contents = player.getinventory().getcontents();     itemstack[] armorcontents = player.getinventory().getarmorcontents();      items.put(uuid, contents);     armor.put(uuid, armorcontents);      player.getinventory().clear();      player.getinventory().sethelmet(null);     player.getinventory().setchestplate(null);     player.getinventory().setleggings(null);     player.getinventory().setboots(null); }  public void restoreinventory(player player){     uuid uuid = player.getuniqueid();      itemstack[] contents = items.get(uuid);     itemstack[] armorcontents = armor.get(uuid);      if(contents != null){         player.getinventory().setcontents(contents);     }     else{//if player has no inventory contents, clear inventory         player.getinventory().clear();     }      if(armorcontents != null){         player.getinventory().setarmorcontents(armorcontents);     }     else{//if player has no armor, set armor null         player.getinventory().sethelmet(null);         player.getinventory().setchestplate(null);         player.getinventory().setleggings(null);         player.getinventory().setboots(null);     } } 

then call storeandclearinventory(player) when want store player's inventory, clear it, , call restoreinventory(player) when want restore player's inventory it's original state.


Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

session - Logging Out Using PHP -