Unify code to fill buckets

This commit is contained in:
HenryLoenwind committed 2015-04-04 17:14:59 +02:00
1 parent f95895a38d
commit ebfdf54091
6 files changed
+172 -117

No files matched your search

@@ -102,47 +102,10 @@ public class BlockTank extends AbstractMachineBlock<TileTank> implements IAdvanc
}
}
//now check for empty fluid containers to fill
FluidStack available = tank.tank.getFluid();
if(available != null) {
ItemStack res = FluidContainerRegistry.fillFluidContainer(available.copy(), item);
FluidStack filled = FluidContainerRegistry.getFluidForFilledItem(res);
if(filled == null) { //this shouldn't be necessary but it appears to be a bug as the above method doesnt work
FluidContainerData[] datas = FluidContainerRegistry.getRegisteredFluidContainerData();
for (FluidContainerData data : datas) {
if(data != null && data.fluid != null && data.fluid.getFluid() != null &&
data.fluid.getFluid().getName() != null && data.emptyContainer != null &&
data.fluid.getFluid().getName().equals(available.getFluid().getName()) &&
data.emptyContainer.isItemEqual(item)) {
res = data.filledContainer.copy();
filled = FluidContainerRegistry.getFluidForFilledItem(res);
}
}
}
if(filled != null) {
tank.drainInternal(filled, true);
if(item.stackSize > 1) {
item.stackSize--;
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, item);
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if(entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, res);
return true;
}
}
if(!world.isRemote) {
Util.dropItems(world, res, x, y, z, true);
}
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, res);
}
return true;
}
if (FluidUtil.fillPlayerHandItemFromInternalTank(world, x, y, z, entityPlayer, tank)) {
return true;
}
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
@@ -9,6 +9,7 @@ import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.machine.AbstractMachineEntity;
@@ -17,8 +18,10 @@ import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
import crazypants.util.ItemUtil;
public class TileTank extends AbstractMachineEntity implements IFluidHandler {
public class TileTank extends AbstractMachineEntity implements IFluidHandler, ITankAccess {
private static int IO_MB_TICK = 100;
@@ -275,49 +278,32 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler {
return false;
}
ItemStack filledItem = FluidContainerRegistry.fillFluidContainer(tank.getFluid(), toFill);
FluidStack filledFluid = FluidContainerRegistry.getFluidForFilledItem(filledItem);
ItemStack filledContainer = FluidUtil.fillContainerFromInternalTank(this, toFill, false);
if (filledContainer == null) {
return false;
}
if(filledFluid == null) { //this shouldn't be necessary but it appears to be a bug as the above method doesnt work
FluidContainerData[] datas = FluidContainerRegistry.getRegisteredFluidContainerData();
for (FluidContainerData data : datas) {
if(data.fluid.getFluid().getName().equals(tank.getFluid().getFluid().getName()) && data.emptyContainer.isItemEqual(toFill)) {
filledItem = data.filledContainer.copy();
filledFluid = FluidContainerRegistry.getFluidForFilledItem(filledItem);
}
if (inventory[3] != null) {
if (filledContainer.isStackable() && ItemUtil.areStackMergable(inventory[3], filledContainer)
&& inventory[3].stackSize < inventory[3].getMaxStackSize()) {
filledContainer.stackSize += inventory[3].stackSize;
} else {
return false;
}
}
if(filledFluid == null || filledItem == null) {
return false;
}
if(filledFluid.amount > tank.getFluidAmount()) {
return false;
}
if(inventory[3] != null) {
if(!inventory[3].isItemEqual(filledItem) || inventory[3].getMaxStackSize() < inventory[3].stackSize + 1) {
return false; //can't stack the full container
}
}
tank.drain(filledFluid.amount, true);
tankDirty = true;
FluidUtil.fillContainerFromInternalTank(this, toFill, true);
toFill = toFill.copy();
toFill.stackSize--;
if(toFill.stackSize == 0) {
if (toFill.stackSize == 0) {
setInventorySlotContents(1, null);
} else {
setInventorySlotContents(1, toFill);
}
if(inventory[3] == null) {
setInventorySlotContents(3, filledItem);
} else {
ItemStack newStack = inventory[3].copy();
newStack.stackSize++;
setInventorySlotContents(3, newStack);
}
setInventorySlotContents(3, filledContainer);
markDirty();
return false;
}
@@ -397,4 +383,19 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler {
}
}
@Override
public FluidTank getInputTank() {
return tank;
}
@Override
public FluidTank getOutputTank() {
return tank;
}
@Override
public void setTanksDirty() {
tankDirty = true;
}
}
@@ -25,7 +25,6 @@ import crazypants.render.VertexRotation;
import crazypants.util.FluidUtil;
import crazypants.util.Util;
import crazypants.vecmath.Vector3d;
import static crazypants.util.FluidUtil.isValidFluid;
public class BlockVat extends AbstractMachineBlock<TileVat> {
@@ -159,49 +158,8 @@ public class BlockVat extends AbstractMachineBlock<TileVat> {
}
}
//now check for empty fluid containers to fill
FluidStack available = vat.outputTank.getFluid();
if(isValidFluid(available)) {
ItemStack res = FluidContainerRegistry.fillFluidContainer(available.copy(), item);
FluidStack filled = FluidContainerRegistry.getFluidForFilledItem(res);
if(filled == null) { //this shouldn't be necessary but it appears to be a bug as the above method doesnt work
FluidContainerData[] datas = FluidContainerRegistry.getRegisteredFluidContainerData();
for (FluidContainerData data : datas) {
if(data != null && isValidFluid(data.fluid) && data.fluid.getFluid().getName().equals(available.getFluid().getName())
&& data.emptyContainer.isItemEqual(item)) {
res = data.filledContainer.copy();
filled = FluidContainerRegistry.getFluidForFilledItem(res);
}
}
}
if(isValidFluid(filled)) {
if(filled.amount > available.amount) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
vat.drain(ForgeDirection.DOWN, filled, true);
if(item.stackSize > 1) {
item.stackSize--;
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, item);
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if(entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, res);
return true;
}
}
if(!world.isRemote) {
Util.dropItems(world, res, x, y, z, true);
}
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, res);
}
return true;
}
if (FluidUtil.fillPlayerHandItemFromInternalTank(world, x, y, z, entityPlayer, vat)) {
return true;
}
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
@@ -19,8 +19,9 @@ import crazypants.enderio.machine.SlotDefinition;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public class TileVat extends AbstractPoweredTaskEntity implements IFluidHandler {
public class TileVat extends AbstractPoweredTaskEntity implements IFluidHandler, ITankAccess {
final FluidTank inputTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 8);
final FluidTank outputTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 8);
@@ -327,4 +328,19 @@ public class TileVat extends AbstractPoweredTaskEntity implements IFluidHandler
this.currentTask = currentTask;
}
@Override
public FluidTank getInputTank() {
return inputTank;
}
@Override
public FluidTank getOutputTank() {
return outputTank;
}
@Override
public void setTanksDirty() {
tanksDirty = true;
}
}
@@ -1,24 +1,30 @@
package crazypants.util;
import static crazypants.util.FluidUtil.isValidFluid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidBlock;
import net.minecraftforge.fluids.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
import cpw.mods.fml.common.Loader;
import crazypants.enderio.Log;
import crazypants.enderio.conduit.IConduitBundle;
@@ -166,4 +172,102 @@ public class FluidUtil {
return fluidStack != null && fluidStack.getFluid() != null && fluidStack.getFluid().getName() != null;
}
public static ItemStack fillContainerFromInternalTank(ITankAccess tank, ItemStack item, boolean drain) {
FluidStack available = tank.getOutputTank().getFluid();
if (isValidFluid(available)) {
ItemStack res = FluidContainerRegistry.fillFluidContainer(available.copy(), item);
FluidStack filled = FluidContainerRegistry.getFluidForFilledItem(res);
/*
* We had some code here that replicated the two methods above if they did
* not return a result. As far as *I* know, that is not needed (anymore?).
* It works without it for vanilla buckets and Forestry cans. In the case
* of issues like "cannot fill xxx anymore", that code needs to be
* re-inserted here.
*/
if (isValidFluid(filled) && (filled.amount <= available.amount)) {
if (drain) {
tank.getOutputTank().drain(filled.amount, true);
tank.setTanksDirty();
}
return res;
}
}
return null;
}
/**
* If the currently held item of the given player can be filled with the
* liquid in the given tank's output tank, do so and put the resultant filled
* container item where it can go. This will also drain the tank and set it to
* dirty.
*
* <p>
* Cases handled for the the filled container:
*
* <ul>
* <li>If the stacksize of the held item is one, then it will be replaced by
* the filled container unless the player in in creative.
* <li>If the filled container is stackable and the player already has a
* non-maxed stack in the inventory, it is put there.
* <li>If the player has space in his inventory, it is put there.
* <li>Otherwise it will be dropped on the ground between the position given
* as parameter and the player's position.
* </ul>
*
* @param world
* @param x
* @param y
* @param z
* @param entityPlayer
* @param tank
* @return true if a container was filled, false otherwise
*/
public static boolean fillPlayerHandItemFromInternalTank(World world, int x, int y, int z, EntityPlayer entityPlayer,
ITankAccess tank) {
ItemStack item = entityPlayer.inventory.getCurrentItem();
if (item == null || item.getItem() == null) {
return false;
}
ItemStack filledContainer = fillContainerFromInternalTank(tank, item, !entityPlayer.capabilities.isCreativeMode);
if (filledContainer == null) {
return false;
}
if (item.stackSize == 1 && !entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, filledContainer);
} else {
if (!entityPlayer.capabilities.isCreativeMode) {
item.stackSize--;
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, item);
}
if (filledContainer.isStackable()) {
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
ItemStack inventoryItem = entityPlayer.inventory.mainInventory[i];
if (ItemUtil.areStackMergable(inventoryItem, filledContainer)
&& inventoryItem.stackSize < inventoryItem.getMaxStackSize()) {
filledContainer.stackSize += inventoryItem.stackSize;
entityPlayer.inventory.setInventorySlotContents(i, filledContainer);
return true;
}
}
}
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if (entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, filledContainer);
return true;
}
}
if (!world.isRemote) {
double x0 = (x + entityPlayer.posX) / 2.0D;
double y0 = (y + entityPlayer.posY) / 2.0D + 0.5D;
double z0 = (z + entityPlayer.posZ) / 2.0D;
Util.dropItems(world, filledContainer, x0, y0, z0, true);
}
}
return true;
}
}
@@ -0,0 +1,13 @@
package crazypants.util;
import net.minecraftforge.fluids.FluidTank;
public interface ITankAccess {
FluidTank getInputTank();
FluidTank getOutputTank();
void setTanksDirty();
}