Untested crafting task persistence
This commit is contained in:
		| @@ -9,10 +9,12 @@ import net.minecraft.entity.player.EntityPlayerMP; | ||||
| import net.minecraft.inventory.Container; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import net.minecraft.nbt.NBTTagList; | ||||
| import net.minecraft.tileentity.TileEntity; | ||||
| import net.minecraft.util.EnumFacing; | ||||
| import net.minecraft.util.EnumHand; | ||||
| import net.minecraft.util.math.BlockPos; | ||||
| import net.minecraftforge.common.util.Constants; | ||||
| import net.minecraftforge.fml.common.network.ByteBufUtils; | ||||
| import refinedstorage.RefinedStorage; | ||||
| import refinedstorage.RefinedStorageBlocks; | ||||
| @@ -52,6 +54,8 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor | ||||
|  | ||||
|     public static final int ENERGY_CAPACITY = 32000; | ||||
|  | ||||
|     public static final String NBT_CRAFTING_TASKS = "CraftingTasks"; | ||||
|  | ||||
|     private List<ItemGroup> itemGroups = new ArrayList<ItemGroup>(); | ||||
|     private List<IStorage> storages = new ArrayList<IStorage>(); | ||||
|     private List<WirelessGridConsumer> wirelessGridConsumers = new ArrayList<WirelessGridConsumer>(); | ||||
| @@ -502,6 +506,23 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor | ||||
|         if (nbt.hasKey(RedstoneMode.NBT)) { | ||||
|             redstoneMode = RedstoneMode.getById(nbt.getInteger(RedstoneMode.NBT)); | ||||
|         } | ||||
|  | ||||
|         if (nbt.hasKey(NBT_CRAFTING_TASKS)) { | ||||
|             NBTTagList taskList = nbt.getTagList(NBT_CRAFTING_TASKS, Constants.NBT.TAG_COMPOUND); | ||||
|  | ||||
|             for (int i = 0; i < taskList.tagCount(); ++i) { | ||||
|                 NBTTagCompound taskTag = taskList.getCompoundTagAt(i); | ||||
|  | ||||
|                 switch (taskTag.getInteger("Type")) { | ||||
|                     case 0: | ||||
|                         addCraftingTask(new BasicCraftingTask(worldObj, taskTag)); | ||||
|                         break; | ||||
|                     case 1: | ||||
|                         addCraftingTask(new ProcessingCraftingTask(worldObj, taskTag)); | ||||
|                         break; | ||||
|                 } | ||||
|             } | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
| @@ -509,6 +530,16 @@ public class TileController extends TileBase implements IEnergyReceiver, INetwor | ||||
|         super.writeToNBT(nbt); | ||||
|  | ||||
|         nbt.setInteger(RedstoneMode.NBT, redstoneMode.id); | ||||
|  | ||||
|         NBTTagList list = new NBTTagList(); | ||||
|  | ||||
|         for (ICraftingTask task : craftingTasks) { | ||||
|             NBTTagCompound taskTag = new NBTTagCompound(); | ||||
|             task.writeToNBT(taskTag); | ||||
|             list.appendTag(taskTag); | ||||
|         } | ||||
|  | ||||
|         nbt.setTag(NBT_CRAFTING_TASKS, list); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|   | ||||
| @@ -1,9 +1,21 @@ | ||||
| package refinedstorage.tile.autocrafting; | ||||
|  | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import net.minecraft.nbt.NBTTagList; | ||||
| import net.minecraft.tileentity.TileEntity; | ||||
| import net.minecraft.util.math.BlockPos; | ||||
| import net.minecraft.world.World; | ||||
| import net.minecraftforge.common.util.Constants; | ||||
| import refinedstorage.item.ItemPattern; | ||||
| import refinedstorage.util.InventoryUtils; | ||||
|  | ||||
| public class CraftingPattern { | ||||
|     public static final String NBT = "Pattern"; | ||||
|     public static final String NBT_CRAFTER_X = "CrafterX"; | ||||
|     public static final String NBT_CRAFTER_Y = "CrafterY"; | ||||
|     public static final String NBT_CRAFTER_Z = "CrafterZ"; | ||||
|  | ||||
|     private TileCrafter crafter; | ||||
|     private boolean processing; | ||||
|     private ItemStack[] inputs; | ||||
| @@ -58,4 +70,52 @@ public class CraftingPattern { | ||||
|  | ||||
|         return true; | ||||
|     } | ||||
|  | ||||
|     public void writeToNBT(NBTTagCompound tag) { | ||||
|         tag.setBoolean(ItemPattern.NBT_PROCESSING, processing); | ||||
|  | ||||
|         NBTTagList inputsTag = new NBTTagList(); | ||||
|         for (ItemStack input : inputs) { | ||||
|             inputsTag.appendTag(input.serializeNBT()); | ||||
|         } | ||||
|         tag.setTag(ItemPattern.NBT_INPUTS, inputsTag); | ||||
|  | ||||
|         NBTTagList outputsTag = new NBTTagList(); | ||||
|         for (ItemStack output : outputs) { | ||||
|             outputsTag.appendTag(output.serializeNBT()); | ||||
|         } | ||||
|         tag.setTag(ItemPattern.NBT_OUTPUTS, outputsTag); | ||||
|  | ||||
|         tag.setInteger(NBT_CRAFTER_X, crafter.getPos().getX()); | ||||
|         tag.setInteger(NBT_CRAFTER_Y, crafter.getPos().getY()); | ||||
|         tag.setInteger(NBT_CRAFTER_Z, crafter.getPos().getZ()); | ||||
|     } | ||||
|  | ||||
|     public static CraftingPattern readFromNBT(World world, NBTTagCompound tag) { | ||||
|         int cx = tag.getInteger(NBT_CRAFTER_X); | ||||
|         int cy = tag.getInteger(NBT_CRAFTER_Y); | ||||
|         int cz = tag.getInteger(NBT_CRAFTER_Z); | ||||
|  | ||||
|         TileEntity crafter = world.getTileEntity(new BlockPos(cx, cy, cz)); | ||||
|  | ||||
|         if (crafter instanceof TileCrafter) { | ||||
|             boolean processing = tag.getBoolean(ItemPattern.NBT_PROCESSING); | ||||
|  | ||||
|             NBTTagList inputsTag = tag.getTagList(ItemPattern.NBT_INPUTS, Constants.NBT.TAG_COMPOUND); | ||||
|             ItemStack inputs[] = new ItemStack[inputsTag.tagCount()]; | ||||
|             for (int i = 0; i < inputsTag.tagCount(); ++i) { | ||||
|                 inputs[i] = ItemStack.loadItemStackFromNBT(inputsTag.getCompoundTagAt(i)); | ||||
|             } | ||||
|  | ||||
|             NBTTagList outputsTag = tag.getTagList(ItemPattern.NBT_OUTPUTS, Constants.NBT.TAG_COMPOUND); | ||||
|             ItemStack outputs[] = new ItemStack[outputsTag.tagCount()]; | ||||
|             for (int i = 0; i < outputsTag.tagCount(); ++i) { | ||||
|                 outputs[i] = ItemStack.loadItemStackFromNBT(outputsTag.getCompoundTagAt(i)); | ||||
|             } | ||||
|  | ||||
|             return new CraftingPattern((TileCrafter) crafter, processing, inputs, outputs); | ||||
|         } | ||||
|  | ||||
|         return null; | ||||
|     } | ||||
| } | ||||
|   | ||||
| @@ -1,14 +1,25 @@ | ||||
| package refinedstorage.tile.autocrafting.task; | ||||
|  | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import net.minecraft.nbt.NBTTagList; | ||||
| import net.minecraft.util.text.TextFormatting; | ||||
| import net.minecraft.world.World; | ||||
| import refinedstorage.tile.TileController; | ||||
| import refinedstorage.tile.autocrafting.CraftingPattern; | ||||
| import refinedstorage.util.NBTUtils; | ||||
|  | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
|  | ||||
| public class BasicCraftingTask implements ICraftingTask { | ||||
|     public static final int ID = 0; | ||||
|  | ||||
|     public static final String NBT_SATISFIED = "Satisfied"; | ||||
|     public static final String NBT_CHECKED = "Checked"; | ||||
|     public static final String NBT_CHILD_TASKS = "ChildTasks"; | ||||
|     public static final String NBT_TOOK = "Took"; | ||||
|  | ||||
|     private CraftingPattern pattern; | ||||
|     private boolean satisfied[]; | ||||
|     private boolean checked[]; | ||||
| @@ -22,6 +33,13 @@ public class BasicCraftingTask implements ICraftingTask { | ||||
|         this.childTasks = new boolean[pattern.getInputs().length]; | ||||
|     } | ||||
|  | ||||
|     public BasicCraftingTask(World world, NBTTagCompound tag) { | ||||
|         this.pattern = CraftingPattern.readFromNBT(world, tag.getCompoundTag(CraftingPattern.NBT)); | ||||
|         this.satisfied = NBTUtils.readBoolArray(tag, NBT_SATISFIED); | ||||
|         this.checked = NBTUtils.readBoolArray(tag, NBT_CHECKED); | ||||
|         this.childTasks = NBTUtils.readBoolArray(tag, NBT_CHILD_TASKS); | ||||
|     } | ||||
|  | ||||
|     public CraftingPattern getPattern() { | ||||
|         return pattern; | ||||
|     } | ||||
| @@ -76,6 +94,27 @@ public class BasicCraftingTask implements ICraftingTask { | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void writeToNBT(NBTTagCompound tag) { | ||||
|         NBTTagCompound patternTag = new NBTTagCompound(); | ||||
|         pattern.writeToNBT(patternTag); | ||||
|         tag.setTag(CraftingPattern.NBT, patternTag); | ||||
|  | ||||
|         NBTUtils.writeBoolArray(tag, NBT_SATISFIED, satisfied); | ||||
|         NBTUtils.writeBoolArray(tag, NBT_CHECKED, checked); | ||||
|         NBTUtils.writeBoolArray(tag, NBT_CHILD_TASKS, childTasks); | ||||
|  | ||||
|         NBTTagList tookList = new NBTTagList(); | ||||
|  | ||||
|         for (ItemStack took : itemsTook) { | ||||
|             tookList.appendTag(took.serializeNBT()); | ||||
|         } | ||||
|  | ||||
|         tag.setTag(NBT_TOOK, tookList); | ||||
|  | ||||
|         tag.setInteger("Type", ID); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String getInfo() { | ||||
|         StringBuilder builder = new StringBuilder(); | ||||
|   | ||||
| @@ -1,5 +1,6 @@ | ||||
| package refinedstorage.tile.autocrafting.task; | ||||
|  | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import refinedstorage.tile.TileController; | ||||
| import refinedstorage.tile.autocrafting.CraftingPattern; | ||||
|  | ||||
| @@ -12,5 +13,7 @@ public interface ICraftingTask { | ||||
|  | ||||
|     void onCancelled(TileController controller); | ||||
|  | ||||
|     void writeToNBT(NBTTagCompound tag); | ||||
|  | ||||
|     String getInfo(); | ||||
| } | ||||
|   | ||||
| @@ -2,15 +2,24 @@ package refinedstorage.tile.autocrafting.task; | ||||
|  | ||||
| import net.minecraft.inventory.IInventory; | ||||
| import net.minecraft.item.ItemStack; | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import net.minecraft.tileentity.TileEntity; | ||||
| import net.minecraft.tileentity.TileEntityHopper; | ||||
| import net.minecraft.util.text.TextFormatting; | ||||
| import net.minecraft.world.World; | ||||
| import refinedstorage.tile.TileController; | ||||
| import refinedstorage.tile.autocrafting.CraftingPattern; | ||||
| import refinedstorage.tile.autocrafting.TileCrafter; | ||||
| import refinedstorage.util.InventoryUtils; | ||||
| import refinedstorage.util.NBTUtils; | ||||
|  | ||||
| public class ProcessingCraftingTask implements ICraftingTask { | ||||
|     public static final int ID = 1; | ||||
|  | ||||
|     public static final String NBT_INSERTED = "Inserted"; | ||||
|     public static final String NBT_MISSING = "Missing"; | ||||
|     public static final String NBT_SATISFIED = "Satisfied"; | ||||
|  | ||||
|     private CraftingPattern pattern; | ||||
|     private boolean inserted[]; | ||||
|     private boolean missing[]; | ||||
| @@ -23,6 +32,13 @@ public class ProcessingCraftingTask implements ICraftingTask { | ||||
|         this.satisfied = new boolean[pattern.getOutputs().length]; | ||||
|     } | ||||
|  | ||||
|     public ProcessingCraftingTask(World world, NBTTagCompound tag) { | ||||
|         this.pattern = CraftingPattern.readFromNBT(world, tag.getCompoundTag(CraftingPattern.NBT)); | ||||
|         this.inserted = NBTUtils.readBoolArray(tag, NBT_INSERTED); | ||||
|         this.missing = NBTUtils.readBoolArray(tag, NBT_MISSING); | ||||
|         this.satisfied = NBTUtils.readBoolArray(tag, NBT_SATISFIED); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public CraftingPattern getPattern() { | ||||
|         return pattern; | ||||
| @@ -89,6 +105,19 @@ public class ProcessingCraftingTask implements ICraftingTask { | ||||
|         // NO OP | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public void writeToNBT(NBTTagCompound tag) { | ||||
|         NBTTagCompound patternTag = new NBTTagCompound(); | ||||
|         pattern.writeToNBT(patternTag); | ||||
|         tag.setTag(CraftingPattern.NBT, patternTag); | ||||
|  | ||||
|         NBTUtils.writeBoolArray(tag, NBT_INSERTED, satisfied); | ||||
|         NBTUtils.writeBoolArray(tag, NBT_MISSING, missing); | ||||
|         NBTUtils.writeBoolArray(tag, NBT_SATISFIED, satisfied); | ||||
|  | ||||
|         tag.setInteger("Type", ID); | ||||
|     } | ||||
|  | ||||
|     @Override | ||||
|     public String getInfo() { | ||||
|         StringBuilder builder = new StringBuilder(); | ||||
|   | ||||
							
								
								
									
										30
									
								
								src/main/java/refinedstorage/util/NBTUtils.java
									
									
									
									
									
										Executable file
									
								
							
							
						
						
									
										30
									
								
								src/main/java/refinedstorage/util/NBTUtils.java
									
									
									
									
									
										Executable file
									
								
							| @@ -0,0 +1,30 @@ | ||||
| package refinedstorage.util; | ||||
|  | ||||
| import net.minecraft.nbt.NBTTagByte; | ||||
| import net.minecraft.nbt.NBTTagCompound; | ||||
| import net.minecraft.nbt.NBTTagList; | ||||
| import net.minecraftforge.common.util.Constants; | ||||
|  | ||||
| public class NBTUtils { | ||||
|     public static void writeBoolArray(NBTTagCompound tag, String name, boolean[] arr) { | ||||
|         NBTTagList list = new NBTTagList(); | ||||
|  | ||||
|         for (int i = 0; i < arr.length; ++i) { | ||||
|             list.appendTag(new NBTTagByte(arr[i] ? (byte) 1 : (byte) 0)); | ||||
|         } | ||||
|  | ||||
|         tag.setTag(name, list); | ||||
|     } | ||||
|  | ||||
|     public static boolean[] readBoolArray(NBTTagCompound tag, String name) { | ||||
|         NBTTagList list = tag.getTagList(name, Constants.NBT.TAG_COMPOUND); | ||||
|  | ||||
|         boolean[] arr = new boolean[list.tagCount()]; | ||||
|  | ||||
|         for (int i = 0; i < arr.length; ++i) { | ||||
|             arr[i] = ((NBTTagByte) list.get(i)).getByte() == 1 ? true : false; | ||||
|         } | ||||
|  | ||||
|         return arr; | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user