diff --git a/CHANGELOG.md b/CHANGELOG.md index d5a00ac72..8b35d0f68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,7 @@ * Added "8 Coal Blocks -> 9 Carbon" recipe to Carbon Press * Added Tier 2 Auto-Enchanter * Added Tier 2 Auto-Disenchanter +* (API) Added Category#setTier() to modify a category's position in the guide #### Changes * Renamed "Solar Panel" to "Photovoltaic Cell" to avoid confusions with solar generators diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java index 2a2ac688e..cc14165fd 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java @@ -43,7 +43,7 @@ public class Category implements Keyed { protected final List items = new ArrayList<>(); protected final NamespacedKey key; protected final ItemStack item; - protected final int tier; + protected int tier; /** * Constructs a new {@link Category} with the given {@link NamespacedKey} as an identifier @@ -104,10 +104,49 @@ public class Category implements Keyed { */ public void register(@Nonnull SlimefunAddon addon) { Validate.notNull(addon, "The Addon cannot be null"); + + if (isRegistered()) { + throw new UnsupportedOperationException("This Category has already been registered!"); + } + this.addon = addon; SlimefunPlugin.getRegistry().getCategories().add(this); - Collections.sort(SlimefunPlugin.getRegistry().getCategories(), Comparator.comparingInt(Category::getTier)); + sortCategoriesByTier(); + } + + /** + * Returns the tier of this {@link Category}. + * The tier determines the position of this {@link Category} in the {@link SlimefunGuide}. + * + * @return the tier of this {@link Category} + */ + public int getTier() { + return tier; + } + + /** + * This sets the tier of this {@link Category}. + * The tier determines the position of this {@link Category} in the {@link SlimefunGuide}. + * + * @param tier + * The tier for this {@link Category} + */ + public void setTier(int tier) { + this.tier = tier; + + // Refresh Category order if already registered. + if (isRegistered()) { + sortCategoriesByTier(); + } + } + + /** + * This refreshes the {@link Category} order. + */ + private void sortCategoriesByTier() { + List categories = SlimefunPlugin.getRegistry().getCategories(); + Collections.sort(categories, Comparator.comparingInt(Category::getTier)); } /** @@ -230,16 +269,6 @@ public class Category implements Keyed { return item != null && items.contains(item); } - /** - * Returns the tier of this {@link Category}. - * The tier determines the position of this {@link Category} in the {@link SlimefunGuide}. - * - * @return the tier of this {@link Category} - */ - public int getTier() { - return tier; - } - @Override public final boolean equals(Object obj) { if (obj instanceof Category) { diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestCategories.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestCategories.java index bb0aadca7..94659b886 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestCategories.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/registration/TestCategories.java @@ -163,7 +163,7 @@ class TestCategories { Category category = new Category(new NamespacedKey(plugin, "parent"), new CustomItem(Material.EMERALD, "&5I am SHERlocked")); category.register(plugin); - LockedCategory locked = new LockedCategory(new NamespacedKey(plugin, "locked"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), category.getKey()); + LockedCategory locked = new LockedCategory(new NamespacedKey(plugin, "locked2"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), category.getKey()); locked.register(plugin); // No Items, so it should be unlocked