Search in sources :

Example 51 with Slimefun

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

the class AutoCrafterListener method onInteract.

@EventHandler
public void onInteract(PlayerRightClickEvent e) {
    Optional<Block> clickedBlock = e.getClickedBlock();
    // We want to make sure we used the main hand, the interaction was not cancelled and a Block was clicked.
    if (e.getHand() == EquipmentSlot.HAND && e.useBlock() != Result.DENY && clickedBlock.isPresent()) {
        Optional<SlimefunItem> slimefunBlock = e.getSlimefunBlock();
        // Check if the clicked Block is a Slimefun block.
        if (!slimefunBlock.isPresent()) {
            return;
        }
        SlimefunItem block = slimefunBlock.get();
        if (block instanceof AbstractAutoCrafter) {
            Optional<SlimefunItem> slimefunItem = e.getSlimefunItem();
            if (!e.getPlayer().isSneaking() && slimefunItem.isPresent() && slimefunItem.get() instanceof Multimeter) {
                // Allow Multimeters to pass through and do their job
                return;
            }
            // Prevent blocks from being placed, food from being eaten, etc...
            e.cancel();
            // Check for the "doLimitedCrafting" gamerule when using a Vanilla Auto-Crafter
            if (block instanceof VanillaAutoCrafter) {
                boolean doLimitedCrafting = e.getPlayer().getWorld().getGameRuleValue(GameRule.DO_LIMITED_CRAFTING);
                // Check if the recipe of the item is disabled.
                if (doLimitedCrafting && !hasUnlockedRecipe(e.getPlayer(), e.getItem())) {
                    Slimefun.getLocalization().sendMessage(e.getPlayer(), "messages.auto-crafting.recipe-unavailable");
                    return;
                }
            }
            // Fixes 2896 - Forward the interaction before items get handled.
            AbstractAutoCrafter crafter = (AbstractAutoCrafter) block;
            try {
                crafter.onRightClick(clickedBlock.get(), e.getPlayer());
            } catch (Exception | LinkageError x) {
                crafter.error("Something went wrong while right-clicking an Auto-Crafter", x);
            }
        }
    }
}
Also used : Block(org.bukkit.block.Block) VanillaAutoCrafter(io.github.thebusybiscuit.slimefun4.implementation.items.autocrafters.VanillaAutoCrafter) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) Multimeter(io.github.thebusybiscuit.slimefun4.implementation.items.electric.gadgets.Multimeter) AbstractAutoCrafter(io.github.thebusybiscuit.slimefun4.implementation.items.autocrafters.AbstractAutoCrafter) EventHandler(org.bukkit.event.EventHandler)

Example 52 with Slimefun

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

the class MiddleClickListener method onInventoryCreativeEvent.

/*
     * General Discloser: this event has really really really weird behavior on middle click.
     * Has been tested thoroughly to make sure it doesnt break anything else.
     */
@EventHandler
public void onInventoryCreativeEvent(InventoryCreativeEvent e) {
    /*
         * When clicking outside of an inventory with middle click,
         * ClickType is not MIDDLE but CREATIVE (because this ClickType covers
         * multiple cases, we have to filter out more later on)
         */
    if (e.getClick() == ClickType.CREATIVE && e.getSlotType() == SlotType.QUICKBAR) {
        HumanEntity player = e.getWhoClicked();
        // get the block the player is looking at for later
        Block b = player.getTargetBlockExact(5);
        // Fixes: #3483
        if (b == null || !isActualMiddleClick(e, b)) {
            return;
        }
        // find the actual slimefun item the user is looking at
        SlimefunItem sfItem = BlockStorage.check(b);
        // vanilla block -> ignore
        if (sfItem == null) {
            return;
        }
        /*
             * Before giving the item to the user, check if you can swap
             * to the item instead (user already has item in hotbar).
             * This is sometimes bypassed by the client itself (not fixable though).
             */
        for (int i = 0; i < 9; i++) {
            if (sfItem.isItem(player.getInventory().getItem(i))) {
                player.getInventory().setHeldItemSlot(i);
                // Has to be cancelled in order for it to work properly.
                e.setCancelled(true);
                return;
            }
        }
        // Give the item, doing it like this will not alter any other cases.
        e.setCursor(sfItem.getItem().clone());
    }
}
Also used : HumanEntity(org.bukkit.entity.HumanEntity) Block(org.bukkit.block.Block) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) EventHandler(org.bukkit.event.EventHandler)

