Crafting pattern serialization.
This commit is contained in:
@@ -14,6 +14,11 @@ public interface ICraftingPattern {
|
|||||||
*/
|
*/
|
||||||
ICraftingPatternContainer getContainer(World world);
|
ICraftingPatternContainer getContainer(World world);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return The crafting pattern stack
|
||||||
|
*/
|
||||||
|
ItemStack getStack();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return The inputs
|
* @return The inputs
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -2,11 +2,11 @@ package refinedstorage.api.autocrafting;
|
|||||||
|
|
||||||
import net.minecraft.item.ItemStack;
|
import net.minecraft.item.ItemStack;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implement this interface on pattern items.
|
* Implement this interface on pattern items.
|
||||||
* When you implement this interface on your patterns, they will be insertable in crafters.
|
* When you implement this interface on your patterns, they will be insertable in crafters.
|
||||||
*
|
|
||||||
* @todo: Serialization from controller!
|
|
||||||
*/
|
*/
|
||||||
public interface ICraftingPatternProvider {
|
public interface ICraftingPatternProvider {
|
||||||
/**
|
/**
|
||||||
@@ -17,4 +17,13 @@ public interface ICraftingPatternProvider {
|
|||||||
* @return The crafting pattern
|
* @return The crafting pattern
|
||||||
*/
|
*/
|
||||||
ICraftingPattern create(ItemStack stack, ICraftingPatternContainer container);
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import net.minecraft.nbt.NBTTagCompound;
|
|||||||
import net.minecraft.nbt.NBTTagList;
|
import net.minecraft.nbt.NBTTagList;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.Constants;
|
|
||||||
import refinedstorage.api.autocrafting.ICraftingPattern;
|
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||||
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
|
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
|
||||||
import refinedstorage.api.storage.CompareUtils;
|
import refinedstorage.api.storage.CompareUtils;
|
||||||
@@ -14,15 +13,12 @@ import refinedstorage.apiimpl.autocrafting.registry.CraftingTaskFactoryProcessin
|
|||||||
import refinedstorage.item.ItemPattern;
|
import refinedstorage.item.ItemPattern;
|
||||||
import refinedstorage.tile.TileCrafter;
|
import refinedstorage.tile.TileCrafter;
|
||||||
|
|
||||||
/**
|
|
||||||
* @TODO: Move this to ItemCraftingPattern and change slot checks in TileCrafter
|
|
||||||
*/
|
|
||||||
public class CraftingPattern implements ICraftingPattern {
|
public class CraftingPattern implements ICraftingPattern {
|
||||||
public static final String NBT = "Pattern";
|
public static final String NBT_CRAFTER_X = "CrafterX";
|
||||||
private static final String NBT_CRAFTER_X = "CrafterX";
|
public static final String NBT_CRAFTER_Y = "CrafterY";
|
||||||
private static final String NBT_CRAFTER_Y = "CrafterY";
|
public static final String NBT_CRAFTER_Z = "CrafterZ";
|
||||||
private static final String NBT_CRAFTER_Z = "CrafterZ";
|
|
||||||
|
|
||||||
|
private ItemStack stack;
|
||||||
private BlockPos crafterPos;
|
private BlockPos crafterPos;
|
||||||
private TileCrafter crafter;
|
private TileCrafter crafter;
|
||||||
private boolean processing;
|
private boolean processing;
|
||||||
@@ -30,7 +26,8 @@ public class CraftingPattern implements ICraftingPattern {
|
|||||||
private ItemStack[] outputs;
|
private ItemStack[] outputs;
|
||||||
private ItemStack[] byproducts;
|
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.crafterPos = crafterPos;
|
||||||
this.processing = processing;
|
this.processing = processing;
|
||||||
this.inputs = inputs;
|
this.inputs = inputs;
|
||||||
@@ -47,6 +44,11 @@ public class CraftingPattern implements ICraftingPattern {
|
|||||||
return crafter;
|
return crafter;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ItemStack getStack() {
|
||||||
|
return stack;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ItemStack[] getInputs() {
|
public ItemStack[] getInputs() {
|
||||||
return inputs;
|
return inputs;
|
||||||
@@ -120,49 +122,4 @@ public class CraftingPattern implements ICraftingPattern {
|
|||||||
|
|
||||||
return tag;
|
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import net.minecraft.nbt.NBTTagList;
|
|||||||
import net.minecraft.util.ActionResult;
|
import net.minecraft.util.ActionResult;
|
||||||
import net.minecraft.util.EnumActionResult;
|
import net.minecraft.util.EnumActionResult;
|
||||||
import net.minecraft.util.EnumHand;
|
import net.minecraft.util.EnumHand;
|
||||||
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.util.text.TextFormatting;
|
import net.minecraft.util.text.TextFormatting;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
import net.minecraftforge.common.util.Constants;
|
import net.minecraftforge.common.util.Constants;
|
||||||
@@ -19,6 +20,7 @@ import refinedstorage.api.autocrafting.ICraftingPatternProvider;
|
|||||||
import refinedstorage.api.storage.CompareUtils;
|
import refinedstorage.api.storage.CompareUtils;
|
||||||
import refinedstorage.apiimpl.autocrafting.CraftingPattern;
|
import refinedstorage.apiimpl.autocrafting.CraftingPattern;
|
||||||
|
|
||||||
|
import javax.annotation.Nullable;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
@@ -180,12 +182,58 @@ public class ItemPattern extends ItemBase implements ICraftingPatternProvider {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ICraftingPattern create(ItemStack stack, ICraftingPatternContainer container) {
|
public ICraftingPattern create(ItemStack stack, ICraftingPatternContainer container) {
|
||||||
return new CraftingPattern(
|
return new CraftingPattern(stack, container.getPosition(), isProcessing(stack), getInputs(stack), getOutputs(stack), getByproducts(stack));
|
||||||
container.getPosition(),
|
}
|
||||||
isProcessing(stack),
|
|
||||||
getInputs(stack),
|
@Override
|
||||||
getOutputs(stack),
|
@Nullable
|
||||||
getByproducts(stack)
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,7 +32,6 @@ import refinedstorage.api.storage.fluid.IFluidStorage;
|
|||||||
import refinedstorage.api.storage.fluid.IGroupedFluidStorage;
|
import refinedstorage.api.storage.fluid.IGroupedFluidStorage;
|
||||||
import refinedstorage.api.storage.item.IGroupedItemStorage;
|
import refinedstorage.api.storage.item.IGroupedItemStorage;
|
||||||
import refinedstorage.api.storage.item.IItemStorage;
|
import refinedstorage.api.storage.item.IItemStorage;
|
||||||
import refinedstorage.apiimpl.autocrafting.CraftingPattern;
|
|
||||||
import refinedstorage.apiimpl.autocrafting.task.CraftingTaskProcessing;
|
import refinedstorage.apiimpl.autocrafting.task.CraftingTaskProcessing;
|
||||||
import refinedstorage.apiimpl.network.NetworkNodeGraph;
|
import refinedstorage.apiimpl.network.NetworkNodeGraph;
|
||||||
import refinedstorage.apiimpl.network.WirelessGridHandler;
|
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";
|
public static final String NBT_ENERGY_CAPACITY = "EnergyCapacity";
|
||||||
|
|
||||||
private static final String NBT_CRAFTING_TASKS = "CraftingTasks";
|
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) -> {
|
private static final Comparator<IItemStorage> ITEM_SIZE_COMPARATOR = (left, right) -> {
|
||||||
if (left.getStored() == right.getStored()) {
|
if (left.getStored() == right.getStored()) {
|
||||||
@@ -711,16 +711,20 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ICraftingTask readCraftingTask(NBTTagCompound tag) {
|
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) {
|
if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) {
|
||||||
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
|
ICraftingPattern pattern = ((ICraftingPatternProvider) stack.getItem()).create(stack);
|
||||||
|
|
||||||
if (factory != null) {
|
if (pattern != null) {
|
||||||
ICraftingTask task = factory.create(tag, pattern);
|
ICraftingTaskFactory factory = RefinedStorageAPI.CRAFTING_TASK_REGISTRY.getFactory(tag.getString(NBT_CRAFTING_TASK_TYPE));
|
||||||
|
|
||||||
if (task != null) {
|
if (factory != null) {
|
||||||
return task;
|
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);
|
task.writeToNBT(taskTag);
|
||||||
|
|
||||||
taskTag.setString(NBT_CRAFTING_TASK_TYPE, task.getPattern().getId());
|
taskTag.setString(NBT_CRAFTING_TASK_TYPE, task.getPattern().getId());
|
||||||
|
taskTag.setTag(NBT_CRAFTING_TASK_PATTERN, task.getPattern().getStack().serializeNBT());
|
||||||
|
|
||||||
list.appendTag(taskTag);
|
list.appendTag(taskTag);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user