Search in sources :

Example 1 with InventoryMoveItemEvent

use of org.bukkit.event.inventory.InventoryMoveItemEvent in project Arclight by IzzelAliz.

the class DropperBlockMixin method dispense.

/**
 * @author IzzelAliz
 * @reason
 */
@Overwrite
public void dispense(World worldIn, BlockPos pos) {
    ProxyBlockSource proxyblocksource = new ProxyBlockSource(worldIn, pos);
    DispenserTileEntity dispensertileentity = proxyblocksource.getBlockTileEntity();
    int i = dispensertileentity.getDispenseSlot();
    if (i < 0) {
        worldIn.playEvent(1001, pos, 0);
    } else {
        ItemStack itemstack = dispensertileentity.getStackInSlot(i);
        if (!itemstack.isEmpty() && net.minecraftforge.items.VanillaInventoryCodeHooks.dropperInsertHook(worldIn, pos, dispensertileentity, i, itemstack)) {
            Direction direction = worldIn.getBlockState(pos).get(DispenserBlock.FACING);
            IInventory iinventory = HopperTileEntity.getInventoryAtPosition(worldIn, pos.offset(direction));
            ItemStack itemstack1;
            if (iinventory == null) {
                itemstack1 = DISPENSE_BEHAVIOR.dispense(proxyblocksource, itemstack);
            } else {
                ItemStack split = itemstack.copy().split(1);
                CraftItemStack craftItemStack = CraftItemStack.asCraftMirror(split);
                Inventory destinationInventory;
                // Have to special case large chests as they work oddly
                if (iinventory instanceof DoubleSidedInventory) {
                    destinationInventory = new CraftInventoryDoubleChest((DoubleSidedInventory) iinventory);
                } else {
                    destinationInventory = ((IInventoryBridge) iinventory).getOwner().getInventory();
                }
                InventoryMoveItemEvent event = new InventoryMoveItemEvent(((IInventoryBridge) dispensertileentity).getOwner().getInventory(), craftItemStack, destinationInventory, true);
                Bukkit.getPluginManager().callEvent(event);
                if (event.isCancelled()) {
                    return;
                }
                itemstack1 = HopperTileEntity.putStackInInventoryAllSlots(dispensertileentity, iinventory, CraftItemStack.asNMSCopy(event.getItem()), direction.getOpposite());
                if (event.getItem().equals(craftItemStack) && itemstack1.isEmpty()) {
                    itemstack1 = itemstack.copy();
                    itemstack1.shrink(1);
                } else {
                    itemstack1 = itemstack.copy();
                }
            }
            dispensertileentity.setInventorySlotContents(i, itemstack1);
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) ProxyBlockSource(net.minecraft.dispenser.ProxyBlockSource) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) DispenserTileEntity(net.minecraft.tileentity.DispenserTileEntity) CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) ItemStack(net.minecraft.item.ItemStack) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) Direction(net.minecraft.util.Direction) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Overwrite(org.spongepowered.asm.mixin.Overwrite)

Example 2 with InventoryMoveItemEvent

use of org.bukkit.event.inventory.InventoryMoveItemEvent in project WindSpigot by Wind-Development.

the class TileEntityHopper method a.

private static boolean a(IHopper ihopper, IInventory iinventory, int i, EnumDirection enumdirection) {
    ItemStack itemstack = iinventory.getItem(i);
    if (itemstack != null && b(iinventory, itemstack, i, enumdirection)) {
        ItemStack itemstack1 = itemstack.cloneItemStack();
        // ItemStack itemstack2 = addItem(ihopper, iinventory.splitStack(i, 1),
        // (EnumDirection) null);
        // CraftBukkit start - Call event on collection of items from inventories into
        // the hopper
        CraftItemStack oitemstack = CraftItemStack.asCraftMirror(// Spigot
        iinventory.splitStack(i, ihopper.getWorld().spigotConfig.hopperAmount));
        // TacoSpigot start - add an option to turn of InventoryMoveItemEvent
        final org.bukkit.inventory.ItemStack stack;
        if (HopperHelper.isFireInventoryMoveItemEvent(ihopper)) {
            // TacoSpigot end
            Inventory sourceInventory;
            // Have to special case large chests as they work oddly
            if (iinventory instanceof InventoryLargeChest) {
                sourceInventory = new org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest((InventoryLargeChest) iinventory);
            } else {
                sourceInventory = iinventory.getOwner().getInventory();
            }
            InventoryMoveItemEvent event = new InventoryMoveItemEvent(sourceInventory, oitemstack.clone(), ihopper.getOwner().getInventory(), false);
            ihopper.getWorld().getServer().getPluginManager().callEvent(event);
            if (event.isCancelled()) {
                iinventory.setItem(i, itemstack1);
                if (ihopper instanceof TileEntityHopper) {
                    // Spigot
                    ((TileEntityHopper) ihopper).d(ihopper.getWorld().spigotConfig.hopperTransfer);
                } else if (ihopper instanceof EntityMinecartHopper) {
                    // Spigot
                    ((EntityMinecartHopper) ihopper).m(ihopper.getWorld().spigotConfig.hopperTransfer / 2);
                }
                return false;
            }
            // TacoSpigot start
            stack = event.getItem();
        // handle cases where the event is not fired
        } else {
            stack = oitemstack;
        }
        // Spigot
        int origCount = stack.getAmount();
        ItemStack itemstack2 = addItem(ihopper, CraftItemStack.asNMSCopy(stack), null);
        if (itemstack2 == null || itemstack2.count == 0) {
            if (stack.equals(oitemstack)) {
                iinventory.update();
            } else {
                iinventory.setItem(i, itemstack1);
            }
            // CraftBukkit end
            return true;
        }
        // Spigot
        itemstack1.count -= origCount - itemstack2.count;
        iinventory.setItem(i, itemstack1);
    }
    return false;
}
Also used : InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) CraftItemStack(org.bukkit.craftbukkit.inventory.CraftItemStack) CraftItemStack(org.bukkit.craftbukkit.inventory.CraftItemStack) Inventory(org.bukkit.inventory.Inventory)

