From b93c700a8a2bb7b1ccfef1814d4118bafeaaba07 Mon Sep 17 00:00:00 2001 From: SpaceToad Date: Thu, 17 Apr 2014 21:00:56 +0200 Subject: [PATCH] minor progress in saving builder state, for #1575 --- common/buildcraft/api/core/Position.java | 7 ++- .../builders/TileAbstractBuilder.java | 53 +++++++++---------- common/buildcraft/builders/TileBuilder.java | 27 +++++++++- .../core/blueprints/BptBuilderBase.java | 25 ++++++++- 4 files changed, 79 insertions(+), 33 deletions(-) diff --git a/common/buildcraft/api/core/Position.java b/common/buildcraft/api/core/Position.java index 56091e95..4a9203d8 100644 --- a/common/buildcraft/api/core/Position.java +++ b/common/buildcraft/api/core/Position.java @@ -22,7 +22,10 @@ public class Position { public ForgeDirection orientation; public Position() { - + x = 0; + y = 0; + z = 0; + orientation = ForgeDirection.UNKNOWN; } public Position(double ci, double cj, double ck) { @@ -127,12 +130,14 @@ public class Position { nbttagcompound.setDouble("i", x); nbttagcompound.setDouble("j", y); nbttagcompound.setDouble("k", z); + nbttagcompound.setByte("orientation", (byte) orientation.ordinal()); } public void readFromNBT(NBTTagCompound nbttagcompound) { x = nbttagcompound.getDouble("i"); y = nbttagcompound.getDouble("j"); z = nbttagcompound.getDouble("k"); + orientation = ForgeDirection.values() [nbttagcompound.getByte("orientation")]; } @Override diff --git a/common/buildcraft/builders/TileAbstractBuilder.java b/common/buildcraft/builders/TileAbstractBuilder.java index f3c0a61e..b9488045 100755 --- a/common/buildcraft/builders/TileAbstractBuilder.java +++ b/common/buildcraft/builders/TileAbstractBuilder.java @@ -13,7 +13,6 @@ import java.util.LinkedList; import net.minecraft.inventory.IInventory; import net.minecraft.nbt.NBTTagCompound; -import net.minecraft.nbt.NBTTagList; import buildcraft.api.core.SafeTimeTracker; import buildcraft.api.mj.MjBattery; import buildcraft.core.IBoxProvider; @@ -22,8 +21,8 @@ import buildcraft.core.TileBuildCraft; import buildcraft.core.network.NetworkData; import buildcraft.core.network.RPC; import buildcraft.core.network.RPCHandler; +import buildcraft.core.network.RPCMessageInfo; import buildcraft.core.network.RPCSide; -import buildcraft.core.utils.Utils; public abstract class TileAbstractBuilder extends TileBuildCraft implements IInventory, IBoxProvider { @@ -40,6 +39,22 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements IInv protected SafeTimeTracker buildTracker = new SafeTimeTracker(5); + @Override + public void initialize () { + super.initialize(); + + if (worldObj.isRemote) { + RPCHandler.rpcServer(this, "uploadBuildersInAction"); + } + } + + @RPC (RPCSide.SERVER) + private void uploadBuildersInAction (RPCMessageInfo info) { + for (BuildingItem i : buildersInAction) { + RPCHandler.rpcPlayer(this, "launchItem", info.sender, i); + } + } + @Override public void updateEntity() { super.updateEntity(); @@ -87,37 +102,17 @@ public abstract class TileAbstractBuilder extends TileBuildCraft implements IInv mjStored -= quantity; } - @Override - public void readFromNBT(NBTTagCompound nbttagcompound) { - super.readFromNBT(nbttagcompound); - - mjStored = nbttagcompound.getDouble("mjStored"); - - NBTTagList buildingList = new NBTTagList(); - - for (BuildingItem item : buildersInAction) { - NBTTagCompound sub = new NBTTagCompound(); - item.writeToNBT(sub); - buildingList.appendTag(sub); - } - - nbttagcompound.setTag("buildersInAction", buildingList); - } - @Override public void writeToNBT(NBTTagCompound nbttagcompound) { super.writeToNBT(nbttagcompound); + mjStored = nbttagcompound.getDouble("mjStored"); + } + + @Override + public void readFromNBT(NBTTagCompound nbttagcompound) { + super.readFromNBT(nbttagcompound); + nbttagcompound.setDouble("mjStored", mjStored); - - NBTTagList buildingList = nbttagcompound - .getTagList("buildersInAction", - Utils.NBTTag_Types.NBTTagCompound.ordinal()); - - for (int i = 0; i < buildingList.tagCount(); ++i) { - BuildingItem item = new BuildingItem(); - item.readFromNBT(buildingList.getCompoundTagAt(i)); - addBuildingItem(item); - } } } diff --git a/common/buildcraft/builders/TileBuilder.java b/common/buildcraft/builders/TileBuilder.java index c9f02a50..b572785e 100644 --- a/common/buildcraft/builders/TileBuilder.java +++ b/common/buildcraft/builders/TileBuilder.java @@ -56,6 +56,8 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine { private LinkedList requiredToBuild; + NBTTagCompound initNBT = null; + private class PathIterator { public Iterator currentIterator; @@ -189,6 +191,26 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine { return; } + + if (initNBT != null) { + iterateBpt(); + + if (bluePrintBuilder != null) { + NBTTagCompound builderCpt = new NBTTagCompound(); + bluePrintBuilder.loadBuildStateToNBT(initNBT, this); + initNBT.setTag("builderState", builderCpt); + } + + /* + * if (currentPathIterator != null) { NBTTagCompound iteratorNBT = + * new NBTTagCompound(); + * currentPathIterator.to.writeTo(iteratorNBT); + * nbttagcompound.setTag ("iterator", iteratorNBT); } + */ + + initNBT = null; + } + box.kind = Kind.STRIPES; for (int x = xCoord - 1; x <= xCoord + 1; ++x) { @@ -445,6 +467,9 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine { } done = nbttagcompound.getBoolean("done"); + + // The rest of load has to be done upon initialize. + initNBT = nbttagcompound; } @Override @@ -475,7 +500,7 @@ public class TileBuilder extends TileAbstractBuilder implements IMachine { if (bluePrintBuilder != null) { NBTTagCompound builderCpt = new NBTTagCompound(); - bluePrintBuilder.saveBuildStateToNBT(builderCpt); + bluePrintBuilder.saveBuildStateToNBT(builderCpt, this); nbttagcompound.setTag("builderState", builderCpt); } diff --git a/common/buildcraft/core/blueprints/BptBuilderBase.java b/common/buildcraft/core/blueprints/BptBuilderBase.java index 3efe31cb..e974fc2e 100644 --- a/common/buildcraft/core/blueprints/BptBuilderBase.java +++ b/common/buildcraft/core/blueprints/BptBuilderBase.java @@ -154,7 +154,7 @@ public abstract class BptBuilderBase implements IAreaProvider { } } - public void saveBuildStateToNBT (NBTTagCompound nbt) { + public void saveBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) { NBTTagList clearList = new NBTTagList(); for (BlockIndex loc : clearedLocations) { @@ -174,9 +174,19 @@ public abstract class BptBuilderBase implements IAreaProvider { } nbt.setTag("builtList", builtList); + + NBTTagList buildingList = new NBTTagList(); + + for (BuildingItem item : builder.buildersInAction) { + NBTTagCompound sub = new NBTTagCompound(); + item.writeToNBT(sub); + buildingList.appendTag(sub); + } + + nbt.setTag("buildersInAction", buildingList); } - public void loadBuildStateToNBT (NBTTagCompound nbt) { + public void loadBuildStateToNBT (NBTTagCompound nbt, TileAbstractBuilder builder) { NBTTagList clearList = nbt.getTagList("clearList", Utils.NBTTag_Types.NBTTagCompound.ordinal()); for (int i = 0; i < clearList.tagCount(); ++i) { @@ -192,5 +202,16 @@ public abstract class BptBuilderBase implements IAreaProvider { builtLocations.add (new BlockIndex(cpt)); } + + NBTTagList buildingList = nbt + .getTagList("buildersInAction", + Utils.NBTTag_Types.NBTTagCompound.ordinal()); + + for (int i = 0; i < buildingList.tagCount(); ++i) { + BuildingItem item = new BuildingItem(); + item.readFromNBT(buildingList.getCompoundTagAt(i)); + item.context = getContext(); + builder.buildersInAction.add(item); + } } }