Crafting pattern serialization.

This commit is contained in:
Raoul Van den Berge
2016-09-01 20:20:01 +02:00
parent 0d856943fa
commit d0527e4d46
5 changed files with 96 additions and 72 deletions

View File

@@ -14,6 +14,11 @@ public interface ICraftingPattern {
*/
ICraftingPatternContainer getContainer(World world);
/**
* @return The crafting pattern stack
*/
ItemStack getStack();
/**
* @return The inputs
*/

View File

@@ -2,11 +2,11 @@ package refinedstorage.api.autocrafting;
import net.minecraft.item.ItemStack;
import javax.annotation.Nullable;
/**
* Implement this interface on pattern items.
* When you implement this interface on your patterns, they will be insertable in crafters.
*
* @todo: Serialization from controller!
*/
public interface ICraftingPatternProvider {
/**
@@ -17,4 +17,13 @@ public interface ICraftingPatternProvider {
* @return The crafting pattern
*/
ICraftingPattern create(ItemStack stack, ICraftingPatternContainer container);
/**
* Creates a crafting pattern from the pattern item stack.
*
* @param stack The item stack
* @return The crafting pattern, or null if the read failed
*/
@Nullable
ICraftingPattern create(ItemStack stack);
}

View File

@@ -5,7 +5,6 @@ import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import refinedstorage.api.autocrafting.ICraftingPattern;
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
import refinedstorage.api.storage.CompareUtils;
@@ -14,15 +13,12 @@ import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactoryProcessin
import refinedstorage.item.ItemPattern;
import refinedstorage.tile.TileCrafter;
/**
* @TODO: Move this to ItemCraftingPattern and change slot checks in TileCrafter
*/
public class CraftingPattern implements ICraftingPattern {
public static final String NBT = "Pattern";
private static final String NBT_CRAFTER_X = "CrafterX";
private static final String NBT_CRAFTER_Y = "CrafterY";
private static final String NBT_CRAFTER_Z = "CrafterZ";
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 ItemStack stack;
private BlockPos crafterPos;
private TileCrafter crafter;
private boolean processing;
@@ -30,7 +26,8 @@ public class CraftingPattern implements ICraftingPattern {
private ItemStack[] outputs;
private ItemStack[] byproducts;
public CraftingPattern(BlockPos crafterPos, boolean processing, ItemStack[] inputs, ItemStack[] outputs, ItemStack[] byproducts) {
public CraftingPattern(ItemStack stack, BlockPos crafterPos, boolean processing, ItemStack[] inputs, ItemStack[] outputs, ItemStack[] byproducts) {
this.stack = stack;
this.crafterPos = crafterPos;
this.processing = processing;
this.inputs = inputs;
@@ -47,6 +44,11 @@ public class CraftingPattern implements ICraftingPattern {
return crafter;
}
@Override
public ItemStack getStack() {
return stack;
}
@Override
public ItemStack[] getInputs() {
return inputs;
@@ -120,49 +122,4 @@ public class CraftingPattern implements ICraftingPattern {
return tag;
}
public static CraftingPattern readFromNBT(NBTTagCompound tag) {
BlockPos crafterPos = new BlockPos(tag.getInteger(NBT_CRAFTER_X), tag.getInteger(NBT_CRAFTER_Y), tag.getInteger(NBT_CRAFTER_Z));
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));
if (inputs[i] == null) {
return null;
}
}
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));
if (outputs[i] == null) {
return null;
}
}
ItemStack byproducts[] = new ItemStack[0];
if (tag.hasKey(ItemPattern.NBT_BYPRODUCTS)) {
NBTTagList byproductsTag = tag.getTagList(ItemPattern.NBT_BYPRODUCTS, Constants.NBT.TAG_COMPOUND);
byproducts = new ItemStack[byproductsTag.tagCount()];
for (int i = 0; i < byproductsTag.tagCount(); ++i) {
byproducts[i] = ItemStack.loadItemStackFromNBT(byproductsTag.getCompoundTagAt(i));
if (byproducts[i] == null) {
return null;
}
}
}
return new CraftingPattern(crafterPos, processing, inputs, outputs, byproducts);
}
}

View File

