Search in sources :

Example 16 with Slimefun

use of io.github.thebusybiscuit.slimefun4.implementation.Slimefun in project Slimefun4 by Slimefun.

the class SlimefunUtils method canPlayerUseItem.

/**
 * This checks whether the {@link Player} is able to use the given {@link ItemStack}.
 * It will always return <code>true</code> for non-Slimefun items.
 * <p>
 * If you already have an instance of {@link SlimefunItem}, please use {@link SlimefunItem#canUse(Player, boolean)}.
 *
 * @param p
 *            The {@link Player}
 * @param item
 *            The {@link ItemStack} to check
 * @param sendMessage
 *            Whether to send a message response to the {@link Player}
 *
 * @return Whether the {@link Player} is able to use that item.
 */
public static boolean canPlayerUseItem(@Nonnull Player p, @Nullable ItemStack item, boolean sendMessage) {
    Validate.notNull(p, "The player cannot be null");
    SlimefunItem sfItem = SlimefunItem.getByItem(item);
    if (sfItem != null) {
        return sfItem.canUse(p, sendMessage);
    } else {
        return true;
    }
}
Also used : SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)

Example 17 with Slimefun

use of io.github.thebusybiscuit.slimefun4.implementation.Slimefun in project Slimefun4 by Slimefun.

the class SlimefunUtils method isItemSimilar.

public static boolean isItemSimilar(@Nullable ItemStack item, @Nullable ItemStack sfitem, boolean checkLore, boolean checkAmount) {
    if (item == null) {
        return sfitem == null;
    } else if (sfitem == null) {
        return false;
    } else if (item.getType() != sfitem.getType()) {
        return false;
    } else if (checkAmount && item.getAmount() < sfitem.getAmount()) {
        return false;
    } else if (sfitem instanceof SlimefunItemStack && item instanceof SlimefunItemStack) {
        return ((SlimefunItemStack) item).getItemId().equals(((SlimefunItemStack) sfitem).getItemId());
    } else if (item.hasItemMeta()) {
        Debug.log(TestCase.CARGO_INPUT_TESTING, "SlimefunUtils#isItemSimilar - item.hasItemMeta()");
        ItemMeta itemMeta = item.getItemMeta();
        if (sfitem instanceof SlimefunItemStack) {
            Optional<String> id = Slimefun.getItemDataService().getItemData(itemMeta);
            if (id.isPresent()) {
                return id.get().equals(((SlimefunItemStack) sfitem).getItemId());
            }
            ItemMetaSnapshot meta = ((SlimefunItemStack) sfitem).getItemMetaSnapshot();
            return equalsItemMeta(itemMeta, meta, checkLore);
        } else if (sfitem instanceof ItemStackWrapper && sfitem.hasItemMeta()) {
            Debug.log(TestCase.CARGO_INPUT_TESTING, "  is wrapper");
            /*
                 * Cargo optimization (PR #3258)
                 *
                 * Slimefun items may be ItemStackWrapper's in the context of cargo
                 * so let's try to do an ID comparison before meta comparison
                 */
            ItemMeta possibleSfItemMeta = sfitem.getItemMeta();
            Debug.log(TestCase.CARGO_INPUT_TESTING, "  sfitem is ItemStackWrapper - possible SF Item: {}", sfitem);
            // Prioritize SlimefunItem id comparison over ItemMeta comparison
            if (Slimefun.getItemDataService().hasEqualItemData(possibleSfItemMeta, itemMeta)) {
                Debug.log(TestCase.CARGO_INPUT_TESTING, "  Item IDs matched!");
                return true;
            } else {
                Debug.log(TestCase.CARGO_INPUT_TESTING, "  Item IDs don't match, checking meta {} == {} (lore: {})", itemMeta, possibleSfItemMeta, checkLore);
                return equalsItemMeta(itemMeta, possibleSfItemMeta, checkLore);
            }
        } else if (sfitem.hasItemMeta()) {
            ItemMeta sfItemMeta = sfitem.getItemMeta();
            Debug.log(TestCase.CARGO_INPUT_TESTING, "  Comparing meta (vanilla items?) - {} == {} (lore: {})", itemMeta, sfItemMeta, checkLore);
            return equalsItemMeta(itemMeta, sfItemMeta, checkLore);
        } else {
            return false;
        }
    } else {
        return !sfitem.hasItemMeta();
    }
}
Also used : Optional(java.util.Optional) ItemStackWrapper(io.github.thebusybiscuit.slimefun4.utils.itemstack.ItemStackWrapper) ItemMeta(org.bukkit.inventory.meta.ItemMeta) ItemMetaSnapshot(io.github.bakedlibs.dough.items.ItemMetaSnapshot) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)

