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

Further unit tests and jacoco coverage reports

This commit is contained in:
TheBusyBiscuit 2020-05-07 14:28:54 +02:00
parent b456da9ffc
commit 7e04566393
13 changed files with 398 additions and 54 deletions

12
.gitignore vendored
View File

@ -1,10 +1,12 @@
/bin/
/target/
/sonar/
/.settings/
/.idea/
dependency-reduced-pom.xml
.classpath
.project
/.settings/
*.iml
/target
/.idea/
dependency-reduced-pom.xml
javadoc.xml
.DS_Store

31
pom.xml
View File

@ -7,8 +7,7 @@
<groupId>me.mrCookieSlime</groupId>
<artifactId>Slimefun</artifactId>
<!-- Our default version will be UNOFFICIAL, this will prevent the auto
updater -->
<!-- Our default version will be UNOFFICIAL, this will prevent auto updates -->
<!-- from overriding our local test file -->
<version>4.3-UNOFFICIAL</version>
@ -25,6 +24,7 @@
<sonar.projectKey>TheBusyBiscuit_Slimefun4</sonar.projectKey>
<sonar.organization>thebusybiscuit-github</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.coverage.jacoco.xmlReportPaths>target/site/jacoco/jacoco.xml</sonar.coverage.jacoco.xmlReportPaths>
</properties>
<repositories>
@ -59,6 +59,7 @@
<finalName>${project.name} v${project.version}</finalName>
<plugins>
<!-- Compiler plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
@ -72,6 +73,7 @@
</configuration>
</plugin>
<!-- Attach sources -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
@ -86,12 +88,37 @@
</executions>
</plugin>
<!-- Plugin for Unit Tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<!-- Code Coverage Reports -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
<!-- Dependency shading -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>

View File

