Skip to content

Commit

Permalink
Merge pull request #34 from BentoBoxWorld/develop
Browse files Browse the repository at this point in the history
Release 1.14.1
  • Loading branch information
tastybento authored Sep 10, 2024
2 parents 0a5ae2b + ff681e2 commit 78b8f9d
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
<!-- Do not change unless you want different name for local builds. -->
<build.number>-LOCAL</build.number>
<!-- This allows to change between versions. -->
<build.version>1.14.0</build.version>
<build.version>1.14.1</build.version>
<!-- Sonar Cloud -->
<sonar.projectKey>BentoBoxWorld_addon-invSwitcher</sonar.projectKey>
<sonar.organization>bentobox-world</sonar.organization>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.wasteofplastic.invswitcher;


import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.World;

import com.wasteofplastic.invswitcher.listeners.PlayerListener;
Expand All @@ -23,6 +26,9 @@
*/
public class InvSwitcher extends Addon {

protected static final List<Material> MAT = Arrays.stream(Material.values())
.filter(m -> !m.name().startsWith("LEGACY")).toList();

private Store store;

private Settings settings;
Expand Down Expand Up @@ -90,7 +96,7 @@ public void onEnable() {
public void onDisable() {
// save cache
if (store != null) {
getStore().saveOnlinePlayers();
getStore().saveOnShutdown();
}

}
Expand Down
95 changes: 52 additions & 43 deletions src/main/java/com/wasteofplastic/invswitcher/Store.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
*
*/
public class Store {
private static final Material[] MAT = Material.values();
private static final CharSequence THE_END = "_the_end";
private static final CharSequence NETHER = "_nether";
private final Database<InventoryStorage> database;
Expand Down Expand Up @@ -198,7 +197,7 @@ private InventoryStorage getInv(Player player) {
* @param world - the world that is associated with these items/elements
*/
public void storeInventory(Player player, World world) {
storeAndSave(player, world);
storeAndSave(player, world, false);
clearPlayer(player);
// Done!
}
Expand All @@ -207,8 +206,9 @@ public void storeInventory(Player player, World world) {
* Store and save the player to the database
* @param player - player
* @param world - world to save
* @param shutdown - true if this is a shutdown save
*/
public void storeAndSave(Player player, World world) {
public void storeAndSave(Player player, World world, boolean shutdown) {
// Get the player's store
InventoryStorage store = getInv(player);
// Do not differentiate between world environments
Expand Down Expand Up @@ -249,57 +249,66 @@ public void storeAndSave(Player player, World world) {
store.setEnderChest(overworldName, contents);
}
if (addon.getSettings().isStatistics()) {
saveStats(store, player, overworldName).thenAccept(database::saveObjectAsync);
saveStats(store, player, overworldName, shutdown).thenAccept(database::saveObjectAsync);
return;
}
database.saveObjectAsync(store);
}

private CompletableFuture<InventoryStorage> saveStats(InventoryStorage store, Player player, String worldName) {
private CompletableFuture<InventoryStorage> saveStats(InventoryStorage store, Player player, String worldName,
boolean shutdown) {
CompletableFuture<InventoryStorage> result = new CompletableFuture<>();
store.clearStats(worldName);

// Statistics
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(), () -> {
Arrays.stream(Statistic.values()).forEach(s -> {
Map<Material, Integer> map;
Map<EntityType, Integer> entMap;
switch (s.getType()) {
case BLOCK -> {
map = Arrays.stream(MAT).filter(Material::isBlock).filter(m -> !m.isLegacy())
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getBlockStats(worldName).put(s, map);
}
}
case ITEM -> {
map = Arrays.stream(MAT).filter(Material::isItem).filter(m -> !m.isLegacy())
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getItemStats(worldName).put(s, map);
}
if (shutdown) {
saveStatistics(result, store, player, worldName);
} else {
// Cannot schedule tasks on shutdown
Bukkit.getScheduler().runTaskAsynchronously(addon.getPlugin(),
() -> saveStatistics(result, store, player, worldName));
}
return result;

}

private void saveStatistics(CompletableFuture<InventoryStorage> result, InventoryStorage store, Player player,
String worldName) {
Arrays.stream(Statistic.values()).forEach(s -> {
Map<Material, Integer> map;
Map<EntityType, Integer> entMap;
switch (s.getType()) {
case BLOCK -> {
map = InvSwitcher.MAT.stream().filter(Material::isBlock).filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getBlockStats(worldName).put(s, map);
}
case ENTITY -> {
entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive)
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!entMap.isEmpty()) {
store.getEntityStats(worldName).put(s, entMap);
}
}
case ITEM -> {
map = InvSwitcher.MAT.stream().filter(Material::isItem).filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!map.isEmpty()) {
store.getItemStats(worldName).put(s, map);
}
case UNTYPED -> {
int sc = player.getStatistic(s);
if (sc > 0) {
store.getUntypedStats(worldName).put(s, sc);
}
}
case ENTITY -> {
entMap = Arrays.stream(EntityType.values()).filter(EntityType::isAlive)
.filter(m -> player.getStatistic(s, m) > 0)
.collect(Collectors.toMap(k -> k, v -> player.getStatistic(s, v)));
if (!entMap.isEmpty()) {
store.getEntityStats(worldName).put(s, entMap);
}
}
case UNTYPED -> {
int sc = player.getStatistic(s);
if (sc > 0) {
store.getUntypedStats(worldName).put(s, sc);
}
});
result.complete(store);
}
}
});
return result;

result.complete(store);
}

/**
Expand Down Expand Up @@ -461,7 +470,7 @@ private static void setTotalExperience(final Player player, final int exp)
/**
* Save all online players
*/
public void saveOnlinePlayers() {
Bukkit.getOnlinePlayers().forEach(p -> this.storeAndSave(p, p.getWorld()));
public void saveOnShutdown() {
Bukkit.getOnlinePlayers().forEach(p -> this.storeAndSave(p, p.getWorld(), true));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void onPlayerJoin(final PlayerJoinEvent event) {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled=true)
public void onPlayerQuit(final PlayerQuitEvent event) {
if (addon.getWorlds().contains(event.getPlayer().getWorld())) {
addon.getStore().storeAndSave(event.getPlayer(), event.getPlayer().getWorld());
addon.getStore().storeAndSave(event.getPlayer(), event.getPlayer().getWorld(), false);
}
addon.getStore().removeFromCache(event.getPlayer());
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/com/wasteofplastic/invswitcher/StoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -262,14 +262,14 @@ public void testStoreInventoryAll() {
}

/**
* Test method for {@link com.wasteofplastic.invswitcher.Store#saveOnlinePlayers()}.
* Test method for {@link com.wasteofplastic.invswitcher.Store#saveOnShutdown()}.
*/
@Test
public void testSaveOnlinePlayers() {
// Mock the static method
try (MockedStatic<Bukkit> mockedBukkit = mockStatic(Bukkit.class)) {
// Run the code under test
s.saveOnlinePlayers();
s.saveOnShutdown();

// Verify that the static method was called
mockedBukkit.verify(() -> Bukkit.getOnlinePlayers());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void testOnPlayerJoinWithStorage() {
public void testOnPlayerQuit() {
PlayerQuitEvent event = new PlayerQuitEvent(player, "");
pl.onPlayerQuit(event);
verify(store).storeAndSave(player, world);
verify(store).storeAndSave(player, world, false);
verify(store).removeFromCache(player);
}

Expand All @@ -153,7 +153,7 @@ public void testOnPlayerQuitNotCoveredWorld() {
when(player.getWorld()).thenReturn(notWorld);
PlayerQuitEvent event = new PlayerQuitEvent(player, "");
pl.onPlayerQuit(event);
verify(store, never()).storeAndSave(player, world);
verify(store, never()).storeAndSave(player, world, false);
verify(store).removeFromCache(player);
}

Expand Down

0 comments on commit 78b8f9d

Please sign in to comment.