Skip to content

Commit 880c8d8

Browse files
committed
Fix initial usage so that inventory is not wiped.
#20
1 parent 74beba6 commit 880c8d8

3 files changed

Lines changed: 47 additions & 19 deletions

File tree

src/main/java/com/wasteofplastic/invswitcher/Store.java

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ public Store(InvSwitcher addon) {
6363
cache = new HashMap<>();
6464
}
6565

66+
/**
67+
* Check if there is a world storage for the player for this world or not
68+
* @param player - player
69+
* @param world - world
70+
* @return true if there is a world stored for this player, otherwise false
71+
*/
72+
public boolean isWorldStored(Player player, World world) {
73+
// Get the store
74+
InventoryStorage store = getInv(player);
75+
String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", "");
76+
return store.isInventory(overworldName);
77+
}
78+
6679
/**
6780
* Gets items for world. Changes the inventory of player immediately.
6881
* @param player - player
@@ -74,6 +87,7 @@ public void getInventory(Player player, World world) {
7487

7588
// Do not differentiate between world environments. Only the location is different
7689
String worldName = world.getName();
90+
7791
String overworldName = (world.getName().replace("_the_end", "")).replace("_nether", "");
7892

7993
// Inventory
@@ -139,6 +153,11 @@ public void removeFromCache(Player player) {
139153
cache.remove(player.getUniqueId());
140154
}
141155

156+
/**
157+
* Get the inventory storage object for player from the database or make a new one
158+
* @param player - player
159+
* @return inventory storage object
160+
*/
142161
private InventoryStorage getInv(Player player) {
143162
if (cache.containsKey(player.getUniqueId())) {
144163
return cache.get(player.getUniqueId());
@@ -283,32 +302,32 @@ private void getStats(InventoryStorage store, Player player, String worldName) {
283302
for (Entry<Material, Integer> en : store.getBlockStats(worldName).get(s).entrySet()) {
284303
player.setStatistic(s, en.getKey(), en.getValue());
285304
}
286-
}
305+
}
287306
break;
288307
case ITEM:
289308
if (store.getItemStats(worldName).containsKey(s)) {
290309
for (Entry<Material, Integer> en : store.getItemStats(worldName).get(s).entrySet()) {
291310
player.setStatistic(s, en.getKey(), en.getValue());
292311
}
293-
}
312+
}
294313
break;
295314
case ENTITY:
296315
if (store.getEntityStats(worldName).containsKey(s)) {
297316
for (Entry<EntityType, Integer> en : store.getEntityStats(worldName).get(s).entrySet()) {
298317
player.setStatistic(s, en.getKey(), en.getValue());
299318
}
300-
}
319+
}
301320
break;
302-
case UNTYPED:
303-
if (store.getUntypedStats(worldName).containsKey(s)) {
321+
case UNTYPED:
322+
if (store.getUntypedStats(worldName).containsKey(s)) {
304323
player.setStatistic(s, store.getUntypedStats(worldName).get(s));
305324
}
306325
break;
307326
default:
308327
break;
309328

310329
}
311-
});
330+
});
312331

313332
}
314333

src/main/java/com/wasteofplastic/invswitcher/dataObjects/InventoryStorage.java

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ public List<ItemStack> getInventory(String overworldName) {
165165
return inventory == null ? new ArrayList<>() : inventory.getOrDefault(overworldName, new ArrayList<>());
166166
}
167167

168+
/**
169+
* Check if an inventory for this world exists or not
170+
* @param overworldName - over world name
171+
* @return true if there is an inventory for this world, false if not.
172+
*/
173+
public boolean isInventory(String overworldName) {
174+
return inventory != null && inventory.containsKey(overworldName);
175+
}
176+
168177
public void getLocation(String worldName) {
169178
if (location != null) {
170179
location.get(worldName);
@@ -182,13 +191,13 @@ public GameMode getGameMode(String worldName) {
182191
public void setAdvancement(String worldName, String key, List<String> criteria) {
183192
this.advancements.computeIfAbsent(worldName, k -> new HashMap<>()).put(key, criteria);
184193
}
185-
194+
186195
/**
187196
* Clears advancements for world
188197
* @param worldName - world name
189198
*/
190199
public void clearAdvancement(String worldName) {
191-
this.advancements.remove(worldName);
200+
this.advancements.remove(worldName);
192201
}
193202

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

210219
/**
211-
*
212-
* @param worldname the world name
213-
* @param inventory the inventory to set
214-
*/
215-
public void setEnderChest(String worldname, List<ItemStack> inventory) {
216-
this.enderChest.put(worldname, inventory);
220+
*
221+
* @param worldname the world name
222+
* @param inventory the inventory to set
223+
*/
224+
public void setEnderChest(String worldname, List<ItemStack> inventory) {
225+
this.enderChest.put(worldname, inventory);
217226

218-
}
227+
}
219228

220229
/**
221230
* @return the enderChest
@@ -266,7 +275,7 @@ public void setUntypedStats(String worldName, Map<Statistic, Integer> untypedSta
266275
public Map<Statistic, Map<Material, Integer>> getBlockStats(String worldName) {
267276
return blockStats.computeIfAbsent(worldName, k -> new EnumMap<>(Statistic.class));
268277
}
269-
278+
270279
/**
271280
* @param worldName World name
272281
* @param blockStats the blockStats to set
@@ -306,6 +315,6 @@ public Map<Statistic, Map<EntityType, Integer>> getEntityStats(String worldName)
306315
public void setEntityStats(String worldName, Map<Statistic, Map<EntityType, Integer>> entityStats) {
307316
this.entityStats.put(worldName, entityStats);
308317
}
309-
310-
318+
319+
311320
}

src/main/java/com/wasteofplastic/invswitcher/listeners/PlayerListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void onWorldExit(final PlayerChangedWorldEvent event) {
5959
*/
6060
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled=true)
6161
public void onPlayerJoin(final PlayerJoinEvent event) {
62-
if (addon.getWorlds().contains(event.getPlayer().getWorld())) {
62+
if (addon.getWorlds().contains(event.getPlayer().getWorld()) && addon.getStore().isWorldStored(event.getPlayer(), event.getPlayer().getWorld())) {
6363
addon.getStore().getInventory(event.getPlayer(), event.getPlayer().getWorld());
6464
}
6565
}

0 commit comments

Comments
 (0)