1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00
This commit is contained in:
TheBusyBiscuit 2020-12-01 11:46:59 +01:00
parent b4edc6c5bf
commit ac7bb44d98
5 changed files with 207 additions and 138 deletions

View File

@ -83,6 +83,7 @@
* Fixed color in android script downloading screen * Fixed color in android script downloading screen
* Fixed #2576 * Fixed #2576
* Fixed #2496 * Fixed #2496
* Fixed #2585
## Release Candidate 17 (17 Oct 2020) ## Release Candidate 17 (17 Oct 2020)

View File

@ -2,14 +2,15 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.seasonal;
import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.ThreadLocalRandom;
import javax.annotation.ParametersAreNonnullByDefault;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils; import io.github.thebusybiscuit.cscorelib2.inventory.ItemUtils;
import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable; import io.github.thebusybiscuit.slimefun4.core.attributes.NotPlaceable;
import io.github.thebusybiscuit.slimefun4.core.handlers.BlockPlaceHandler; import io.github.thebusybiscuit.slimefun4.core.handlers.ItemUseHandler;
import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem; import io.github.thebusybiscuit.slimefun4.implementation.items.SimpleSlimefunItem;
import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils; import io.github.thebusybiscuit.slimefun4.utils.FireworkUtils;
import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Lists.RecipeType;
@ -26,10 +27,11 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
* @see EasterEgg * @see EasterEgg
* *
*/ */
public class ChristmasPresent extends SimpleSlimefunItem<BlockPlaceHandler> implements NotPlaceable { public class ChristmasPresent extends SimpleSlimefunItem<ItemUseHandler> implements NotPlaceable {
private final ItemStack[] gifts; private final ItemStack[] gifts;
@ParametersAreNonnullByDefault
public ChristmasPresent(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack... gifts) { public ChristmasPresent(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, ItemStack... gifts) {
super(category, item, recipeType, recipe); super(category, item, recipeType, recipe);
@ -37,24 +39,21 @@ public class ChristmasPresent extends SimpleSlimefunItem<BlockPlaceHandler> impl
} }
@Override @Override
public BlockPlaceHandler getItemHandler() { public ItemUseHandler getItemHandler() {
return new BlockPlaceHandler(false) { return e -> {
e.cancel();
@Override
public void onPlayerPlace(BlockPlaceEvent e) {
e.setCancelled(true);
e.getClickedBlock().ifPresent(block -> {
if (e.getPlayer().getGameMode() != GameMode.CREATIVE) { if (e.getPlayer().getGameMode() != GameMode.CREATIVE) {
ItemUtils.consumeItem(e.getItemInHand(), false); ItemUtils.consumeItem(e.getItem(), false);
} }
FireworkUtils.launchRandom(e.getPlayer(), 3); FireworkUtils.launchRandom(e.getPlayer(), 3);
Block b = e.getBlock(); Block b = block.getRelative(e.getClickedFace());
ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone(); ItemStack gift = gifts[ThreadLocalRandom.current().nextInt(gifts.length)].clone();
b.getWorld().dropItemNaturally(b.getLocation(), gift); b.getWorld().dropItemNaturally(b.getLocation(), gift);
} });
}; };
} }

View File

@ -979,6 +979,20 @@ public class SlimefunItem implements Placeable {
} }
} }
@Override
public final boolean equals(Object obj) {
if (obj instanceof SlimefunItem) {
return ((SlimefunItem) obj).getId().equals(getId());
} else {
return false;
}
}
@Override
public final int hashCode() {
return getId().hashCode();
}
@Nullable @Nullable
public static SlimefunItem getByID(@Nonnull String id) { public static SlimefunItem getByID(@Nonnull String id) {
return SlimefunPlugin.getRegistry().getSlimefunItemIds().get(id); return SlimefunPlugin.getRegistry().getSlimefunItemIds().get(id);
@ -1028,4 +1042,5 @@ public class SlimefunItem implements Placeable {
public static void registerBlockHandler(String id, SlimefunBlockHandler handler) { public static void registerBlockHandler(String id, SlimefunBlockHandler handler) {
SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler); SlimefunPlugin.getRegistry().getBlockHandlers().put(id, handler);
} }
} }

View File