Example 53 with Slimefun

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

the class GiveRecipe method execute.

@Override
protected void execute(@Nonnull CommandSender sender, @Nonnull String[] args) {
    if (!(sender instanceof Player)) {
        sender.sendMessage("Only players can use this!");
        return;
    }
    if (args.length != 1) {
        sender.sendMessage("Usage: /ie giverecipe <ID>");
        return;
    }
    SlimefunItem sfItem = SlimefunItem.getById(args[0].toUpperCase());
    if (sfItem == null || sfItem instanceof MultiBlockMachine || sfItem.getRecipeType() == RecipeType.GEO_MINER) {
        sender.sendMessage(ChatColor.RED + "Invalid Slimefun item!");
        return;
    }
    sender.sendMessage(ChatColor.GREEN + "Gave recipe for " + sfItem.getItemName());
    Player p = (Player) sender;
    List<ItemStack> recipe = new ArrayList<>();
    for (ItemStack e : sfItem.getRecipe()) {
        if (e != null) {
            recipe.add(e);
        }
    }
    p.getInventory().addItem(recipe.toArray(new ItemStack[0]));
}
Also used : Player(org.bukkit.entity.Player) ArrayList(java.util.ArrayList) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) ItemStack(org.bukkit.inventory.ItemStack) MultiBlockMachine(io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine)

Example 54 with Slimefun

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

the class AdvancedAnvil method craft.

private void craft(BlockMenu inv, Block b, Player p) {
    Location l = b.getLocation();
    if (getCharge(l) < this.energy) {
        // not enough energy
        p.sendMessage(ChatColor.RED + "Not enough energy!", ChatColor.GREEN + "Charge: " + ChatColor.RED + getCharge(l) + ChatColor.GREEN + "/" + this.energy + " J");
        return;
    }
    ItemStack item1 = inv.getItemInSlot(INPUT_SLOTS[0]);
    SlimefunItem sfItem1 = SlimefunItem.getByItem(inv.getItemInSlot(INPUT_SLOTS[0]));
    ItemStack item2 = inv.getItemInSlot(INPUT_SLOTS[1]);
    SlimefunItem sfItem2 = SlimefunItem.getByItem(inv.getItemInSlot(INPUT_SLOTS[1]));
    if (item1 == null || item2 == null || (item2.getType() != Material.ENCHANTED_BOOK && item1.getType() != item2.getType())) {
        p.sendMessage(ChatColor.RED + "Invalid items!");
        return;
    }
    if (sfItem2 != null && !sfItem2.isDisenchantable()) {
        p.sendMessage(ChatColor.RED + "Slimefun item is not disenchantable!");
        return;
    }
    if (sfItem1 != null && !sfItem1.isEnchantable()) {
        p.sendMessage(ChatColor.RED + "Slimefun item is not enchantable!");
        return;
    }
    ItemStack output = getOutput(item1, item2);
    if (output == null) {
        p.sendMessage(ChatColor.RED + "No upgrades!");
        return;
    }
    if (!inv.fits(output, OUTPUT_SLOTS)) {
        p.sendMessage(ChatColor.GOLD + "Not enough room!");
        return;
    }
    p.playSound(l, Sound.BLOCK_ANVIL_USE, 1, 1);
    item1.setAmount(item1.getAmount() - 1);
    item2.setAmount(item2.getAmount() - 1);
    inv.pushItem(output, OUTPUT_SLOTS);
    removeCharge(l, this.energy);
    update(inv);
}
Also used : SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) CustomItemStack(io.github.thebusybiscuit.slimefun4.libraries.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) Location(org.bukkit.Location)

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