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).
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) {
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);