Virtual Inventory

Discussion in 'Plugin Development' started by AppleMC, Jul 7, 2015.

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

    AppleMC

    Code:
        Inventory inventory;
        Player owner;
        String name;
       
        public VirtualInventory(Player p, String name) {
           
            this.inventory = Bukkit.createInventory(p, 27, name);
            this.owner = p;
            this.name = name;
           
        }
       
        public Inventory getInventory() {
           
            return this.inventory;
           
        }
       
        public Player getOwner() {
           
            return this.owner;
           
        }
       
        public String getName() {
           
            return this.name;
           
        }
       
        public VirtualInventory createInventory(Player p, String name) {
           
            VirtualInventory vi = new VirtualInventory(p, name);
           
            return vi;
           
        }
       
        HashMap<Player, VirtualInventory> inventories = new HashMap<Player, VirtualInventory>();
       
        public void saveInventory(Player p, VirtualInventory vi) {
           
            inventories.put(p, vi);
           
        }
    This is my current code that I have in my VirtualInventory class, but I'm wondering how to check if a player already has a virtual inventory? How would I go about checking if the player is in the hashmap? (I don't have much experience with hashmaps).
     
  2. Offline

    BagduFagdu

    @AppleMC
    Use player#getInventory() to compare, perhaps?
     
  3. Just check if the key is already in the map
    Code:
    Map<UUID, VirtualInventory> inventorys = new HashMap<>();
    
    public boolean exist(UUID uuid) {
       return invetorys.containsKey(uuid);
    }
    
    //But if you are going to do something like this :
    
    if(exist(uuid)) {
       VirtualInventory vi = inventorys.get(uuid); 
    
    //You can just do this
    
    VirtualIventory vi = iventorys.get(uuid);
    
    if(vi != null) {
    
     
  4. Offline

    Garnetty

    You can check if the hashmap contains the key using map.containsKey
     
  5. Just a quick tip:
    This will cause memory leaks, replace Player owner with String owner, to set it use p.getName(); and to get it use
    Bukkit.getPlayerExact(owner);
     
Thread Status:
Not open for further replies.

Share This Page