buildcraft/common/buildcraft/builders/TileBuilder.java

825 lines
19 KiB
Java
Raw Normal View History

/**
2014-01-20 21:15:30 +01:00
* Copyright (c) 2011-2014, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
2014-01-20 21:15:30 +01:00
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
2012-05-09 22:43:05 +02:00
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
2012-07-25 12:45:15 +02:00
package buildcraft.builders;
2012-05-09 22:43:05 +02:00
2014-02-22 00:14:49 +01:00
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2014-02-22 00:14:49 +01:00
import net.minecraft.nbt.NBTTagList;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.WorldSettings.GameType;
import net.minecraftforge.common.util.Constants;
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.FluidTankInfo;
import net.minecraftforge.fluids.IFluidHandler;
2014-02-22 00:14:49 +01:00
import buildcraft.BuildCraftBuilders;
import buildcraft.api.blueprints.BuildingPermission;
import buildcraft.api.blueprints.Translation;
2014-06-13 13:36:02 +02:00
import buildcraft.api.core.BlockIndex;
import buildcraft.api.core.NetworkData;
import buildcraft.api.core.Position;
import buildcraft.api.gates.IAction;
2012-07-25 12:45:15 +02:00
import buildcraft.core.Box;
2014-02-22 00:14:49 +01:00
import buildcraft.core.Box.Kind;
2012-07-25 12:45:15 +02:00
import buildcraft.core.IMachine;
import buildcraft.core.LaserData;
import buildcraft.core.blueprints.Blueprint;
import buildcraft.core.blueprints.BlueprintBase;
2014-02-22 00:14:49 +01:00
import buildcraft.core.blueprints.BptBuilderBase;
import buildcraft.core.blueprints.BptBuilderBlueprint;
2014-03-10 23:33:36 +01:00
import buildcraft.core.blueprints.BptBuilderTemplate;
2014-02-22 00:14:49 +01:00
import buildcraft.core.blueprints.BptContext;
import buildcraft.core.fluids.Tank;
import buildcraft.core.fluids.TankManager;
import buildcraft.core.inventory.InvUtils;
import buildcraft.core.inventory.SimpleInventory;
import buildcraft.core.network.RPC;
import buildcraft.core.network.RPCHandler;
import buildcraft.core.network.RPCMessageInfo;
import buildcraft.core.network.RPCSide;
public class TileBuilder extends TileAbstractBuilder implements IMachine, IFluidHandler {
2014-02-22 00:14:49 +01:00
private static int POWER_ACTIVATION = 50;
2014-02-22 00:14:49 +01:00
@NetworkData
public Box box = new Box();
public PathIterator currentPathIterator;
public Tank[] fluidTanks = new Tank[] {
new Tank("fluid1", FluidContainerRegistry.BUCKET_VOLUME * 8, this),
new Tank("fluid2", FluidContainerRegistry.BUCKET_VOLUME * 8, this),
new Tank("fluid3", FluidContainerRegistry.BUCKET_VOLUME * 8, this),
new Tank("fluid4", FluidContainerRegistry.BUCKET_VOLUME * 8, this)
};
@NetworkData
public TankManager<Tank> fluidTank = new TankManager<Tank>(fluidTanks);
2012-06-04 22:48:18 +02:00
private SimpleInventory inv = new SimpleInventory(28, "Builder", 64);
private BptBuilderBase bluePrintBuilder;
2014-02-22 00:14:49 +01:00
private LinkedList<BlockIndex> path;
private LinkedList<ItemStack> requiredToBuild;
private NBTTagCompound initNBT = null;
private boolean done = true;
2014-02-22 00:14:49 +01:00
private class PathIterator {
public Iterator<BlockIndex> currentIterator;
public double cx, cy, cz;
public float ix, iy, iz;
public BlockIndex to;
public double lastDistance;
AxisAlignedBB oldBoundingBox = null;
ForgeDirection o = null;
public PathIterator(BlockIndex from, Iterator<BlockIndex> it, ForgeDirection initialDir) {
2014-02-22 00:14:49 +01:00
this.to = it.next();
currentIterator = it;
2014-03-15 17:37:33 +01:00
double dx = to.x - from.x;
2014-02-22 00:14:49 +01:00
double dy = to.y - from.y;
double dz = to.z - from.z;
double size = Math.sqrt(dx * dx + dy * dy + dz * dz);
cx = dx / size / 10;
cy = dy / size / 10;
cz = dz / size / 10;
ix = from.x;
iy = from.y;
iz = from.z;
lastDistance = (ix - to.x) * (ix - to.x) + (iy - to.y)
* (iy - to.y) + (iz - to.z) * (iz - to.z);
if (dx == 0 && dz == 0) {
o = initialDir;
} else if (Math.abs(dx) > Math.abs(dz)) {
2014-02-22 00:14:49 +01:00
if (dx > 0) {
o = ForgeDirection.EAST;
} else {
o = ForgeDirection.WEST;
}
} else {
if (dz > 0) {
o = ForgeDirection.SOUTH;
} else {
o = ForgeDirection.NORTH;
}
}
}
/**
* Return false when reached the end of the iteration
*/
public BptBuilderBase next() {
while (true) {
BptBuilderBase bpt;
int newX = Math.round(ix);
int newY = Math.round(iy);
int newZ = Math.round(iz);
bpt = instanciateBluePrint(newX, newY, newZ, o);
2014-03-01 17:35:32 +01:00
if (bpt == null) {
2014-02-22 00:14:49 +01:00
return null;
2014-03-01 17:35:32 +01:00
}
2014-02-22 00:14:49 +01:00
AxisAlignedBB boundingBox = bpt.getBoundingBox();
if (oldBoundingBox == null || !collision(oldBoundingBox, boundingBox)) {
oldBoundingBox = boundingBox;
2014-03-01 17:35:32 +01:00
if (bpt != null) {
2014-02-22 00:14:49 +01:00
return bpt;
2014-03-01 17:35:32 +01:00
}
2014-02-22 00:14:49 +01:00
}
ix += cx;
iy += cy;
iz += cz;
double distance = (ix - to.x) * (ix - to.x) + (iy - to.y)
* (iy - to.y) + (iz - to.z) * (iz - to.z);
2014-03-01 17:35:32 +01:00
if (distance > lastDistance) {
2014-02-22 00:14:49 +01:00
return null;
2014-03-01 17:35:32 +01:00
} else {
2014-02-22 00:14:49 +01:00
lastDistance = distance;
}
}
}
public PathIterator iterate() {
if (currentIterator.hasNext()) {
PathIterator next = new PathIterator(to, currentIterator, o);
2014-02-22 00:14:49 +01:00
next.oldBoundingBox = oldBoundingBox;
return next;
2014-03-01 17:35:32 +01:00
} else {
2014-02-22 00:14:49 +01:00
return null;
2014-03-01 17:35:32 +01:00
}
2014-02-22 00:14:49 +01:00
}
public boolean collision(AxisAlignedBB left, AxisAlignedBB right) {
2014-03-01 17:35:32 +01:00
if (left.maxX < right.minX || left.minX > right.maxX) {
2014-02-22 00:14:49 +01:00
return false;
2014-03-01 17:35:32 +01:00
}
if (left.maxY < right.minY || left.minY > right.maxY) {
2014-02-22 00:14:49 +01:00
return false;
2014-03-01 17:35:32 +01:00
}
if (left.maxZ < right.minZ || left.minZ > right.maxZ) {
2014-02-22 00:14:49 +01:00
return false;
2014-03-01 17:35:32 +01:00
}
2014-02-22 00:14:49 +01:00
return true;
}
}
2012-06-04 22:48:18 +02:00
public TileBuilder() {
super();
2014-02-22 00:14:49 +01:00
box.kind = Kind.STRIPES;
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2012-05-09 22:43:05 +02:00
@Override
2014-02-22 00:14:49 +01:00
public void initialize() {
super.initialize();
2012-06-04 22:48:18 +02:00
if (worldObj.isRemote) {
return;
}
2012-12-17 23:30:54 +01:00
if (initNBT != null) {
iterateBpt(true);
if (initNBT.hasKey("iterator")) {
BlockIndex expectedTo = new BlockIndex(initNBT.getCompoundTag("iterator"));
while (!done && bluePrintBuilder != null && currentPathIterator != null) {
BlockIndex bi = new BlockIndex((int) currentPathIterator.ix,
(int) currentPathIterator.iy, (int) currentPathIterator.iz);
if (bi.equals(expectedTo)) {
break;
}
iterateBpt(true);
}
}
if (bluePrintBuilder != null) {
bluePrintBuilder.loadBuildStateToNBT(
initNBT.getCompoundTag("builderState"), this);
}
initNBT = null;
}
2014-03-30 00:03:09 +01:00
box.kind = Kind.STRIPES;
2014-02-22 00:14:49 +01:00
for (int x = xCoord - 1; x <= xCoord + 1; ++x) {
for (int y = yCoord - 1; y <= yCoord + 1; ++y) {
for (int z = zCoord - 1; z <= zCoord + 1; ++z) {
TileEntity tile = worldObj.getTileEntity(x, y, z);
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
if (tile instanceof TilePathMarker) {
path = ((TilePathMarker) tile).getPath();
2012-05-09 22:43:05 +02:00
2014-02-22 00:14:49 +01:00
for (BlockIndex b : path) {
worldObj.setBlockToAir(b.x, b.y, b.z);
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
BuildCraftBuilders.pathMarkerBlock.dropBlockAsItem(
worldObj, b.x, b.y, b.z,
0, 0);
}
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
break;
}
}
}
}
if (path != null && pathLasers.size() == 0) {
2014-02-22 00:14:49 +01:00
createLasersForPath();
sendNetworkUpdate();
}
2014-02-22 00:14:49 +01:00
iterateBpt(false);
}
2014-02-22 00:14:49 +01:00
public void createLasersForPath() {
pathLasers = new LinkedList<LaserData>();
2014-02-22 00:14:49 +01:00
BlockIndex previous = null;
for (BlockIndex b : path) {
if (previous != null) {
LaserData laser = new LaserData(new Position(previous.x + 0.5,
previous.y + 0.5, previous.z + 0.5), new Position(
2014-02-22 00:14:49 +01:00
b.x + 0.5, b.y + 0.5, b.z + 0.5));
pathLasers.add(laser);
}
previous = b;
}
}
2014-02-22 00:14:49 +01:00
public BptBuilderBase instanciateBluePrint(int x, int y, int z, ForgeDirection o) {
BlueprintBase bpt = null;
try {
bpt = ItemBlueprint.loadBlueprint(getStackInSlot(0));
} catch (Throwable t) {
setInventorySlotContents(0, null);
t.printStackTrace();
return null;
}
2014-02-22 00:14:49 +01:00
2014-02-22 13:54:47 +01:00
if (bpt == null) {
2014-02-22 00:14:49 +01:00
return null;
2014-02-22 13:54:47 +01:00
}
2014-02-22 00:14:49 +01:00
if (bpt.buildingPermission == BuildingPermission.NONE
|| (bpt.buildingPermission == BuildingPermission.CREATIVE_ONLY && worldObj
.getWorldInfo().getGameType() != GameType.CREATIVE)) {
return null;
}
2014-02-23 23:16:28 +01:00
BptContext context = bpt.getContext(worldObj, bpt.getBoxForPos(x, y, z));
2014-02-22 00:14:49 +01:00
if (bpt.rotate) {
if (o == ForgeDirection.EAST) {
// Do nothing
} else if (o == ForgeDirection.SOUTH) {
bpt.rotateLeft(context);
} else if (o == ForgeDirection.WEST) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
} else if (o == ForgeDirection.NORTH) {
bpt.rotateLeft(context);
bpt.rotateLeft(context);
bpt.rotateLeft(context);
}
2012-06-04 22:48:18 +02:00
}
Translation transform = new Translation();
transform.x = x - bpt.anchorX;
transform.y = y - bpt.anchorY;
transform.z = z - bpt.anchorZ;
bpt.translateToWorld(transform);
if (getStackInSlot(0).getItem() instanceof ItemBlueprintStandard) {
2014-04-08 15:56:56 +02:00
return new BptBuilderBlueprint((Blueprint) bpt, worldObj, x, y, z);
} else if (getStackInSlot(0).getItem() instanceof ItemBlueprintTemplate) {
2014-03-10 23:33:36 +01:00
return new BptBuilderTemplate(bpt, worldObj, x, y, z);
} else {
2014-04-08 15:56:56 +02:00
return null;
}
2014-02-22 00:14:49 +01:00
}
2012-06-04 22:48:18 +02:00
public void iterateBpt(boolean forceIterate) {
if (getStackInSlot(0) == null || !(getStackInSlot(0).getItem() instanceof ItemBlueprint)) {
2014-02-22 00:14:49 +01:00
if (bluePrintBuilder != null) {
bluePrintBuilder = null;
}
if (box.isInitialized()) {
box.reset();
}
if (currentPathIterator != null) {
currentPathIterator = null;
}
updateRequirements();
sendNetworkUpdate();
2014-02-22 00:14:49 +01:00
return;
}
if (bluePrintBuilder == null || (bluePrintBuilder.isDone(this) || forceIterate)) {
2014-02-22 00:14:49 +01:00
if (path != null && path.size() > 1) {
if (currentPathIterator == null) {
Iterator<BlockIndex> it = path.iterator();
BlockIndex start = it.next();
currentPathIterator = new PathIterator(start, it,
ForgeDirection.values()[worldObj.getBlockMetadata(
xCoord, yCoord, zCoord)].getOpposite());
2014-02-22 00:14:49 +01:00
}
2014-04-30 23:41:31 +02:00
if (bluePrintBuilder != null && bluePrintBuilder.isDone(this)) {
bluePrintBuilder.postProcessing(worldObj);
}
2014-02-22 00:14:49 +01:00
bluePrintBuilder = currentPathIterator.next();
if (bluePrintBuilder != null) {
box.reset();
box.initialize(bluePrintBuilder);
2014-03-30 00:03:09 +01:00
sendNetworkUpdate();
2014-02-22 00:14:49 +01:00
}
if (bluePrintBuilder == null) {
currentPathIterator = currentPathIterator.iterate();
}
if (currentPathIterator == null) {
done = true;
} else {
done = false;
2014-02-22 00:14:49 +01:00
}
} else {
2014-03-31 23:20:45 +02:00
if (bluePrintBuilder != null && bluePrintBuilder.isDone(this)) {
2014-04-30 23:41:31 +02:00
bluePrintBuilder.postProcessing(worldObj);
2014-02-22 00:14:49 +01:00
done = true;
bluePrintBuilder = null;
} else {
bluePrintBuilder = instanciateBluePrint(xCoord, yCoord, zCoord,
ForgeDirection.values()[worldObj.getBlockMetadata(xCoord, yCoord, zCoord)].getOpposite());
if (bluePrintBuilder != null) {
box.initialize(bluePrintBuilder);
2014-03-30 00:03:09 +01:00
sendNetworkUpdate();
done = false;
2014-02-22 00:14:49 +01:00
}
}
}
updateRequirements();
2014-02-22 00:14:49 +01:00
}
2014-04-30 23:41:31 +02:00
if (done) {
boolean dropBlueprint = true;
for (int i = 1; i < getSizeInventory(); ++i) {
if (getStackInSlot(i) == null) {
setInventorySlotContents(i, getStackInSlot(0));
dropBlueprint = false;
2014-04-30 23:41:31 +02:00
break;
}
}
if (dropBlueprint) {
InvUtils.dropItems(getWorld(), getStackInSlot(0), xCoord, yCoord, zCoord);
}
2014-04-30 23:41:31 +02:00
setInventorySlotContents(0, null);
box.reset();
2014-04-30 23:41:31 +02:00
}
}
2012-05-09 22:43:05 +02:00
@Override
public int getSizeInventory() {
return inv.getSizeInventory();
2012-05-09 22:43:05 +02:00
}
@Override
2014-02-22 00:14:49 +01:00
public ItemStack getStackInSlot(int i) {
return inv.getStackInSlot(i);
2012-05-09 22:43:05 +02:00
}
@Override
2014-02-22 00:14:49 +01:00
public ItemStack decrStackSize(int i, int j) {
ItemStack result = inv.decrStackSize(i, j);
2014-02-22 00:14:49 +01:00
if (!worldObj.isRemote) {
if (i == 0) {
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements",
null, null);
iterateBpt(false);
}
2014-02-22 00:14:49 +01:00
}
return result;
2012-05-09 22:43:05 +02:00
}
@Override
2014-02-22 00:14:49 +01:00
public void setInventorySlotContents(int i, ItemStack itemstack) {
inv.setInventorySlotContents(i, itemstack);
2014-02-22 00:14:49 +01:00
2014-03-01 17:35:32 +01:00
if (!worldObj.isRemote) {
if (i == 0) {
iterateBpt(false);
2014-03-01 17:35:32 +01:00
done = false;
}
2014-02-22 00:14:49 +01:00
}
2012-05-09 22:43:05 +02:00
}
@Override
public ItemStack getStackInSlotOnClosing(int slot) {
return inv.getStackInSlotOnClosing(slot);
2012-05-09 22:43:05 +02:00
}
@Override
2014-02-08 14:47:31 +01:00
public String getInventoryName() {
2012-05-09 22:43:05 +02:00
return "Builder";
}
@Override
public int getInventoryStackLimit() {
return 64;
}
2012-05-09 22:43:05 +02:00
@Override
public boolean isUseableByPlayer(EntityPlayer entityplayer) {
2014-02-08 14:47:31 +01:00
return worldObj.getTileEntity(xCoord, yCoord, zCoord) == this;
2012-05-09 22:43:05 +02:00
}
2012-06-04 22:48:18 +02:00
2012-05-09 22:43:05 +02:00
@Override
2014-02-22 00:14:49 +01:00
public void readFromNBT(NBTTagCompound nbttagcompound) {
super.readFromNBT(nbttagcompound);
inv.readFromNBT(nbttagcompound);
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
if (nbttagcompound.hasKey("box")) {
box.initialize(nbttagcompound.getCompoundTag("box"));
}
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
if (nbttagcompound.hasKey("path")) {
path = new LinkedList<BlockIndex>();
NBTTagList list = nbttagcompound.getTagList("path",
Constants.NBT.TAG_COMPOUND);
2014-02-22 00:14:49 +01:00
for (int i = 0; i < list.tagCount(); ++i) {
path.add(new BlockIndex(list.getCompoundTagAt(i)));
}
2012-06-04 22:48:18 +02:00
}
2014-02-22 00:14:49 +01:00
done = nbttagcompound.getBoolean("done");
fluidTank.readFromNBT(nbttagcompound);
// The rest of load has to be done upon initialize.
initNBT = (NBTTagCompound) nbttagcompound.getCompoundTag("bptBuilder").copy();
2012-06-04 22:48:18 +02:00
}
2012-05-09 22:43:05 +02:00
@Override
2014-02-22 00:14:49 +01:00
public void writeToNBT(NBTTagCompound nbttagcompound) {
super.writeToNBT(nbttagcompound);
2012-06-04 22:48:18 +02:00
inv.writeToNBT(nbttagcompound);
2012-06-04 22:48:18 +02:00
if (box.isInitialized()) {
NBTTagCompound boxStore = new NBTTagCompound();
box.writeToNBT(boxStore);
2014-02-22 00:14:49 +01:00
nbttagcompound.setTag("box", boxStore);
2012-06-04 22:48:18 +02:00
}
2014-02-22 00:14:49 +01:00
if (path != null) {
NBTTagList list = new NBTTagList();
for (BlockIndex i : path) {
NBTTagCompound c = new NBTTagCompound();
i.writeTo(c);
list.appendTag(c);
}
nbttagcompound.setTag("path", list);
}
nbttagcompound.setBoolean("done", done);
fluidTank.writeToNBT(nbttagcompound);
NBTTagCompound bptNBT = new NBTTagCompound();
if (bluePrintBuilder != null) {
NBTTagCompound builderCpt = new NBTTagCompound();
bluePrintBuilder.saveBuildStateToNBT(builderCpt, this);
bptNBT.setTag("builderState", builderCpt);
}
if (currentPathIterator != null) {
NBTTagCompound iteratorNBT = new NBTTagCompound();
new BlockIndex((int) currentPathIterator.ix,
(int) currentPathIterator.iy, (int) currentPathIterator.iz)
.writeTo(iteratorNBT);
bptNBT.setTag ("iterator", iteratorNBT);
}
nbttagcompound.setTag("bptBuilder", bptNBT);
2012-06-04 22:48:18 +02:00
}
@Override
public void invalidate() {
super.invalidate();
2012-06-04 22:48:18 +02:00
destroy();
}
2012-05-09 22:43:05 +02:00
@Override
2014-02-08 14:47:31 +01:00
public void openInventory() {
2012-05-09 22:43:05 +02:00
}
@Override
2014-02-08 14:47:31 +01:00
public void closeInventory() {
2012-05-09 22:43:05 +02:00
}
2014-02-22 00:14:49 +01:00
@Override
public void updateEntity() {
super.updateEntity();
2014-03-30 00:03:09 +01:00
if (worldObj.isRemote) {
return;
}
if (bluePrintBuilder != null) {
2014-03-31 23:20:45 +02:00
bluePrintBuilder.removeDoneBuilders(this);
}
2014-03-31 23:20:45 +02:00
if ((bluePrintBuilder == null || bluePrintBuilder.isDone(this))
&& box.isInitialized()) {
2014-02-22 00:14:49 +01:00
box.reset();
2014-03-30 00:03:09 +01:00
sendNetworkUpdate();
2014-02-22 00:14:49 +01:00
return;
}
iterateBpt(false);
if (getWorld().getWorldInfo().getGameType() == GameType.CREATIVE) {
build();
} else {
if (mjStored > POWER_ACTIVATION) {
build();
}
2014-02-22 00:14:49 +01:00
}
if (done) {
return;
} else if (mjStored < 25) {
return;
}
2014-02-22 00:14:49 +01:00
}
2012-05-09 22:43:05 +02:00
@Override
public boolean isActive() {
2014-02-22 00:14:49 +01:00
return !done;
2012-05-09 22:43:05 +02:00
}
@Override
2013-07-08 04:57:05 +02:00
public boolean manageFluids() {
return true;
2012-05-09 22:43:05 +02:00
}
@Override
public boolean manageSolids() {
return true;
}
2012-06-04 22:48:18 +02:00
2014-02-22 00:14:49 +01:00
public boolean isBuildingBlueprint() {
return getStackInSlot(0) != null && getStackInSlot(0).getItem() instanceof ItemBlueprint;
2014-02-22 00:14:49 +01:00
}
public Collection<ItemStack> getNeededItems() {
return requiredToBuild;
}
@RPC (RPCSide.CLIENT)
public void setItemRequirements(LinkedList<ItemStack> rq, LinkedList<Integer> realSizes) {
// Item stack serialized are represented through bytes, so 0-255. In
// order to get the real amounts, we need to pass the real sizes of the
// stacks as a separate list.
requiredToBuild = rq;
if (rq != null && rq.size() > 0) {
Iterator<ItemStack> itStack = rq.iterator();
Iterator<Integer> size = realSizes.iterator();
while (true) {
ItemStack stack = itStack.next();
stack.stackSize = size.next();
if (stack.stackSize > 999) {
stack.stackSize = 999;
}
if (!itStack.hasNext()) {
break;
}
}
2014-02-22 00:14:49 +01:00
}
}
2012-05-09 22:43:05 +02:00
@Override
2014-03-31 23:20:45 +02:00
public boolean isBuildingMaterialSlot(int i) {
2012-05-09 22:43:05 +02:00
return i != 0;
}
2012-06-04 22:48:18 +02:00
2012-05-09 22:43:05 +02:00
@Override
public boolean allowAction(IAction action) {
2012-05-09 22:43:05 +02:00
return false;
}
2014-02-08 14:47:31 +01:00
@Override
public boolean hasCustomInventoryName() {
return false;
}
2014-02-22 00:14:49 +01:00
@Override
public boolean isItemValidForSlot(int slot, ItemStack stack) {
if (slot == 0) {
return stack.getItem() instanceof ItemBlueprint;
} else {
return true;
}
2014-02-22 00:14:49 +01:00
}
@Override
public Box getBox() {
return box;
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
2014-03-16 14:15:00 +01:00
Box renderBox = new Box (this).extendToEncompass(box);
for (LaserData l : pathLasers) {
renderBox = renderBox.extendToEncompass(l.head);
renderBox = renderBox.extendToEncompass(l.tail);
}
return renderBox.expand(50).getBoundingBox();
2014-02-22 00:14:49 +01:00
}
public void build () {
if (!buildTracker.markTimeIfDelay(worldObj)) {
return;
}
if (bluePrintBuilder != null) {
2014-03-31 23:20:45 +02:00
bluePrintBuilder.buildNextSlot(worldObj, this, xCoord, yCoord, zCoord);
updateRequirements();
}
}
public void updateRequirements () {
if (bluePrintBuilder instanceof BptBuilderBlueprint) {
LinkedList<Integer> realSize = new LinkedList<Integer>();
for (ItemStack stack : ((BptBuilderBlueprint) bluePrintBuilder).neededItems) {
realSize.add(stack.stackSize);
stack.stackSize = 0;
}
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements",
((BptBuilderBlueprint) bluePrintBuilder).neededItems, realSize);
} else {
RPCHandler.rpcBroadcastWorldPlayers(worldObj, this, "setItemRequirements", null, null);
2014-02-22 13:54:47 +01:00
}
2014-02-22 13:54:47 +01:00
}
public BptBuilderBase getBlueprint () {
if (bluePrintBuilder != null) {
return bluePrintBuilder;
} else {
return null;
}
}
@Override
public boolean canDrain(ForgeDirection from, Fluid fluid) {
return false;
}
@Override
public FluidStack drain(ForgeDirection from, FluidStack resource, boolean doDrain) {
return null;
}
@Override
public FluidStack drain(ForgeDirection from, int maxDrain, boolean doDrain) {
return null;
}
public boolean drainBuild(FluidStack fluidStack, boolean realDrain) {
for (Tank tank : fluidTanks) {
if (tank.getFluidType() == fluidStack.getFluid()) {
return tank.getFluidAmount() >= fluidStack.amount && tank.drain(fluidStack.amount, realDrain).amount > 0;
}
}
return false;
}
@Override
public int fill(ForgeDirection from, FluidStack resource, boolean doFill) {
Fluid fluid = resource.getFluid();
Tank emptyTank = null;
for (Tank tank : fluidTanks) {
Fluid type = tank.getFluidType();
if (type == fluid) {
int used = tank.fill(resource, doFill);
if (used > 0 && doFill) {
sendNetworkUpdate();
}
return used;
} else if (emptyTank == null && tank.isEmpty()) {
emptyTank = tank;
}
}
if (emptyTank != null) {
int used = emptyTank.fill(resource, doFill);
if (used > 0 && doFill) {
sendNetworkUpdate();
}
return used;
}
return 0;
}
@Override
public boolean canFill(ForgeDirection from, Fluid fluid) {
boolean emptyAvailable = false;
for (Tank tank : fluidTanks) {
Fluid type = tank.getFluidType();
if (type == fluid) {
return !tank.isFull();
} else if (!emptyAvailable) {
emptyAvailable = tank.isEmpty();
}
}
return emptyAvailable;
}
@Override
public FluidTankInfo[] getTankInfo(ForgeDirection from) {
return fluidTank.getTankInfo(from);
}
@RPC(RPCSide.SERVER)
public void eraseFluidTank(int id, RPCMessageInfo info) {
if (id < 0 || id >= fluidTanks.length) {
return;
}
if (isUseableByPlayer(info.sender) && info.sender.getDistanceSq(xCoord, yCoord, zCoord) <= 64) {
fluidTanks[id].setFluid(null);
sendNetworkUpdate();
}
}
}