Search in sources :

Example 1 with InventoryCopy

use of mods.railcraft.common.util.inventory.wrappers.InventoryCopy in project Railcraft by Railcraft.

the class TileRockCrusher method update.

@Override
public void update() {
    super.update();
    if (Game.isHost(getWorld())) {
        BlockPos pos = getPos();
        double x = pos.getX();
        double y = pos.getZ();
        double z = pos.getZ();
        if (isStructureValid()) {
            // TileEntityHopper.getItemsAroundAPointOrSomethingLikeThat
            for (EntityItem item : TileEntityHopper.getCaptureItems(getWorld(), x, y + 1, z)) {
                if (item != null && useMasterEnergy(SUCKING_POWER_COST, false)) {
                    ItemStack stack = item.getEntityItem().copy();
                    InventoryManipulator.get((IInventory) invInput).addStack(stack);
                    useMasterEnergy(SUCKING_POWER_COST, true);
                    item.setDead();
                }
            }
            EntityLivingBase entity = MiscTools.getEntityAt(worldObj, EntityLivingBase.class, getPos().up());
            if (entity != null && useMasterEnergy(KILLING_POWER_COST, false))
                if (entity.attackEntityFrom(RailcraftDamageSource.CRUSHER, 10))
                    useMasterEnergy(KILLING_POWER_COST, true);
        }
        if (isMaster()) {
            if (clock % 16 == 0)
                processActions();
            if (paused)
                return;
            ItemStack input = InvTools.emptyStack();
            ICrusherCraftingManager.ICrusherRecipe recipe = null;
            for (IInvSlot slot : InventoryIterator.getVanilla((IInventory) invInput)) {
                input = slot.getStack();
                if (!InvTools.isEmpty(input)) {
                    recipe = RailcraftCraftingManager.rockCrusher.getRecipe(input);
                    if (recipe == null)
                        recipe = RockCrusherCraftingManager.NULL_RECIPE;
                    break;
                }
            }
            if (recipe != null)
                if (processTime >= PROCESS_TIME) {
                    isWorking = false;
                    InventoryCopy tempInv = new InventoryCopy(invOutput);
                    boolean hasRoom = true;
                    List<ItemStack> outputs = recipe.getProcessedOutputs();
                    for (ItemStack output : outputs) {
                        output = InvTools.moveItemStack(output, tempInv);
                        if (!InvTools.isEmpty(output)) {
                            hasRoom = false;
                            break;
                        }
                    }
                    if (hasRoom) {
                        for (ItemStack output : outputs) {
                            InvTools.moveItemStack(output, invOutput);
                        }
                        InvTools.removeOneItem(invInput, input);
                        SoundHelper.playSound(worldObj, null, getPos(), SoundEvents.ENTITY_IRONGOLEM_DEATH, SoundCategory.BLOCKS, 1.0f, worldObj.rand.nextFloat() * 0.25F + 0.7F);
                        processTime = 0;
                    }
                } else {
                    isWorking = true;
                    if (energyStorage != null) {
                        int energy = energyStorage.extractEnergy(CRUSHING_POWER_COST_PER_TICK, true);
                        if (energy >= CRUSHING_POWER_COST_PER_TICK) {
                            processTime++;
                            energyStorage.extractEnergy(CRUSHING_POWER_COST_PER_TICK, false);
                        }
                    } else
                        processTime++;
                }
            else {
                processTime = 0;
                isWorking = false;
            }
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) IInvSlot(mods.railcraft.common.util.inventory.iterators.IInvSlot) EntityLivingBase(net.minecraft.entity.EntityLivingBase) BlockPos(net.minecraft.util.math.BlockPos) InventoryCopy(mods.railcraft.common.util.inventory.wrappers.InventoryCopy) ItemStack(net.minecraft.item.ItemStack) ICrusherCraftingManager(mods.railcraft.api.crafting.ICrusherCraftingManager) EntityItem(net.minecraft.entity.item.EntityItem)

Example 2 with InventoryCopy

use of mods.railcraft.common.util.inventory.wrappers.InventoryCopy in project Railcraft by Railcraft.

the class RockCrusherLogic method craftAndPush.

@Override
protected boolean craftAndPush() {
    final IRockCrusherCrafter.IRecipe recipe = currentRecipe.orElseThrow(NullPointerException::new);
    InventoryCopy tempInv = new InventoryCopy(invOutput);
    List<ItemStack> outputs = recipe.pollOutputs(random);
    boolean hasRoom = outputs.stream().map(tempInv::addStack).allMatch(InvTools::isEmpty);
    if (hasRoom) {
        outputs.forEach(invOutput::addStack);
        crushed = invInput.removeOneItem(recipe.getInput());
        SoundHelper.playSound(theWorldAsserted(), null, getPos(), SoundEvents.ENTITY_IRONGOLEM_DEATH, SoundCategory.BLOCKS, 1.0f, random.nextFloat() * 0.25F + 0.7F);
        return true;
    }
    return false;
}
Also used : IRockCrusherCrafter(mods.railcraft.api.crafting.IRockCrusherCrafter) InvTools(mods.railcraft.common.util.inventory.InvTools) InventoryCopy(mods.railcraft.common.util.inventory.wrappers.InventoryCopy) ItemStack(net.minecraft.item.ItemStack)

Aggregations

InventoryCopy (mods.railcraft.common.util.inventory.wrappers.InventoryCopy)2 ItemStack (net.minecraft.item.ItemStack)2 ICrusherCraftingManager (mods.railcraft.api.crafting.ICrusherCraftingManager)1 IRockCrusherCrafter (mods.railcraft.api.crafting.IRockCrusherCrafter)1 InvTools (mods.railcraft.common.util.inventory.InvTools)1 IInvSlot (mods.railcraft.common.util.inventory.iterators.IInvSlot)1 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 IInventory (net.minecraft.inventory.IInventory)1 BlockPos (net.minecraft.util.math.BlockPos)1