1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

More tests and relocated a class

This commit is contained in:
TheBusyBiscuit 2020-05-08 17:13:16 +02:00
parent a2512ce03a
commit aa897a5de7
22 changed files with 378 additions and 177 deletions

View File

@ -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!");
}
}

View File

@ -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.
* <p>
* 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<Category> 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<NamespacedKey> 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<Category> 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;
}
}

View File

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

View File

@ -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<ItemStack> 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<RecipeChoice> choices = new LinkedList<>();

View File

@ -33,7 +33,7 @@ public class PermissionsService {
config.getConfiguration().options().copyHeader(true);
}
public void register(Iterable<SlimefunItem> items) {
public void register(Iterable<SlimefunItem> 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

View File

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

View File

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

View File

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

View File

@ -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;
/**

View File

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

View File

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

View File

@ -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.
* <p>
* 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<Category> 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<NamespacedKey> 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<Category> 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);
}
}

View File

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

View File

@ -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());
}
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<ItemStack> optional = service.getFurnaceOutput(new ItemStack(Material.DIRT));
Assertions.assertTrue(optional.isPresent());
Assertions.assertEquals(result, optional.get());
Optional<ItemStack> 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));
}
}

View File

@ -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);
}

View File

@ -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);
}