Fixes #255 and fixes #258

This commit is contained in:
Modmuss50 2015-11-12 21:38:11 +00:00
parent 61a600713a
commit 2331e3c35d
4 changed files with 125 additions and 7 deletions

View file

@ -10,6 +10,7 @@ import net.minecraft.block.material.Material;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.EnumCreatureType;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
@ -20,7 +21,7 @@ import net.minecraft.util.MathHelper;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import net.minecraftforge.fluids.BlockFluidBase;
import net.minecraftforge.fluids.*;
import techreborn.client.TechRebornCreativeTab;
import techreborn.init.ModBlocks;
import techreborn.tiles.TileMachineBase;
@ -200,4 +201,117 @@ public class BlockMachineBase extends BlockContainer {
return false;
}
}
@Override
public boolean onBlockActivated(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ) {
if(super.onBlockActivated(world, x, y, z, entityplayer, side, hitX, hitY, hitZ)){
return true;
}
return fillBlockWithFluid(world, x, y, z, entityplayer, side, hitX, hitY, hitZ);
}
public boolean fillBlockWithFluid(World world, int x, int y, int z, EntityPlayer entityplayer, int side, float hitX, float hitY, float hitZ) {
ItemStack current = entityplayer.inventory.getCurrentItem();
if (current != null) {
TileEntity tile = world.getTileEntity(x, y, z);
if (tile instanceof IFluidHandler) {
IFluidHandler tank = (IFluidHandler) tile;
// Handle FluidContainerRegistry
if (FluidContainerRegistry.isContainer(current)) {
FluidStack liquid = FluidContainerRegistry.getFluidForFilledItem(current);
// Handle filled containers
if (liquid != null) {
int qty = tank.fill(ForgeDirection.UNKNOWN, liquid, true);
if (qty != 0 && !entityplayer.capabilities.isCreativeMode) {
if (current.stackSize > 1) {
if (!entityplayer.inventory.addItemStackToInventory(FluidContainerRegistry.drainFluidContainer(current))) {
entityplayer.dropPlayerItemWithRandomChoice(FluidContainerRegistry.drainFluidContainer(current), false);
}
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
} else {
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, FluidContainerRegistry.drainFluidContainer(current));
}
}
return true;
// Handle empty containers
} else {
FluidStack available = tank.getTankInfo(ForgeDirection.UNKNOWN)[0].fluid;
if (available != null) {
ItemStack filled = FluidContainerRegistry.fillFluidContainer(available, current);
liquid = FluidContainerRegistry.getFluidForFilledItem(filled);
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, consumeItem(current));
}
} else {
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, consumeItem(current));
entityplayer.inventory.setInventorySlotContents(entityplayer.inventory.currentItem, filled);
}
}
tank.drain(ForgeDirection.UNKNOWN, liquid.amount, true);
return true;
}
}
}
} else if (current.getItem() instanceof IFluidContainerItem) {
if (current.stackSize != 1) {
return false;
}
if (!world.isRemote) {
IFluidContainerItem container = (IFluidContainerItem) current.getItem();
FluidStack liquid = container.getFluid(current);
FluidStack tankLiquid = tank.getTankInfo(ForgeDirection.UNKNOWN)[0].fluid;
boolean mustDrain = liquid == null || liquid.amount == 0;
boolean mustFill = tankLiquid == null || tankLiquid.amount == 0;
if (mustDrain && mustFill) {
// Both are empty, do nothing
} else if (mustDrain || !entityplayer.isSneaking()) {
liquid = tank.drain(ForgeDirection.UNKNOWN, 1000, false);
int qtyToFill = container.fill(current, liquid, true);
tank.drain(ForgeDirection.UNKNOWN, qtyToFill, true);
} else if (mustFill || entityplayer.isSneaking()) {
if (liquid.amount > 0) {
int qty = tank.fill(ForgeDirection.UNKNOWN, liquid, false);
tank.fill(ForgeDirection.UNKNOWN, container.drain(current, qty, true), true);
}
}
}
return true;
}
}
}
return false;
}
public static ItemStack consumeItem(ItemStack stack) {
if (stack.stackSize == 1) {
if (stack.getItem().hasContainerItem(stack)) {
return stack.getItem().getContainerItem(stack);
} else {
return null;
}
} else {
stack.splitStack(1);
return stack;
}
}
}

View file

@ -90,7 +90,6 @@ public class TileDigitalChest extends TileMachineBase implements IInventory,
}
}
}
syncWithAll();
}
public Packet getDescriptionPacket() {

View file

@ -90,7 +90,6 @@ public class TileQuantumChest extends TileMachineBase implements IInventory,
}
}
}
syncWithAll();
}
public Packet getDescriptionPacket() {

View file

@ -76,25 +76,31 @@ public class TileQuantumTank extends TileMachineBase implements IFluidHandler,
} else if (tank.getFluidType() == null && getStackInSlot(2) != null) {
setInventorySlotContents(2, null);
}
syncWithAll();
tank.compareAndUpdate();
}
}
// IFluidHandler
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
return tank.fill(resource, doFill);
int fill = tank.fill(resource, doFill);
tank.compareAndUpdate();
return fill;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource,
boolean doDrain) {
return tank.drain(resource.amount, doDrain);
FluidStack drain = tank.drain(resource.amount, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return tank.drain(maxDrain, doDrain);
FluidStack drain = tank.drain(maxDrain, doDrain);
tank.compareAndUpdate();
return drain;
}
@Override