@ -0,0 +1,179 @@
package io.github.thebusybiscuit.slimefun4.testing.tests.items;
import java.util.Optional;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import be.seeseemelk.mockbukkit.MockBukkit;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.testing.TestUtilities;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
class TestSlimefunItem {
private static SlimefunPlugin plugin;
@BeforeAll
public static void load() {
MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
@Test
@DisplayName("Test wiki pages getting assigned correctly")
void testWikiPages() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "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/Slimefun/Slimefun4/wiki/Test", wiki.get());
}
@Test
@DisplayName("Test SlimefunItem registering Recipes properly")
void testRecipe() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TEST", new CustomItem(Material.DIAMOND, "&dAnother one bites the test"));
ItemStack[] recipe = { null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null };
item.setRecipe(recipe);
item.register(plugin);
Assertions.assertArrayEquals(recipe, item.getRecipe());
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(null));
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[3]));
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[20]));
}
@Test
@DisplayName("Test Recipe outputs being handled correctly")
void testRecipeOutput() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "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
@DisplayName("Test Recipe Types being handled properly")
void testRecipeType() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TYPE_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
Assertions.assertNotNull(item.getRecipeType());
item.setRecipeType(RecipeType.ENHANCED_CRAFTING_TABLE);
Assertions.assertEquals(RecipeType.ENHANCED_CRAFTING_TABLE, item.getRecipeType());
item.setRecipeType(RecipeType.NULL);
Assertions.assertEquals(RecipeType.NULL, item.getRecipeType());
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipeType(null));
}
@ParameterizedTest
@DisplayName("Test SlimefunItem#isItem(...)")
@ValueSource(booleans = { true, false })
void testIsItem(boolean compatibility) {
CustomItem item = new CustomItem(Material.BEACON, "&cItem Test");
String id = "IS_ITEM_TEST" + (compatibility ? "_COMPATIBLE" : "");
SlimefunItem sfItem = TestUtilities.mockSlimefunItem(plugin, id, item);
sfItem.register(plugin);
Assertions.assertTrue(sfItem.isItem(sfItem.getItem()));
Assertions.assertFalse(sfItem.isItem(null));
Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON)));
Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest")));
if (compatibility) {
SlimefunPlugin.getRegistry().setBackwardsCompatible(true);
Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item));
Assertions.assertTrue(sfItem.isItem(item));
Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test")));
SlimefunPlugin.getRegistry().setBackwardsCompatible(false);
} else {
Assertions.assertFalse(sfItem.isItem(item));
Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test")));
}
Assertions.assertEquals(sfItem, SlimefunItem.getByItem(new SlimefunItemStack(sfItem.getId(), item)));
}
@Test
@DisplayName("Test WrongItemStackException")
void testWrongItemStackException() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WRONG_ITEMSTACK_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me"));
item.register(plugin);
item.load();
ItemStack itemStack = item.getItem();
Assertions.assertThrows(WrongItemStackException.class, () -> itemStack.setAmount(40));
}
@Test
@DisplayName("Test UnregisteredItemException")
void testUnregisteredItemException() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "UNREGISTERED_ITEM_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me"));
Assertions.assertThrows(UnregisteredItemException.class, () -> item.getAddon());
}
@Test
@DisplayName("Test SlimefunItem#equals(...)")
void testEquals() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal"));
SlimefunItem item2 = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal"));
SlimefunItem differentItem = TestUtilities.mockSlimefunItem(plugin, "I_AM_DIFFERENT", new CustomItem(Material.LANTERN, "&6We are equal"));
Assertions.assertEquals(item, item2);
Assertions.assertNotEquals(item, differentItem);
Assertions.assertNotEquals(item2, differentItem);
}
@Test
@DisplayName("Test SlimefunItem#hashCode()")
void testHashCode() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal"));
SlimefunItem item2 = TestUtilities.mockSlimefunItem(plugin, "EQUALS_TEST", new CustomItem(Material.LANTERN, "&6We are equal"));
SlimefunItem differentItem = TestUtilities.mockSlimefunItem(plugin, "I_AM_DIFFERENT", new CustomItem(Material.LANTERN, "&6We are equal"));
Assertions.assertEquals(item.hashCode(), item2.hashCode());
Assertions.assertNotEquals(item.hashCode(), differentItem.hashCode());
Assertions.assertNotEquals(item2.hashCode(), differentItem.hashCode());
}
}

View File

