From e5cd4817a1c85e862d7319ee27ca20b7a16c397f Mon Sep 17 00:00:00 2001 From: Kamesuta Date: Mon, 1 May 2023 18:51:35 +0900 Subject: [PATCH] Save used chest on main thread --- .../enderchest/EnderChestManager.java | 7 ++----- .../enderchest/context/PlayerContext.java | 19 +++++++++++++++++-- .../enderchest/context/SaveTask.java | 12 +++++++++++- .../enderchest/EnderChestManagerTest.java | 2 +- .../enderchest/context/PlayerContextTest.java | 2 +- .../enderchest/context/SaveTaskTest.java | 3 ++- 6 files changed, 34 insertions(+), 11 deletions(-) diff --git a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/EnderChestManager.java b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/EnderChestManager.java index 0f6b5226..eef83dc7 100644 --- a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/EnderChestManager.java +++ b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/EnderChestManager.java @@ -11,10 +11,7 @@ import fr.utarwyn.endercontainers.inventory.InventoryManager; import org.bukkit.entity.Player; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; import java.util.function.Consumer; @@ -64,7 +61,7 @@ protected synchronized void unload() { // Save and unload all data this.loadingContexts.clear(); - this.contextMap.values().forEach(PlayerContext::save); + this.contextMap.values().forEach(pc -> pc.save(Collections.emptySet())); this.contextMap.clear(); } diff --git a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContext.java b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContext.java index c14570bb..6d1df6aa 100644 --- a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContext.java +++ b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContext.java @@ -14,6 +14,7 @@ import org.bukkit.entity.Player; import java.util.*; +import java.util.stream.Collectors; /** * A context in which all enderchests of a player are loaded. @@ -188,11 +189,25 @@ public boolean openEnderchestInventory(Player viewer, int num) { return accessible; } + /** + * Update used inventories in the context. + * This method is called synchronously before the async SaveTask. + * + * @return set of used enderchests + */ + public Set preSave() { + Set usedChests = this.chests.stream().filter(EnderChest::isContainerUsed).collect(Collectors.toSet()); + usedChests.forEach(EnderChest::updateContainer); + return usedChests; + } + /** * Save all datas stored in the context. + * + * @param usedChests set of used enderchests (previously returned by preSave) */ - public void save() { - this.chests.forEach(EnderChest::updateContainer); + public void save(Set usedChests) { + this.chests.stream().filter(chest -> !usedChests.contains(chest)).forEach(EnderChest::updateContainer); this.data.saveContext(this.chests); } diff --git a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/SaveTask.java b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/SaveTask.java index 25060d59..73da7759 100644 --- a/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/SaveTask.java +++ b/plugin/src/main/java/fr/utarwyn/endercontainers/enderchest/context/SaveTask.java @@ -1,5 +1,9 @@ package fr.utarwyn.endercontainers.enderchest.context; +import fr.utarwyn.endercontainers.enderchest.EnderChest; + +import java.util.Set; + /** * Represents the task which saves in a persistant storage all data * of the context of a specific player. @@ -14,6 +18,11 @@ public class SaveTask implements Runnable { */ private final PlayerContext context; + /** + * Set of used enderchests + */ + private final Set usedChests; + /** * Construct a new saving task. * @@ -21,6 +30,7 @@ public class SaveTask implements Runnable { */ public SaveTask(PlayerContext context) { this.context = context; + this.usedChests = context.preSave(); } /** @@ -28,7 +38,7 @@ public SaveTask(PlayerContext context) { */ @Override public void run() { - this.context.save(); + this.context.save(this.usedChests); } } diff --git a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/EnderChestManagerTest.java b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/EnderChestManagerTest.java index d228e991..d13a575e 100644 --- a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/EnderChestManagerTest.java +++ b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/EnderChestManagerTest.java @@ -70,7 +70,7 @@ public void unload() throws TestInitializationException { this.manager.unload(); - verify(context).save(); + verify(context).save(any()); verify(inventoryManager).closeAll(); assertThat(this.manager.contextMap).isEmpty(); } diff --git a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContextTest.java b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContextTest.java index 99235772..5199bd57 100644 --- a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContextTest.java +++ b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/PlayerContextTest.java @@ -163,7 +163,7 @@ public void openEnderchestInventory() { @Test public void save() { - this.context.save(); + this.context.save(Collections.emptySet()); verify(this.playerData).saveContext(any()); } diff --git a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/SaveTaskTest.java b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/SaveTaskTest.java index 38c5c5c7..c7ae6aa5 100644 --- a/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/SaveTaskTest.java +++ b/plugin/src/test/java/fr/utarwyn/endercontainers/enderchest/context/SaveTaskTest.java @@ -6,6 +6,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; +import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.verify; @ExtendWith(MockitoExtension.class) @@ -24,7 +25,7 @@ public void setUp() { @Test public void run() { this.task.run(); - verify(this.context).save(); + verify(this.context).save(any()); } }