Search in sources :

Example 1 with TileEntityEnderChest

use of net.minecraft.tileentity.TileEntityEnderChest in project malmo by Microsoft.

the class InventoryCommandsImplementation method combineSlots.

static ItemStack[] combineSlots(EntityPlayerMP player, String invDst, int dst, String invAdd, int add, BlockPos containerPos) {
    IInventory container = null;
    String containerName = "";
    if (containerPos != null) {
        TileEntity te = player.world.getTileEntity(containerPos);
        if (te != null && te instanceof TileEntityLockableLoot) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName((IInventory) te);
            container = (IInventory) te;
        } else if (te != null && te instanceof TileEntityEnderChest) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName(player.getInventoryEnderChest());
            container = player.getInventoryEnderChest();
        }
    }
    IInventory dstInv = invDst.equals("inventory") ? player.inventory : (invDst.equals(containerName) ? container : null);
    IInventory addInv = invAdd.equals("inventory") ? player.inventory : (invAdd.equals(containerName) ? container : null);
    if (dstInv == null || addInv == null)
        // Source or dest container not available.
        return null;
    ItemStack dstStack = dstInv.getStackInSlot(dst);
    ItemStack addStack = addInv.getStackInSlot(add);
    if (addStack == null)
        // Combination is a no-op.
        return null;
    ItemStack[] returnStacks = null;
    if (// Do a straight move - nothing to combine with.
    dstStack == null) {
        if (dstInv != addInv) {
            // Items are moving between our inventory and the foreign inventory - may need to trigger
            // rewards for collecting / discarding.
            returnStacks = new ItemStack[2];
            ItemStack stackBeingLost = (addInv == player.inventory) ? addStack : null;
            ItemStack stackBeingGained = (dstInv == player.inventory) ? addStack : null;
            if (stackBeingGained != null)
                returnStacks[0] = stackBeingGained.copy();
            if (stackBeingLost != null)
                returnStacks[1] = stackBeingLost.copy();
        }
        dstInv.setInventorySlotContents(dst, addStack);
        addInv.setInventorySlotContents(add, null);
        return returnStacks;
    }
    // Check we can combine. This logic comes from InventoryPlayer.storeItemStack():
    boolean itemsMatch = dstStack.getItem() == addStack.getItem();
    boolean dstCanStack = dstStack.isStackable() && dstStack.getCount() < dstStack.getMaxStackSize() && dstStack.getCount() < dstInv.getInventoryStackLimit();
    boolean subTypesMatch = !dstStack.getHasSubtypes() || dstStack.getMetadata() == addStack.getMetadata();
    boolean tagsMatch = ItemStack.areItemStackTagsEqual(dstStack, addStack);
    if (itemsMatch && dstCanStack && subTypesMatch && tagsMatch) {
        // We can combine, so figure out how much we have room for:
        int limit = Math.min(dstStack.getMaxStackSize(), dstInv.getInventoryStackLimit());
        int room = limit - dstStack.getCount();
        ItemStack itemsTransferred = dstStack.copy();
        if (addStack.getCount() > room) {
            // Not room for all of it, so shift across as much as possible.
            addStack.shrink(room);
            dstStack.grow(room);
            itemsTransferred.setCount(room);
        } else {
            // Room for the whole lot, so empty out the add slot.
            dstStack.grow(addStack.getCount());
            itemsTransferred.setCount(addStack.getCount());
            // setInventorySlotContents(add, null);
            addInv.removeStackFromSlot(add);
        }
        if (dstInv != addInv) {
            // Items are moving between our inventory and the foreign inventory - may need to trigger
            // rewards for collecting / discarding.
            returnStacks = new ItemStack[2];
            if (dstInv == player.inventory)
                // We're gaining them
                returnStacks[0] = itemsTransferred;
            else
                // We're losing them
                returnStacks[1] = itemsTransferred;
        }
    }
    return returnStacks;
}
Also used : IInventory(net.minecraft.inventory.IInventory) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityLockableLoot(net.minecraft.tileentity.TileEntityLockableLoot) TileEntityEnderChest(net.minecraft.tileentity.TileEntityEnderChest) ItemStack(net.minecraft.item.ItemStack)

Example 2 with TileEntityEnderChest

use of net.minecraft.tileentity.TileEntityEnderChest in project malmo by Microsoft.

the class InventoryCommandsImplementation method getParameters.

