Search in sources :

Example 1 with HopperTileEntity

use of net.minecraft.tileentity.HopperTileEntity in project LoliServer by Loli-Server.

the class VanillaInventoryCodeHooks method insertStack.

/**
 * Copied from TileEntityHopper#insertStack and added capability support
 */
private static ItemStack insertStack(TileEntity source, Object destination, IItemHandler destInventory, ItemStack stack, int slot) {
    ItemStack itemstack = destInventory.getStackInSlot(slot);
    if (destInventory.insertItem(slot, stack, true).isEmpty()) {
        boolean insertedItem = false;
        boolean inventoryWasEmpty = isEmpty(destInventory);
        if (itemstack.isEmpty()) {
            destInventory.insertItem(slot, stack, false);
            stack = ItemStack.EMPTY;
            insertedItem = true;
        } else if (ItemHandlerHelper.canItemStacksStack(itemstack, stack)) {
            int originalSize = stack.getCount();
            stack = destInventory.insertItem(slot, stack, false);
            insertedItem = originalSize < stack.getCount();
        }
        if (insertedItem) {
            if (inventoryWasEmpty && destination instanceof HopperTileEntity) {
                HopperTileEntity destinationHopper = (HopperTileEntity) destination;
                if (!destinationHopper.isOnCustomCooldown()) {
                    int k = 0;
                    if (source instanceof HopperTileEntity) {
                        if (destinationHopper.getLastUpdateTime() >= ((HopperTileEntity) source).getLastUpdateTime()) {
                            k = 1;
                        }
                    }
                    destinationHopper.setCooldown(8 - k);
                }
            }
        }
    }
    return stack;
}
Also used : HopperTileEntity(net.minecraft.tileentity.HopperTileEntity) ItemStack(net.minecraft.item.ItemStack)

Example 2 with HopperTileEntity

use of net.minecraft.tileentity.HopperTileEntity in project Magma-1.16.x by magmafoundation.

the class VanillaInventoryCodeHooks method insertStack.

/**
 * Copied from TileEntityHopper#insertStack and added capability support
 */
private static ItemStack insertStack(TileEntity source, Object destination, IItemHandler destInventory, ItemStack stack, int slot) {
    ItemStack itemstack = destInventory.getStackInSlot(slot);
    if (destInventory.insertItem(slot, stack, true).isEmpty()) {
        boolean insertedItem = false;
        boolean inventoryWasEmpty = isEmpty(destInventory);
        if (itemstack.isEmpty()) {
            destInventory.insertItem(slot, stack, false);
            stack = ItemStack.EMPTY;
            insertedItem = true;
        } else if (ItemHandlerHelper.canItemStacksStack(itemstack, stack)) {
            int originalSize = stack.getCount();
            stack = destInventory.insertItem(slot, stack, false);
            insertedItem = originalSize < stack.getCount();
        }
        if (insertedItem) {
            if (inventoryWasEmpty && destination instanceof HopperTileEntity) {
                HopperTileEntity destinationHopper = (HopperTileEntity) destination;
                if (!destinationHopper.isOnCustomCooldown()) {
                    int k = 0;
                    if (source instanceof HopperTileEntity) {
                        if (destinationHopper.getLastUpdateTime() >= ((HopperTileEntity) source).getLastUpdateTime()) {
                            k = 1;
                        }
                    }
                    destinationHopper.setCooldown(8 - k);
                }
            }
        }
    }
    return stack;
}
Also used : HopperTileEntity(net.minecraft.tileentity.HopperTileEntity) ItemStack(net.minecraft.item.ItemStack)

Example 3 with HopperTileEntity

use of net.minecraft.tileentity.HopperTileEntity 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

HopperTileEntity (net.minecraft.tileentity.HopperTileEntity)3 ItemStack (net.minecraft.item.ItemStack)2 IInventoryBridge (io.izzel.arclight.common.bridge.inventory.IInventoryBridge)1 HopperMinecartEntity (net.minecraft.entity.item.minecart.HopperMinecartEntity)1 DoubleSidedInventory (net.minecraft.inventory.DoubleSidedInventory)1 IInventory (net.minecraft.inventory.IInventory)1 CraftInventoryDoubleChest (org.bukkit.craftbukkit.v.inventory.CraftInventoryDoubleChest)1 CraftItemStack (org.bukkit.craftbukkit.v.inventory.CraftItemStack)1 InventoryMoveItemEvent (org.bukkit.event.inventory.InventoryMoveItemEvent)1 Inventory (org.bukkit.inventory.Inventory)1 Redirect (org.spongepowered.asm.mixin.injection.Redirect)1