@ -42,8 +42,9 @@ public class SoulboundRune extends SimpleSlimefunItem<ItemDropHandler> {
@Override
public ItemDropHandler getItemHandler() {
return (e, p, i) -> {
ItemStack item = i.getItemStack();
return (e, p, droppedItem) -> {
ItemStack item = droppedItem.getItemStack();
if (isItem(item)) {
if (!Slimefun.hasUnlocked(p, SlimefunItems.RUNE_SOULBOUND, true)) {
@ -52,42 +53,39 @@ public class SoulboundRune extends SimpleSlimefunItem<ItemDropHandler> {
Slimefun.runSync(() -> {
// Being sure the entity is still valid and not picked up or whatsoever.
if (!i.isValid()) return;
if (!droppedItem.isValid()) {
return;
}
Location l = i.getLocation();
Location l = droppedItem.getLocation();
Collection<Entity> entites = l.getWorld().getNearbyEntities(l, 1.5, 1.5, 1.5, this::findCompatibleItem);
if (entites.isEmpty()) return;
if (entites.isEmpty()) {
return;
}
Entity entity = entites.stream().findFirst().get();
ItemStack ench = ((Item) entity).getItemStack();
Item ent = (Item) entity;
ItemStack target = ((Item) entity).getItemStack();
Item targetItem = (Item) entity;
if (ench.getAmount() == 1) {
if (target.getAmount() == 1) {
e.setCancelled(true);
ItemMeta enchMeta = ench.getItemMeta();
List<String> lore = enchMeta.hasLore() ? enchMeta.getLore() : new ArrayList<>();
// This lightning is just an effect, it deals no damage.
l.getWorld().strikeLightningEffect(l);
Slimefun.runSync(() -> {
// Being sure entities are still valid and not picked up or whatsoever.
if (i.isValid() && ent.isValid()) {
if (droppedItem.isValid() && targetItem.isValid() && target.getAmount() == 1) {
l.getWorld().createExplosion(l, 0.0F);
l.getWorld().playSound(l, Sound.ENTITY_GENERIC_EXPLODE, 0.3F, 1F);
lore.add(ChatColor.GRAY + "Soulbound");
apply(target);
enchMeta.setLore(lore);
ench.setItemMeta(enchMeta);
ent.remove();
i.remove();
l.getWorld().dropItemNaturally(l, ench);
targetItem.remove();
droppedItem.remove();
l.getWorld().dropItemNaturally(l, target);
SlimefunPlugin.getLocal().sendMessage(p, "messages.soulbound-rune.success", true);
}
@ -104,6 +102,21 @@ public class SoulboundRune extends SimpleSlimefunItem<ItemDropHandler> {
};
}
/**
* This method applies the {@link Soulbound} effect onto a given {@link ItemStack}.
*
* @param item
* The {@link ItemStack} to apply this effect to
*/
public void apply(ItemStack item) {
ItemMeta meta = item.getItemMeta();
List<String> lore = meta.hasLore() ? meta.getLore() : new ArrayList<>();
lore.add(ChatColor.GRAY + "Soulbound");
meta.setLore(lore);
item.setItemMeta(meta);
}
private boolean findCompatibleItem(Entity n) {
if (n instanceof Item) {
Item item = (Item) n;

View File

@ -80,14 +80,14 @@ public class VanillaMachinesListener implements Listener {
}
}
@EventHandler
@EventHandler(ignoreCancelled = true)
public void onAnvil(InventoryClickEvent e) {
if (e.getRawSlot() == 2 && e.getInventory().getType() == InventoryType.ANVIL && e.getWhoClicked() instanceof Player) {
ItemStack item1 = e.getInventory().getContents()[0];
ItemStack item2 = e.getInventory().getContents()[1];
if (checkForUnallowedItems(item1, item2)) {
e.setCancelled(true);
e.setResult(Result.DENY);
SlimefunPlugin.getLocal().sendMessage((Player) e.getWhoClicked(), "anvil.not-working", true);
}
}
@ -98,7 +98,7 @@ public class VanillaMachinesListener implements Listener {
Inventory inventory = e.getInventory();
if (inventory.getType() == InventoryType.BREWING && e.getRawSlot() < inventory.getSize() && inventory.getHolder() instanceof BrewingStand) {
e.setCancelled(SlimefunItem.getByItem(e.getCursor()) != null);
e.setCancelled(isUnallowed(SlimefunItem.getByItem(e.getCursor())));
}
}

View File

@ -12,7 +12,6 @@ import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.skull.SkullItem;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.core.attributes.MachineTier;
import io.github.thebusybiscuit.slimefun4.core.attributes.MachineType;
@ -710,27 +709,27 @@ public final class SlimefunItems {
public static final SlimefunItemStack AUTO_BREEDER = new SlimefunItemStack("AUTO_BREEDER", Material.HAY_BLOCK, "&eAuto-Breeder", "", "&rRuns on &aOrganic Food", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), LoreBuilder.powerBuffer(1024), "&8\u21E8 &e\u26A1 &760 J/Animal");
public static final ItemStack ORGANIC_FOOD = new CustomItem(SkullItem.fromBase64("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"), "&aOrganic Food", "&7Content: &9X");
public static final SlimefunItemStack WHEAT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_WHEAT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Wheat");
public static final SlimefunItemStack CARROT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_CARROT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Carrots");
public static final SlimefunItemStack POTATO_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_POTATO", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Potatoes");
public static final SlimefunItemStack SEEDS_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SEEDS", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Seeds");
public static final SlimefunItemStack BEETROOT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_BEETROOT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Beetroot");
public static final SlimefunItemStack MELON_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_MELON", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Melon");
public static final SlimefunItemStack APPLE_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_APPLE", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Apple");
public static final SlimefunItemStack SWEET_BERRIES_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SWEET_BERRIES", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Sweet Berries");
public static final SlimefunItemStack KELP_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_KELP", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9Dried Kelp");
public static final SlimefunItemStack ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Food", "&7Content: &9???");
public static final SlimefunItemStack WHEAT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_WHEAT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Wheat");
public static final SlimefunItemStack CARROT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_CARROT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Carrots");
public static final SlimefunItemStack POTATO_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_POTATO", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Potatoes");
public static final SlimefunItemStack SEEDS_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SEEDS", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Seeds");
public static final SlimefunItemStack BEETROOT_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_BEETROOT", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Beetroot");
public static final SlimefunItemStack MELON_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_MELON", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Melon");
public static final SlimefunItemStack APPLE_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_APPLE", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Apple");
public static final SlimefunItemStack SWEET_BERRIES_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_SWEET_BERRIES", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Sweet Berries");
public static final SlimefunItemStack KELP_ORGANIC_FOOD = new SlimefunItemStack("ORGANIC_FOOD_KELP", ORGANIC_FOOD, "&aOrganic Food", "&7Content: &9Dried Kelp");
public static final ItemStack FERTILIZER = new CustomItem(SkullItem.fromBase64("b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79"), "&aOrganic Fertilizer", "&7Content: &9X");
public static final SlimefunItemStack WHEAT_FERTILIZER = new SlimefunItemStack("FERTILIZER_WHEAT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Wheat");
public static final SlimefunItemStack CARROT_FERTILIZER = new SlimefunItemStack("FERTILIZER_CARROT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Carrots");
public static final SlimefunItemStack POTATO_FERTILIZER = new SlimefunItemStack("FERTILIZER_POTATO", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Potatoes");
public static final SlimefunItemStack SEEDS_FERTILIZER = new SlimefunItemStack("FERTILIZER_SEEDS", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Seeds");
public static final SlimefunItemStack BEETROOT_FERTILIZER = new SlimefunItemStack("FERTILIZER_BEETROOT", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Beetroot");
public static final SlimefunItemStack MELON_FERTILIZER = new SlimefunItemStack("FERTILIZER_MELON", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Melon");
public static final SlimefunItemStack APPLE_FERTILIZER = new SlimefunItemStack("FERTILIZER_APPLE", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Apple");
public static final SlimefunItemStack SWEET_BERRIES_FERTILIZER = new SlimefunItemStack("FERTILIZER_SWEET_BERRIES", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Sweet Berries");
public static final SlimefunItemStack KELP_FERTILIZER = new SlimefunItemStack("FERTILIZER_KELP", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9Dried Kelp");
public static final SlimefunItemStack FERTILIZER = new SlimefunItemStack("FERTILIZER", "b439e3f5acbee9be4c4259289d6d9f35c635ffa661114687b3ea6dda8c79", "&aOrganic Fertilizer", "&7Content: &9???");
public static final SlimefunItemStack WHEAT_FERTILIZER = new SlimefunItemStack("FERTILIZER_WHEAT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Wheat");
public static final SlimefunItemStack CARROT_FERTILIZER = new SlimefunItemStack("FERTILIZER_CARROT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Carrots");
public static final SlimefunItemStack POTATO_FERTILIZER = new SlimefunItemStack("FERTILIZER_POTATO", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Potatoes");
public static final SlimefunItemStack SEEDS_FERTILIZER = new SlimefunItemStack("FERTILIZER_SEEDS", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Seeds");
public static final SlimefunItemStack BEETROOT_FERTILIZER = new SlimefunItemStack("FERTILIZER_BEETROOT", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Beetroot");
public static final SlimefunItemStack MELON_FERTILIZER = new SlimefunItemStack("FERTILIZER_MELON", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Melon");
public static final SlimefunItemStack APPLE_FERTILIZER = new SlimefunItemStack("FERTILIZER_APPLE", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Apple");
public static final SlimefunItemStack SWEET_BERRIES_FERTILIZER = new SlimefunItemStack("FERTILIZER_SWEET_BERRIES", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Sweet Berries");
public static final SlimefunItemStack KELP_FERTILIZER = new SlimefunItemStack("FERTILIZER_KELP", FERTILIZER, "&aOrganic Fertilizer", "&7Content: &9Dried Kelp");
public static final SlimefunItemStack ANIMAL_GROWTH_ACCELERATOR = new SlimefunItemStack("ANIMAL_GROWTH_ACCELERATOR", Material.HAY_BLOCK, "&bAnimal Growth Accelerator", "", "&rRuns on &aOrganic Food", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), LoreBuilder.powerBuffer(1024), LoreBuilder.powerPerSecond(28));
public static final SlimefunItemStack CROP_GROWTH_ACCELERATOR = new SlimefunItemStack("CROP_GROWTH_ACCELERATOR", Material.LIME_TERRACOTTA, "&aCrop Growth Accelerator", "", "&rRuns on &aOrganic Fertilizer", "", LoreBuilder.machine(MachineTier.END_GAME, MachineType.MACHINE), "&8\u21E8 &7Radius: 7x7", "&8\u21E8 &7Speed: &a3/time", LoreBuilder.powerBuffer(1024), LoreBuilder.powerPerSecond(50));

View File

@ -152,6 +152,18 @@ public class Category implements Keyed {
return items;
}
/**
* This method returns whether a given {@link SlimefunItem} exists in this {@link Category}.
*
* @param item
* The {@link SlimefunItem} to find
*
* @return Whether the given {@link SlimefunItem} was found in this {@link Category}
*/
public boolean contains(SlimefunItem item) {
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}.

View File

@ -128,7 +128,7 @@ public class SlimefunItem implements Placeable {
*
* @return the identifier of this {@link SlimefunItem}
*/
public String getID() {
public final String getID() {
return id;
}
@ -576,6 +576,9 @@ public class SlimefunItem implements Placeable {
* Any {@link ItemHandler} that should be added to this {@link SlimefunItem}
*/
public final void addItemHandler(ItemHandler... handlers) {
Validate.notEmpty(handlers, "You cannot add zero handlers...");
Validate.noNullElements(handlers, "You cannot add any 'null' ItemHandler!");
if (state != ItemState.UNREGISTERED) {
throw new UnsupportedOperationException("You cannot add an ItemHandler after the SlimefunItem was registered.");
}

View File

@ -22,6 +22,7 @@ import org.bukkit.potion.PotionEffectType;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.cscorelib2.item.ImmutableItemMeta;
import io.github.thebusybiscuit.cscorelib2.skull.SkullItem;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.api.exceptions.PrematureCodeException;
import io.github.thebusybiscuit.slimefun4.utils.PatternUtils;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -184,6 +185,10 @@ public class SlimefunItemStack extends CustomItem {
}
private static ItemStack getSkull(String id, String texture) {
if (SlimefunPlugin.getMinecraftVersion() == MinecraftVersion.UNIT_TEST) {
return new ItemStack(Material.PLAYER_HEAD);
}
return SkullItem.fromBase64(getTexture(id, texture));
}

View File

@ -0,0 +1,12 @@
package io.github.thebusybiscuit.slimefun4.mocks;
import me.mrCookieSlime.Slimefun.Objects.handlers.ItemHandler;
public class MockItemHandler implements ItemHandler {
@Override
public Class<? extends ItemHandler> getIdentifier() {
return getClass();
}
}

View File

@ -20,6 +20,10 @@ public final class SlimefunMocks {
private SlimefunMocks() {}
public static Category getCategory() {
return category;
}
public static Inventory mockInventory(InventoryType type, ItemStack... contents) {
Inventory inv = Mockito.mock(Inventory.class);

View File

@ -0,0 +1,65 @@
package io.github.thebusybiscuit.slimefun4.tests.items;
import java.util.concurrent.atomic.AtomicBoolean;
import org.bukkit.Material;
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.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.mocks.MockItemHandler;
import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
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();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
@Test
public void testIllegalItemHandlers() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_HANDLER_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
Assertions.assertThrows(IllegalArgumentException.class, () -> item.addItemHandler());
Assertions.assertThrows(IllegalArgumentException.class, () -> item.addItemHandler((MockItemHandler) null));
Assertions.assertThrows(UnsupportedOperationException.class, () -> item.addItemHandler(new MockItemHandler()));
}
@Test
public void testItemHandler() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_HANDLER_TEST_2", new CustomItem(Material.DIAMOND, "&cTest"));
MockItemHandler handler = new MockItemHandler();
item.addItemHandler(handler);
item.register(plugin);
Assertions.assertTrue(item.getHandlers().contains(handler));
AtomicBoolean bool = new AtomicBoolean(false);
Assertions.assertTrue(item.callItemHandler(MockItemHandler.class, x -> bool.set(true)));
Assertions.assertTrue(bool.get());
AtomicBoolean bool2 = new AtomicBoolean(false);
Assertions.assertFalse(item.callItemHandler(ItemUseHandler.class, x -> bool2.set(true)));
Assertions.assertFalse(bool2.get());
}
}

View File

@ -1,6 +1,11 @@
package io.github.thebusybiscuit.slimefun4.tests.items;
import java.util.Optional;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
@ -12,6 +17,7 @@ import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
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.SlimefunItem.ItemState;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
@ -33,10 +39,53 @@ public class TestSlimefunItemRegistration {
@Test
public void testSuccessfulRegistration() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("TEST_ITEM", new CustomItem(Material.DIAMOND, "&cTest"));
String id = "TEST_ITEM";
SlimefunItem item = SlimefunMocks.mockSlimefunItem(id, new CustomItem(Material.DIAMOND, "&cTest"));
Assertions.assertEquals(ItemState.UNREGISTERED, item.getState());
item.register(plugin);
Assertions.assertEquals(ItemState.ENABLED, item.getState());
Assertions.assertFalse(item.isDisabled());
Assertions.assertEquals(id, item.getID());
Assertions.assertFalse(item.isAddonItem());
Assertions.assertEquals(item, SlimefunItem.getByID(id));
}
@Test
public void testDisabledItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("DISABLED_ITEM", new CustomItem(Material.DIAMOND, "&cTest"));
SlimefunPlugin.getItemCfg().setValue("DISABLED_ITEM.enabled", false);
item.register(plugin);
Assertions.assertEquals(ItemState.DISABLED, item.getState());
Assertions.assertTrue(item.isDisabled());
}
@Test
public void testWikiPages() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("WIKI_ITEM", new CustomItem(Material.BOOK, "&cTest"));
item.register(plugin);
Assertions.assertFalse(item.getWikipage().isPresent());
// null should not be a valid argument
Assertions.assertThrows(IllegalArgumentException.class, () -> item.addOficialWikipage(null));
item.addOficialWikipage("Test");
Optional<String> wiki = item.getWikipage();
Assertions.assertTrue(wiki.isPresent());
Assertions.assertEquals("https://github.com/TheBusyBiscuit/Slimefun4/wiki/Test", wiki.get());
}
@Test
public void testGetItemName() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("ITEM_NAME_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
Assertions.assertEquals(ChatColor.RED + "Test", item.getItemName());
}
@Test
@ -44,7 +93,9 @@ public class TestSlimefunItemRegistration {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.ACACIA_SIGN, false);
item.register(plugin);
Assertions.assertTrue(item.isUseableInWorkbench());
Assertions.assertEquals(ItemState.VANILLA_FALLBACK, item.getState());
Assertions.assertTrue(item.isDisabled());
}
@Test
@ -59,4 +110,86 @@ public class TestSlimefunItemRegistration {
Assertions.assertEquals(ItemState.UNREGISTERED, item2.getState());
}
@Test
public void testRecipeOutput() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("RECIPE_OUTPUT_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
Assertions.assertEquals(item.getItem(), item.getRecipeOutput());
ItemStack output = new ItemStack(Material.EMERALD, 64);
item.setRecipeOutput(output);
Assertions.assertEquals(output, item.getRecipeOutput());
item.setRecipeOutput(item.getItem());
Assertions.assertEquals(item.getItem(), item.getRecipeOutput());
}
@Test
public void testIsItem() {
CustomItem item = new CustomItem(Material.BEACON, "&cItem Test");
SlimefunItem sfItem = SlimefunMocks.mockSlimefunItem("IS_ITEM_TEST", item);
sfItem.register(plugin);
Assertions.assertTrue(sfItem.isItem(sfItem.getItem()));
Assertions.assertTrue(sfItem.isItem(item));
Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test")));
Assertions.assertFalse(sfItem.isItem(null));
Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON)));
Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest")));
Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item));
}
@Test
public void testCategoryRegistration() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
item.load();
// null should not be a valid argument
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setCategory(null));
Category category = SlimefunMocks.getCategory();
Category category2 = new Category(new NamespacedKey(plugin, "test2"), new CustomItem(Material.OBSIDIAN, "&6Test 2"));
Assertions.assertTrue(category.contains(item));
Assertions.assertFalse(category2.contains(item));
Assertions.assertEquals(category, item.getCategory());
item.setCategory(category2);
Assertions.assertFalse(category.contains(item));
Assertions.assertTrue(category2.contains(item));
Assertions.assertEquals(category2, item.getCategory());
}
@Test
public void testHiddenItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("HIDDEN_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.setHidden(true);
item.register(plugin);
item.load();
Category category = SlimefunMocks.getCategory();
Assertions.assertTrue(item.isHidden());
Assertions.assertFalse(category.contains(item));
Assertions.assertEquals(category, item.getCategory());
item.setHidden(false);
Assertions.assertFalse(item.isHidden());
Assertions.assertTrue(category.contains(item));
Assertions.assertEquals(category, item.getCategory());
item.setHidden(true);
Assertions.assertTrue(item.isHidden());
Assertions.assertFalse(category.contains(item));
Assertions.assertEquals(category, item.getCategory());
// Do nothing if the value hasn't changed
item.setHidden(true);
Assertions.assertTrue(item.isHidden());
Assertions.assertFalse(category.contains(item));
}
}

View File

@ -63,6 +63,27 @@ public class TestVanillaMachinesListener {
return event;
}
private InventoryClickEvent mockAnvilEvent(ItemStack item) {
Player player = server.addPlayer();
Inventory inv = SlimefunMocks.mockInventory(InventoryType.ANVIL, item, null, new ItemStack(Material.IRON_CHESTPLATE));
InventoryView view = player.openInventory(inv);
InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 2, ClickType.LEFT, InventoryAction.PICKUP_ONE);
listener.onAnvil(event);
return event;
}
private InventoryClickEvent mockBrewingEvent(ItemStack item) {
Player player = server.addPlayer();
Inventory inv = SlimefunMocks.mockInventory(InventoryType.BREWING);
InventoryView view = player.openInventory(inv);
view.setCursor(item);
InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 1, ClickType.LEFT, InventoryAction.PICKUP_ONE);
listener.onPreBrew(event);
return event;
}
private CraftItemEvent mockCraftingEvent(ItemStack item) {
Recipe recipe = new ShapedRecipe(new NamespacedKey(plugin, "test_recipe"), new ItemStack(Material.EMERALD));
Player player = server.addPlayer();
@ -106,7 +127,7 @@ public class TestVanillaMachinesListener {
}
@Test
public void testGrindStoneWithSlimefunItems() {
public void testGrindStoneWithSlimefunItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("ENCHANTED_MOCK_BOOK", new CustomItem(Material.ENCHANTED_BOOK, "&6Mock"));
item.register(plugin);
@ -176,4 +197,52 @@ public class TestVanillaMachinesListener {
PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem());
Assertions.assertNotNull(event.getInventory().getResult());
}
@Test
public void testAnvilWithoutSlimefunItems() {
InventoryClickEvent event = mockAnvilEvent(new ItemStack(Material.IRON_SWORD));
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testAnvilWithSlimefunItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCKED_IRON_SWORD", new CustomItem(Material.IRON_SWORD, "&6Mock"));
item.register(plugin);
InventoryClickEvent event = mockAnvilEvent(item.getItem());
Assertions.assertEquals(Result.DENY, event.getResult());
}
@Test
public void testAnvilWithVanillaItem() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.IRON_SWORD, true);
item.register(plugin);
InventoryClickEvent event = mockAnvilEvent(item.getItem());
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testBrewingWithoutSlimefunItems() {
InventoryClickEvent event = mockBrewingEvent(new ItemStack(Material.BLAZE_POWDER));
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testBrewingWithSlimefunItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCK_POWDER", new CustomItem(Material.BLAZE_POWDER, "&6Mock"));
item.register(plugin);
InventoryClickEvent event = mockBrewingEvent(item.getItem());
Assertions.assertEquals(Result.DENY, event.getResult());
}
@Test
public void testBrewingithVanillaItem() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.BLAZE_POWDER, true);
item.register(plugin);
InventoryClickEvent event = mockBrewingEvent(item.getItem());
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
}