Search in sources :

Example 1 with IPressureChamberRecipe

use of pneumaticCraft.api.recipe.IPressureChamberRecipe 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

EntityLivingBase (net.minecraft.entity.EntityLivingBase)1 EntityItem (net.minecraft.entity.item.EntityItem)1 EntityVillager (net.minecraft.entity.passive.EntityVillager)1 ItemStack (net.minecraft.item.ItemStack)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 TileEntity (net.minecraft.tileentity.TileEntity)1 AxisAlignedBB (net.minecraft.util.AxisAlignedBB)1 ForgeDirection (net.minecraftforge.common.util.ForgeDirection)1 ImmutablePair (org.apache.commons.lang3.tuple.ImmutablePair)1 Pair (org.apache.commons.lang3.tuple.Pair)1 IPressureChamberRecipe (pneumaticCraft.api.recipe.IPressureChamberRecipe)1 PressureChamberRecipe (pneumaticCraft.api.recipe.PressureChamberRecipe)1 IAirHandler (pneumaticCraft.api.tileentity.IAirHandler)1