Search in sources :

Example 1 with ModularStorageItemInventory

use of mcjty.rftools.blocks.storage.ModularStorageItemInventory in project RFTools by McJty.

the class FindBlockAction method performServer.

@Override
public void performServer(EntityPlayer player, World world, @Nullable BlockPos pos, boolean extended) {
    if (pos != null) {
        // If we come here we know that on client side we couldn't find a suitable block in the players inventory.
        List<ItemStack> inventory = Collections.unmodifiableList(player.inventory.mainInventory);
        IBlockState state = world.getBlockState(pos);
        ItemStack result = state.getBlock().getItem(world, pos, state);
        if (result == null || result.isEmpty()) {
            return;
        }
        ItemStack storage = ItemStack.EMPTY;
        for (ItemStack stack : inventory) {
            if (!stack.isEmpty()) {
                if (stack.getItem() == ModularStorageSetup.storageModuleTabletItem) {
                    // Found!
                    storage = stack;
                    break;
                }
            }
        }
        if (storage.isEmpty()) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "No storage tablet in inventory!"), false);
            return;
        }
        NBTTagCompound tagCompound = storage.getTagCompound();
        if (tagCompound == null || !tagCompound.hasKey("childDamage")) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "No storage module in tablet!"), false);
            return;
        }
        int firstEmptyStack = player.inventory.getFirstEmptyStack();
        if (firstEmptyStack < 0) {
            player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "No room in inventory for block!"), false);
            return;
        }
        int moduleDamage = tagCompound.getInteger("childDamage");
        ItemStack extracted = ItemStack.EMPTY;
        if (moduleDamage == META_FOR_SCANNER) {
            if (tagCompound.hasKey("monitorx")) {
                int monitordim = tagCompound.getInteger("monitordim");
                int monitorx = tagCompound.getInteger("monitorx");
                int monitory = tagCompound.getInteger("monitory");
                int monitorz = tagCompound.getInteger("monitorz");
                BlockPos mpos = new BlockPos(monitorx, monitory, monitorz);
                WorldServer w = DimensionManager.getWorld(monitordim);
                if (w == null || !RFToolsTools.chunkLoaded(w, mpos)) {
                    player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Storage scanner is out of range!"), false);
                } else {
                    TileEntity te = w.getTileEntity(mpos);
                    if (te instanceof StorageScannerTileEntity) {
                        StorageScannerTileEntity scanner = (StorageScannerTileEntity) te;
                        extracted = scanner.requestItem(result, result.getMaxStackSize(), true, false);
                        if (extracted.isEmpty()) {
                            extracted = scanner.requestItem(result, result.getMaxStackSize(), true, true);
                        }
                    }
                }
            } else {
                player.sendStatusMessage(new TextComponentString(TextFormatting.RED + "Storage module is not linked to a storage scanner!"), false);
            }
        } else if (moduleDamage == StorageModuleItem.STORAGE_REMOTE) {
            if (!tagCompound.hasKey("id")) {
                Logging.message(player, TextFormatting.YELLOW + "This remote storage module is not linked!");
            } else {
                RemoteStorageItemInventory storageInv = new RemoteStorageItemInventory(player, storage);
                for (int i = 0; i < storageInv.getSizeInventory(); i++) {
                    ItemStack s = storageInv.getStackInSlot(i);
                    if (!s.isEmpty() && stackEqualExact(result, s)) {
                        extracted = s;
                        storageInv.setInventorySlotContents(i, ItemStack.EMPTY);
                        break;
                    }
                }
            }
        } else {
            ModularStorageItemInventory storageInv = new ModularStorageItemInventory(player, storage);
            for (int i = 0; i < storageInv.getSizeInventory(); i++) {
                ItemStack s = storageInv.getStackInSlot(i);
                if (!s.isEmpty() && stackEqualExact(result, s)) {
                    extracted = s;
                    storageInv.setInventorySlotContents(i, ItemStack.EMPTY);
                    break;
                }
            }
        }
        if (!extracted.isEmpty()) {
            int currentItem = player.inventory.currentItem;
            if (currentItem == firstEmptyStack) {
                player.inventory.setInventorySlotContents(currentItem, extracted);
            } else {
                player.inventory.setInventorySlotContents(firstEmptyStack, inventory.get(currentItem));
                player.inventory.setInventorySlotContents(currentItem, extracted);
            }
            player.openContainer.detectAndSendChanges();
        }
    }
}
Also used : StorageScannerTileEntity(mcjty.rftools.blocks.storagemonitor.StorageScannerTileEntity) TileEntity(net.minecraft.tileentity.TileEntity) RemoteStorageItemInventory(mcjty.rftools.blocks.storage.RemoteStorageItemInventory) IBlockState(net.minecraft.block.state.IBlockState) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) BlockPos(net.minecraft.util.math.BlockPos) WorldServer(net.minecraft.world.WorldServer) ItemStack(net.minecraft.item.ItemStack) ModularStorageItemInventory(mcjty.rftools.blocks.storage.ModularStorageItemInventory) StorageScannerTileEntity(mcjty.rftools.blocks.storagemonitor.StorageScannerTileEntity) TextComponentString(net.minecraft.util.text.TextComponentString)

Aggregations

ModularStorageItemInventory (mcjty.rftools.blocks.storage.ModularStorageItemInventory)1 RemoteStorageItemInventory (mcjty.rftools.blocks.storage.RemoteStorageItemInventory)1 StorageScannerTileEntity (mcjty.rftools.blocks.storagemonitor.StorageScannerTileEntity)1 IBlockState (net.minecraft.block.state.IBlockState)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 TileEntity (net.minecraft.tileentity.TileEntity)1 BlockPos (net.minecraft.util.math.BlockPos)1 TextComponentString (net.minecraft.util.text.TextComponentString)1 WorldServer (net.minecraft.world.WorldServer)1