private boolean getParameters(String parameter, List<Object> parsedParams) {
    String[] params = parameter.split(" ");
    if (params.length != 2) {
        System.out.println("Malformed parameter string (" + parameter + ") - expected <x> <y>");
        // Error - incorrect number of parameters.
        return false;
    }
    String[] lhsParams = params[0].split(":");
    String[] rhsParams = params[1].split(":");
    Integer lhsIndex, rhsIndex;
    String lhsName, rhsName, lhsStrIndex, rhsStrIndex;
    boolean checkContainers = false;
    if (lhsParams.length == 2) {
        lhsName = lhsParams[0];
        lhsStrIndex = lhsParams[1];
        checkContainers = true;
    } else if (lhsParams.length == 1) {
        lhsName = "inventory";
        lhsStrIndex = lhsParams[0];
    } else {
        System.out.println("Malformed parameter string (" + params[0] + ")");
        return false;
    }
    if (rhsParams.length == 2) {
        rhsName = rhsParams[0];
        rhsStrIndex = rhsParams[1];
        checkContainers = true;
    } else if (rhsParams.length == 1) {
        rhsName = "inventory";
        rhsStrIndex = rhsParams[0];
    } else {
        System.out.println("Malformed parameter string (" + params[1] + ")");
        return false;
    }
    try {
        lhsIndex = Integer.valueOf(lhsStrIndex);
        rhsIndex = Integer.valueOf(rhsStrIndex);
    } catch (NumberFormatException e) {
        System.out.println("Malformed parameter string (" + parameter + ") - " + e.getMessage());
        return false;
    }
    if (lhsIndex == null || rhsIndex == null) {
        System.out.println("Malformed parameter string (" + parameter + ")");
        // Error - incorrect parameters.
        return false;
    }
    BlockPos containerPos = null;
    if (checkContainers) {
        String containerName = "";
        RayTraceResult rtr = Minecraft.getMinecraft().objectMouseOver;
        if (rtr != null && rtr.typeOfHit == RayTraceResult.Type.BLOCK) {
            containerPos = rtr.getBlockPos();
            TileEntity te = Minecraft.getMinecraft().world.getTileEntity(containerPos);
            if (te instanceof TileEntityLockableLoot)
                containerName = ObservationFromFullInventoryImplementation.getInventoryName((IInventory) te);
            else if (te instanceof TileEntityEnderChest)
                containerName = ObservationFromFullInventoryImplementation.getInventoryName(Minecraft.getMinecraft().player.getInventoryEnderChest());
        }
        boolean containerMatches = (lhsName.equals("inventory") || lhsName.equals(containerName)) && (rhsName.equals("inventory") || rhsName.equals(containerName));
        if (!containerMatches) {
            System.out.println("Missing container requested in parameter string (" + parameter + ")");
            return false;
        }
    }
    parsedParams.add(lhsName);
    parsedParams.add(lhsIndex);
    parsedParams.add(rhsName);
    parsedParams.add(rhsIndex);
    if (containerPos != null)
        parsedParams.add(containerPos);
    return true;
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) TileEntityLockableLoot(net.minecraft.tileentity.TileEntityLockableLoot) RayTraceResult(net.minecraft.util.math.RayTraceResult) BlockPos(net.minecraft.util.math.BlockPos) TileEntityEnderChest(net.minecraft.tileentity.TileEntityEnderChest)

Example 3 with TileEntityEnderChest

use of net.minecraft.tileentity.TileEntityEnderChest in project malmo by Microsoft.

the class InventoryCommandsImplementation method swapSlots.

static ItemStack[] swapSlots(EntityPlayerMP player, String lhsInv, int lhs, String rhsInv, int rhs, BlockPos containerPos) {
    IInventory container = null;
    String containerName = "";
    if (containerPos != null) {
        TileEntity te = player.world.getTileEntity(containerPos);
        if (te != null && te instanceof TileEntityLockableLoot) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName((IInventory) te);
            container = (IInventory) te;
        } else if (te != null && te instanceof TileEntityEnderChest) {
            containerName = ObservationFromFullInventoryImplementation.getInventoryName(player.getInventoryEnderChest());
            container = player.getInventoryEnderChest();
        }
    }
    IInventory lhsInventory = lhsInv.equals("inventory") ? player.inventory : (lhsInv.equals(containerName) ? container : null);
    IInventory rhsInventory = rhsInv.equals("inventory") ? player.inventory : (rhsInv.equals(containerName) ? container : null);
    if (lhsInventory == null || rhsInventory == null)
        // Source or dest container not available.
        return null;
    if (rhs < 0 || lhs < 0)
        // Out of bounds.
        return null;
    if (lhs >= lhsInventory.getSizeInventory() || rhs >= rhsInventory.getSizeInventory())
        // Out of bounds.
        return null;
    ItemStack srcStack = lhsInventory.getStackInSlot(lhs);
    ItemStack dstStack = rhsInventory.getStackInSlot(rhs);
    lhsInventory.setInventorySlotContents(lhs, dstStack);
    rhsInventory.setInventorySlotContents(rhs, srcStack);
    if (lhsInventory != rhsInventory) {
        // Items have moved between our inventory and the foreign inventory - may need to trigger
        // rewards for collecting / discarding.
        ItemStack[] returnStacks = new ItemStack[2];
        ItemStack stackBeingLost = (lhsInventory == player.inventory) ? srcStack : dstStack;
        ItemStack stackBeingGained = (lhsInventory == player.inventory) ? dstStack : srcStack;
        if (stackBeingGained != null)
            returnStacks[0] = stackBeingGained.copy();
        if (stackBeingLost != null)
            returnStacks[1] = stackBeingLost.copy();
        return returnStacks;
    }
    return null;
}
Also used : IInventory(net.minecraft.inventory.IInventory) TileEntity(net.minecraft.tileentity.TileEntity) TileEntityLockableLoot(net.minecraft.tileentity.TileEntityLockableLoot) TileEntityEnderChest(net.minecraft.tileentity.TileEntityEnderChest) ItemStack(net.minecraft.item.ItemStack)

Aggregations

TileEntity (net.minecraft.tileentity.TileEntity)3 TileEntityEnderChest (net.minecraft.tileentity.TileEntityEnderChest)3 TileEntityLockableLoot (net.minecraft.tileentity.TileEntityLockableLoot)3 IInventory (net.minecraft.inventory.IInventory)2 ItemStack (net.minecraft.item.ItemStack)2 BlockPos (net.minecraft.util.math.BlockPos)1 RayTraceResult (net.minecraft.util.math.RayTraceResult)1