Skip to content

Commit

Permalink
Save used chest on main thread
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamesuta authored and utarwyn committed Feb 4, 2024
1 parent 58e9262 commit e5cd481
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<EnderChest> preSave() {
Set<EnderChest> 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<EnderChest> usedChests) {
this.chests.stream().filter(chest -> !usedChests.contains(chest)).forEach(EnderChest::updateContainer);
this.data.saveContext(this.chests);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -14,21 +18,27 @@ public class SaveTask implements Runnable {
*/
private final PlayerContext context;

/**
* Set of used enderchests
*/
private final Set<EnderChest> usedChests;

/**
* Construct a new saving task.
*
* @param context the player context to save
*/
public SaveTask(PlayerContext context) {
this.context = context;
this.usedChests = context.preSave();
}

/**
* {@inheritDoc}
*/
@Override
public void run() {
this.context.save();
this.context.save(this.usedChests);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void openEnderchestInventory() {

@Test
public void save() {
this.context.save();
this.context.save(Collections.emptySet());
verify(this.playerData).saveContext(any());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -24,7 +25,7 @@ public void setUp() {
@Test
public void run() {
this.task.run();
verify(this.context).save();
verify(this.context).save(any());
}

}

0 comments on commit e5cd481

Please sign in to comment.