From d8015cbffb1c760c73ef32e53e663ce50a311ebb Mon Sep 17 00:00:00 2001 From: Siroshun09 Date: Tue, 16 Apr 2024 21:17:22 +0900 Subject: [PATCH] feat(category): add `disabled` key to prevent loading a category --- .../internal/category/defaults/DefaultCategory.java | 2 ++ .../feature/category/internal/file/CategoryFile.java | 11 ++++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/features/category/src/main/java/net/okocraft/box/feature/category/internal/category/defaults/DefaultCategory.java b/features/category/src/main/java/net/okocraft/box/feature/category/internal/category/defaults/DefaultCategory.java index e0ebf6abdb..4cb3678e0a 100644 --- a/features/category/src/main/java/net/okocraft/box/feature/category/internal/category/defaults/DefaultCategory.java +++ b/features/category/src/main/java/net/okocraft/box/feature/category/internal/category/defaults/DefaultCategory.java @@ -1,5 +1,6 @@ package net.okocraft.box.feature.category.internal.category.defaults; +import com.github.siroshun09.configapi.core.node.BooleanValue; import com.github.siroshun09.configapi.core.node.MapNode; import net.okocraft.box.api.model.manager.ItemManager; import net.okocraft.box.feature.category.api.category.Category; @@ -33,6 +34,7 @@ public record DefaultCategory(@NotNull String key, } public void storeToMapNode(@NotNull MapNode mapNode) { + mapNode.set(CategoryFile.DISABLED_CATEGORY, BooleanValue.FALSE); mapNode.set(CategoryFile.ICON_KEY, this.icon.name()); var displayName = mapNode.createMap(CategoryFile.DISPLAY_NAME_KEY); diff --git a/features/category/src/main/java/net/okocraft/box/feature/category/internal/file/CategoryFile.java b/features/category/src/main/java/net/okocraft/box/feature/category/internal/file/CategoryFile.java index f4d9bc36fe..11cee6f247 100644 --- a/features/category/src/main/java/net/okocraft/box/feature/category/internal/file/CategoryFile.java +++ b/features/category/src/main/java/net/okocraft/box/feature/category/internal/file/CategoryFile.java @@ -39,6 +39,7 @@ public final class CategoryFile implements AutoCloseable { public static final String ICON_KEY = "icon"; public static final String DISPLAY_NAME_KEY = "display-name"; public static final String LOCALE_DEFAULT = "default"; + public static final String DISABLED_CATEGORY = "disabled"; private final Path filepath; private final CategoryRegistry registry; @@ -103,7 +104,7 @@ public CategoryFile readCategoriesIfExists() { for (var entry : this.loadedSource.value().entrySet()) { var key = String.valueOf(entry.getKey()); if (!key.startsWith("$") && entry.getValue() instanceof MapNode section) { - this.registry.register(key, loadCategory(key, section, BoxAPI.api().getItemManager().getItemNameConverter(this.version))); + this.loadAndRegisterCategory(key, section, BoxAPI.api().getItemManager().getItemNameConverter(this.version)); } } @@ -165,7 +166,11 @@ public void addNewDefaultItemsIfNeeded() throws IOException { YamlFormat.COMMENT_PROCESSING.save(mapNode, this.filepath); } - private static LoadedCategory loadCategory(@NotNull String key, @NotNull MapNode source, @NotNull UnaryOperator itemNameConverter) { + private void loadAndRegisterCategory(@NotNull String key, @NotNull MapNode source, @NotNull UnaryOperator itemNameConverter) { + if (source.getBoolean(DISABLED_CATEGORY)) { + return; + } + Material iconMaterial; try { @@ -204,7 +209,7 @@ private static LoadedCategory loadCategory(@NotNull String key, @NotNull MapNode var category = new LoadedCategory(iconMaterial, displayNameMap); category.addItems(items); - return category; + this.registry.register(key, category); } private @NotNull Collection toBoxItems(@NotNull List itemNames) {