1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added Category#setTier()

This commit is contained in:
TheBusyBiscuit 2021-04-25 19:47:58 +02:00
parent c975fe234e
commit bffef56407
3 changed files with 43 additions and 13 deletions

View File

@ -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

View File

@ -43,7 +43,7 @@ public class Category implements Keyed {
protected final List<SlimefunItem> 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<Category> 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) {

View File

@ -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