Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ private void reset(@NotNull UUID uuid) {
MapNode data = MapNode.create();

if (setting.isEnabled()) data.set("enable", true);
if (setting.isAllMode()) data.set("all-mode", true);
data.set("all-mode", setting.isAllMode());
if (setting.isDirect()) data.set("direct", true);

IntSet items = setting.getPerItemModeSetting().getEnabledItems();
Expand All @@ -147,6 +147,14 @@ private void reset(@NotNull UUID uuid) {
private static @NotNull AutoStoreSetting deserialize(@NotNull UUID uuid, @NotNull MapNode data) {
AutoStoreSetting setting = new AutoStoreSetting(uuid);

if (data.isEmpty()) {
// loadData returns the same empty node for missing data and for old
// disabled settings with no selected items (storage deletes empty data).
// Both use new defaults;
// the old per-item mode cannot be recovered from this representation.
return setting;
}

setting.setEnabled(data.getBoolean("enable"));
setting.setAllMode(data.getBoolean("all-mode"));
setting.setDirect(data.getBoolean("direct"));
Expand Down Expand Up @@ -192,8 +200,8 @@ private static void trimMapNode(MapNode target) {
target.set("enable", true);
}

if (source.getBoolean("all-mode")) {
target.set("all-mode", true);
if (source.get("all-mode") != null) {
target.set("all-mode", source.getBoolean("all-mode"));
}

if (source.getBoolean("direct")) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package net.okocraft.box.feature.autostore;

import dev.siroshun.configapi.core.node.MapNode;
import net.kyori.adventure.key.Key;
import net.okocraft.box.api.BoxAPI;
import net.okocraft.box.api.event.customdata.CustomDataExportEvent;
import net.okocraft.box.api.model.customdata.CustomDataManager;
import net.okocraft.box.feature.autostore.setting.AutoStoreSetting;
import net.okocraft.box.test.shared.model.item.DummyItem;
import org.bukkit.Bukkit;
import org.bukkit.UnsafeValues;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;

@SuppressWarnings("deprecation")
class AutoStoreSettingContainerTest {

private static final DummyItem ITEM = new DummyItem(1, "STONE");
private final Map<Key, MapNode> savedData = new HashMap<>();
private MockedStatic<BoxAPI> boxApi;
private MockedStatic<Bukkit> bukkit;

@BeforeEach
void setUp() throws Exception {
BoxAPI api = Mockito.mock(BoxAPI.class);
CustomDataManager dataManager = Mockito.mock(CustomDataManager.class);
Mockito.when(api.getCustomDataManager()).thenReturn(dataManager);
Mockito.when(dataManager.loadData(any())).thenAnswer(invocation ->
this.savedData.getOrDefault(invocation.getArgument(0), MapNode.create()).copy());
Mockito.doAnswer(invocation -> {
this.savedData.put(invocation.getArgument(0), ((MapNode) invocation.getArgument(1)).copy());
return null;
}).when(dataManager).saveData(any(), any());
this.boxApi = Mockito.mockStatic(BoxAPI.class);
this.boxApi.when(BoxAPI::api).thenReturn(api);
UnsafeValues unsafe = Mockito.mock(UnsafeValues.class);
Mockito.when(unsafe.getDataVersion()).thenReturn(5023);
this.bukkit = Mockito.mockStatic(Bukkit.class);
this.bukkit.when(Bukkit::getUnsafe).thenReturn(unsafe);
}

@AfterEach
void tearDown() {
this.bukkit.close();
this.boxApi.close();
}

@Test
void newPlayerUsesAllMode() throws Exception {
AutoStoreSetting setting = new AutoStoreSettingContainer().getOrLoad(UUID.randomUUID());
assertFalse(setting.isEnabled());
assertTrue(setting.isAllMode());
setting.setEnabled(true);
assertTrue(setting.shouldAutoStore(ITEM));
}

@Test
void legacyEmptyDataUsesNewDefaultsAndStaysDisabledUntilEnabled() throws Exception {
UUID uuid = UUID.randomUUID();
Key key = Key.key("autostore", uuid.toString());
// The old serializer emitted an empty node for disabled per-item mode
// with no selected items. Its load result is identical to missing data.
this.savedData.put(key, MapNode.create());
AutoStoreSettingContainer container = new AutoStoreSettingContainer();
AutoStoreSetting setting = container.getOrLoad(uuid);
assertFalse(setting.isEnabled());
assertTrue(setting.isAllMode());
container.save(setting);
assertFalse(this.savedData.get(key).getBoolean("enable"));
assertTrue(this.savedData.get(key).getBoolean("all-mode"));
setting.setEnabled(true);
assertTrue(setting.shouldAutoStore(ITEM));
container.save(setting);
assertTrue(this.savedData.get(key).getBoolean("all-mode"));
}

@Test
void disabledEmptyPerItemModeSurvivesSaveAndExport() throws Exception {
UUID uuid = UUID.randomUUID();
AutoStoreSettingContainer container = new AutoStoreSettingContainer();
AutoStoreSetting setting = new AutoStoreSetting(uuid);
setting.setAllMode(false);
container.save(setting);

AutoStoreSetting reloaded = new AutoStoreSettingContainer().getOrLoad(uuid);
assertFalse(reloaded.isEnabled());
assertFalse(reloaded.isAllMode());
assertFalse(reloaded.shouldAutoStore(ITEM));

Key key = Key.key("autostore", uuid.toString());
CustomDataExportEvent event = new CustomDataExportEvent(key, this.savedData.get(key));
AutoStoreSettingContainer.onExportAutoStoreSetting(event);
assertFalse(event.getResultNode().isEmpty());
this.savedData.put(key, event.getResultNode());
assertFalse(new AutoStoreSettingContainer().getOrLoad(uuid).isAllMode());
}

@Test
void enabledPerItemSelectionSurvivesReload() throws Exception {
UUID uuid = UUID.randomUUID();
AutoStoreSetting setting = new AutoStoreSetting(uuid);
setting.setEnabled(true);
setting.setAllMode(false);
setting.getPerItemModeSetting().setEnabled(ITEM, true);
new AutoStoreSettingContainer().save(setting);
AutoStoreSetting reloaded = new AutoStoreSettingContainer().getOrLoad(uuid);
assertTrue(reloaded.isEnabled());
assertFalse(reloaded.isAllMode());
assertTrue(reloaded.shouldAutoStore(ITEM));
assertFalse(reloaded.shouldAutoStore(new DummyItem(2, "DIRT")));
}
}
Loading