Search in sources :

Example 1 with IAirHandler

use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.

the class ModuleRegulatorTube method getMaxDispersion.

@Override
public int getMaxDispersion() {
    IAirHandler connectedHandler = null;
    for (Pair<ForgeDirection, IAirHandler> entry : pressureTube.getAirHandler().getConnectedPneumatics()) {
        if (entry.getKey().equals(dir)) {
            connectedHandler = entry.getValue();
            break;
        }
    }
    if (connectedHandler == null)
        return 0;
    int maxDispersion = (int) ((getThreshold() - connectedHandler.getPressure(ForgeDirection.UNKNOWN)) * connectedHandler.getVolume());
    if (maxDispersion < 0)
        return 0;
    return maxDispersion;
}
Also used : IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 2 with IAirHandler

use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.

the class ProgWidgetPressureCondition method getEvaluator.

@Override
protected DroneAIBlockCondition getEvaluator(IDroneBase drone, IProgWidget widget) {
    return new DroneAIBlockCondition(drone, (ProgWidgetAreaItemBase) widget) {

        @Override
        protected boolean evaluate(ChunkPosition pos) {
            TileEntity te = drone.getWorld().getTileEntity(pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ);
            if (te instanceof IPneumaticMachine) {
                IAirHandler airHandler = ((IPneumaticMachine) te).getAirHandler();
                float pressure = Float.MIN_VALUE;
                for (ForgeDirection d : ForgeDirection.VALID_DIRECTIONS) {
                    if (getSides()[d.ordinal()]) {
                        pressure = Math.max(airHandler.getPressure(d), pressure);
                    }
                }
                return ((ICondition) widget).getOperator() == ICondition.Operator.EQUALS ? pressure == ((ICondition) widget).getRequiredCount() : pressure >= ((ICondition) widget).getRequiredCount();
            }
            return false;
        }
    };
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) DroneAIBlockCondition(pneumaticCraft.common.ai.DroneAIBlockCondition) IPneumaticMachine(pneumaticCraft.api.tileentity.IPneumaticMachine) ChunkPosition(net.minecraft.world.ChunkPosition) ForgeDirection(net.minecraftforge.common.util.ForgeDirection)

Example 3 with IAirHandler

use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.

the class TileEntityPressureTube method updateEntity.

@Override
public void updateEntity() {
    super.updateEntity();
    for (TubeModule module : modules) {
        if (module != null) {
            module.shouldDrop = true;
            module.update();
        }
    }
    List<Pair<ForgeDirection, IAirHandler>> teList = getConnectedPneumatics();
    boolean hasModules = false;
    for (TubeModule module : modules) {
        if (module != null) {
            hasModules = true;
            break;
        }
    }
    if (!hasModules && teList.size() - specialConnectedHandlers.size() == 1 && !worldObj.isRemote) {
        for (Pair<ForgeDirection, IAirHandler> entry : teList) {
            if (entry.getKey() != ForgeDirection.UNKNOWN && modules[entry.getKey().getOpposite().ordinal()] == null && isConnectedTo(entry.getKey().getOpposite()))
                airLeak(entry.getKey().getOpposite());
        }
    }
}
Also used : IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) TubeModule(pneumaticCraft.common.block.tubes.TubeModule) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) Pair(org.apache.commons.lang3.tuple.Pair)

Example 4 with IAirHandler

use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.

the class ModuleCharging method update.

