use of micdoodle8.mods.galacticraft.core.items.ItemCanisterGeneric in project Galacticraft by micdoodle8.
the class TileEntityGasLiquefier method update.
@Override
public void update() {
super.update();
if (this.airProducts == -1) {
this.airProducts = this.getAirProducts();
}
if (!this.worldObj.isRemote) {
FluidStack currentgas = this.gasTank.getFluid();
if (currentgas == null || currentgas.amount <= 0) {
this.gasTankType = -1;
} else {
this.gasTankType = this.getIdFromName(currentgas.getFluid().getName());
}
// If somehow it has air in an airless dimension, flush it out
if (this.airProducts == 0 && this.gasTankType == TankGases.AIR.index) {
this.gasTank.drain(this.gasTank.getFluidAmount(), true);
}
FluidStack currentLiquid = this.liquidTank.getFluid();
if (currentLiquid == null || currentLiquid.amount == 0) {
this.fluidTankType = -1;
} else {
this.fluidTankType = this.getProductIdFromName(currentLiquid.getFluid().getName());
}
currentLiquid = this.liquidTank2.getFluid();
if (currentLiquid == null || currentLiquid.amount == 0) {
this.fluidTank2Type = -1;
} else {
this.fluidTank2Type = this.getProductIdFromName(currentLiquid.getFluid().getName());
}
// First, see if any gas needs to be put into the gas storage
ItemStack inputCanister = this.containingItems[1];
if (inputCanister != null) {
if (inputCanister.getItem() instanceof ItemAtmosphericValve && this.airProducts > 0) {
// Air -> Air tank
if (this.gasTankType == -1 || (this.gasTankType == TankGases.AIR.index && this.gasTank.getFluid().amount < this.gasTank.getCapacity())) {
Block blockAbove = this.worldObj.getBlockState(getPos().up()).getBlock();
if (blockAbove != null && blockAbove.getMaterial() == Material.air && blockAbove != GCBlocks.breatheableAir && blockAbove != GCBlocks.brightBreatheableAir) {
FluidStack gcAtmosphere = FluidRegistry.getFluidStack(TankGases.AIR.gas, 4);
this.gasTank.fill(gcAtmosphere, true);
this.gasTankType = TankGases.AIR.index;
}
}
} else if (inputCanister.getItem() instanceof ItemCanisterGeneric) {
int amount = ItemCanisterGeneric.EMPTY - inputCanister.getItemDamage();
if (amount > 0) {
Item canisterType = inputCanister.getItem();
FluidStack canisterGas = null;
int factor = 1;
if (this.gasTankType <= 0 && canisterType == AsteroidsItems.methaneCanister) {
this.gasTankType = TankGases.METHANE.index;
canisterGas = FluidRegistry.getFluidStack(TankGases.METHANE.gas, amount);
}
if ((this.gasTankType == TankGases.OXYGEN.index || this.gasTankType == -1) && canisterType == AsteroidsItems.canisterLOX) {
this.gasTankType = TankGases.OXYGEN.index;
canisterGas = FluidRegistry.getFluidStack(TankGases.OXYGEN.gas, amount * 2);
factor = 2;
}
if ((this.gasTankType == TankGases.NITROGEN.index || this.gasTankType == -1) && canisterType == AsteroidsItems.canisterLN2) {
this.gasTankType = TankGases.NITROGEN.index;
canisterGas = FluidRegistry.getFluidStack(TankGases.NITROGEN.gas, amount * 2);
factor = 2;
}
if (canisterGas != null) {
int used = this.gasTank.fill(canisterGas, true) / factor;
if (used == amount) {
this.containingItems[1] = new ItemStack(GCItems.oilCanister, 1, ItemCanisterGeneric.EMPTY);
} else {
this.containingItems[1] = new ItemStack(canisterType, 1, ItemCanisterGeneric.EMPTY - amount + used);
}
}
}
} else {
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(inputCanister);
if (liquid != null && liquid.amount > 0) {
String inputName = FluidRegistry.getFluidName(liquid);
// Methane -> Methane tank
if (this.gasTankType <= 0 && inputName.contains("methane")) {
if (currentgas == null || currentgas.amount + liquid.amount <= this.gasTank.getCapacity()) {
FluidStack gcMethane = FluidRegistry.getFluidStack(TankGases.METHANE.gas, liquid.amount);
this.gasTank.fill(gcMethane, true);
this.gasTankType = 0;
this.containingItems[1] = FluidUtil.getUsedContainer(inputCanister);
}
} else // Oxygen -> Oxygen tank
if ((this.gasTankType == TankGases.OXYGEN.index || this.gasTankType == -1) && inputName.contains("oxygen")) {
int tankedAmount = liquid.amount * (inputName.contains("liquid") ? 2 : 1);
if (currentgas == null || currentgas.amount + tankedAmount <= this.gasTank.getCapacity()) {
FluidStack gcgas = FluidRegistry.getFluidStack(TankGases.OXYGEN.gas, tankedAmount);
this.gasTank.fill(gcgas, true);
this.gasTankType = TankGases.OXYGEN.index;
this.containingItems[1] = FluidUtil.getUsedContainer(inputCanister);
}
} else // Nitrogen -> Nitrogen tank
if ((this.gasTankType == TankGases.NITROGEN.index || this.gasTankType == -1) && inputName.contains("nitrogen")) {
int tankedAmount = liquid.amount * (inputName.contains("liquid") ? 2 : 1);
if (currentgas == null || currentgas.amount + tankedAmount <= this.gasTank.getCapacity()) {
FluidStack gcgas = FluidRegistry.getFluidStack(TankGases.NITROGEN.gas, tankedAmount);
this.gasTank.fill(gcgas, true);
this.gasTankType = TankGases.NITROGEN.index;
this.containingItems[1] = FluidUtil.getUsedContainer(inputCanister);
}
}
}
}
}
// Now see if any liquids from the output tanks need to be put into the output slot
checkFluidTankTransfer(2, this.liquidTank);
checkFluidTankTransfer(3, this.liquidTank2);
if (this.hasEnoughEnergyToRun && this.canProcess()) {
// 50% extra speed boost for Tier 2 machine if powered by Tier 2 power
if (this.tierGC == 2) {
this.processTimeRequired = Math.max(1, 4 - this.poweredByTierGC);
}
if (this.processTicks <= 0) {
this.processTicks = this.processTimeRequired;
} else {
if (--this.processTicks <= 0) {
this.doLiquefaction();
this.processTicks = this.canProcess() ? this.processTimeRequired : 0;
}
}
} else {
if (this.processTicks > 0) {
this.processTicks = 0;
} else if (--this.processTicks <= -10) {
this.processTicks = -10;
}
}
}
}
use of micdoodle8.mods.galacticraft.core.items.ItemCanisterGeneric in project Galacticraft by micdoodle8.
the class FluidUtil method loadFromContainer.
/**
* This tries to empty the container (at inventory[slot]) into the specified tank
* forcing the tank contents to Fluid: desiredLiquid even if the container had
* something different (useful for different types of fuel, for example).
* If successful, it replaces inventory[slot] with the corresponding empty container
* <p>
*
* @param tank The tank to fill with the fluid
* @param desiredLiquid The type of liquid intended for that tank
* @param inventory
* @param slot
* @param amountOffered The amount in the container being offered
*/
public static void loadFromContainer(FluidTank tank, Fluid desiredLiquid, ItemStack[] inventory, int slot, int amountOffered) {
ItemStack slotItem = inventory[slot];
if (slotItem.getItem() instanceof ItemCanisterGeneric) {
int originalDamage = slotItem.getItemDamage();
int used = tank.fill(new FluidStack(desiredLiquid, ItemCanisterGeneric.EMPTY - originalDamage), true);
if (originalDamage + used >= ItemCanisterGeneric.EMPTY) {
inventory[slot] = new ItemStack(GCItems.oilCanister, 1, ItemCanisterGeneric.EMPTY);
} else {
inventory[slot] = new ItemStack(slotItem.getItem(), 1, originalDamage + used);
}
} else {
if (tank.getFluid() == null || amountOffered <= tank.getCapacity() - tank.getFluid().amount) {
tank.fill(new FluidStack(desiredLiquid, amountOffered), true);
if (FluidContainerRegistry.isFilledContainer(slotItem)) {
final int bucketCount = slotItem.stackSize;
if (FluidContainerRegistry.isBucket(slotItem)) {
if (bucketCount > 1) {
tank.fill(new FluidStack(desiredLiquid, (bucketCount - 1) * FluidContainerRegistry.BUCKET_VOLUME), true);
}
inventory[slot] = new ItemStack(Items.bucket, bucketCount);
} else {
ItemStack emptyStack = FluidContainerRegistry.drainFluidContainer(slotItem);
if (bucketCount > 1) {
tank.fill(new FluidStack(desiredLiquid, (bucketCount - 1) * FluidContainerRegistry.getContainerCapacity(slotItem)), true);
if (emptyStack != null) {
emptyStack.stackSize = bucketCount;
}
}
inventory[slot] = emptyStack;
}
} else {
slotItem.stackSize--;
if (slotItem.stackSize == 0) {
inventory[slot] = null;
}
}
}
}
}
use of micdoodle8.mods.galacticraft.core.items.ItemCanisterGeneric in project Galacticraft by micdoodle8.
the class FluidUtil method tryFillContainer.
/**
* This tries to fill the given container (at inventory[slot]) with fluid from the specified tank
* If successful, it places the resulting filled container in inventory[slot]
* <p>
* Note: this deals with the issue where FluidContainerRegistry.fillFluidContainer() returns null for failed fills
*
* @param tank The tank to take the fluid from
* @param liquid The type of liquid in that tank (the calling method will normally have checked this already)
* @param inventory
* @param slot
* @param canisterType The type of canister to return, if it's a canister being filled (pre-matched with the liquid type)
*/
public static void tryFillContainer(FluidTank tank, FluidStack liquid, ItemStack[] inventory, int slot, Item canisterType) {
ItemStack slotItem = inventory[slot];
boolean isCanister = slotItem.getItem() instanceof ItemCanisterGeneric;
final int amountToFill = Math.min(liquid.amount, isCanister ? slotItem.getItemDamage() - 1 : FluidContainerRegistry.BUCKET_VOLUME);
if (amountToFill <= 0 || (isCanister && slotItem.getItem() != canisterType && slotItem.getItemDamage() != ItemCanisterGeneric.EMPTY)) {
return;
}
if (isCanister) {
inventory[slot] = new ItemStack(canisterType, 1, slotItem.getItemDamage() - amountToFill);
tank.drain(amountToFill, true);
} else if (amountToFill == FluidContainerRegistry.BUCKET_VOLUME) {
inventory[slot] = FluidContainerRegistry.fillFluidContainer(liquid, inventory[slot]);
if (inventory[slot] == null) {
// Failed to fill container: restore item that was there before
inventory[slot] = slotItem;
} else {
tank.drain(amountToFill, true);
}
}
}
use of micdoodle8.mods.galacticraft.core.items.ItemCanisterGeneric in project Galacticraft by micdoodle8.
the class FluidUtil method interactWithTank.
public static boolean interactWithTank(ItemStack container, EntityPlayer playerIn, TileEntityFluidTank tank, EnumFacing side) {
if (container == null || playerIn.worldObj.isRemote) {
return true;
}
ItemStack result;
if (container.getItem() instanceof ItemCanisterGeneric) {
if ((result = FluidUtil.tryEmptyCanister(container, tank, side, playerIn.capabilities.isCreativeMode)) != null || (result = FluidUtil.tryFillCanister(container, tank, side, playerIn.capabilities.isCreativeMode)) != null) {
// send inventory updates to client
if (playerIn.inventoryContainer != null) {
playerIn.inventoryContainer.detectAndSendChanges();
}
}
return true;
}
// The following code deals with a bug in 1.8.9 (filling a bucket from a tank with less than 1000mB returns an empty bucket, but clears out the tank)
// This code may not be required in 1.10.2+
int slot = playerIn.inventory.currentItem;
if ((result = FluidUtil.tryFillBucket(container, tank, side)) != null || (result = net.minecraftforge.fluids.FluidUtil.tryEmptyBucket(container, tank, side)) != null) {
if (!playerIn.capabilities.isCreativeMode) {
playerIn.inventory.decrStackSize(slot, 1);
ItemHandlerHelper.giveItemToPlayer(playerIn, result, slot);
}
if (playerIn.inventoryContainer != null) {
playerIn.inventoryContainer.detectAndSendChanges();
}
return true;
} else {
// Code for IFluidContainerItems - unchanged from Forge
ItemStack copy = container.copy();
boolean changedBucket = false;
if (ItemStack.areItemsEqual(container, FluidContainerRegistry.EMPTY_BUCKET) && FluidRegistry.isUniversalBucketEnabled()) {
container = new ItemStack(ForgeModContainer.getInstance().universalBucket, copy.stackSize);
changedBucket = true;
}
if (net.minecraftforge.fluids.FluidUtil.tryFillFluidContainerItem(container, tank, side, playerIn) || net.minecraftforge.fluids.FluidUtil.tryEmptyFluidContainerItem(container, tank, side, playerIn)) {
if (playerIn.capabilities.isCreativeMode) {
playerIn.inventory.setInventorySlotContents(slot, copy);
} else {
if (changedBucket && container.stackSize != copy.stackSize) {
copy.stackSize = container.stackSize;
playerIn.inventory.setInventorySlotContents(slot, copy);
} else {
if (copy.stackSize > 1) {
playerIn.inventory.setInventorySlotContents(slot, container);
} else {
playerIn.inventory.setInventorySlotContents(slot, null);
ItemHandlerHelper.giveItemToPlayer(playerIn, container, slot);
}
}
}
if (playerIn.inventoryContainer != null) {
playerIn.inventoryContainer.detectAndSendChanges();
}
return true;
}
}
return false;
}
Aggregations