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

First Unit Tests

This commit is contained in:
TheBusyBiscuit 2020-05-06 22:08:15 +02:00
parent 0e58c3fa35
commit b456da9ffc
9 changed files with 432 additions and 267 deletions

473
pom.xml
View File

@ -1,153 +1,156 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.mrCookieSlime</groupId>
<artifactId>Slimefun</artifactId>
<!-- Our default version will be UNOFFICIAL, this will prevent the auto updater -->
<!-- from overriding our local test file -->
<version>4.3-UNOFFICIAL</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Bukkit properties -->
<bukkit.version>1.15.2</bukkit.version>
<bukkit.javadocs>https://hub.spigotmc.org/javadocs/bukkit/</bukkit.javadocs>
<!-- Default settings for sonarcloud.io -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>me.mrCookieSlime</groupId>
<artifactId>Slimefun</artifactId>
<!-- Our default version will be UNOFFICIAL, this will prevent the auto
updater -->
<!-- from overriding our local test file -->
<version>4.3-UNOFFICIAL</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Bukkit properties -->
<bukkit.version>1.15.2</bukkit.version>
<bukkit.javadocs>https://hub.spigotmc.org/javadocs/bukkit/</bukkit.javadocs>
<!-- Default settings for sonarcloud.io -->
<sonar.projectKey>TheBusyBiscuit_Slimefun4</sonar.projectKey>
<sonar.organization>thebusybiscuit-github</sonar.organization>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
</properties>
<repositories>
<repository>
<id>paper-repo</id>
<url>https://repo.destroystokyo.com/repository/maven-public/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>worldedit-worldguard-repo</id>
<url>https://maven.sk89q.com/repo/</url>
</repository>
<repository>
<id>bStats-repo</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
<repository>
<id>placeholderapi-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<finalName>${project.name} v${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<!-- package info files are only important to the Javadocs -->
<!-- We can exclude them from the final jar -->
<exclude>**/package-info.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<!-- Shade bStats and CS-CoreLib2 into the output jar -->
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>me.mrCookieSlime.Slimefun.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>io.github.thebusybiscuit.cscorelib2</pattern>
<shadedPattern>me.mrCookieSlime.Slimefun.cscorelib2</shadedPattern>
</relocation>
</relocations>
<!-- Exclude unneeded metadata files from shaded dependencies -->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</properties>
<repositories>
<repository>
<id>paper-repo</id>
<url>https://repo.destroystokyo.com/repository/maven-public/</url>
</repository>
<repository>
<id>spigot-repo</id>
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
</repository>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
<repository>
<id>worldedit-worldguard-repo</id>
<url>https://maven.sk89q.com/repo/</url>
</repository>
<repository>
<id>bStats-repo</id>
<url>https://repo.codemc.org/repository/maven-public</url>
</repository>
<repository>
<id>placeholderapi-repo</id>
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
</repository>
</repositories>
<build>
<sourceDirectory>${project.basedir}/src/main/java</sourceDirectory>
<finalName>${project.name} v${project.version}</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<excludes>
<!-- package info files are only important to the Javadocs -->
<!-- We can exclude them from the final jar -->
<exclude>**/package-info.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.3</version>
<configuration>
<!-- Shade bStats and CS-CoreLib2 into the output jar -->
<relocations>
<relocation>
<pattern>org.bstats</pattern>
<shadedPattern>me.mrCookieSlime.Slimefun.bstats</shadedPattern>
</relocation>
<relocation>
<pattern>io.github.thebusybiscuit.cscorelib2</pattern>
<shadedPattern>me.mrCookieSlime.Slimefun.cscorelib2</shadedPattern>
</relocation>
</relocations>
<!-- Exclude unneeded metadata files from shaded dependencies -->
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.2.0</version>
<configuration>
<reportOutputDirectory>${project.basedir}</reportOutputDirectory>
<destDir>docs</destDir>
<doctitle>Slimefun4 - Javadocs</doctitle>
<windowtitle>Slimefun4 - Javadocs</windowtitle>
<detectOfflineLinks>false</detectOfflineLinks>
<additionalJOption>-html5</additionalJOption>
<!-- We can reference Bukkit's API in our Javadocs -->
<links>
<link>${bukkit.javadocs}</link>
</links>
<!-- We can group pakages together in our Javadocs -->
<groups>
<group>
@ -177,108 +180,108 @@
</groups>
</configuration>
</plugin>
</plugins>
<resources>
<!-- Resources we want to include, such as configs or language files -->
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
<include>languages/*</include>
</includes>
</resource>
<!-- We also want to include our LICENSE file -->
<resource>
<directory>${basedir}</directory>
<includes>
<include>LICENSE</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<!-- Hard dependencies -->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>${bukkit.version}-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TheBusyBiscuit</groupId>
<artifactId>CS-CoreLib</artifactId>
<version>8081bb4fe4</version>
<scope>provided</scope>
</dependency>
<!-- Development / Testing dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.seeseemelk</groupId>
<artifactId>MockBukkit</artifactId>
<version>v1.15-SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
<!-- Shaded packages -->
<dependency>
<groupId>com.github.thebusybiscuit</groupId>
<artifactId>CS-CoreLib2</artifactId>
<version>0.17</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
<!-- Third party plugin integrations -->
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TheBusyBiscuit</groupId>
<artifactId>EmeraldEnchants2</artifactId>
<version>3cd370b5d8</version>
<scope>provided</scope>
</dependency>
<!-- System dependency, this project has no maven repository -->
<dependency>
<groupId>me.minebuilders</groupId>
<artifactId>clearlag-core</artifactId>
<version>2.9.7</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/Clearlag.jar</systemPath>
</dependency>
</dependencies>
</plugins>
<resources>
<!-- Resources we want to include, such as configs or language files -->
<resource>
<directory>${basedir}/src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>*</include>
<include>languages/*</include>
</includes>
</resource>
<!-- We also want to include our LICENSE file -->
<resource>
<directory>${basedir}</directory>
<includes>
<include>LICENSE</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<!-- Hard dependencies -->
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>${bukkit.version}-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TheBusyBiscuit</groupId>
<artifactId>CS-CoreLib</artifactId>
<version>8081bb4fe4</version>
<scope>provided</scope>
</dependency>
<!-- Development / Testing dependencies -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.seeseemelk</groupId>
<artifactId>MockBukkit</artifactId>
<version>v1.15-279489afe1-1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.3.3</version>
<scope>test</scope>
</dependency>
<!-- Shaded packages -->
<dependency>
<groupId>com.github.thebusybiscuit</groupId>
<artifactId>CS-CoreLib2</artifactId>
<version>0.17</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
<version>1.7</version>
<scope>compile</scope>
</dependency>
<!-- Third party plugin integrations -->
<dependency>
<groupId>com.sk89q.worldedit</groupId>
<artifactId>worldedit-bukkit</artifactId>
<version>7.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.10.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.github.TheBusyBiscuit</groupId>
<artifactId>EmeraldEnchants2</artifactId>
<version>3cd370b5d8</version>
<scope>provided</scope>
</dependency>
<!-- System dependency, this project has no maven repository -->
<dependency>
<groupId>me.minebuilders</groupId>
<artifactId>clearlag-core</artifactId>
<version>2.9.7</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/Clearlag.jar</systemPath>
</dependency>
</dependencies>
</project>

View File

@ -11,14 +11,7 @@ import me.mrCookieSlime.Slimefun.SlimefunPlugin;
*
*/
public enum MinecraftVersion {
/**
* This is a very special state that represents the environment being a Unit
* Test and not an actual running Minecraft Server. This constant stands at
* the very top because it is the one with the least features.
*/
UNIT_TEST("Unit Test Environment"),
/**
* This constant represents Minecraft (Java Edition) Version 1.14
* (The Update Aquatic)
@ -41,7 +34,13 @@ public enum MinecraftVersion {
* This constant represents an exceptional state in which we were unable
* to identify the Minecraft Version we are using
*/
UNKNOWN("Unknown");
UNKNOWN("Unknown"),
/**
* This is a very special state that represents the environment being a Unit
* Test and not an actual running Minecraft Server.
*/
UNIT_TEST("Unit Test Environment");
private final String name;
private final String prefix;

View File

@ -5,7 +5,7 @@ import io.github.thebusybiscuit.slimefun4.api.SlimefunAddon;
/**
* A {@link PrematureCodeException} is thrown when a {@link SlimefunAddon} tried
* to access Slimefun code before Slimefun was enabled.
* Always let your code inside onEnable() or later, never on class initialization.
* Always let your code run inside onEnable() or later, never on class initialization.
*
* @author TheBusyBiscuit
*

View File

@ -10,6 +10,7 @@ import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
@ -40,17 +41,21 @@ public class IronGolemListener implements Listener {
item = inv.getItemInOffHand();
}
if (item != null && item.getType() == Material.IRON_INGOT && SlimefunItem.getByItem(item) != null) {
e.setCancelled(true);
SlimefunPlugin.getLocal().sendMessage(e.getPlayer(), "messages.no-iron-golem-heal");
if (item != null && item.getType() == Material.IRON_INGOT) {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
// This is just there to update the Inventory...
// Somehow cancelling it isn't enough.
if (e.getHand() == EquipmentSlot.HAND) {
inv.setItemInMainHand(item);
}
else if (e.getHand() == EquipmentSlot.OFF_HAND) {
inv.setItemInOffHand(item);
if (sfItem != null && !(sfItem instanceof VanillaItem)) {
e.setCancelled(true);
SlimefunPlugin.getLocal().sendMessage(e.getPlayer(), "messages.no-iron-golem-heal");
// This is just there to update the Inventory...
// Somehow cancelling it isn't enough.
if (e.getHand() == EquipmentSlot.HAND) {
inv.setItemInMainHand(item);
}
else if (e.getHand() == EquipmentSlot.OFF_HAND) {
inv.setItemInOffHand(item);
}
}
}
}

View File

@ -14,9 +14,8 @@ import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Lists.SlimefunItems;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
/**
@ -39,7 +38,7 @@ public class VanillaMachinesListener implements Listener {
public void onGrindstone(InventoryClickEvent e) {
// The Grindstone was only ever added in MC 1.14
MinecraftVersion minecraftVersion = SlimefunPlugin.getMinecraftVersion();
if (minecraftVersion != MinecraftVersion.UNIT_TEST && !minecraftVersion.isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
if (!minecraftVersion.isAtLeast(MinecraftVersion.MINECRAFT_1_14)) {
return;
}
@ -60,7 +59,7 @@ public class VanillaMachinesListener implements Listener {
SlimefunItem sfItem = SlimefunItem.getByItem(item);
if (sfItem != null && !sfItem.isUseableInWorkbench()) {
e.setCancelled(true);
e.setResult(Result.DENY);
SlimefunPlugin.getLocal().sendMessage((Player) e.getWhoClicked(), "workbench.not-enhanced", true);
break;
}
@ -87,7 +86,7 @@ public class VanillaMachinesListener implements Listener {
ItemStack item1 = e.getInventory().getContents()[0];
ItemStack item2 = e.getInventory().getContents()[1];
if (!SlimefunUtils.isItemSimilar(item1, SlimefunItems.ELYTRA, true) && checkForUnallowedItems(item1, item2)) {
if (checkForUnallowedItems(item1, item2)) {
e.setCancelled(true);
SlimefunPlugin.getLocal().sendMessage((Player) e.getWhoClicked(), "anvil.not-working", true);
}
@ -111,11 +110,15 @@ public class VanillaMachinesListener implements Listener {
SlimefunItem sfItem1 = SlimefunItem.getByItem(item1);
SlimefunItem sfItem2 = SlimefunItem.getByItem(item2);
if ((sfItem1 != null && !sfItem1.isDisabled()) || (sfItem2 != null && !sfItem2.isDisabled())) {
if (isUnallowed(sfItem1) || isUnallowed(sfItem2)) {
return true;
}
}
return false;
}
private boolean isUnallowed(SlimefunItem item) {
return item != null && !(item instanceof VanillaItem) && !item.isDisabled();
}
}

View File

@ -1,13 +1,15 @@
package io.github.thebusybiscuit.slimefun4.mocks;
import static org.mockito.Mockito.when;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.mockito.Mockito;
import static org.mockito.Mockito.when;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
@ -31,4 +33,10 @@ public final class SlimefunMocks {
return new MockSlimefunItem(category, item, id);
}
public static VanillaItem mockVanillaItem(Material type, boolean enabled) {
VanillaItem item = new VanillaItem(category, new ItemStack(type), type.name(), null, new ItemStack[9]);
SlimefunPlugin.getItemCfg().setValue(type.name() + ".enabled", enabled);
return item;
}
}

View File

@ -9,6 +9,7 @@ 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;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.ItemState;
@ -38,6 +39,14 @@ public class TestSlimefunItemRegistration {
Assertions.assertEquals(ItemState.ENABLED, item.getState());
}
@Test
public void testVanillaItemFallback() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.ACACIA_SIGN, false);
item.register(plugin);
Assertions.assertEquals(ItemState.VANILLA_FALLBACK, item.getState());
}
@Test
public void testIdConflict() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("DUPLICATE_ID", new CustomItem(Material.DIAMOND, "&cTest"));

View File

@ -16,6 +16,7 @@ import org.mockito.Mockito;
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.implementation.listeners.IronGolemListener;
import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
@ -76,6 +77,7 @@ public class TestIronGolemListener {
public void testWithSlimefunIron() {
SlimefunItem slimefunItem = SlimefunMocks.mockSlimefunItem("SLIMEFUN_IRON", new CustomItem(Material.IRON_INGOT, "&cSlimefun Iron"));
slimefunItem.register(plugin);
// The Event should be cancelled, we do not wanna use Slimefun Items for this
PlayerInteractEntityEvent event = callIronGolemEvent(EquipmentSlot.HAND, slimefunItem.getItem());
Assertions.assertTrue(event.isCancelled());
@ -84,4 +86,16 @@ public class TestIronGolemListener {
Assertions.assertTrue(event2.isCancelled());
}
@Test
public void testWithVanillaIron() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.IRON_INGOT, true);
item.register(plugin);
PlayerInteractEntityEvent event = callIronGolemEvent(EquipmentSlot.HAND, item.getItem());
Assertions.assertFalse(event.isCancelled());
PlayerInteractEntityEvent event2 = callIronGolemEvent(EquipmentSlot.OFF_HAND, item.getItem());
Assertions.assertFalse(event2.isCancelled());
}
}

View File

@ -1,27 +1,39 @@
package io.github.thebusybiscuit.slimefun4.tests.listeners;
import org.apache.commons.lang.mutable.MutableObject;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.event.Event.Result;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.CraftItemEvent;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.inventory.InventoryType.SlotType;
import org.bukkit.event.inventory.PrepareItemCraftEvent;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.Recipe;
import org.bukkit.inventory.ShapedRecipe;
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 be.seeseemelk.mockbukkit.inventory.meta.EnchantedBookMetaMock;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide;
import io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideLayout;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import io.github.thebusybiscuit.slimefun4.implementation.listeners.VanillaMachinesListener;
import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class TestVanillaMachinesListener {
@ -41,15 +53,127 @@ public class TestVanillaMachinesListener {
MockBukkit.unmock();
}
@Test
public void testGrindStoneWithoutSlimefunItems() {
private InventoryClickEvent mockGrindStoneEvent(ItemStack item) {
Player player = server.addPlayer();
Inventory inv = SlimefunMocks.mockInventory(InventoryType.GRINDSTONE, new ItemStack(Material.ENCHANTED_BOOK), null);
Inventory inv = SlimefunMocks.mockInventory(InventoryType.GRINDSTONE, item, null);
InventoryView view = player.openInventory(inv);
InventoryClickEvent event = new InventoryClickEvent(view, SlotType.CONTAINER, 2, ClickType.LEFT, InventoryAction.PICKUP_ONE);
listener.onGrindstone(event);
listener.onGrindstone(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();
CraftingInventory inv = Mockito.mock(CraftingInventory.class);
Mockito.when(inv.getContents()).thenReturn(new ItemStack[] { item, null, null, null, null, null, null, null, null });
InventoryView view = player.openInventory(inv);
CraftItemEvent event = new CraftItemEvent(recipe, view, SlotType.RESULT, 9, ClickType.LEFT, InventoryAction.PICKUP_ALL);
listener.onCraft(event);
return event;
}
private PrepareItemCraftEvent mockPreCraftingEvent(ItemStack item) {
Player player = server.addPlayer();
CraftingInventory inv = Mockito.mock(CraftingInventory.class);
MutableObject result = new MutableObject(new ItemStack(Material.EMERALD));
Mockito.doAnswer(invocation -> {
ItemStack argument = invocation.getArgument(0);
result.setValue(argument);
return null;
}).when(inv).setResult(Mockito.any());
Mockito.when(inv.getResult()).thenAnswer(invocation -> result.getValue());
Mockito.when(inv.getContents()).thenReturn(new ItemStack[] { null, null, item, null, null, null, null, null, null });
InventoryView view = player.openInventory(inv);
PrepareItemCraftEvent event = new PrepareItemCraftEvent(inv, view, false);
listener.onPrepareCraft(event);
return event;
}
@Test
public void testGrindStoneWithoutSlimefunItems() {
InventoryClickEvent event = mockGrindStoneEvent(new ItemStack(Material.ENCHANTED_BOOK));
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testGrindStoneWithSlimefunItems() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("ENCHANTED_MOCK_BOOK", new CustomItem(Material.ENCHANTED_BOOK, "&6Mock"));
item.register(plugin);
InventoryClickEvent event = mockGrindStoneEvent(item.getItem());
Assertions.assertEquals(Result.DENY, event.getResult());
}
@Test
public void testGrindStoneWithVanillaItem() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.ENCHANTED_BOOK, true);
item.register(plugin);
InventoryClickEvent event = mockGrindStoneEvent(item.getItem());
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testGrindStoneWithSlimefunGuide() {
InventoryClickEvent event = mockGrindStoneEvent(SlimefunGuide.getItem(SlimefunGuideLayout.CHEST));
Assertions.assertEquals(Result.DENY, event.getResult());
}
@Test
public void testCraftEventWithoutSlimefunItems() {
CraftItemEvent event = mockCraftingEvent(new ItemStack(Material.DIAMOND));
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testCraftEventWithSlimefunItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCK_DIAMOND", new CustomItem(Material.DIAMOND, "&cMock Diamond"));
item.register(plugin);
CraftItemEvent event = mockCraftingEvent(item.getItem());
Assertions.assertEquals(Result.DENY, event.getResult());
}
@Test
public void testCraftEventWithVanillaItem() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.DIAMOND, true);
item.register(plugin);
CraftItemEvent event = mockCraftingEvent(item.getItem());
Assertions.assertEquals(Result.DEFAULT, event.getResult());
}
@Test
public void testPreCraftEventWithoutSlimefunItems() {
PrepareItemCraftEvent event = mockPreCraftingEvent(new ItemStack(Material.DIAMOND));
Assertions.assertNotNull(event.getInventory().getResult());
}
@Test
public void testPreCraftEventWithSlimefunItem() {
SlimefunItem item = SlimefunMocks.mockSlimefunItem("MOCK_DIAMOND2", new CustomItem(Material.DIAMOND, "&cMock Diamond"));
item.register(plugin);
PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem());
Assertions.assertNull(event.getInventory().getResult());
}
@Test
public void testPreCraftEventWithVanillaItem() {
VanillaItem item = SlimefunMocks.mockVanillaItem(Material.GOLD_INGOT, true);
item.register(plugin);
PrepareItemCraftEvent event = mockPreCraftingEvent(item.getItem());
Assertions.assertNotNull(event.getInventory().getResult());
}
}