@Override
public void update() {
    super.update();
    IInventory inv = getConnectedInventory();
    if (inv != null) {
        int[] accessibleSlots = IOHelper.getAccessibleSlotsForInventory(inv, dir.getOpposite());
        for (int i = 0; i < (upgraded ? 10 : 1) * PneumaticValues.CHARGING_STATION_CHARGE_RATE; i++) {
            boolean charged = false;
            for (int slot : accessibleSlots) {
                ItemStack chargedItem = inv.getStackInSlot(slot);
                if (chargedItem != null && chargedItem.getItem() instanceof IPressurizable) {
                    IPressurizable chargingItem = (IPressurizable) chargedItem.getItem();
                    IAirHandler airHandler = ((IPneumaticMachine) pressureTube).getAirHandler();
                    if (chargingItem.getPressure(chargedItem) > airHandler.getPressure(ForgeDirection.UNKNOWN) + 0.01F && chargingItem.getPressure(chargedItem) > 0F) {
                        chargingItem.addAir(chargedItem, -1);
                        airHandler.addAir(1, ForgeDirection.UNKNOWN);
                        charged = true;
                    } else if (chargingItem.getPressure(chargedItem) < airHandler.getPressure(ForgeDirection.UNKNOWN) - 0.01F && chargingItem.getPressure(chargedItem) < chargingItem.maxPressure(chargedItem)) {
                        // if there is pressure, and the item isn't fully charged yet..
                        chargingItem.addAir(chargedItem, 1);
                        airHandler.addAir(-1, ForgeDirection.UNKNOWN);
                        charged = true;
                    }
                }
            }
            if (!charged)
                break;
        }
    }
}
Also used : IInventory(net.minecraft.inventory.IInventory) IPressurizable(pneumaticCraft.api.item.IPressurizable) IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) IPneumaticMachine(pneumaticCraft.api.tileentity.IPneumaticMachine) ItemStack(net.minecraft.item.ItemStack)

Example 5 with IAirHandler

use of pneumaticCraft.api.tileentity.IAirHandler in project PneumaticCraft by MineMaarten.

the class TileEntityPressureChamberValve method updateEntity.