@ -1,31 +1,22 @@
package io.github.thebusybiscuit.slimefun4.testing.tests.registration; package io.github.thebusybiscuit.slimefun4.testing.tests.registration;
import java.util.Optional;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.MockBukkit;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem; import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.api.exceptions.IdConflictException; import io.github.thebusybiscuit.slimefun4.api.exceptions.IdConflictException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.UnregisteredItemException;
import io.github.thebusybiscuit.slimefun4.api.exceptions.WrongItemStackException;
import io.github.thebusybiscuit.slimefun4.api.items.ItemState; import io.github.thebusybiscuit.slimefun4.api.items.ItemState;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem; import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import io.github.thebusybiscuit.slimefun4.testing.TestUtilities; import io.github.thebusybiscuit.slimefun4.testing.TestUtilities;
import me.mrCookieSlime.Slimefun.Lists.RecipeType;
import me.mrCookieSlime.Slimefun.Objects.Category; import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
import me.mrCookieSlime.Slimefun.api.SlimefunItemStack;
class TestSlimefunItemRegistration { class TestSlimefunItemRegistration {
@ -69,24 +60,6 @@ class TestSlimefunItemRegistration {
Assertions.assertTrue(item.isDisabled()); Assertions.assertTrue(item.isDisabled());
} }
@Test
@DisplayName("Test wiki pages getting assigned correctly")
void testWikiPages() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "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/Slimefun/Slimefun4/wiki/Test", wiki.get());
}
@Test @Test
@DisplayName("Test VanillaItem falling back to vanilla.") @DisplayName("Test VanillaItem falling back to vanilla.")
void testVanillaItemFallback() { void testVanillaItemFallback() {
@ -111,86 +84,6 @@ class TestSlimefunItemRegistration {
Assertions.assertEquals(ItemState.UNREGISTERED, item2.getState()); Assertions.assertEquals(ItemState.UNREGISTERED, item2.getState());
} }
@Test
@DisplayName("Test SlimefunItem registering Recipes properly")
void testRecipe() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TEST", new CustomItem(Material.DIAMOND, "&dAnother one bites the test"));
ItemStack[] recipe = { null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null, null, new ItemStack(Material.DIAMOND), null };
item.setRecipe(recipe);
item.register(plugin);
Assertions.assertArrayEquals(recipe, item.getRecipe());
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(null));
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[3]));
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipe(new ItemStack[20]));
}
@Test
@DisplayName("Test Recipe outputs being handled correctly")
void testRecipeOutput() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "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
@DisplayName("Test Recipe Types being handled properly")
void testRecipeType() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "RECIPE_TYPE_TEST", new CustomItem(Material.DIAMOND, "&cTest"));
item.register(plugin);
Assertions.assertNotNull(item.getRecipeType());
item.setRecipeType(RecipeType.ENHANCED_CRAFTING_TABLE);
Assertions.assertEquals(RecipeType.ENHANCED_CRAFTING_TABLE, item.getRecipeType());
item.setRecipeType(RecipeType.NULL);
Assertions.assertEquals(RecipeType.NULL, item.getRecipeType());
Assertions.assertThrows(IllegalArgumentException.class, () -> item.setRecipeType(null));
}
@ParameterizedTest
@DisplayName("Test SlimefunItem#isItem(...)")
@ValueSource(booleans = { true, false })
void testIsItem(boolean compatibility) {
CustomItem item = new CustomItem(Material.BEACON, "&cItem Test");
String id = "IS_ITEM_TEST" + (compatibility ? "_COMPATIBLE" : "");
SlimefunItem sfItem = TestUtilities.mockSlimefunItem(plugin, id, item);
sfItem.register(plugin);
Assertions.assertTrue(sfItem.isItem(sfItem.getItem()));
Assertions.assertFalse(sfItem.isItem(null));
Assertions.assertFalse(sfItem.isItem(new ItemStack(Material.BEACON)));
Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.REDSTONE, "&cTest")));
if (compatibility) {
SlimefunPlugin.getRegistry().setBackwardsCompatible(true);
Assertions.assertEquals(sfItem, SlimefunItem.getByItem(item));
Assertions.assertTrue(sfItem.isItem(item));
Assertions.assertTrue(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test")));
SlimefunPlugin.getRegistry().setBackwardsCompatible(false);
} else {
Assertions.assertFalse(sfItem.isItem(item));
Assertions.assertFalse(sfItem.isItem(new CustomItem(Material.BEACON, "&cItem Test")));
}
Assertions.assertEquals(sfItem, SlimefunItem.getByItem(new SlimefunItemStack(sfItem.getId(), item)));
}
@Test @Test
@DisplayName("Test Category registration when registering an item") @DisplayName("Test Category registration when registering an item")
void testCategoryRegistration() { void testCategoryRegistration() {
@ -243,22 +136,4 @@ class TestSlimefunItemRegistration {
Assertions.assertTrue(item.isHidden()); Assertions.assertTrue(item.isHidden());
Assertions.assertFalse(category.contains(item)); Assertions.assertFalse(category.contains(item));
} }
@Test
@DisplayName("Test WrongItemStackException")
void testWrongItemStackException() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "WRONG_ITEMSTACK_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me"));
item.register(plugin);
item.load();
ItemStack itemStack = item.getItem();
Assertions.assertThrows(WrongItemStackException.class, () -> itemStack.setAmount(40));
}
@Test
@DisplayName("Test UnregisteredItemException")
void testUnregisteredItemException() {
SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "UNREGISTERED_ITEM_EXCEPTION", new CustomItem(Material.NETHER_STAR, "&4Do not modify me"));
Assertions.assertThrows(UnregisteredItemException.class, () -> item.getAddon());
}
} }