Save child task

This commit is contained in:
Raoul Van den Berge
2016-08-29 21:28:49 +02:00
parent 9d71f8bb61
commit 543c811a57
4 changed files with 45 additions and 14 deletions

View File

@@ -39,6 +39,8 @@ public class CraftingTaskFactoryNormal implements ICraftingTaskFactory {
} }
task.setTook(took); task.setTook(took);
task.readChildNBT(tag);
} }
return task; return task;

View File

@@ -1,11 +1,15 @@
package refinedstorage.apiimpl.autocrafting.task; package refinedstorage.apiimpl.autocrafting.task;
import net.minecraft.nbt.NBTTagCompound;
import refinedstorage.api.autocrafting.task.ICraftingTask; import refinedstorage.api.autocrafting.task.ICraftingTask;
import refinedstorage.api.network.INetworkMaster; import refinedstorage.api.network.INetworkMaster;
import refinedstorage.tile.TileController;
import javax.annotation.Nullable; import javax.annotation.Nullable;
public abstract class CraftingTask implements ICraftingTask { public abstract class CraftingTask implements ICraftingTask {
private static final String NBT_CHILD = "Child";
protected ICraftingTask child; protected ICraftingTask child;
@Override @Override
@@ -25,4 +29,19 @@ public abstract class CraftingTask implements ICraftingTask {
child.onCancelled(network); child.onCancelled(network);
} }
} }
@Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) {
if (child != null) {
tag.setTag(NBT_CHILD, child.writeToNBT(new NBTTagCompound()));
}
return tag;
}
public void readChildNBT(NBTTagCompound tag) {
if (tag.hasKey(NBT_CHILD)) {
child = TileController.readCraftingTask(tag.getCompoundTag(NBT_CHILD));
}
}
} }

View File

@@ -104,6 +104,8 @@ public class CraftingTaskNormal extends CraftingTask {
@Override @Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) { public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag);
writeBooleanArray(tag, NBT_SATISFIED, satisfied); writeBooleanArray(tag, NBT_SATISFIED, satisfied);
writeBooleanArray(tag, NBT_CHILDREN, childrenCreated); writeBooleanArray(tag, NBT_CHILDREN, childrenCreated);

View File

@@ -130,7 +130,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
public static final String NBT_ENERGY_CAPACITY = "EnergyCapacity"; public static final String NBT_ENERGY_CAPACITY = "EnergyCapacity";
private static final String NBT_CRAFTING_TASKS = "CraftingTasks"; private static final String NBT_CRAFTING_TASKS = "CraftingTasks";
private static final String NBT_CRAFTING_TASK_TYPE = "Type"; public static final String NBT_CRAFTING_TASK_TYPE = "Type";
private static final Comparator<IItemStorage> ITEM_SIZE_COMPARATOR = (left, right) -> { private static final Comparator<IItemStorage> ITEM_SIZE_COMPARATOR = (left, right) -> {
if (left.getStored() == right.getStored()) { if (left.getStored() == right.getStored()) {
@@ -692,25 +692,33 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
NBTTagList taskList = tag.getTagList(NBT_CRAFTING_TASKS, Constants.NBT.TAG_COMPOUND); NBTTagList taskList = tag.getTagList(NBT_CRAFTING_TASKS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < taskList.tagCount(); ++i) { for (int i = 0; i < taskList.tagCount(); ++i) {
NBTTagCompound taskTag = taskList.getCompoundTagAt(i); ICraftingTask task = readCraftingTask(taskList.getCompoundTagAt(i));
CraftingPattern pattern = CraftingPattern.readFromNBT(taskTag.getCompoundTag(CraftingPattern.NBT)); if (task != null) {
addCraftingTask(task);
if (pattern != null) {
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(taskTag.getString(NBT_CRAFTING_TASK_TYPE));
if (factory != null) {
ICraftingTask task = factory.create(taskTag, pattern);
if (task != null) {
addCraftingTask(task);
}
}
} }
} }
} }
} }
public static ICraftingTask readCraftingTask(NBTTagCompound tag) {
CraftingPattern pattern = CraftingPattern.readFromNBT(tag.getCompoundTag(CraftingPattern.NBT));
if (pattern != null) {
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
if (factory != null) {
ICraftingTask task = factory.create(tag, pattern);
if (task != null) {
return task;
}
}
}
return null;
}
@Override @Override
public NBTTagCompound writeToNBT(NBTTagCompound tag) { public NBTTagCompound writeToNBT(NBTTagCompound tag) {
super.writeToNBT(tag); super.writeToNBT(tag);