@@ -9,6 +9,7 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
@@ -19,6 +20,7 @@ import refinedstorage.api.autocrafting.ICraftingPatternProvider;
import refinedstorage.api.storage.CompareUtils;
import refinedstorage.apiimpl.autocrafting.CraftingPattern;
import javax.annotation.Nullable;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
@@ -180,12 +182,58 @@ public class ItemPattern extends ItemBase implements ICraftingPatternProvider {
@Override
public ICraftingPattern create(ItemStack stack, ICraftingPatternContainer container) {
return new CraftingPattern(
container.getPosition(),
isProcessing(stack),
getInputs(stack),
getOutputs(stack),
getByproducts(stack)
);
return new CraftingPattern(stack, container.getPosition(), isProcessing(stack), getInputs(stack), getOutputs(stack), getByproducts(stack));
}
@Override
@Nullable
public ICraftingPattern create(ItemStack stack) {
NBTTagCompound tag = stack.getTagCompound();
BlockPos crafterPos = new BlockPos(tag.getInteger(CraftingPattern.NBT_CRAFTER_X), tag.getInteger(CraftingPattern.NBT_CRAFTER_Y), tag.getInteger(CraftingPattern.NBT_CRAFTER_Z));
boolean processing = tag.getBoolean(NBT_PROCESSING);
NBTTagList inputsTag = tag.getTagList(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));
if (inputs[i] == null) {
return null;
}
}
NBTTagList outputsTag = tag.getTagList(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));
if (outputs[i] == null) {
return null;
}
}
ItemStack byproducts[] = new ItemStack[0];
if (tag.hasKey(ItemPattern.NBT_BYPRODUCTS)) {
NBTTagList byproductsTag = tag.getTagList(NBT_BYPRODUCTS, Constants.NBT.TAG_COMPOUND);
byproducts = new ItemStack[byproductsTag.tagCount()];
for (int i = 0; i < byproductsTag.tagCount(); ++i) {
byproducts[i] = ItemStack.loadItemStackFromNBT(byproductsTag.getCompoundTagAt(i));
if (byproducts[i] == null) {
return null;
}
}
}
return new CraftingPattern(stack, crafterPos, processing, inputs, outputs, byproducts);
}
}

View File

@@ -32,7 +32,6 @@ import refinedstorage.api.storage.fluid.IFluidStorage;
import refinedstorage.api.storage.fluid.IGroupedFluidStorage;
import refinedstorage.api.storage.item.IGroupedItemStorage;
import refinedstorage.api.storage.item.IItemStorage;
import refinedstorage.apiimpl.autocrafting.CraftingPattern;
import refinedstorage.apiimpl.autocrafting.task.CraftingTaskProcessing;
import refinedstorage.apiimpl.network.NetworkNodeGraph;
import refinedstorage.apiimpl.network.WirelessGridHandler;
@@ -131,7 +130,8 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
public static final String NBT_ENERGY_CAPACITY = "EnergyCapacity";
private static final String NBT_CRAFTING_TASKS = "CraftingTasks";
public static final String NBT_CRAFTING_TASK_TYPE = "Type";
private static final String NBT_CRAFTING_TASK_PATTERN = "Pattern";
private static final String NBT_CRAFTING_TASK_TYPE = "Type";
private static final Comparator<IItemStorage> ITEM_SIZE_COMPARATOR = (left, right) -> {
if (left.getStored() == right.getStored()) {
@@ -711,16 +711,20 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
}
public static ICraftingTask readCraftingTask(NBTTagCompound tag) {
CraftingPattern pattern = CraftingPattern.readFromNBT(tag.getCompoundTag(CraftingPattern.NBT));
ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(NBT_CRAFTING_TASK_PATTERN));
if (pattern != null) {
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) {
ICraftingPattern pattern = ((ICraftingPatternProvider) stack.getItem()).create(stack);
if (factory != null) {
ICraftingTask task = factory.create(tag, pattern);
if (pattern != null) {
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
if (task != null) {
return task;
if (factory != null) {
ICraftingTask task = factory.create(tag, pattern);
if (task != null) {
return task;
}
}
}
}
@@ -744,6 +748,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
task.writeToNBT(taskTag);
taskTag.setString(NBT_CRAFTING_TASK_TYPE, task.getPattern().getId());
taskTag.setTag(NBT_CRAFTING_TASK_PATTERN, task.getPattern().getStack().serializeNBT());
list.appendTag(taskTag);
}