diff --git a/src/main/java/com/wasteofplastic/invswitcher/Store.java b/src/main/java/com/wasteofplastic/invswitcher/Store.java index 230323d..76370a1 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/Store.java +++ b/src/main/java/com/wasteofplastic/invswitcher/Store.java @@ -63,6 +63,19 @@ public Store(InvSwitcher addon) { cache = new HashMap<>(); } + /** + * Check if there is a world storage for the player for this world or not + * @param player - player + * @param world - world + * @return true if there is a world stored for this player, otherwise false + */ + public boolean isWorldStored(Player player, World world) { + // Get the store + InventoryStorage store = getInv(player); + String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); + return store.isInventory(overworldName); + } + /** * Gets items for world. Changes the inventory of player immediately. * @param player - player @@ -74,6 +87,7 @@ public void getInventory(Player player, World world) { // Do not differentiate between world environments. Only the location is different String worldName = world.getName(); + String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", ""); // Inventory @@ -139,6 +153,11 @@ public void removeFromCache(Player player) { cache.remove(player.getUniqueId()); } + /** + * Get the inventory storage object for player from the database or make a new one + * @param player - player + * @return inventory storage object + */ private InventoryStorage getInv(Player player) { if (cache.containsKey(player.getUniqueId())) { return cache.get(player.getUniqueId()); @@ -283,24 +302,24 @@ private void getStats(InventoryStorage store, Player player, String worldName) { for (Entry en : store.getBlockStats(worldName).get(s).entrySet()) { player.setStatistic(s, en.getKey(), en.getValue()); } - } + } break; case ITEM: if (store.getItemStats(worldName).containsKey(s)) { for (Entry en : store.getItemStats(worldName).get(s).entrySet()) { player.setStatistic(s, en.getKey(), en.getValue()); } - } + } break; case ENTITY: if (store.getEntityStats(worldName).containsKey(s)) { for (Entry en : store.getEntityStats(worldName).get(s).entrySet()) { player.setStatistic(s, en.getKey(), en.getValue()); } - } + } break; - case UNTYPED: - if (store.getUntypedStats(worldName).containsKey(s)) { + case UNTYPED: + if (store.getUntypedStats(worldName).containsKey(s)) { player.setStatistic(s, store.getUntypedStats(worldName).get(s)); } break; @@ -308,7 +327,7 @@ private void getStats(InventoryStorage store, Player player, String worldName) { break; } - }); + }); } diff --git a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java index 263997d..ef0575c 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java +++ b/src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java @@ -165,6 +165,15 @@ public List getInventory(String overworldName) { return inventory == null ? new ArrayList<>() : inventory.getOrDefault(overworldName, new ArrayList<>()); } + /** + * Check if an inventory for this world exists or not + * @param overworldName - over world name + * @return true if there is an inventory for this world, false if not. + */ + public boolean isInventory(String overworldName) { + return inventory != null && inventory.containsKey(overworldName); + } + public void getLocation(String worldName) { if (location != null) { location.get(worldName); @@ -182,13 +191,13 @@ public GameMode getGameMode(String worldName) { public void setAdvancement(String worldName, String key, List criteria) { this.advancements.computeIfAbsent(worldName, k -> new HashMap<>()).put(key, criteria); } - + /** * Clears advancements for world * @param worldName - world name */ public void clearAdvancement(String worldName) { - this.advancements.remove(worldName); + this.advancements.remove(worldName); } /** @@ -208,14 +217,14 @@ public List getEnderChest(String overworldName) { } /** - * - * @param worldname the world name - * @param inventory the inventory to set - */ - public void setEnderChest(String worldname, List inventory) { - this.enderChest.put(worldname, inventory); + * + * @param worldname the world name + * @param inventory the inventory to set + */ + public void setEnderChest(String worldname, List inventory) { + this.enderChest.put(worldname, inventory); - } + } /** * @return the enderChest @@ -266,7 +275,7 @@ public void setUntypedStats(String worldName, Map untypedSta public Map> getBlockStats(String worldName) { return blockStats.computeIfAbsent(worldName, k -> new EnumMap<>(Statistic.class)); } - + /** * @param worldName World name * @param blockStats the blockStats to set @@ -306,6 +315,6 @@ public Map> getEntityStats(String worldName) public void setEntityStats(String worldName, Map> entityStats) { this.entityStats.put(worldName, entityStats); } - - + + } \ No newline at end of file diff --git a/src/main/java/com/wasteofplastic/invswitcher/listeners/PlayerListener.java b/src/main/java/com/wasteofplastic/invswitcher/listeners/PlayerListener.java index 2da43ee..ed9e2aa 100644 --- a/src/main/java/com/wasteofplastic/invswitcher/listeners/PlayerListener.java +++ b/src/main/java/com/wasteofplastic/invswitcher/listeners/PlayerListener.java @@ -59,7 +59,7 @@ public void onWorldExit(final PlayerChangedWorldEvent event) { */ @EventHandler(priority = EventPriority.HIGH, ignoreCancelled=true) public void onPlayerJoin(final PlayerJoinEvent event) { - if (addon.getWorlds().contains(event.getPlayer().getWorld())) { + if (addon.getWorlds().contains(event.getPlayer().getWorld()) && addon.getStore().isWorldStored(event.getPlayer(), event.getPlayer().getWorld())) { addon.getStore().getInventory(event.getPlayer(), event.getPlayer().getWorld()); } }