Skip to content

Commit

Permalink
improve(category): auto registering/unregistering the custom item cat…
Browse files Browse the repository at this point in the history
…egory
  • Loading branch information
Siroshun09 committed Apr 16, 2024
1 parent 53b65b4 commit 16804a3
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class CategoryFeature extends AbstractBoxFeature implements Reloadable {
private static final Key ITEM_INFO_COLLECT_EVENT_LISTENER_KEY = Key.key("box", "feature/category/item_info_collect_event");

private final Path filepath;
private final CategoryRegistryImpl categoryRegistry = new CategoryRegistryImpl();
private final CategoryRegistry categoryRegistry = new CategoryRegistryImpl();
private final CustomItemListener customItemListener;
private final ItemInfoEventListener itemInfoEventListener;
private final MiniMessageBase reloaded;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import net.kyori.adventure.text.Component;
import net.okocraft.box.api.BoxAPI;
import net.okocraft.box.api.message.DefaultMessageCollector;
import net.okocraft.box.api.model.item.BoxItem;
import net.okocraft.box.feature.category.api.registry.CategoryRegistry;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Collection;

public final class CustomItemCategory extends AbstractCategory {

public static final String CONFIG_KEY = "$custom-items";
Expand All @@ -18,8 +22,13 @@ public static void addDefaultCategoryName(@NotNull DefaultMessageCollector colle
collector.add(DISPLAY_NAME_KEY, "Custom Items");
}

private final CategoryRegistry registry;
private final MiniMessageBase displayName = MiniMessageBase.messageKey(DISPLAY_NAME_KEY);

public CustomItemCategory(CategoryRegistry registry) {
this.registry = registry;
}

@Override
public @NotNull Material getIconMaterial() {
return Material.NETHER_STAR;
Expand All @@ -30,4 +39,27 @@ public static void addDefaultCategoryName(@NotNull DefaultMessageCollector colle
return this.displayName.create(BoxAPI.api().getMessageProvider().findSource(viewer));
}

@Override
public void addItem(@NotNull BoxItem item) {
super.addItem(item);
if (this.getItems().size() == 1) {
this.registry.register(REGISTRY_KEY, this);
}
}

@Override
public void addItems(@NotNull Collection<BoxItem> items) {
super.addItems(items);
if (!items.isEmpty() && this.getItems().size() == items.size()) {
this.registry.register(REGISTRY_KEY, this);
}
}

@Override
public void removeItem(@NotNull BoxItem item) {
super.removeItem(item);
if (this.getItems().isEmpty()) {
this.registry.unregister(REGISTRY_KEY);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,9 @@ public CategoryFile readCategoriesIfExists() {
}

public CategoryFile readCustomItemsIfExists() {
var category = this.registry.getCustomItemCategory();

if (this.loadedSource != null) {
category.addItems(this.toBoxItems(this.loadedSource.getList(CustomItemCategory.CONFIG_KEY).asList(String.class)));
}

if (!category.getItems().isEmpty()) {
this.registry.register(CustomItemCategory.REGISTRY_KEY, category);
this.registry.getCustomItemCategory().addItems(this.toBoxItems(this.loadedSource.getList(CustomItemCategory.CONFIG_KEY).asList(String.class)));
}

return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import net.okocraft.box.api.event.item.CustomItemRenameEvent;
import net.okocraft.box.api.model.item.BoxItem;
import net.okocraft.box.api.util.BoxLogger;
import net.okocraft.box.feature.category.api.registry.CategoryRegistry;
import net.okocraft.box.feature.category.internal.category.CustomItemCategory;
import net.okocraft.box.feature.category.internal.file.CategoryFile;
import net.okocraft.box.feature.category.internal.registry.CategoryRegistryImpl;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand All @@ -21,9 +21,9 @@
public class CustomItemListener {

private final Path filepath;
private final CategoryRegistryImpl registry;
private final CategoryRegistry registry;

public CustomItemListener(@NotNull Path filepath, @NotNull CategoryRegistryImpl registry) {
public CustomItemListener(@NotNull Path filepath, @NotNull CategoryRegistry registry) {
this.filepath = filepath;
this.registry = registry;
}
Expand All @@ -41,10 +41,6 @@ public void unregister(@NotNull Key listenerKey) {
private void processEvent(@NotNull CustomItemRegisterEvent event) {
this.registry.getCustomItemCategory().addItem(event.getItem());

if (this.registry.getByName(CustomItemCategory.REGISTRY_KEY).isEmpty()) {
this.registry.register(CustomItemCategory.REGISTRY_KEY, this.registry.getCustomItemCategory());
}

try {
this.addItemToCustomItems(event.getItem());
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
public class CategoryRegistryImpl implements CategoryRegistry {

private final Map<String, Category> registry = new LinkedHashMap<>();
private final CustomItemCategory customItemCategory = new CustomItemCategory();
private final CustomItemCategory customItemCategory = new CustomItemCategory(this);
private final Object lock = new Object();

private List<Category> snapshot = Collections.emptyList();
Expand Down

0 comments on commit 16804a3

Please sign in to comment.