Example 3 with InventoryMoveItemEvent

use of org.bukkit.event.inventory.InventoryMoveItemEvent in project NachoSpigot by CobbleSword.

the class BlockDropper method dispense.

public void dispense(World world, BlockPosition blockposition) {
    SourceBlock sourceblock = new SourceBlock(world, blockposition);
    TileEntityDispenser tileentitydispenser = (TileEntityDispenser) sourceblock.getTileEntity();
    if (tileentitydispenser != null) {
        int i = tileentitydispenser.m();
        if (i < 0) {
            world.triggerEffect(1001, blockposition, 0);
        } else {
            ItemStack itemstack = tileentitydispenser.getItem(i);
            if (itemstack != null) {
                EnumDirection enumdirection = (EnumDirection) world.getType(blockposition).get(BlockDropper.FACING);
                BlockPosition blockposition1 = blockposition.shift(enumdirection);
                IInventory iinventory = TileEntityHopper.b(world, (double) blockposition1.getX(), (double) blockposition1.getY(), (double) blockposition1.getZ());
                ItemStack itemstack1;
                if (iinventory == null) {
                    itemstack1 = this.P.a(sourceblock, itemstack);
                    if (itemstack1 != null && itemstack1.count <= 0) {
                        itemstack1 = null;
                    }
                } else {
                    // CraftBukkit start - Fire event when pushing items into other inventories
                    CraftItemStack oitemstack = CraftItemStack.asCraftMirror(itemstack.cloneItemStack().cloneAndSubtract(1));
                    org.bukkit.inventory.Inventory destinationInventory;
                    // Have to special case large chests as they work oddly
                    if (iinventory instanceof InventoryLargeChest) {
                        destinationInventory = new org.bukkit.craftbukkit.inventory.CraftInventoryDoubleChest((InventoryLargeChest) iinventory);
                    } else {
                        destinationInventory = iinventory.getOwner().getInventory();
                    }
                    InventoryMoveItemEvent event = new InventoryMoveItemEvent(tileentitydispenser.getOwner().getInventory(), oitemstack.clone(), destinationInventory, true);
                    world.getServer().getPluginManager().callEvent(event);
                    if (event.isCancelled()) {
                        return;
                    }
                    itemstack1 = TileEntityHopper.addItem(iinventory, CraftItemStack.asNMSCopy(event.getItem()), enumdirection.opposite());
                    if (event.getItem().equals(oitemstack) && itemstack1 == null) {
                        // CraftBukkit end
                        itemstack1 = itemstack.cloneItemStack();
                        if (--itemstack1.count <= 0) {
                            itemstack1 = null;
                        }
                    } else {
                        itemstack1 = itemstack.cloneItemStack();
                    }
                }
                tileentitydispenser.setItem(i, itemstack1);
            }
        }
    }
}
Also used : InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) CraftItemStack(org.bukkit.craftbukkit.inventory.CraftItemStack) CraftItemStack(org.bukkit.craftbukkit.inventory.CraftItemStack)

Example 4 with InventoryMoveItemEvent

use of org.bukkit.event.inventory.InventoryMoveItemEvent in project Arclight by IzzelAliz.

the class HopperTileEntityMixin method arclight$moveItem.

