From aa897a5de7b9b9362d62e2b5e8bed033cf943e18 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 8 May 2020 17:13:16 +0200 Subject: [PATCH] More tests and relocated a class --- .../core/categories/FlexCategory.java | 10 ++ .../core/categories/LockedCategory.java | 159 ++++++++++++++++++ .../core/categories/SeasonalCategory.java | 1 - .../core/services/MinecraftRecipeService.java | 7 + .../core/services/PermissionsService.java | 6 +- .../guide/BookSlimefunGuide.java | 2 +- .../guide/ChestSlimefunGuide.java | 2 +- .../items/electric/machines/AutoBreeder.java | 16 +- .../magical/talismans/EnderTalisman.java | 2 +- .../setup/DefaultCategories.java | 2 +- .../Slimefun/Objects/Category.java | 1 + .../Slimefun/Objects/LockedCategory.java | 147 +--------------- .../Slimefun/SlimefunPlugin.java | 2 +- .../slimefun4/tests/items/TestCategories.java | 29 +++- .../tests/items/TestItemHandlers.java | 4 +- .../tests/items/TestItemSettings.java | 4 +- .../items/TestSlimefunItemRegistration.java | 4 +- .../tests/services/TestBlockDataService.java | 4 +- .../tests/services/TestItemDataService.java | 4 +- .../tests/services/TestRecipeService.java | 139 +++++++++++++++ .../tests/services/TestUpdaterService.java | 6 +- .../services/TextCustomTextureService.java | 4 +- 22 files changed, 378 insertions(+), 177 deletions(-) create mode 100644 src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/LockedCategory.java create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestRecipeService.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/FlexCategory.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/FlexCategory.java index 3ae4959e9..c8e178e7a 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/FlexCategory.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/FlexCategory.java @@ -77,4 +77,14 @@ public abstract class FlexCategory extends Category { throw new UnsupportedOperationException("A FlexCategory has no items!"); } + @Override + public final boolean contains(SlimefunItem item) { + throw new UnsupportedOperationException("A FlexCategory has no items!"); + } + + @Override + public final void remove(SlimefunItem item) { + throw new UnsupportedOperationException("A FlexCategory has no items, so there is nothing remove!"); + } + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/LockedCategory.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/LockedCategory.java new file mode 100644 index 000000000..d4cea59d6 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/LockedCategory.java @@ -0,0 +1,159 @@ +package io.github.thebusybiscuit.slimefun4.core.categories; + +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; + +import org.apache.commons.lang.Validate; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.Slimefun; + +/** + * Represents a {@link Category} that cannot be opened until the parent category/categories + * are fully unlocked. + *

+ * See {@link Category} for the complete documentation. + * + * @author TheBusyBiscuit + * + * @see Category + * @see SeasonalCategory + * + */ +public class LockedCategory extends Category { + + private final NamespacedKey[] keys; + private final Set parents = new HashSet<>(); + + /** + * The basic constructor for a LockedCategory. + * Like {@link Category}, the default tier is automatically set to 3. + * + * @param key + * A unique identifier for this category + * @param item + * The display item for this category + * @param parents + * The parent categories for this category + * + */ + public LockedCategory(NamespacedKey key, ItemStack item, NamespacedKey... parents) { + this(key, item, 3, parents); + } + + /** + * The constructor for a LockedCategory. + * + * @param key + * A unique identifier for this category + * @param item + * The display item for this category + * @param tier + * The tier of this category + * @param parents + * The parent categories for this category + * + */ + public LockedCategory(NamespacedKey key, ItemStack item, int tier, NamespacedKey... parents) { + super(key, item, tier); + Validate.noNullElements(parents, "A LockedCategory must not have any 'null' parents!"); + + this.keys = parents; + } + + @Override + public void register() { + super.register(); + + List namespacedKeys = new ArrayList<>(); + + for (NamespacedKey key : keys) { + if (key != null) { + namespacedKeys.add(key); + } + } + + for (Category category : SlimefunPlugin.getRegistry().getCategories()) { + if (namespacedKeys.remove(category.getKey())) { + addParent(category); + } + } + + for (NamespacedKey key : namespacedKeys) { + Slimefun.getLogger().log(Level.INFO, "Parent \"{0}\" for Category \"{1}\" was not found, probably just disabled.", new Object[] { key, getKey() }); + } + } + + /** + * Gets the list of parent categories for this {@link LockedCategory}. + * + * @return the list of parent categories + * + * @see #addParent(Category) + * @see #removeParent(Category) + */ + public Set getParents() { + return parents; + } + + /** + * Adds a parent {@link Category} to this {@link LockedCategory}. + * + * @param category + * The {@link Category} to add as a parent + * + * @see #getParents() + * @see #removeParent(Category) + */ + public void addParent(Category category) { + if (category == this || category == null) { + throw new IllegalArgumentException("Category '" + item.getItemMeta().getDisplayName() + "' cannot be a parent of itself or have a 'null' parent."); + } + + parents.add(category); + } + + /** + * Removes a {@link Category} from the parents of this {@link LockedCategory}. + * + * @param category + * The {@link Category} to remove from the parents of this {@link LockedCategory} + * + * @see #getParents() + * @see #addParent(Category) + */ + public void removeParent(Category category) { + parents.remove(category); + } + + /** + * Checks if the {@link Player} has fully unlocked all parent categories. + * + * @param p + * The {@link Player} to check + * @param profile + * The {@link PlayerProfile} that belongs to the given {@link Player} + * @return Whether the {@link Player} has fully completed all parent categories, otherwise false + */ + public boolean hasUnlocked(Player p, PlayerProfile profile) { + for (Category category : parents) { + for (SlimefunItem item : category.getItems()) { + // Should we replace this all with Slimefun.hasUnlocked() ? + if (Slimefun.isEnabled(p, item, false) && Slimefun.hasPermission(p, item, false) && item.getResearch() != null && !profile.hasUnlocked(item.getResearch())) { + return false; + } + } + } + + return true; + } +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/SeasonalCategory.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/SeasonalCategory.java index b47d12266..dcb3cc516 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/SeasonalCategory.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/categories/SeasonalCategory.java @@ -8,7 +8,6 @@ import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; /** * Represents a {@link Category} that is only displayed in the Guide during diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/MinecraftRecipeService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/MinecraftRecipeService.java index 4466bf9ab..025b3a119 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/MinecraftRecipeService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/MinecraftRecipeService.java @@ -4,6 +4,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Optional; +import org.apache.commons.lang.Validate; import org.bukkit.inventory.FurnaceRecipe; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; @@ -60,6 +61,10 @@ public class MinecraftRecipeService { * @return An {@link Optional} describing the furnace output of the given {@link ItemStack} */ public Optional getFurnaceOutput(ItemStack input) { + if (input == null) { + return Optional.empty(); + } + return snapshot.getRecipeOutput(MinecraftRecipe.FURNACE, input); } @@ -75,6 +80,8 @@ public class MinecraftRecipeService { * @return An Array of {@link RecipeChoice} representing the shape of this {@link Recipe} */ public RecipeChoice[] getRecipeShape(Recipe recipe) { + Validate.notNull(recipe, "Recipe must not be null!"); + if (recipe instanceof ShapedRecipe) { List choices = new LinkedList<>(); diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PermissionsService.java b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PermissionsService.java index 879c917e5..8944f712b 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PermissionsService.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/core/services/PermissionsService.java @@ -33,7 +33,7 @@ public class PermissionsService { config.getConfiguration().options().copyHeader(true); } - public void register(Iterable items) { + public void register(Iterable items, boolean save) { for (SlimefunItem item : items) { if (item != null && item.getID() != null && !migrate(item)) { config.setDefaultValue(item.getID() + ".permission", "none"); @@ -42,7 +42,9 @@ public class PermissionsService { } } - config.save(); + if (save) { + config.save(); + } } // Temporary migration method for the old system diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java index 6c22451da..cb9bd0381 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/BookSlimefunGuide.java @@ -21,6 +21,7 @@ import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.core.categories.FlexCategory; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideLayout; @@ -28,7 +29,6 @@ import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; import me.mrCookieSlime.Slimefun.Objects.Research; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.api.Slimefun; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java index a9f28a350..62e3b81aa 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/guide/ChestSlimefunGuide.java @@ -28,6 +28,7 @@ import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; import io.github.thebusybiscuit.slimefun4.core.MultiBlock; import io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem; import io.github.thebusybiscuit.slimefun4.core.categories.FlexCategory; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import io.github.thebusybiscuit.slimefun4.core.guide.GuideHistory; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation; @@ -40,7 +41,6 @@ import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.MenuClickHan import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; import me.mrCookieSlime.Slimefun.Objects.Research; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.multiblocks.MultiBlockMachine; diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBreeder.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBreeder.java index e4ced6a19..0a6427a4d 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBreeder.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/AutoBreeder.java @@ -97,10 +97,12 @@ public class AutoBreeder extends SlimefunItem implements InventoryBlock, EnergyN protected void tick(Block b) { BlockMenu inv = BlockStorage.getInventory(b); - for (Entity n : b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 2.0, 4.0, n -> n instanceof Animals && n.isValid() && ((Animals) n).isAdult() && !((Animals) n).isLoveMode())) { + for (Entity n : b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 2.0, 4.0, this::canBreed)) { for (int slot : getInputSlots()) { if (SlimefunUtils.isItemSimilar(inv.getItemInSlot(slot), SlimefunItems.ORGANIC_FOOD, false)) { - if (ChargableBlock.getCharge(b) < ENERGY_CONSUMPTION) return; + if (ChargableBlock.getCharge(b) < ENERGY_CONSUMPTION) { + return; + } ChargableBlock.addCharge(b, -ENERGY_CONSUMPTION); inv.consumeItem(slot); @@ -113,4 +115,14 @@ public class AutoBreeder extends SlimefunItem implements InventoryBlock, EnergyN } } + private boolean canBreed(Entity n) { + if (n.isValid() && n instanceof Animals) { + Animals animal = (Animals) n; + + return animal.isAdult() && !animal.isLoveMode(); + } + + return false; + } + } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/EnderTalisman.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/EnderTalisman.java index 529b15180..08b267992 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/EnderTalisman.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/magical/talismans/EnderTalisman.java @@ -5,9 +5,9 @@ import org.bukkit.block.EnderChest; import org.bukkit.inventory.ItemStack; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.SlimefunItems; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; /** diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/DefaultCategories.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/DefaultCategories.java index a893ddf8d..332d0d397 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/DefaultCategories.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/setup/DefaultCategories.java @@ -7,12 +7,12 @@ import org.bukkit.NamespacedKey; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.skull.SkullItem; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory; import io.github.thebusybiscuit.slimefun4.utils.ChatUtils; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Lists.SlimefunItems; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; /** * This class holds a reference to every {@link Category} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java index 45e2f9b2e..af72379e0 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/Category.java @@ -16,6 +16,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory; import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide; import me.mrCookieSlime.Slimefun.SlimefunPlugin; diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Objects/LockedCategory.java b/src/main/java/me/mrCookieSlime/Slimefun/Objects/LockedCategory.java index 76be44aff..29a187c78 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Objects/LockedCategory.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Objects/LockedCategory.java @@ -1,159 +1,20 @@ package me.mrCookieSlime.Slimefun.Objects; -import java.util.ArrayList; -import java.util.HashSet; -import java.util.List; -import java.util.Set; -import java.util.logging.Level; - -import org.apache.commons.lang.Validate; import org.bukkit.NamespacedKey; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; -import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory; -import me.mrCookieSlime.Slimefun.SlimefunPlugin; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; -import me.mrCookieSlime.Slimefun.api.Slimefun; - /** - * Represents a {@link Category} that cannot be opened until the parent category/categories - * are fully unlocked. - *

- * See {@link Category} for the complete documentation. - * - * @author TheBusyBiscuit - * - * @see Category - * @see SeasonalCategory + * @deprecated Moved to io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory * */ -public class LockedCategory extends Category { +@Deprecated +public class LockedCategory extends io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory { - private final NamespacedKey[] keys; - private final Set parents = new HashSet<>(); - - /** - * The basic constructor for a LockedCategory. - * Like {@link Category}, the default tier is automatically set to 3. - * - * @param key - * A unique identifier for this category - * @param item - * The display item for this category - * @param parents - * The parent categories for this category - * - */ public LockedCategory(NamespacedKey key, ItemStack item, NamespacedKey... parents) { this(key, item, 3, parents); } - /** - * The constructor for a LockedCategory. - * - * @param key - * A unique identifier for this category - * @param item - * The display item for this category - * @param tier - * The tier of this category - * @param parents - * The parent categories for this category - * - */ public LockedCategory(NamespacedKey key, ItemStack item, int tier, NamespacedKey... parents) { - super(key, item, tier); - Validate.noNullElements(parents, "A LockedCategory must not have any 'null' parents!"); - - this.keys = parents; - } - - @Override - public void register() { - super.register(); - - List namespacedKeys = new ArrayList<>(); - - for (NamespacedKey key : keys) { - if (key != null) { - namespacedKeys.add(key); - } - } - - for (Category category : SlimefunPlugin.getRegistry().getCategories()) { - if (namespacedKeys.remove(category.getKey())) { - addParent(category); - } - } - - for (NamespacedKey key : namespacedKeys) { - Slimefun.getLogger().log(Level.INFO, "Parent \"{0}\" for Category \"{1}\" was not found, probably just disabled.", new Object[] { key, getKey() }); - } - } - - /** - * Gets the list of parent categories for this {@link LockedCategory}. - * - * @return the list of parent categories - * - * @see #addParent(Category) - * @see #removeParent(Category) - */ - public Set getParents() { - return parents; - } - - /** - * Adds a parent {@link Category} to this {@link LockedCategory}. - * - * @param category - * The {@link Category} to add as a parent - * - * @see #getParents() - * @see #removeParent(Category) - */ - public void addParent(Category category) { - if (category == this || category == null) { - throw new IllegalArgumentException("Category '" + item.getItemMeta().getDisplayName() + "' cannot be a parent of itself or have a 'null' parent."); - } - - parents.add(category); - } - - /** - * Removes a {@link Category} from the parents of this {@link LockedCategory}. - * - * @param category - * The {@link Category} to remove from the parents of this {@link LockedCategory} - * - * @see #getParents() - * @see #addParent(Category) - */ - public void removeParent(Category category) { - parents.remove(category); - } - - /** - * Checks if the {@link Player} has fully unlocked all parent categories. - * - * @param p - * The {@link Player} to check - * @param profile - * The {@link PlayerProfile} that belongs to the given {@link Player} - * @return Whether the {@link Player} has fully completed all parent categories, otherwise false - */ - public boolean hasUnlocked(Player p, PlayerProfile profile) { - for (Category category : parents) { - for (SlimefunItem item : category.getItems()) { - // Should we replace this all with Slimefun.hasUnlocked() ? - if (Slimefun.isEnabled(p, item, false) && Slimefun.hasPermission(p, item, false) && item.getResearch() != null && !profile.hasUnlocked(item.getResearch())) { - return false; - } - } - } - - return true; + super(key, item, tier, parents); } } diff --git a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java index 0865963f8..ebd4d5486 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/SlimefunPlugin.java @@ -263,7 +263,7 @@ public class SlimefunPlugin extends JavaPlugin implements SlimefunAddon { Slimefun.runSync(new SlimefunStartupTask(this, () -> { protections = new ProtectionManager(getServer()); textureService.register(registry.getAllSlimefunItems(), true); - permissionsService.register(registry.getAllSlimefunItems()); + permissionsService.register(registry.getAllSlimefunItems(), true); recipeService.refresh(); }), 0); diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java index b8dca05cf..718847f79 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestCategories.java @@ -15,12 +15,15 @@ import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import io.github.thebusybiscuit.slimefun4.core.categories.FlexCategory; +import io.github.thebusybiscuit.slimefun4.core.categories.LockedCategory; import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory; +import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideLayout; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; import me.mrCookieSlime.Slimefun.SlimefunPlugin; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.LockedCategory; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; public class TestCategories { @@ -136,4 +139,28 @@ public class TestCategories { SeasonalCategory category2 = new SeasonalCategory(category.getKey(), month.plus(6), 1, new CustomItem(Material.MILK_BUCKET, "&dSeasonal Test")); Assertions.assertTrue(category2.isHidden(player)); } + + @Test + public void testFlexCategory() { + FlexCategory category = new FlexCategory(new NamespacedKey(plugin, "flex"), new CustomItem(Material.REDSTONE, "&4Weird flex but ok")) { + + @Override + public void open(Player p, PlayerProfile profile, SlimefunGuideLayout layout) { + // Nothing + } + + @Override + public boolean isVisible(Player p, PlayerProfile profile, SlimefunGuideLayout layout) { + return true; + } + }; + + Player player = server.addPlayer(); + Assertions.assertFalse(category.isHidden(player)); + + Assertions.assertThrows(UnsupportedOperationException.class, () -> category.add(null)); + Assertions.assertThrows(UnsupportedOperationException.class, () -> category.contains(null)); + Assertions.assertThrows(UnsupportedOperationException.class, () -> category.remove(null)); + Assertions.assertThrows(UnsupportedOperationException.class, () -> category.getItems()); + } } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java index fde847873..32d510b4c 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemHandlers.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.mocks.MockItemHandler; import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; @@ -19,12 +18,11 @@ import me.mrCookieSlime.Slimefun.Objects.handlers.ItemUseHandler; public class TestItemHandlers { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemSettings.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemSettings.java index 21faae605..3b0b221d1 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemSettings.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestItemSettings.java @@ -9,7 +9,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.api.items.ItemSetting; import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; @@ -18,12 +17,11 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; public class TestItemSettings { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java index bf0117e55..a43aa3d50 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/items/TestSlimefunItemRegistration.java @@ -12,7 +12,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks; @@ -23,12 +22,11 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; public class TestSlimefunItemRegistration { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestBlockDataService.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestBlockDataService.java index cce6c3cd2..8d4bf2cbf 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestBlockDataService.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestBlockDataService.java @@ -8,18 +8,16 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.slimefun4.core.services.BlockDataService; import me.mrCookieSlime.Slimefun.SlimefunPlugin; public class TestBlockDataService { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestItemDataService.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestItemDataService.java index cd682e6c1..9360834de 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestItemDataService.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestItemDataService.java @@ -12,18 +12,16 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.slimefun4.core.services.CustomItemDataService; import me.mrCookieSlime.Slimefun.SlimefunPlugin; public class TestItemDataService { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestRecipeService.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestRecipeService.java new file mode 100644 index 000000000..0ee1d0458 --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestRecipeService.java @@ -0,0 +1,139 @@ +package io.github.thebusybiscuit.slimefun4.tests.services; + +import java.util.Optional; + +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.FurnaceRecipe; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.RecipeChoice; +import org.bukkit.inventory.RecipeChoice.MaterialChoice; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.ShapelessRecipe; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import io.github.thebusybiscuit.slimefun4.core.services.MinecraftRecipeService; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; + +public class TestRecipeService { + + private static ServerMock server; + private static SlimefunPlugin plugin; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + plugin = MockBukkit.load(SlimefunPlugin.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + public void testRecipe() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + + NamespacedKey key = new NamespacedKey(plugin, "furnace_recipe_test"); + ItemStack result = new ItemStack(Material.EMERALD_BLOCK); + FurnaceRecipe recipe = new FurnaceRecipe(key, result, new MaterialChoice(Material.DIAMOND), 1, 2); + server.addRecipe(recipe); + + service.refresh(); + + Recipe[] recipes = service.getRecipesFor(result); + Assertions.assertEquals(1, recipes.length); + Assertions.assertEquals(recipe, recipes[0]); + } + + @Test + public void testNoRecipes() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + service.refresh(); + + Assertions.assertEquals(0, service.getRecipesFor(null).length); + Assertions.assertEquals(0, service.getRecipesFor(new ItemStack(Material.BEDROCK)).length); + } + + @Test + public void testFurnaceOutput() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + + NamespacedKey key = new NamespacedKey(plugin, "furnace_recipe_test2"); + ItemStack result = new ItemStack(Material.GOLD_BLOCK); + MaterialChoice materials = new MaterialChoice(Material.DIRT, Material.COBBLESTONE); + FurnaceRecipe recipe = new FurnaceRecipe(key, result, materials, 1, 2); + server.addRecipe(recipe); + + service.refresh(); + + Assertions.assertFalse(service.getFurnaceOutput(null).isPresent()); + Assertions.assertFalse(service.getFurnaceOutput(new ItemStack(Material.BEDROCK)).isPresent()); + + Optional optional = service.getFurnaceOutput(new ItemStack(Material.DIRT)); + Assertions.assertTrue(optional.isPresent()); + Assertions.assertEquals(result, optional.get()); + + Optional optional2 = service.getFurnaceOutput(new ItemStack(Material.COBBLESTONE)); + Assertions.assertTrue(optional2.isPresent()); + Assertions.assertEquals(result, optional2.get()); + } + + @Test + public void testBigShapedRecipe() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + + NamespacedKey key = new NamespacedKey(plugin, "shaped_recipe_9"); + ShapedRecipe recipe = new ShapedRecipe(key, new ItemStack(Material.ENCHANTED_GOLDEN_APPLE)); + MaterialChoice choice = new MaterialChoice(Material.TNT, Material.TNT_MINECART); + + recipe.shape("t t", " t ", "t t"); + recipe.setIngredient('t', choice); + server.addRecipe(recipe); + service.refresh(); + + RecipeChoice[] shape = service.getRecipeShape(recipe); + Assertions.assertArrayEquals(new RecipeChoice[] { choice, null, choice, null, choice, null, choice, null, choice }, shape); + } + + @Test + public void testSmallShapedRecipe() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + + NamespacedKey key = new NamespacedKey(plugin, "shaped_recipe_4"); + ShapedRecipe recipe = new ShapedRecipe(key, new ItemStack(Material.ENCHANTED_GOLDEN_APPLE)); + MaterialChoice choice = new MaterialChoice(Material.TNT, Material.TNT_MINECART); + + recipe.shape("tt", "tt"); + recipe.setIngredient('t', choice); + server.addRecipe(recipe); + service.refresh(); + + RecipeChoice[] shape = service.getRecipeShape(recipe); + Assertions.assertArrayEquals(new RecipeChoice[] { choice, choice, null, choice, choice, null }, shape); + } + + @Test + public void testShapelessRecipeShape() { + MinecraftRecipeService service = new MinecraftRecipeService(plugin); + + Assertions.assertThrows(IllegalArgumentException.class, () -> service.getRecipeShape(null)); + + NamespacedKey key = new NamespacedKey(plugin, "shapeless_test"); + ShapelessRecipe recipe = new ShapelessRecipe(key, new ItemStack(Material.TNT_MINECART)); + MaterialChoice choice = new MaterialChoice(Material.TNT); + recipe.addIngredient(choice); + + server.addRecipe(recipe); + service.refresh(); + + Assertions.assertArrayEquals(new RecipeChoice[] { choice }, service.getRecipeShape(recipe)); + } +} diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestUpdaterService.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestUpdaterService.java index 4669c753c..e30b33441 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestUpdaterService.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TestUpdaterService.java @@ -2,29 +2,25 @@ package io.github.thebusybiscuit.slimefun4.tests.services; import java.io.File; -import org.bukkit.plugin.PluginDescriptionFile; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.mockito.Mockito; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.slimefun4.api.SlimefunBranch; import io.github.thebusybiscuit.slimefun4.core.services.UpdaterService; import me.mrCookieSlime.Slimefun.SlimefunPlugin; public class TestUpdaterService { - private static ServerMock server; private static SlimefunPlugin plugin; private final File file = new File("test.jar"); @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TextCustomTextureService.java b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TextCustomTextureService.java index eb72a783a..ec71f77cb 100644 --- a/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TextCustomTextureService.java +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/tests/services/TextCustomTextureService.java @@ -10,7 +10,6 @@ import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import be.seeseemelk.mockbukkit.MockBukkit; -import be.seeseemelk.mockbukkit.ServerMock; import io.github.thebusybiscuit.cscorelib2.config.Config; import io.github.thebusybiscuit.cscorelib2.reflection.ReflectionUtils; import io.github.thebusybiscuit.slimefun4.core.services.CustomTextureService; @@ -20,12 +19,11 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; public class TextCustomTextureService { - private static ServerMock server; private static SlimefunPlugin plugin; @BeforeAll public static void load() { - server = MockBukkit.mock(); + MockBukkit.mock(); plugin = MockBukkit.load(SlimefunPlugin.class); }