// main update method
@Override
public void updateEntity() {
    if (readNBT) {
        // this way of doing this is needed because other TE's are
        // loaded after the reading of the NBT of this TE.
        readNBT = false;
        accessoryValves.clear();
        for (int[] valve : nbtValveList) {
            TileEntity te = worldObj.getTileEntity(valve[0], valve[1], valve[2]);
            if (te instanceof TileEntityPressureChamberValve) {
                accessoryValves.add((TileEntityPressureChamberValve) te);
            }
        }
        if (accessoryValves.isEmpty()) {
            //Hacky solution for an unexplainable bug.
            invalidateMultiBlock();
            checkIfProperlyFormed(worldObj, xCoord, yCoord, zCoord);
        }
        if (worldObj.isRemote)
            worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
    }
    if (!worldObj.isRemote) {
        //code to check if we need to leak air.
        //assume we are not leaking at any side
        boolean[] connected = new boolean[] { true, true, true, true, true, true };
        switch(//take off the sides that tubes can connect to.
        ForgeDirection.getOrientation(getBlockMetadata())) {
            case UP:
            case DOWN:
                connected[ForgeDirection.UP.ordinal()] = false;
                connected[ForgeDirection.DOWN.ordinal()] = false;
                break;
            case NORTH:
            case SOUTH:
                connected[ForgeDirection.NORTH.ordinal()] = false;
                connected[ForgeDirection.SOUTH.ordinal()] = false;
                break;
            case EAST:
            case WEST:
                connected[ForgeDirection.EAST.ordinal()] = false;
                connected[ForgeDirection.WEST.ordinal()] = false;
                break;
        }
        //we need the super method, as the overridden method adds the other valves.
        List<Pair<ForgeDirection, IAirHandler>> teList = super.getConnectedPneumatics();
        for (Pair<ForgeDirection, IAirHandler> entry : teList) {
            connected[entry.getKey().ordinal()] = true;
        }
        //retrieve the valve that is controlling the (potential) chamber.
        TileEntityPressureChamberValve baseValve = null;
        for (TileEntityPressureChamberValve valve : accessoryValves) {
            if (valve.multiBlockSize > 0) {
                baseValve = valve;
                break;
            }
        }
        //if we found one, we can scratch one side to be leaking air.
        if (baseValve != null) {
            switch(ForgeDirection.getOrientation(getBlockMetadata())) {
                case UP:
                case DOWN:
                    if (baseValve.multiBlockY == yCoord)
                        connected[ForgeDirection.UP.ordinal()] = true;
                    else
                        connected[ForgeDirection.DOWN.ordinal()] = true;
                    break;
                case NORTH:
                case SOUTH:
                    if (baseValve.multiBlockZ == zCoord)
                        connected[ForgeDirection.SOUTH.ordinal()] = true;
                    else
                        connected[ForgeDirection.NORTH.ordinal()] = true;
                    break;
                case EAST:
                case WEST:
                    if (baseValve.multiBlockX == xCoord)
                        connected[ForgeDirection.EAST.ordinal()] = true;
                    else
                        connected[ForgeDirection.WEST.ordinal()] = true;
                    break;
            }
        }
        for (int i = 0; i < 6; i++) if (!connected[i])
            airLeak(ForgeDirection.getOrientation(i));
    }
    super.updateEntity();
    if (multiBlockSize != 0 && !worldObj.isRemote) {
        ItemStack[] stacksInChamber = getStacksInChamber();
        isValidRecipeInChamber = false;
        isSufficientPressureInChamber = false;
        recipePressure = Float.MAX_VALUE;
        //simple recipes
        for (PressureChamberRecipe recipe : PressureChamberRecipe.chamberRecipes) {
            boolean isValidRecipeInChamberFlag = canBeCompressed(recipe, stacksInChamber);
            boolean isSufficientPressureInChamberFlag = recipe.pressure <= getPressure(ForgeDirection.UNKNOWN) && recipe.pressure > 0F || recipe.pressure >= getPressure(ForgeDirection.UNKNOWN) && recipe.pressure < 0F;
            if (isValidRecipeInChamberFlag) {
                isValidRecipeInChamber = true;
                if (Math.abs(recipe.pressure) < Math.abs(recipePressure)) {
                    recipePressure = recipe.pressure;
                }
            }
            if (isSufficientPressureInChamberFlag)
                isSufficientPressureInChamber = true;
            if (isValidRecipeInChamberFlag && isSufficientPressureInChamberFlag && areEntitiesDoneMoving) {
                double[] outputPosition = clearStacksInChamber(recipe.input);
                giveOutput(recipe.output, outputPosition);
            }
        }
        //special recipes
        for (IPressureChamberRecipe recipe : PressureChamberRecipe.specialRecipes) {
            ItemStack[] removedStacks = recipe.isValidRecipe(stacksInChamber);
            boolean isValidRecipeInChamberFlag = removedStacks != null;
            boolean isSufficientPressureInChamberFlag = recipe.getCraftingPressure() <= getPressure(ForgeDirection.UNKNOWN) && recipe.getCraftingPressure() > 0F || recipe.getCraftingPressure() >= getPressure(ForgeDirection.UNKNOWN) && recipe.getCraftingPressure() < 0F;
            if (isValidRecipeInChamberFlag) {
                isValidRecipeInChamber = true;
                if (Math.abs(recipe.getCraftingPressure()) < Math.abs(recipePressure)) {
                    recipePressure = recipe.getCraftingPressure();
                }
            }
            if (isSufficientPressureInChamberFlag)
                isSufficientPressureInChamber = true;
            if (isValidRecipeInChamberFlag && isSufficientPressureInChamberFlag && areEntitiesDoneMoving) {
                double[] outputPosition = clearStacksInChamber(removedStacks);
                giveOutput(recipe.craftRecipe(stacksInChamber, removedStacks), outputPosition);
            }
        }
        if (getPressure(ForgeDirection.UNKNOWN) > PneumaticValues.MAX_PRESSURE_LIVING_ENTITY) {
            AxisAlignedBB bbBox = AxisAlignedBB.getBoundingBox(multiBlockX + 1, multiBlockY + 1, multiBlockZ + 1, multiBlockX + multiBlockSize - 1, multiBlockY + multiBlockSize - 1, multiBlockZ + multiBlockSize - 1);
            List<EntityLivingBase> entities = worldObj.getEntitiesWithinAABB(EntityLivingBase.class, bbBox);
            for (EntityLivingBase entity : entities) {
                if (entity instanceof EntityVillager) {
                    EntityVillager villager = (EntityVillager) entity;
                    if (villager.getProfession() != Config.villagerMechanicID) {
                        villager.setProfession(Config.villagerMechanicID);
                        NBTTagCompound tag = new NBTTagCompound();
                        villager.writeEntityToNBT(tag);
                        if (tag.hasKey("Offers")) {
                            //reset the trade list
                            tag.removeTag("Offers");
                            villager.readEntityFromNBT(tag);
                        }
                    }
                }
                entity.attackEntityFrom(DamageSourcePneumaticCraft.pressure, (int) (getPressure(ForgeDirection.UNKNOWN) * 2D));
            }
        }
    }
    // move entities to eachother.
    AxisAlignedBB bbBox = AxisAlignedBB.getBoundingBox(multiBlockX, multiBlockY, multiBlockZ, multiBlockX + multiBlockSize, multiBlockY + multiBlockSize, multiBlockZ + multiBlockSize);
    List<EntityItem> entities = worldObj.getEntitiesWithinAABB(EntityItem.class, bbBox);
    // set to true, set to false when one of
    areEntitiesDoneMoving = true;
    // the entities is moving.
    for (int i = 0; i < entities.size() - 1; i++) {
        EntityItem lastEntity = entities.get(i);
        EntityItem entity = entities.get(i + 1);
        // XP Orb code snippet
        double d0 = 8.0D;
        double d1 = (lastEntity.posX - entity.posX) / d0;
        double d3 = (lastEntity.posZ - entity.posZ) / d0;
        double d4 = Math.sqrt(d1 * d1 + d3 * d3);
        double d5 = 1.0D - d4;
        if (d5 > 0.0D && d4 > 0.02D) {
            d5 *= d5;
            entity.motionX += d1 / d4 * d5 * 0.01D;
            entity.motionZ += d3 / d4 * d5 * 0.01D;
            lastEntity.motionX -= d1 / d4 * d5 * 0.01D;
            lastEntity.motionZ -= d3 / d4 * d5 * 0.01D;
            areEntitiesDoneMoving = false;
        }
    }
    boolean lifeUpgrade = getUpgrades(ItemMachineUpgrade.UPGRADE_ITEM_LIFE, getUpgradeSlots()) > 0;
    if (lifeUpgrade && !worldObj.isRemote) {
        for (EntityItem entity : entities) {
            entity.age--;
        }
    }
    // particles
    if (worldObj.isRemote && getPressure(ForgeDirection.UNKNOWN) > 0.2D) {
        int particles = (int) Math.pow(multiBlockSize - 2, 3);
        for (int i = 0; i < particles; i++) {
            if (rand.nextInt(Math.max(1, 8 - (int) (getPressure(ForgeDirection.UNKNOWN) * 2D))) == 0) {
                double posX = multiBlockX + 1D + rand.nextDouble() * (multiBlockSize - 2D);
                double posY = multiBlockY + 1D + rand.nextDouble() * (multiBlockSize - 2D);
                double posZ = multiBlockZ + 1D + rand.nextDouble() * (multiBlockSize - 2D);
                worldObj.spawnParticle("explode", posX, posY, posZ, 0D, 0D, 0D);
            }
        }
    }
}
Also used : AxisAlignedBB(net.minecraft.util.AxisAlignedBB) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IPressureChamberRecipe(pneumaticCraft.api.recipe.IPressureChamberRecipe) PressureChamberRecipe(pneumaticCraft.api.recipe.PressureChamberRecipe) TileEntity(net.minecraft.tileentity.TileEntity) IPressureChamberRecipe(pneumaticCraft.api.recipe.IPressureChamberRecipe) IAirHandler(pneumaticCraft.api.tileentity.IAirHandler) EntityVillager(net.minecraft.entity.passive.EntityVillager) ForgeDirection(net.minecraftforge.common.util.ForgeDirection) EntityLivingBase(net.minecraft.entity.EntityLivingBase) ItemStack(net.minecraft.item.ItemStack) EntityItem(net.minecraft.entity.item.EntityItem) Pair(org.apache.commons.lang3.tuple.Pair) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair)

Aggregations

IAirHandler (pneumaticCraft.api.tileentity.IAirHandler)8 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)7 Pair (org.apache.commons.lang3.tuple.Pair)5 TileEntity (net.minecraft.tileentity.TileEntity)4 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)4 IPneumaticMachine (pneumaticCraft.api.tileentity.IPneumaticMachine)4 ArrayList (java.util.ArrayList)3 ItemStack (net.minecraft.item.ItemStack)2 MutablePair (org.apache.commons.lang3.tuple.MutablePair)2 ISidedPneumaticMachine (pneumaticCraft.api.tileentity.ISidedPneumaticMachine)2 EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 EntityVillager (net.minecraft.entity.passive.EntityVillager)1 IInventory (net.minecraft.inventory.IInventory)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 AxisAlignedBB (net.minecraft.util.AxisAlignedBB)1 ChunkPosition (net.minecraft.world.ChunkPosition)1 IPressurizable (pneumaticCraft.api.item.IPressurizable)1 IPressureChamberRecipe (pneumaticCraft.api.recipe.IPressureChamberRecipe)1 PressureChamberRecipe (pneumaticCraft.api.recipe.PressureChamberRecipe)1