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;
}
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;
}
};
}
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());
}
}
}
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;
}
}
}
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);
}
}
}
}
Aggregations