Made Fluids save in Blocks when breaking them

This commit is contained in:
Ellpeck 2015-12-15 18:51:13 +01:00
parent 32056e090b
commit 99ccd4465e
7 changed files with 112 additions and 5 deletions

View file

@ -13,6 +13,7 @@ package ellpeck.actuallyadditions.blocks.base;
import cpw.mods.fml.common.registry.GameRegistry;
import ellpeck.actuallyadditions.creative.CreativeTab;
import ellpeck.actuallyadditions.tile.IEnergySaver;
import ellpeck.actuallyadditions.tile.IFluidSaver;
import ellpeck.actuallyadditions.tile.TileEntityBase;
import ellpeck.actuallyadditions.tile.TileEntityInventoryBase;
import ellpeck.actuallyadditions.util.ModUtil;
@ -30,6 +31,7 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import net.minecraftforge.fluids.FluidStack;
import java.util.ArrayList;
@ -158,6 +160,25 @@ public abstract class BlockContainerBase extends BlockContainer{
}
}
if(tile instanceof IFluidSaver){
FluidStack[] fluids = ((IFluidSaver)tile).getFluids();
if(fluids != null && fluids.length > 0){
if(stack.getTagCompound() == null){
stack.setTagCompound(new NBTTagCompound());
}
stack.getTagCompound().setInteger("FluidAmount", fluids.length);
for(int i = 0; i < fluids.length; i++){
if(fluids[i] != null && fluids[i].amount > 0){
NBTTagCompound compound = new NBTTagCompound();
fluids[i].writeToNBT(compound);
stack.getTagCompound().setTag("Fluid"+i, compound);
}
}
}
}
drops.add(stack);
}
@ -168,9 +189,24 @@ public abstract class BlockContainerBase extends BlockContainer{
public void onBlockPlacedBy(World world, int x, int y, int z, EntityLivingBase entity, ItemStack stack){
if(stack.getTagCompound() != null){
TileEntity tile = world.getTileEntity(x, y, z);
if(tile instanceof IEnergySaver){
((IEnergySaver)tile).setEnergy(stack.getTagCompound().getInteger("Energy"));
}
if(tile instanceof IFluidSaver){
int amount = stack.getTagCompound().getInteger("FluidAmount");
FluidStack[] fluids = new FluidStack[amount];
for(int i = 0; i < amount; i++){
NBTTagCompound compound = stack.getTagCompound().getCompoundTag("Fluid"+i);
if(compound != null){
fluids[i] = FluidStack.loadFluidStackFromNBT(compound);
}
}
((IFluidSaver)tile).setFluids(fluids);
}
}
}

View file

@ -0,0 +1,20 @@
/*
* This file ("IFluidSaver.java") is part of the Actually Additions Mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://github.com/Ellpeck/ActuallyAdditions/blob/master/README.md
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015 Ellpeck
*/
package ellpeck.actuallyadditions.tile;
import net.minecraftforge.fluids.FluidStack;
public interface IFluidSaver{
FluidStack[] getFluids();
void setFluids(FluidStack[] fluids);
}

View file

@ -24,7 +24,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityCanolaPress extends TileEntityInventoryBase implements IEnergyReceiver, IFluidHandler, IEnergySaver{
public class TileEntityCanolaPress extends TileEntityInventoryBase implements IEnergyReceiver, IFluidHandler, IEnergySaver, IFluidSaver{
public static final int PRODUCE = 100;
public static final int ENERGY_USE = 35;
@ -198,4 +198,14 @@ public class TileEntityCanolaPress extends TileEntityInventoryBase implements IE
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
}

View file

@ -26,7 +26,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, IEnergyReceiver, IFluidHandler, IEnergySaver{
public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements IButtonReactor, IEnergyReceiver, IFluidSaver, IFluidHandler, IEnergySaver{
public static final int SLOT_COFFEE_BEANS = 0;
public static final int SLOT_INPUT = 1;
@ -246,4 +246,14 @@ public class TileEntityCoffeeMachine extends TileEntityInventoryBase implements
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
}

View file

@ -20,7 +20,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityFermentingBarrel extends TileEntityInventoryBase implements IFluidHandler{
public class TileEntityFermentingBarrel extends TileEntityInventoryBase implements IFluidHandler, IFluidSaver{
private static final int PROCESS_TIME = 100;
public FluidTank canolaTank = new FluidTank(2*FluidContainerRegistry.BUCKET_VOLUME);
@ -158,4 +158,15 @@ public class TileEntityFermentingBarrel extends TileEntityInventoryBase implemen
public FluidTankInfo[] getTankInfo(ForgeDirection from){
return new FluidTankInfo[]{this.canolaTank.getInfo(), this.oilTank.getInfo()};
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.oilTank.getFluid(), this.canolaTank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.oilTank.setFluid(fluids[0]);
this.canolaTank.setFluid(fluids[1]);
}
}

View file

@ -21,7 +21,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityFluidCollector extends TileEntityInventoryBase implements IFluidHandler{
public class TileEntityFluidCollector extends TileEntityInventoryBase implements IFluidHandler, IFluidSaver{
public FluidTank tank = new FluidTank(8*FluidContainerRegistry.BUCKET_VOLUME);
public boolean isPlacer;
@ -194,6 +194,16 @@ public class TileEntityFluidCollector extends TileEntityInventoryBase implements
return slot == 1;
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
public static class TileEntityFluidPlacer extends TileEntityFluidCollector{
public TileEntityFluidPlacer(){

View file

@ -21,7 +21,7 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.*;
public class TileEntityOilGenerator extends TileEntityInventoryBase implements IEnergyProvider, IFluidHandler, IEnergySaver{
public class TileEntityOilGenerator extends TileEntityInventoryBase implements IEnergyProvider, IFluidHandler, IEnergySaver, IFluidSaver{
public static final int ENERGY_PRODUCED = 76;
private static final int BURN_TIME = 100;
@ -196,4 +196,14 @@ public class TileEntityOilGenerator extends TileEntityInventoryBase implements I
public void setEnergy(int energy){
this.storage.setEnergyStored(energy);
}
@Override
public FluidStack[] getFluids(){
return new FluidStack[]{this.tank.getFluid()};
}
@Override
public void setFluids(FluidStack[] fluids){
this.tank.setFluid(fluids[0]);
}
}