Unify code to fill/empty buckets, Part 2

* New: Emptying buckets
* Fix: Fixed many bugs, including some item-loss types
* Full IN+OUT: Tank, Reservoir, XP Obelisk, Soul Binder
* IN for input liquid, OUT for output liquid: Vat
* IN for fuel, OUT fro XP: KillerJoe
* IN for fuel: Zombie Generator
* IN for fuel/coolant: Combustion Generator
This commit is contained in:
HenryLoenwind committed 2015-04-12 23:18:58 +02:00
1 parent d99aa045ba
commit f2f6bdd325
22 files changed
+541 -408

No files matched your search

@@ -21,6 +21,8 @@ import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.api.tool.ITool;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.tool.ToolUtil;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public abstract class BlockEio extends Block {
@@ -91,6 +93,14 @@ public abstract class BlockEio extends Block {
world.markBlockForUpdate(x, y, z);
return true;
}
if (te instanceof ITankAccess) {
if (FluidUtil.fillInternalTankFromPlayerHandItem(world, x, y, z, entityPlayer, (ITankAccess) te)) {
return true;
}
if (FluidUtil.fillPlayerHandItemFromInternalTank(world, x, y, z, entityPlayer, (ITankAccess) te)) {
return true;
}
}
}
if(entityPlayer.isSneaking()) {
@@ -454,6 +454,8 @@ public final class Config {
public static boolean replaceWitherSkeletons = true;
public static boolean enableWaterFromBottles = true;
public static boolean telepadLockDimension = true;
public static boolean telepadLockCoords = true;
@@ -1212,6 +1214,14 @@ public final class Config {
replaceWitherSkeletons = config.get(sectionMisc.name, "replaceWitherSkeletons", replaceWitherSkeletons,
"Separates wither and normal skeletons into different entities, enables the powered spawner to treat them differently [EXPERIMENTAL - MAY CAUSE ISSUES WITH OTHER MODS]").getBoolean();
enableWaterFromBottles = config
.get(
sectionMisc.name,
"enableWaterFromBottles",
enableWaterFromBottles,
"Enables emptying vanilla water bottles without breaking the bottle. In combination with a water source block this allows duping of water without cost.")
.getBoolean();
telepadLockDimension = config.get(sectionTelepad.name, "lockDimension", telepadLockDimension,
"If true, the dimension cannot be set via the GUI, the coord selector must be used.").getBoolean();
telepadLockCoords = config.get(sectionTelepad.name, "lockCoords", telepadLockCoords,
@@ -9,7 +9,6 @@ import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.IIcon;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.GuiHandler;
@@ -18,8 +17,6 @@ import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.IoMode;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.FluidUtil;
import crazypants.util.Util;
public class BlockCombustionGenerator extends AbstractMachineBlock<TileCombustionGenerator> {
@@ -44,36 +41,6 @@ public class BlockCombustionGenerator extends AbstractMachineBlock<TileCombustio
return 0;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
TileEntity te = world.getTileEntity(x, y, z);
if(!(te instanceof TileCombustionGenerator)) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileCombustionGenerator gen = (TileCombustionGenerator) te;
ItemStack item = entityPlayer.inventory.getCurrentItem();
if(item == null) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
//check for filled fluid containers and see if we can empty them into our tanks
FluidStack fluid = FluidUtil.getFluidFromItem(item);
if(fluid != null) {
int filled = gen.fill(ForgeDirection.UP, fluid, false);
if(filled >= fluid.amount) {
gen.fill(ForgeDirection.UP, fluid, true);
if(!entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(item));
}
return true;
}
}
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
@@ -21,8 +21,9 @@ import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.power.PowerDistributor;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public class TileCombustionGenerator extends AbstractGeneratorEntity implements IFluidHandler {
public class TileCombustionGenerator extends AbstractGeneratorEntity implements IFluidHandler, ITankAccess {
private final FluidTank coolantTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 5);
private final FluidTank fuelTank = new FluidTank(FluidContainerRegistry.BUCKET_VOLUME * 5);
@@ -416,4 +417,25 @@ public class TileCombustionGenerator extends AbstractGeneratorEntity implements
return fuelTank;
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
if (FluidFuelRegister.instance.getCoolant(forFluidType.getFluid()) != null) {
return coolantTank;
}
if (FluidFuelRegister.instance.getFuel(forFluidType.getFluid()) != null) {
return fuelTank;
}
return null;
}
@Override
public FluidTank[] getOutputTanks() {
return new FluidTank[] { /* coolantTank, fuelTank */};
}
@Override
public void setTanksDirty() {
tanksDirty = true;
}
}
@@ -7,12 +7,10 @@ import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.particle.EntityFX;
import net.minecraft.entity.player.EntityPlayer;
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.FluidStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.EnderIO;
@@ -21,8 +19,6 @@ import crazypants.enderio.ModObject;
import crazypants.enderio.config.Config;
import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.FluidUtil;
import crazypants.util.Util;
import crazypants.util.Lang;
public class BlockZombieGenerator extends AbstractMachineBlock<TileZombieGenerator> {
@@ -40,36 +36,6 @@ public class BlockZombieGenerator extends AbstractMachineBlock<TileZombieGenerat
super(ModObject.blockZombieGenerator, TileZombieGenerator.class, Material.anvil);
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
TileEntity te = world.getTileEntity(x, y, z);
if(!(te instanceof TileZombieGenerator)) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileZombieGenerator gen = (TileZombieGenerator) te;
ItemStack item = entityPlayer.inventory.getCurrentItem();
if(item == null) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
//check for filled fluid containers and see if we can empty them into our tanks
FluidStack fluid = FluidUtil.getFluidFromItem(item);
if(fluid != null) {
int filled = gen.fill(ForgeDirection.UP, fluid, false);
if(filled >= fluid.amount) {
gen.fill(ForgeDirection.UP, fluid, true);
if(!entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(item));
}
return true;
}
}
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
@Override
public boolean isSideSolid(IBlockAccess world, int x, int y, int z, ForgeDirection side) {
return true;
@@ -7,6 +7,7 @@ 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.IFluidHandler;
import crazypants.enderio.EnderIO;
@@ -19,8 +20,9 @@ import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.power.PowerDistributor;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public class TileZombieGenerator extends AbstractGeneratorEntity implements IFluidHandler {
public class TileZombieGenerator extends AbstractGeneratorEntity implements IFluidHandler, ITankAccess {
private static int IO_MB_TICK = 250;
@@ -267,4 +269,22 @@ public class TileZombieGenerator extends AbstractGeneratorEntity implements IFlu
nbtRoot.setBoolean("active", active);
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
if (forFluidType != null && forFluidType.getFluid() == EnderIO.fluidNutrientDistillation) {
return fuelTank;
}
return null;
}
@Override
public FluidTank[] getOutputTanks() {
return new FluidTank[] { /* fuelTank */};
}
@Override
public void setTanksDirty() {
tanksDirty = true;
}
}
@@ -3,31 +3,17 @@ package crazypants.enderio.machine.killera;
import net.minecraft.block.Block;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import cpw.mods.fml.relauncher.Side;
import crazypants.enderio.GuiHandler;
import crazypants.enderio.ModObject;
import crazypants.enderio.gui.IResourceTooltipProvider;
import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.machine.generator.zombie.ContainerZombieGenerator;
import crazypants.enderio.machine.generator.zombie.GuiZombieGenerator;
import crazypants.enderio.machine.generator.zombie.PacketZombieTank;
import crazypants.enderio.machine.generator.zombie.TileZombieGenerator;
import crazypants.enderio.machine.tank.BlockTank;
import crazypants.enderio.machine.tank.PacketTank;
import crazypants.enderio.machine.tank.TileTank;
import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.xp.PacketExperianceContainer;
import crazypants.enderio.xp.PacketGivePlayerXP;
import crazypants.util.FluidUtil;
import crazypants.util.Util;
/**
* Name proudly created by Xaw4
@@ -65,36 +51,6 @@ public class BlockKillerJoe extends AbstractMachineBlock<TileKillerJoe> {
}
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
TileEntity te = world.getTileEntity(x, y, z);
if(!(te instanceof TileKillerJoe)) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileKillerJoe gen = (TileKillerJoe) te;
ItemStack item = entityPlayer.inventory.getCurrentItem();
if(item == null) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
//check for filled fluid containers and see if we can empty them into our tank
FluidStack fluid = FluidUtil.getFluidFromItem(item);
if(fluid != null) {
int filled = gen.fill(ForgeDirection.UP, fluid, false);
if(filled >= fluid.amount) {
gen.fill(ForgeDirection.UP, fluid, true);
if(!entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(item));
}
return true;
}
}
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
return new ContainerKillerJoe(player.inventory, (TileKillerJoe) world.getTileEntity(x, y, z));
@@ -21,6 +21,7 @@ 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.IFluidHandler;
@@ -42,9 +43,10 @@ import crazypants.render.BoundingBox;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ForgeDirectionOffsets;
import crazypants.util.ITankAccess;
import crazypants.vecmath.Vector3d;
public class TileKillerJoe extends AbstractMachineEntity implements IFluidHandler, IEntitySelector, IHaveExperience {
public class TileKillerJoe extends AbstractMachineEntity implements IFluidHandler, IEntitySelector, IHaveExperience, ITankAccess {
private static final int IO_MB_TICK = 250;
@@ -548,4 +550,26 @@ public class TileKillerJoe extends AbstractMachineEntity implements IFluidHandle
protected void onFinishedPotionEffect(PotionEffect p_70688_1_) {
}
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
if (forFluidType != null && forFluidType.getFluid() == EnderIO.fluidNutrientDistillation) {
return fuelTank;
}
/*
* if (forFluidType != null && forFluidType.getFluid() ==
* EnderIO.fluidXpJuice) { return xpCon; }
*/
return null;
}
@Override
public FluidTank[] getOutputTanks() {
return new FluidTank[] { xpCon /* , fuelTank */};
}
@Override
public void setTanksDirty() {
tanksDirty = true;
}
}
@@ -97,7 +97,7 @@ public class ItemXpTransfer extends Item implements IResourceTooltipProvider {
return false;
}
int xpToGive = XpUtil.liquidToExperiance(res.amount);
int xpToGive = XpUtil.liquidToExperience(res.amount);
player.addExperience(xpToGive);
return true;
@@ -124,7 +124,7 @@ public class ItemXpTransfer extends Item implements IResourceTooltipProvider {
if(takenVolume <= 0) {
return false;
}
int xpToTake = XpUtil.liquidToExperiance(takenVolume);
int xpToTake = XpUtil.liquidToExperience(takenVolume);
XpUtil.addPlayerXP(player, -xpToTake);
return true;
}
@@ -5,6 +5,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.ModObject;
@@ -17,8 +18,9 @@ import crazypants.enderio.xp.IHaveExperience;
import crazypants.enderio.xp.PacketExperianceContainer;
import crazypants.enderio.xp.XpUtil;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public class TileExperienceOblisk extends AbstractMachineEntity implements IFluidHandler, IHaveExperience {
public class TileExperienceOblisk extends AbstractMachineEntity implements IFluidHandler, IHaveExperience, ITankAccess {
private ExperienceContainer xpCont = new ExperienceContainer(XpUtil.getExperienceForLevel(Config.xpObeliskMaxXpLevel));
@@ -111,4 +113,19 @@ public class TileExperienceOblisk extends AbstractMachineEntity implements IFlui
return xpCont;
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
return xpCont;
}
@Override
public FluidTank[] getOutputTanks() {
return new FluidTank[] { xpCont };
}
@Override
public void setTanksDirty() {
xpCont.setDirty(true);
}
}
@@ -4,7 +4,6 @@ import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.renderer.texture.IIconRegister;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
@@ -12,9 +11,6 @@ import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.BlockEio;
@@ -23,8 +19,6 @@ import crazypants.enderio.gui.IResourceTooltipProvider;
import crazypants.enderio.machine.reservoir.TileReservoir.Pos;
import crazypants.enderio.tool.ToolUtil;
import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.Util;
import crazypants.vecmath.Vector3d;
public class BlockReservoir extends BlockEio implements IResourceTooltipProvider {
@@ -63,61 +57,17 @@ public class BlockReservoir extends BlockEio implements IResourceTooltipProvider
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
TileEntity te;
ItemStack current = entityPlayer.inventory.getCurrentItem();
if(current != null) {
TileReservoir tank = (TileReservoir) world.getTileEntity(x, y, z);
FluidStack liquid = FluidUtil.getFluidFromItem(current);
if(liquid != null) {
// Handle filled containers
int qty = tank.getController().doFill(ForgeDirection.UNKNOWN, liquid, true);
if(qty != 0 && !entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(current));
if (!entityPlayer.isSneaking() && entityPlayer.inventory.getCurrentItem() != null
&& (te = world.getTileEntity(x, y, z)) instanceof TileReservoir) {
TileReservoir tank = ((TileReservoir) te).getController();
if (ToolUtil.isToolEquipped(entityPlayer) && tank.isMultiblock()) {
tank.setAutoEject(!tank.isAutoEject());
for (BlockCoord bc : tank.multiblock) {
world.markBlockForUpdate(bc.x, bc.y, bc.z);
}
return true;
} else {
// Handle empty containers
FluidStack available = tank.getTankInfo(ForgeDirection.UNKNOWN)[0].fluid;
if(available != null && available.amount > 0) {
ItemStack filled = FluidContainerRegistry.fillFluidContainer(available, current);
if(current.getItem() == Items.bucket) {
filled = new ItemStack(Items.water_bucket);
}
liquid = FluidContainerRegistry.getFluidForFilledItem(filled);
if(current.getItem() == Items.bucket) {
liquid = new FluidStack(FluidRegistry.WATER, 1000);
}
if(liquid != null) {
if(!entityPlayer.capabilities.isCreativeMode) {
if(current.stackSize > 1) {
if(!entityPlayer.inventory.addItemStackToInventory(filled)) {
return false;
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(current));
}
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(current));
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, filled);
}
}
tank.drain(ForgeDirection.UNKNOWN, liquid.amount, true);
return true;
} else if(ToolUtil.isToolEquipped(entityPlayer) && tank.isMultiblock()) {
tank.setAutoEject(!tank.isAutoEject());
for (BlockCoord bc : tank.multiblock) {
world.markBlockForUpdate(bc.x, bc.y, bc.z);
}
}
}
}
}
@@ -11,15 +11,17 @@ import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidRegistry;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.EnderIO;
import crazypants.enderio.TileEntityEio;
import crazypants.render.BoundingBox;
import crazypants.util.BlockCoord;
import crazypants.util.ITankAccess;
import crazypants.vecmath.Vector3f;
public class TileReservoir extends TileEntityEio implements IFluidHandler {
public class TileReservoir extends TileEntityEio implements IFluidHandler, ITankAccess {
enum Pos {
TL(true, false),
@@ -662,4 +664,33 @@ public class TileReservoir extends TileEntityEio implements IFluidHandler {
return false;
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
if (forFluidType != null && forFluidType.getFluid() == FluidRegistry.WATER) {
return tank;
}
return null;
}
@Override
public FluidTank[] getOutputTanks() {
if (isMaster()) {
return new FluidTank[] { tank, regenTank };
} else {
return new FluidTank[] { tank };
}
}
@Override
public void setTanksDirty() {
if (isMaster() && regenTank != null && tank != null) {
if (!regenTank.isFull() && tank.getAmount() > 0) {
int toMove = Math.min(tank.getAmount(), regenTank.getCapacity() - regenTank.getAmount());
regenTank.setAmount(regenTank.getAmount() + toMove);
tank.setAmount(tank.getAmount() - toMove);
}
}
tankDirty = true;
}
}
@@ -15,15 +15,6 @@ import crazypants.enderio.GuiHandler;
import crazypants.enderio.ModObject;
import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.MachineRecipeRegistry;
import crazypants.enderio.machine.farm.BlockFarmStation;
import crazypants.enderio.machine.farm.PacketFarmAction;
import crazypants.enderio.machine.farm.PacketUpdateNotification;
import crazypants.enderio.machine.farm.TileFarmStation;
import crazypants.enderio.machine.painter.GuiPainter;
import crazypants.enderio.machine.painter.PainterContainer;
import crazypants.enderio.machine.painter.TileEntityPainter;
import crazypants.enderio.network.PacketHandler;
import crazypants.enderio.xp.PacketDrainPlayerXP;
import crazypants.enderio.xp.PacketExperianceContainer;
@@ -7,6 +7,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.ModObject;
@@ -25,8 +26,9 @@ import crazypants.enderio.xp.IHaveExperience;
import crazypants.enderio.xp.PacketExperianceContainer;
import crazypants.enderio.xp.XpUtil;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
public class TileSoulBinder extends AbstractPoweredTaskEntity implements IHaveExperience, IFluidHandler {
public class TileSoulBinder extends AbstractPoweredTaskEntity implements IHaveExperience, IFluidHandler, ITankAccess {
public static final int POWER_PER_TICK_ONE = Config.soulBinderLevelOnePowerPerTickRF;
private static final BasicCapacitor CAP_ONE = new BasicCapacitor(POWER_PER_TICK_ONE * 2,
@@ -206,6 +208,19 @@ public class TileSoulBinder extends AbstractPoweredTaskEntity implements IHaveEx
xpCont.writeToNBT(nbtRoot);
}
@Override
public FluidTank getInputTank(FluidStack forFluidType) {
return xpCont;
}
@Override
public FluidTank[] getOutputTanks() {
return new FluidTank[] { xpCont };
}
@Override
public void setTanksDirty() {
xpCont.setDirty(true);
}
}
@@ -15,8 +15,6 @@ import net.minecraft.util.IIcon;
import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.relauncher.Side;
@@ -31,9 +29,7 @@ import crazypants.enderio.machine.AbstractMachineBlock;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.power.PowerDisplayUtil;
import crazypants.enderio.network.PacketHandler;
import crazypants.util.FluidUtil;
import crazypants.util.Lang;
import crazypants.util.Util;
public class BlockTank extends AbstractMachineBlock<TileTank> implements IAdvancedTooltipProvider {
@@ -75,40 +71,6 @@ public class BlockTank extends AbstractMachineBlock<TileTank> implements IAdvanc
return new TileTank(metadata);
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
TileEntity te = world.getTileEntity(x, y, z);
if(!(te instanceof TileTank)) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileTank tank = (TileTank) te;
ItemStack item = entityPlayer.inventory.getCurrentItem();
if(item == null) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
//check for filled fluid containers and see if we can empty them into our input tank
FluidStack fluid = FluidUtil.getFluidFromItem(item);
if(fluid != null) {
int filled = tank.fillInternal(fluid, false);
if(filled >= fluid.amount) {
tank.fillInternal(fluid, true);
if(!entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(item));
}
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);
}
@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) {
TileEntity te = world.getTileEntity(x, y, z);
@@ -11,6 +11,7 @@ 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.IFluidContainerItem;
import net.minecraftforge.fluids.IFluidHandler;
import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.IoMode;
@@ -20,6 +21,7 @@ import crazypants.util.BlockCoord;
import crazypants.util.FluidUtil;
import crazypants.util.ITankAccess;
import crazypants.util.ItemUtil;
import crazypants.util.FluidUtil.FluidAndStackResult;
public class TileTank extends AbstractMachineEntity implements IFluidHandler, ITankAccess {
@@ -220,8 +222,16 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
if(item.getItem() == Items.lava_bucket) {
return true;
}
if (item.getItem() instanceof IFluidContainerItem && ((IFluidContainerItem) item.getItem()).getFluid(item) != null) {
return true;
}
return false;
} else if(i == 1) {
if (item.getItem() instanceof IFluidContainerItem
&& (((IFluidContainerItem) item.getItem()).getFluid(item) == null || ((IFluidContainerItem) item.getItem())
.getFluid(item).amount < ((IFluidContainerItem) item.getItem()).getCapacity(item))) {
return true;
}
return FluidContainerRegistry.isEmptyContainer(item) || item.getItem() == Items.bucket;
}
return false;
@@ -265,85 +275,49 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
}
private boolean fillEmptyContainer() {
ItemStack toFill = inventory[1];
if(toFill == null) {
return false;
}
if(tank.getFluidAmount() <= 0) {
return false;
}
ItemStack filledContainer = FluidUtil.fillContainerFromInternalTank(this, toFill, false);
if (filledContainer == null) {
FluidAndStackResult fill = FluidUtil.tryFillContainer(inventory[1], getOutputTanks()[0].getFluid());
if (fill.result.fluidStack == null) {
return false;
}
if (inventory[3] != null) {
if (filledContainer.isStackable() && ItemUtil.areStackMergable(inventory[3], filledContainer)
if (inventory[3].isStackable() && ItemUtil.areStackMergable(inventory[3], fill.result.itemStack)
&& inventory[3].stackSize < inventory[3].getMaxStackSize()) {
filledContainer.stackSize += inventory[3].stackSize;
fill.result.itemStack.stackSize += inventory[3].stackSize;
} else {
return false;
}
}
FluidUtil.fillContainerFromInternalTank(this, toFill, true);
toFill = toFill.copy();
toFill.stackSize--;
if (toFill.stackSize == 0) {
setInventorySlotContents(1, null);
} else {
setInventorySlotContents(1, toFill);
}
setInventorySlotContents(3, filledContainer);
getOutputTanks()[0].setFluid(fill.remainder.fluidStack);
setInventorySlotContents(1, fill.remainder.itemStack);
setInventorySlotContents(3, fill.result.itemStack);
setTanksDirty();
markDirty();
return false;
}
private boolean drainFullContainer() {
ItemStack fillFrom = inventory[0];
if(fillFrom == null) {
FluidAndStackResult fill = FluidUtil.tryDrainContainer(inventory[0], this);
if (fill.result.fluidStack == null) {
return false;
}
FluidStack fluid = FluidUtil.getFluidFromItem(fillFrom);
if(fluid == null) {
return false;
}
ItemStack emptyItem = FluidUtil.getEmptyContainer(fillFrom);
if(emptyItem != null && inventory[2] != null) {
if(!inventory[2].isItemEqual(emptyItem) || inventory[2].getMaxStackSize() < inventory[2].stackSize + 1) {
return false; //can't stack the empty container
if (inventory[2] != null) {
if (inventory[2].isStackable() && ItemUtil.areStackMergable(inventory[2], fill.result.itemStack)
&& inventory[2].stackSize < inventory[2].getMaxStackSize()) {
fill.result.itemStack.stackSize += inventory[2].stackSize;
} else {
return false;
}
}
int filled = fillInternal(fluid, false);
if(filled < fluid.amount) {
return false; //can't empty the entire thing
}
fillInternal(fluid, true);
getInputTank(fill.result.fluidStack).setFluid(fill.remainder.fluidStack);
setInventorySlotContents(0, fill.remainder.itemStack);
setInventorySlotContents(2, fill.result.itemStack);
fillFrom = fillFrom.copy();
fillFrom.stackSize--;
if(fillFrom.stackSize == 0) {
setInventorySlotContents(0, null);
} else {
setInventorySlotContents(0, fillFrom);
}
if(emptyItem == null) {
return true;
}
if(inventory[2] == null) {
setInventorySlotContents(2, emptyItem);
} else {
ItemStack newStack = inventory[2].copy();
newStack.stackSize++;
setInventorySlotContents(2, newStack);
}
setTanksDirty();
markDirty();
return false;
}
@@ -379,13 +353,13 @@ public class TileTank extends AbstractMachineEntity implements IFluidHandler, IT
}
@Override
public FluidTank getInputTank() {
public FluidTank getInputTank(FluidStack forFluidType) {
return tank;
}
@Override
public FluidTank getOutputTank() {
return tank;
public FluidTank[] getOutputTanks() {
return new FluidTank[] { tank };
}
@Override
@@ -10,9 +10,6 @@ import net.minecraft.util.IIcon;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.FluidContainerRegistry;
import net.minecraftforge.fluids.FluidContainerRegistry.FluidContainerData;
import net.minecraftforge.fluids.FluidStack;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import crazypants.enderio.GuiHandler;
@@ -22,10 +19,7 @@ import crazypants.enderio.machine.AbstractMachineEntity;
import crazypants.enderio.machine.IoMode;
import crazypants.enderio.network.PacketHandler;
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> {
@@ -127,44 +121,6 @@ public class BlockVat extends AbstractMachineBlock<TileVat> {
return blockIcon;
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityPlayer, int par6, float par7, float par8, float par9) {
if (entityPlayer.isSneaking()) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileEntity te = world.getTileEntity(x, y, z);
if(!(te instanceof TileVat)) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
TileVat vat = (TileVat) te;
ItemStack item = entityPlayer.inventory.getCurrentItem();
if(item == null) {
return super.onBlockActivated(world, x, y, z, entityPlayer, par6, par7, par8, par9);
}
//check for filled fluid containers and see if we can empty them into our input tank
FluidStack fluid = FluidUtil.getFluidFromItem(item);
if(isValidFluid(fluid)) {
int filled = vat.fill(ForgeDirection.UP, fluid, false);
if(filled >= fluid.amount) {
vat.fill(ForgeDirection.UP, fluid, true);
if(!entityPlayer.capabilities.isCreativeMode) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, Util.consumeItem(item));
}
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);
}
@Override
public int getRenderType() {
return renderId;
@@ -329,13 +329,13 @@ public class TileVat extends AbstractPoweredTaskEntity implements IFluidHandler,
}
@Override
public FluidTank getInputTank() {
public FluidTank getInputTank(FluidStack forFluidType) {
return inputTank;
}
@Override
public FluidTank getOutputTank() {
return outputTank;
public FluidTank[] getOutputTanks() {
return new FluidTank[] { outputTank /* , inputTank */};
}
@Override
@@ -1,15 +1,20 @@
package crazypants.enderio.xp;
import java.security.InvalidParameterException;
import io.netty.buffer.ByteBuf;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
import net.minecraftforge.fluids.FluidTankInfo;
import crazypants.enderio.EnderIO;
public class ExperienceContainer {
public class ExperienceContainer extends FluidTank {
// Note: We extend FluidTank instead of implementing IFluidTank because it has
// some methods we need.
private int experienceLevel;
private float experience;
@@ -22,6 +27,7 @@ public class ExperienceContainer {
}
public ExperienceContainer(int maxStored) {
super(null, 0);
maxXp = maxStored;
}
@@ -141,7 +147,7 @@ public class ExperienceContainer {
int available = getFluidAmount();
int canDrain = Math.min(available, maxDrain);
if(doDrain) {
int newXp = experienceTotal - XpUtil.liquidToExperiance(canDrain);
int newXp = experienceTotal - XpUtil.liquidToExperience(canDrain);
experience = 0;
experienceLevel = 0;
experienceTotal = 0;
@@ -165,7 +171,7 @@ public class ExperienceContainer {
return 0;
}
//need to do these calcs in XP instead of fluid space to avoid type overflows
int xp = XpUtil.liquidToExperiance(resource.amount);
int xp = XpUtil.liquidToExperience(resource.amount);
int xpSpace = getMaximumExperiance() - getExperienceTotal();
int canFillXP = Math.min(xp, xpSpace);
if(canFillXP <= 0) {
@@ -186,32 +192,36 @@ public class ExperienceContainer {
return new FluidTankInfo[0];
}
return new FluidTankInfo[] {
new FluidTankInfo(new FluidStack(EnderIO.fluidXpJuice, getFluidAmount()), getMaxFluidAmount())
new FluidTankInfo(new FluidStack(EnderIO.fluidXpJuice, getFluidAmount()), getCapacity())
};
}
private int getMaxFluidAmount() {
@Override
public int getCapacity() {
if(maxXp == Integer.MAX_VALUE) {
return Integer.MAX_VALUE;
}
return XpUtil.experienceToLiquid(maxXp);
}
private int getFluidAmount() {
@Override
public int getFluidAmount() {
return XpUtil.experienceToLiquid(experienceTotal);
}
public void readFromNBT(NBTTagCompound nbtRoot) {
public FluidTank readFromNBT(NBTTagCompound nbtRoot) {
experienceLevel = nbtRoot.getInteger("experienceLevel");
experienceTotal = nbtRoot.getInteger("experienceTotal");
experience = nbtRoot.getFloat("experience");
return this;
}
public void writeToNBT(NBTTagCompound nbtRoot) {
public NBTTagCompound writeToNBT(NBTTagCompound nbtRoot) {
nbtRoot.setInteger("experienceLevel", experienceLevel);
nbtRoot.setInteger("experienceTotal", experienceTotal);
nbtRoot.setFloat("experience", experience);
return nbtRoot;
}
public void toBytes(ByteBuf buf) {
@@ -226,4 +236,45 @@ public class ExperienceContainer {
experience = buf.readFloat();
}
@Override
public FluidStack getFluid() {
return new FluidStack(EnderIO.fluidXpJuice, getFluidAmount());
}
@Override
public FluidTankInfo getInfo() {
return getTankInfo(ForgeDirection.UNKNOWN)[0];
}
@Override
public int fill(FluidStack resource, boolean doFill) {
return fill(ForgeDirection.UNKNOWN, resource, doFill);
}
@Override
public FluidStack drain(int maxDrain, boolean doDrain) {
return drain(ForgeDirection.UNKNOWN, maxDrain, doDrain);
}
@Override
public void setFluid(FluidStack fluid) {
experience = 0;
experienceLevel = 0;
experienceTotal = 0;
if (fluid != null && fluid.getFluid() != null) {
if (EnderIO.fluidXpJuice == fluid.getFluid()) {
addExperience(XpUtil.liquidToExperience(fluid.amount));
} else {
throw new InvalidParameterException(fluid.getFluid() + " is no XP juice");
}
}
xpDirty = true;
}
@Override
public void setCapacity(int capacity) {
throw new InvalidParameterException();
}
}
@@ -16,7 +16,7 @@ public class XpUtil {
public static final int LIQUID_PER_XP_BOTTLE = XP_PER_BOTTLE * RATIO;
public static int liquidToExperiance(int liquid) {
public static int liquidToExperience(int liquid) {
return liquid / RATIO;
}
+256 -65
View File
@@ -1,7 +1,5 @@
package crazypants.util;
import static crazypants.util.FluidUtil.isValidFluid;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -24,10 +22,11 @@ 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 net.minecraftforge.fluids.IFluidTank;
import cpw.mods.fml.common.Loader;
import crazypants.enderio.Log;
import crazypants.enderio.conduit.IConduitBundle;
import crazypants.enderio.config.Config;
public class FluidUtil {
@@ -101,18 +100,6 @@ public class FluidUtil {
return null;
}
public static ItemStack getEmptyContainer(ItemStack stack) {
if(stack.getItem().hasContainerItem(stack)) {
return stack.getItem().getContainerItem(stack);
}
else if(stack.getItem() instanceof ItemPotion && stack.stackTagCompound == null) {
return new ItemStack(Items.glass_bottle);
}
else {
return null;
}
}
public static boolean doPull(IFluidHandler into, ForgeDirection fromDir, int maxVolume) {
TileEntity te = (TileEntity) into;
BlockCoord loc = new BlockCoord(te).getLocation(fromDir);
@@ -168,34 +155,148 @@ public class FluidUtil {
return res;
}
public static boolean isValidFluid(FluidStack fluidStack) {
return fluidStack != null && fluidStack.getFluid() != null && fluidStack.getFluid().getName() != null;
public static FluidAndStackResult tryFillContainer(ItemStack target, FluidStack source) {
if (target != null && target.getItem() != null && source != null && source.getFluid() != null && source.amount > 0) {
if (target.getItem() instanceof IFluidContainerItem) {
ItemStack resultStack = target.copy();
resultStack.stackSize = 1;
int amount = ((IFluidContainerItem) target.getItem()).fill(resultStack, source, true);
if (amount <= 0) {
return new FluidAndStackResult(null, null, target, source);
}
FluidStack resultFluid = source.copy();
resultFluid.amount = amount;
ItemStack remainderStack = target.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = source.copy();
remainderFluid.amount -= amount;
if (remainderFluid.amount <= 0) {
remainderFluid = null;
}
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
ItemStack resultStack = FluidContainerRegistry.fillFluidContainer(source.copy(), target);
if (resultStack != null) {
FluidStack resultFluid = FluidContainerRegistry.getFluidForFilledItem(resultStack);
if (resultFluid != null) {
ItemStack remainderStack = target.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = source.copy();
remainderFluid.amount -= resultFluid.amount;
if (remainderFluid.amount <= 0) {
remainderFluid = null;
}
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
}
}
return new FluidAndStackResult(null, null, target, source);
}
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);
public static FluidAndStackResult tryDrainContainer(ItemStack source, FluidStack target, int capacity) {
if (source != null && source.getItem() != null) {
/*
* 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();
int maxDrain = capacity - (target != null ? target.amount : 0);
if (source.getItem() instanceof IFluidContainerItem) {
ItemStack resultStack = source.copy();
resultStack.stackSize = 1;
FluidStack resultFluid = ((IFluidContainerItem) source.getItem()).drain(resultStack, maxDrain, true);
if (resultFluid == null || resultFluid.amount <= 0 || (target != null && resultFluid.getFluid() != target.getFluid())) {
return new FluidAndStackResult(null, null, source, target);
}
return res;
ItemStack remainderStack = source.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = target != null ? target.copy() : new FluidStack(resultFluid.getFluid(), 0);
remainderFluid.amount += resultFluid.amount;
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
}
return null;
FluidStack resultFluid = FluidContainerRegistry.getFluidForFilledItem(source);
if (resultFluid != null && resultFluid.amount > 0 && resultFluid.amount <= maxDrain
&& (target == null || resultFluid.getFluid() == target.getFluid())) {
ItemStack resultStack = source.getItem().getContainerItem(source);
if (resultStack == null && Config.enableWaterFromBottles && source.getItem() instanceof ItemPotion
&& source.stackTagCompound == null) {
resultStack = new ItemStack(Items.glass_bottle);
}
ItemStack remainderStack = source.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = target != null ? target.copy() : new FluidStack(resultFluid.getFluid(), 0);
remainderFluid.amount += resultFluid.amount;
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
}
return new FluidAndStackResult(null, null, source, target);
}
public static FluidAndStackResult tryDrainContainer(ItemStack source, ITankAccess tank) {
if (source != null && source.getItem() != null) {
if (source.getItem() instanceof IFluidContainerItem) {
IFluidTank targetTank = tank.getInputTank(((IFluidContainerItem) source.getItem()).getFluid(source));
if (targetTank != null) {
FluidStack target = targetTank.getFluid();
int maxDrain = targetTank.getCapacity() - (target != null ? target.amount : 0);
ItemStack resultStack = source.copy();
resultStack.stackSize = 1;
FluidStack resultFluid = ((IFluidContainerItem) source.getItem()).drain(resultStack, maxDrain, true);
if (resultFluid == null || resultFluid.amount <= 0 || (target != null && resultFluid.getFluid() != target.getFluid())) {
return new FluidAndStackResult(null, null, source, target);
}
ItemStack remainderStack = source.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = target != null ? target.copy() : new FluidStack(resultFluid.getFluid(), 0);
remainderFluid.amount += resultFluid.amount;
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
}
FluidStack resultFluid = FluidContainerRegistry.getFluidForFilledItem(source);
if (resultFluid != null && resultFluid.amount > 0) {
IFluidTank targetTank = tank.getInputTank(resultFluid);
if (targetTank != null) {
FluidStack target = targetTank.getFluid();
int maxDrain = targetTank.getCapacity() - (target != null ? target.amount : 0);
if (resultFluid.amount <= maxDrain && (target == null || resultFluid.getFluid() == target.getFluid())) {
ItemStack resultStack = source.getItem().getContainerItem(source);
if (resultStack == null && Config.enableWaterFromBottles && source.getItem() instanceof ItemPotion
&& source.stackTagCompound == null) {
resultStack = new ItemStack(Items.glass_bottle);
}
ItemStack remainderStack = source.copy();
remainderStack.stackSize--;
if (remainderStack.stackSize <= 0) {
remainderStack = null;
}
FluidStack remainderFluid = target != null ? target.copy() : new FluidStack(resultFluid.getFluid(), 0);
remainderFluid.amount += resultFluid.amount;
return new FluidAndStackResult(resultStack, resultFluid, remainderStack, remainderFluid);
}
}
}
}
return new FluidAndStackResult(null, null, source, null);
}
/**
@@ -227,47 +328,137 @@ public class FluidUtil {
*/
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 (FluidTank subTank : tank.getOutputTanks()) {
FluidAndStackResult fill = tryFillContainer(entityPlayer.inventory.getCurrentItem(), subTank.getFluid());
if (fill.result.fluidStack != null) {
if (!entityPlayer.capabilities.isCreativeMode) {
subTank.setFluid(fill.remainder.fluidStack);
tank.setTanksDirty();
if (fill.remainder.itemStack == null) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, fill.result.itemStack);
return true;
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, fill.remainder.itemStack);
}
}
if (fill.result.itemStack.isStackable()) {
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
ItemStack inventoryItem = entityPlayer.inventory.mainInventory[i];
if (ItemUtil.areStackMergable(inventoryItem, fill.result.itemStack)
&& inventoryItem.stackSize < inventoryItem.getMaxStackSize()) {
fill.result.itemStack.stackSize += inventoryItem.stackSize;
entityPlayer.inventory.setInventorySlotContents(i, fill.result.itemStack);
return true;
}
}
}
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);
if (entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, fill.result.itemStack);
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, fill.result.itemStack, x0, y0, z0, true);
}
return true;
}
}
return false;
}
public static boolean fillInternalTankFromPlayerHandItem(World world, int x, int y, int z, EntityPlayer entityPlayer,
ITankAccess tank) {
FluidAndStackResult fill = tryDrainContainer(entityPlayer.inventory.getCurrentItem(), tank);
if (fill.result.fluidStack == null) {
return false;
}
tank.getInputTank(fill.result.fluidStack).setFluid(fill.remainder.fluidStack);
tank.setTanksDirty();
if (!entityPlayer.capabilities.isCreativeMode) {
if (fill.remainder.itemStack == null) {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, fill.result.itemStack);
return true;
} else {
entityPlayer.inventory.setInventorySlotContents(entityPlayer.inventory.currentItem, fill.remainder.itemStack);
}
}
if (fill.result.itemStack.isStackable()) {
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if (entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, filledContainer);
ItemStack inventoryItem = entityPlayer.inventory.mainInventory[i];
if (ItemUtil.areStackMergable(inventoryItem, fill.result.itemStack)
&& inventoryItem.stackSize < inventoryItem.getMaxStackSize()) {
fill.result.itemStack.stackSize += inventoryItem.stackSize;
entityPlayer.inventory.setInventorySlotContents(i, fill.result.itemStack);
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);
}
for (int i = 0; i < entityPlayer.inventory.mainInventory.length; i++) {
if (entityPlayer.inventory.mainInventory[i] == null) {
entityPlayer.inventory.setInventorySlotContents(i, fill.result.itemStack);
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, fill.result.itemStack, x0, y0, z0, true);
}
return true;
}
public static class FluidAndStack {
public final FluidStack fluidStack;
public final ItemStack itemStack;
public FluidAndStack(FluidStack fluidStack, ItemStack itemStack) {
this.fluidStack = fluidStack;
this.itemStack = itemStack;
}
public FluidAndStack(ItemStack itemStack, FluidStack fluidStack) {
this.fluidStack = fluidStack;
this.itemStack = itemStack;
}
}
public static class FluidAndStackResult {
public final FluidAndStack result;
public final FluidAndStack remainder;
public FluidAndStackResult(FluidAndStack result, FluidAndStack remainder) {
this.result = result;
this.remainder = remainder;
}
public FluidAndStackResult(FluidStack fluidStackResult, ItemStack itemStackResult, FluidStack fluidStackRemainder,
ItemStack itemStackRemainder) {
this.result = new FluidAndStack(fluidStackResult, itemStackResult);
this.remainder = new FluidAndStack(fluidStackRemainder, itemStackRemainder);
}
public FluidAndStackResult(ItemStack itemStackResult, FluidStack fluidStackResult, ItemStack itemStackRemainder,
FluidStack fluidStackRemainder) {
this.result = new FluidAndStack(fluidStackResult, itemStackResult);
this.remainder = new FluidAndStack(fluidStackRemainder, itemStackRemainder);
}
}
}
+22 -2
View File
@@ -1,13 +1,33 @@
package crazypants.util;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidTank;
public interface ITankAccess {
FluidTank getInputTank();
/**
* Find tank to insert fluid.
*
* @param forFluidType
* The type of fluid that should be inserted. May be null.
* @return An internal tank that can take the given type of fluid. If multiple
* tanks can take the fluid, the first one that is not full will be
* returned. If no tank can take the fluid, returns null.
*/
FluidTank getInputTank(FluidStack forFluidType);
FluidTank getOutputTank();
/**
* Get tank(s) to remove liquid from.
*
* @return Tank that can be drained. Tanks are returned in order or priority.
* If there's no tank, an empty array is returned.
*/
FluidTank[] getOutputTanks();
/**
* Will be called after a tank that was returned by one of the other methods
* was manipulated.
*/
void setTanksDirty();
}