Example usage for net.minecraftforge.items.wrapper SidedInvWrapper SidedInvWrapper

List of usage examples for net.minecraftforge.items.wrapper SidedInvWrapper SidedInvWrapper

Introduction

In this page you can find the example usage for net.minecraftforge.items.wrapper SidedInvWrapper SidedInvWrapper.

Prototype

public SidedInvWrapper(ISidedInventory inv, @Nullable Direction side) 

Source Link

Usage

From source file:com.teambr.bookshelf.util.InventoryUtils.java

License:Creative Commons License

/**
 * Used to move items from one inventory to another. You can wrap it if you have to
 *     but since you'll be calling from us, there shouldn't be an issue
 *
 * @param source The source inventory, can be IInventory, ISideInventory, or preferably IItemHandler
 * @param fromSlot The from slot, -1 for any
 * @param target The target inventory, can be IInventory, ISideInventory, or preferably IItemHandler
 * @param intoSlot The slot to move into the target, -1 for any
 * @param maxAmount The max amount to move/extract
 * @param dir The direction moving into, so the face of the fromInventory
 * @param doMove True to actually do the move, false to simulate
 * @return True if something was moved//from   w  w w.j a  v  a2s.c  om
 */
public static boolean moveItemInto(Object source, int fromSlot, Object target, int intoSlot, int maxAmount,
        EnumFacing dir, boolean doMove, boolean checkSidedSource, boolean checkSidedTarget) {
    // Null Checks
    if (source == null || target == null)
        return false;

    // Object to hold source
    IItemHandler fromInventory;

    // If source is not an item handler, attempt to cast
    if (!(source instanceof IItemHandler)) {
        if (source instanceof IInventory) {
            if (!(source instanceof ISidedInventory))
                fromInventory = new InvWrapper((IInventory) source); // Wrap vanilla inventory
            else
                fromInventory = new SidedInvWrapper((ISidedInventory) source, dir.getOpposite()); // Wrap sided
        } else
            return false; // Not valid
    } else {
        // Cast item handlers
        fromInventory = (IItemHandler) source;
    }

    // If required, check for sidedness on tiles
    if (checkSidedSource) {
        if (source instanceof TileEntity) {
            TileEntity tile = (TileEntity) source;
            if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir))
                fromInventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir);
            else
                return false; // Source does not want to expose access
        }
    }

    IItemHandler targetInventory;

    // If sink is not an item handler, attempt to cast
    if (!(target instanceof IItemHandler)) {
        if (target instanceof IInventory) {
            if (!(target instanceof ISidedInventory))
                targetInventory = new InvWrapper((IInventory) target); // Wrap vanilla inventory
            else
                targetInventory = new SidedInvWrapper((ISidedInventory) target, dir); // Wrap sided
        } else
            return false; // Not valid
    } else {
        // Cast item handlers
        targetInventory = (IItemHandler) target;
    }

    // If required, check for sidedness on tiles
    if (checkSidedTarget) {
        if (target instanceof TileEntity) {
            TileEntity tile = (TileEntity) target;
            if (tile.hasCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, dir.getOpposite()))
                targetInventory = tile.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY,
                        dir.getOpposite());
            else
                return false; // Target does not want to expose access
        }
    }

    // Load slots
    List<Integer> fromSlots = new ArrayList<>();
    List<Integer> toSlots = new ArrayList<>();

    // Add From Slots
    if (fromSlot != -1)
        fromSlots.add(fromSlot);
    else
        for (int x = 0; x < fromInventory.getSlots(); x++)
            fromSlots.add(x);

    // Add to slots
    if (intoSlot != -1)
        toSlots.add(intoSlot);
    else
        for (int x = 0; x < targetInventory.getSlots(); x++)
            toSlots.add(x);

    // Do actual movement
    for (Integer fromSlot1 : fromSlots) { // Cycle fromInventory
        if (fromInventory.getStackInSlot(fromSlot1) != null) { // If we have something
            ItemStack fromStack = fromInventory.extractItem(fromSlot1, maxAmount, true); // Simulate to get stack
            if (!fromStack.isEmpty()) { // Make sure we got something
                for (Integer toSlot : toSlots) { // Cycle to inventory
                    int slotID = toSlot; // Grab slot
                    ItemStack movedStack = targetInventory.insertItem(slotID, fromStack.copy(), !doMove); // Try insert
                    if (!ItemStack.areItemStacksEqual(fromStack, movedStack)) { // If a change was made to the stack
                        fromInventory.extractItem(fromSlot1,
                                (!movedStack.isEmpty()) ? fromStack.getCount() - movedStack.getCount()
                                        : maxAmount,
                                !doMove); // Extract from original
                        return true; // Exit
                    }
                }
            }
        }
    }
    return false; // Failed to move something
}

From source file:de.sanandrew.mods.turretmod.tileentity.assembly.TileEntityTurretAssembly.java

License:Creative Commons License

public TileEntityTurretAssembly() {
    this.robotArmX = 2.0F;
    this.robotArmY = -9.0F;
    this.robotMotionX = 0.0F;
    this.robotMotionY = 0.0F;
    this.fluxConsumption = 0;
    this.ticksCrafted = 0;
    this.maxTicksCrafted = 0;
    this.ticksExisted = 0L;

    this.energyStorage = new AssemblyEnergyStorage();
    this.invHandler = new AssemblyInventoryHandler(this);
    this.itemHandlerBottom = new SidedInvWrapper(this.invHandler, EnumFacing.DOWN);
    this.itemHandlerSide = new SidedInvWrapper(this.invHandler, EnumFacing.WEST);
}

From source file:mal.lootbags.tileentity.TileEntityOpener.java

License:Open Source License

@Override
public <T> T getCapability(Capability<T> capability, EnumFacing facing) {
    if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
        return (T) new SidedInvWrapper(this, facing);
    }/*from  w  w  w .j  av  a2  s.co m*/
    return super.getCapability(capability, facing);
}

From source file:net.teamio.taam.util.InventoryUtils.java

License:Open Source License

/**
 * Gets an {@link IItemHandler} from the given tile entity.
 * <p>/*from   www  .j av  a  2s . c o m*/
 * If enabled in the config, {@link ISidedInventory} and {@link IInventory}
 * will be wrapped & returned accordingly.
 *
 * @param tileEntity
 * @param side
 * @return An {@link IItemHandler} for the given tileEntity, or null if no
 * inventory was found or tileEntity was null.
 * @author Oliver Kahrmann
 */
public static IItemHandler getInventory(TileEntity tileEntity, EnumFacing side) {
    if (tileEntity == null) {
        return null;
    }
    IItemHandler itemHandler = tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
    if (itemHandler == null && Config.use_iinventory_compat) {
        if (tileEntity instanceof ISidedInventory) {
            itemHandler = new SidedInvWrapper((ISidedInventory) tileEntity, side);
        } else if (tileEntity instanceof IInventory) {
            itemHandler = new InvWrapper((IInventory) tileEntity);
        }
    }
    return itemHandler;
}

From source file:si.meansoft.traincraft.tile.TileEntityInventory.java

public TileEntityInventory(String name, int slotAmount) {
    super(name);/*w  w w  .j a  v  a 2  s.  co  m*/
    slots = new ItemStack[slotAmount];
    this.invName = "Inventory" + name;
    for (EnumFacing side : EnumFacing.values()) {
        this.invWrappers[side.getIndex()] = new SidedInvWrapper(this, side);
    }
}