Skip to content

Commit

Permalink
feat(category): add disabled key to prevent loading a category
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Apr 16, 2024
1 parent a1450ed commit d8015cb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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<String> itemNameConverter) {
private void loadAndRegisterCategory(@NotNull String key, @NotNull MapNode source, @NotNull UnaryOperator<String> itemNameConverter) {
if (source.getBoolean(DISABLED_CATEGORY)) {
return;
}

Material iconMaterial;

try {
Expand Down Expand Up @@ -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<BoxItem> toBoxItems(@NotNull List<String> itemNames) {
Expand Down

0 comments on commit d8015cb

Please sign in to comment.