@Redirect(method = "transferItemsOut", at = @At(value = "INVOKE", target = "Lnet/minecraft/tileentity/HopperTileEntity;putStackInInventoryAllSlots(Lnet/minecraft/inventory/IInventory;Lnet/minecraft/inventory/IInventory;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/Direction;)Lnet/minecraft/item/ItemStack;"))
public ItemStack arclight$moveItem(IInventory source, IInventory destination, ItemStack stack, Direction direction) {
    CraftItemStack original = CraftItemStack.asCraftMirror(stack);
    Inventory destinationInventory;
    // Have to special case large chests as they work oddly
    if (destination instanceof DoubleSidedInventory) {
        destinationInventory = new CraftInventoryDoubleChest(((DoubleSidedInventory) destination));
    } else {
        destinationInventory = ((IInventoryBridge) destination).getOwner().getInventory();
    }
    InventoryMoveItemEvent event = new InventoryMoveItemEvent(this.getOwner().getInventory(), original.clone(), destinationInventory, true);
    Bukkit.getPluginManager().callEvent(event);
    if (arclight$moveItem = event.isCancelled()) {
        // Delay hopper checks
        this.setTransferCooldown(8);
        return null;
    }
    return HopperTileEntity.putStackInInventoryAllSlots(source, destination, CraftItemStack.asNMSCopy(event.getItem()), direction);
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Example 5 with InventoryMoveItemEvent

use of org.bukkit.event.inventory.InventoryMoveItemEvent in project Arclight by IzzelAliz.

the class HopperTileEntityMixin method arclight$pullItem.

@Redirect(method = "pullItemFromSlot", at = @At(value = "INVOKE", target = "Lnet/minecraft/tileentity/HopperTileEntity;putStackInInventoryAllSlots(Lnet/minecraft/inventory/IInventory;Lnet/minecraft/inventory/IInventory;Lnet/minecraft/item/ItemStack;Lnet/minecraft/util/Direction;)Lnet/minecraft/item/ItemStack;"))
private static ItemStack arclight$pullItem(IInventory source, IInventory destination, ItemStack stack, Direction direction) {
    CraftItemStack original = CraftItemStack.asCraftMirror(stack);
    Inventory sourceInventory;
    // Have to special case large chests as they work oddly
    if (source instanceof DoubleSidedInventory) {
        sourceInventory = new CraftInventoryDoubleChest(((DoubleSidedInventory) source));
    } else {
        sourceInventory = ((IInventoryBridge) source).getOwner().getInventory();
    }
    InventoryMoveItemEvent event = new InventoryMoveItemEvent(sourceInventory, original.clone(), ((IInventoryBridge) destination).getOwner().getInventory(), false);
    Bukkit.getPluginManager().callEvent(event);
    if (arclight$moveItem = event.isCancelled()) {
        if (destination instanceof HopperTileEntity) {
            // Delay hopper checks
            ((HopperTileEntity) destination).setTransferCooldown(8);
        } else if (destination instanceof HopperMinecartEntity) {
            // Delay hopper minecart checks
            ((HopperMinecartEntity) destination).setTransferTicker(4);
        }
        return null;
    }
    return HopperTileEntity.putStackInInventoryAllSlots(source, destination, CraftItemStack.asNMSCopy(event.getItem()), direction);
}
Also used : CraftItemStack(org.bukkit.craftbukkit.v.inventory.CraftItemStack) HopperTileEntity(net.minecraft.tileentity.HopperTileEntity) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) IInventoryBridge(io.izzel.arclight.common.bridge.inventory.IInventoryBridge) HopperMinecartEntity(net.minecraft.entity.item.minecart.HopperMinecartEntity) CraftInventoryDoubleChest(org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest) InventoryMoveItemEvent(org.bukkit.event.inventory.InventoryMoveItemEvent) DoubleSidedInventory(net.minecraft.inventory.DoubleSidedInventory) Inventory(org.bukkit.inventory.Inventory) IInventory(net.minecraft.inventory.IInventory) Redirect(org.spongepowered.asm.mixin.injection.Redirect)

Aggregations

InventoryMoveItemEvent (org.bukkit.event.inventory.InventoryMoveItemEvent)10 Inventory (org.bukkit.inventory.Inventory)8 CraftItemStack (org.bukkit.craftbukkit.inventory.CraftItemStack)6 IInventoryBridge (io.izzel.arclight.common.bridge.inventory.IInventoryBridge)3 DoubleSidedInventory (net.minecraft.inventory.DoubleSidedInventory)3 IInventory (net.minecraft.inventory.IInventory)3 CraftInventoryDoubleChest (org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest)3 CraftItemStack (org.bukkit.craftbukkit.v.inventory.CraftItemStack)3 Redirect (org.spongepowered.asm.mixin.injection.Redirect)2 ProxyBlockSource (net.minecraft.dispenser.ProxyBlockSource)1 HopperMinecartEntity (net.minecraft.entity.item.minecart.HopperMinecartEntity)1 ItemStack (net.minecraft.item.ItemStack)1 DispenserTileEntity (net.minecraft.tileentity.DispenserTileEntity)1 HopperTileEntity (net.minecraft.tileentity.HopperTileEntity)1 Direction (net.minecraft.util.Direction)1 Entity (org.bukkit.entity.Entity)1 HumanEntity (org.bukkit.entity.HumanEntity)1 Player (org.bukkit.entity.Player)1 BlockShearEntityEvent (org.bukkit.event.block.BlockShearEntityEvent)1 EntityDamageByEntityEvent (org.bukkit.event.entity.EntityDamageByEntityEvent)1