Example 18 with Slimefun

use of io.github.thebusybiscuit.slimefun4.implementation.Slimefun in project Slimefun4 by Slimefun.

the class TestFireworksListener method load.

@BeforeAll
public static void load() {
    server = MockBukkit.mock();
    Slimefun plugin = MockBukkit.load(Slimefun.class);
    new FireworksListener(plugin);
}
Also used : Slimefun(io.github.thebusybiscuit.slimefun4.implementation.Slimefun) FireworksListener(io.github.thebusybiscuit.slimefun4.implementation.listeners.entity.FireworksListener) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 19 with Slimefun

use of io.github.thebusybiscuit.slimefun4.implementation.Slimefun in project Slimefun4 by Slimefun.

the class TestSmithingTableListener method load.

@BeforeAll
public static void load() {
    server = MockBukkit.mock();
    Slimefun plugin = MockBukkit.load(Slimefun.class);
    listener = new SmithingTableListener(plugin);
    slimefunTool = TestUtilities.mockSlimefunItem(plugin, "MOCK_DIAMOND_SWORD", new CustomItemStack(Material.DIAMOND_SWORD, "&6Mock"));
    slimefunIngot = TestUtilities.mockSlimefunItem(plugin, "MOCK_NETHERITE_INGOT", new CustomItemStack(Material.NETHERITE_INGOT, "&6Mock"));
    vanillaTool = TestUtilities.mockVanillaItem(plugin, Material.DIAMOND_SWORD, true);
    vanillaIngot = TestUtilities.mockVanillaItem(plugin, Material.NETHERITE_INGOT, true);
    slimefunTool.register(plugin);
    slimefunIngot.register(plugin);
    vanillaTool.register(plugin);
    vanillaIngot.register(plugin);
}
Also used : SmithingTableListener(io.github.thebusybiscuit.slimefun4.implementation.listeners.crafting.SmithingTableListener) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) Slimefun(io.github.thebusybiscuit.slimefun4.implementation.Slimefun) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 20 with Slimefun

use of io.github.thebusybiscuit.slimefun4.implementation.Slimefun in project Slimefun4 by Slimefun.

the class TestCauldronListener method testCauldronWithSlimefunItem.

@Test
@DisplayName("Test Cauldron working as normal with non-leather slimefun items")
void testCauldronWithSlimefunItem() {
    SlimefunItem item = TestUtilities.mockSlimefunItem(plugin, "CAULDRON_TEST_MOCK", new CustomItemStack(Material.GOLDEN_APPLE, "&6Mock"));
    item.register(plugin);
    PlayerInteractEvent event = mockCauldronEvent(item.getItem());
    Assertions.assertEquals(Result.DEFAULT, event.useItemInHand());
}
Also used : PlayerInteractEvent(org.bukkit.event.player.PlayerInteractEvent) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)17 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)16 ItemStack (org.bukkit.inventory.ItemStack)14 DisplayName (org.junit.jupiter.api.DisplayName)14 Test (org.junit.jupiter.api.Test)14 Player (org.bukkit.entity.Player)12 CustomItemStack (io.github.bakedlibs.dough.items.CustomItemStack)10 Slimefun (io.github.thebusybiscuit.slimefun4.implementation.Slimefun)8 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)7 NamespacedKey (org.bukkit.NamespacedKey)7 PlayerProfile (io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile)6 ArrayList (java.util.ArrayList)6 Nonnull (javax.annotation.Nonnull)6 BeforeAll (org.junit.jupiter.api.BeforeAll)4 TagMisconfigurationException (io.github.thebusybiscuit.slimefun4.api.exceptions.TagMisconfigurationException)3 ItemGroup (io.github.thebusybiscuit.slimefun4.api.items.ItemGroup)3 Block (org.bukkit.block.Block)3 ItemMeta (org.bukkit.inventory.meta.ItemMeta)3 PotionEffect (org.bukkit.potion.PotionEffect)3 AndroidMineEvent (io.github.thebusybiscuit.slimefun4.api.events.AndroidMineEvent)2