Skip to content

Commit

Permalink
Fix initial usage so that inventory is not wiped.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jun 2, 2021
1 parent 74beba6 commit 880c8d8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 19 deletions.
31 changes: 25 additions & 6 deletions src/main/java/com/wasteofplastic/invswitcher/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -283,32 +302,32 @@ private void getStats(InventoryStorage store, Player player, String worldName) {
for (Entry<Material, Integer> 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<Material, Integer> 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<EntityType, Integer> 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;
default:
break;

}
});
});

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ public List<ItemStack> 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);
Expand All @@ -182,13 +191,13 @@ public GameMode getGameMode(String worldName) {
public void setAdvancement(String worldName, String key, List<String> 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);
}

/**
Expand All @@ -208,14 +217,14 @@ public List<ItemStack> getEnderChest(String overworldName) {
}

/**
*
* @param worldname the world name
* @param inventory the inventory to set
*/
public void setEnderChest(String worldname, List<ItemStack> inventory) {
this.enderChest.put(worldname, inventory);
*
* @param worldname the world name
* @param inventory the inventory to set
*/
public void setEnderChest(String worldname, List<ItemStack> inventory) {
this.enderChest.put(worldname, inventory);

}
}

/**
* @return the enderChest
Expand Down Expand Up @@ -266,7 +275,7 @@ public void setUntypedStats(String worldName, Map<Statistic, Integer> untypedSta
public Map<Statistic, Map<Material, Integer>> getBlockStats(String worldName) {
return blockStats.computeIfAbsent(worldName, k -> new EnumMap<>(Statistic.class));
}

/**
* @param worldName World name
* @param blockStats the blockStats to set
Expand Down Expand Up @@ -306,6 +315,6 @@ public Map<Statistic, Map<EntityType, Integer>> getEntityStats(String worldName)
public void setEntityStats(String worldName, Map<Statistic, Map<EntityType, Integer>> entityStats) {
this.entityStats.put(worldName, entityStats);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Expand Down

0 comments on commit 880c8d8

Please sign in to comment.