Crafting task read.

This commit is contained in:
raoulvdberge
2018-06-18 16:03:11 +02:00
parent 3b73c615a6
commit 7a1954d84b
13 changed files with 208 additions and 24 deletions

View File

@@ -100,7 +100,7 @@ public interface ICraftingManager {
/**
* @param tag the tag to read from
*/
void readFromNBT(NBTTagCompound tag);
void readFromNbt(NBTTagCompound tag);
/**
* @param tag the tag to write to

View File

@@ -1,13 +1,13 @@
package com.raoulvdberge.refinedstorage.api.autocrafting.registry;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/**
* A factory that creates a crafting task.
@@ -15,15 +15,23 @@ import javax.annotation.Nullable;
*/
public interface ICraftingTaskFactory {
/**
* Returns a crafting task for a given NBT tag and pattern.
* Returns a crafting task for a given pattern.
*
* @param network the network
* @param stack the stack to create a task for
* @param pattern the pattern
* @param quantity the quantity
* @param tag the NBT tag, if this is null it isn't reading from disk but is used for making a task on demand
* @return the crafting task
*/
@Nonnull
ICraftingTask create(INetwork network, ItemStack stack, int quantity, ICraftingPattern pattern, @Nullable NBTTagCompound tag);
ICraftingTask create(INetwork network, ItemStack stack, int quantity, ICraftingPattern pattern);
/**
* Returns a crafting task for a given NBT tag.
*
* @param network the network
* @param tag the tag
* @return the crafting task
*/
ICraftingTask createFromNbt(INetwork network, NBTTagCompound tag) throws CraftingTaskReadException;
}

View File

@@ -0,0 +1,16 @@
package com.raoulvdberge.refinedstorage.api.autocrafting.task;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import net.minecraft.nbt.NBTTagCompound;
/**
* Gets thrown from {@link com.raoulvdberge.refinedstorage.api.autocrafting.registry.ICraftingTaskFactory#createFromNbt(INetwork, NBTTagCompound)}.
*/
public class CraftingTaskReadException extends Exception {
/**
* @param message the message
*/
public CraftingTaskReadException(String message) {
super(message);
}
}

View File

@@ -6,6 +6,7 @@ import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternChainLis
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPatternContainer;
import com.raoulvdberge.refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorListener;
import com.raoulvdberge.refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTaskError;
import com.raoulvdberge.refinedstorage.api.network.node.INetworkNode;
@@ -14,6 +15,7 @@ import com.raoulvdberge.refinedstorage.tile.TileController;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.items.IItemHandlerModifiable;
import javax.annotation.Nonnull;
@@ -22,6 +24,8 @@ import java.util.*;
public class CraftingManager implements ICraftingManager {
private static final String NBT_TASKS = "Tasks";
private static final String NBT_TASK_TYPE = "Type";
private static final String NBT_TASK_DATA = "Task";
private TileController network;
@@ -32,6 +36,7 @@ public class CraftingManager implements ICraftingManager {
private Map<UUID, ICraftingTask> tasks = new LinkedHashMap<>();
private List<ICraftingTask> tasksToAdd = new ArrayList<>();
private List<UUID> tasksToCancel = new ArrayList<>();
private NBTTagList tasksToRead;
private Set<ICraftingMonitorListener> listeners = new HashSet<>();
@@ -80,7 +85,7 @@ public class CraftingManager implements ICraftingManager {
return null;
}
return factory.create(network, stack, quantity, pattern, null);
return factory.create(network, stack, quantity, pattern);
}
@Override
@@ -91,6 +96,28 @@ public class CraftingManager implements ICraftingManager {
@Override
public void update() {
if (network.canRun()) {
if (tasksToRead != null) {
for (int i = 0; i < tasksToRead.tagCount(); ++i) {
NBTTagCompound taskTag = tasksToRead.getCompoundTagAt(i);
String taskType = taskTag.getString(NBT_TASK_TYPE);
NBTTagCompound taskData = taskTag.getCompoundTag(NBT_TASK_DATA);
ICraftingTaskFactory factory = API.instance().getCraftingTaskRegistry().get(taskType);
if (factory != null) {
try {
ICraftingTask task = factory.createFromNbt(network, taskData);
tasks.put(task.getId(), task);
} catch (CraftingTaskReadException e) {
e.printStackTrace();
}
}
}
this.tasksToRead = null;
}
boolean changed = !tasksToCancel.isEmpty() || !tasksToAdd.isEmpty();
for (UUID idToCancel : tasksToCancel) {
@@ -125,8 +152,9 @@ public class CraftingManager implements ICraftingManager {
}
}
@Override // TODO
public void readFromNBT(NBTTagCompound tag) {
@Override
public void readFromNbt(NBTTagCompound tag) {
this.tasksToRead = tag.getTagList(NBT_TASKS, Constants.NBT.TAG_COMPOUND);
}
@Override
@@ -134,7 +162,12 @@ public class CraftingManager implements ICraftingManager {
NBTTagList list = new NBTTagList();
for (ICraftingTask task : tasks.values()) {
list.appendTag(task.writeToNbt(new NBTTagCompound()));
NBTTagCompound taskTag = new NBTTagCompound();
taskTag.setString(NBT_TASK_TYPE, task.getPattern().getId());
taskTag.setTag(NBT_TASK_DATA, task.writeToNbt(new NBTTagCompound()));
list.appendTag(taskTag);
}
tag.setTag(NBT_TASKS, list);

View File

@@ -2,6 +2,7 @@ package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.registry;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.CraftingTask;
@@ -9,15 +10,18 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
public class CraftingTaskFactory implements ICraftingTaskFactory {
public static final String ID = "normal";
@Nonnull
@Override
// TODO: handle tag?
public ICraftingTask create(INetwork network, ItemStack stack, int quantity, ICraftingPattern pattern, @Nullable NBTTagCompound tag) {
public ICraftingTask create(INetwork network, ItemStack stack, int quantity, ICraftingPattern pattern) {
return new CraftingTask(network, stack, quantity, pattern);
}
@Override
public ICraftingTask createFromNbt(INetwork network, NBTTagCompound tag) throws CraftingTaskReadException {
return new CraftingTask(network, tag);
}
}

View File

@@ -5,6 +5,7 @@ import com.raoulvdberge.refinedstorage.api.autocrafting.craftingmonitor.ICraftin
import com.raoulvdberge.refinedstorage.api.autocrafting.craftingmonitor.ICraftingMonitorElementList;
import com.raoulvdberge.refinedstorage.api.autocrafting.preview.ICraftingPreviewElement;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskErrorType;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.ICraftingTaskError;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
@@ -29,6 +30,7 @@ import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.NonNullList;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.util.Constants;
import org.apache.commons.lang3.tuple.Pair;
import javax.annotation.Nullable;
@@ -69,6 +71,39 @@ public class CraftingTask implements ICraftingTask {
this.pattern = pattern;
}
public CraftingTask(INetwork network, NBTTagCompound tag) throws CraftingTaskReadException {
this.network = network;
this.requested = new ItemStack(tag.getCompoundTag(NBT_REQUESTED));
if (requested.isEmpty()) {
throw new CraftingTaskReadException("Requested item doesn't exist anymore");
}
this.quantity = tag.getInteger(NBT_QUANTITY);
this.pattern = readPatternFromNbt(tag.getCompoundTag(NBT_PATTERN), network.world());
this.inserter = new CraftingInserter(network, tag.getTagList(NBT_INSERTER, Constants.NBT.TAG_COMPOUND));
this.ticks = tag.getInteger(NBT_TICKS);
this.id = tag.getUniqueId(NBT_ID);
NBTTagList steps = tag.getTagList(NBT_STEPS, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < steps.tagCount(); ++i) {
NBTTagCompound stepTag = steps.getCompoundTagAt(i);
this.steps.add(CraftingStep.readFromNbt(network, inserter, stepTag));
}
NBTTagList missing = tag.getTagList(NBT_MISSING, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < missing.tagCount(); ++i) {
ItemStack missingItem = new ItemStack(missing.getCompoundTagAt(i));
if (missingItem.isEmpty()) {
throw new CraftingTaskReadException("Missing item is empty");
}
this.missing.add(missingItem);
}
}
@Override
@Nullable
public ICraftingTaskError calculate() {
@@ -534,8 +569,7 @@ public class CraftingTask implements ICraftingTask {
return tag;
}
@Nullable
public static ICraftingPattern readPatternFromNbt(NBTTagCompound tag, World world) {
public static ICraftingPattern readPatternFromNbt(NBTTagCompound tag, World world) throws CraftingTaskReadException {
BlockPos containerPos = BlockPos.fromLong(tag.getLong(NBT_PATTERN_CONTAINER_POS));
INetworkNode node = API.instance().getNetworkNodeManager(world).getNode(containerPos);
@@ -545,9 +579,11 @@ public class CraftingTask implements ICraftingTask {
if (stack.getItem() instanceof ICraftingPatternProvider) {
return ((ICraftingPatternProvider) stack.getItem()).create(world, stack, (ICraftingPatternContainer) node);
} else {
throw new CraftingTaskReadException("Pattern stack is not a crafting pattern provider");
}
} else {
throw new CraftingTaskReadException("Crafting pattern container doesn't exist anymore");
}
return null;
}
}

View File

@@ -1,5 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.extractor;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -30,7 +31,7 @@ public class CraftingExtractor {
}
}
public CraftingExtractor(INetwork network, NBTTagList tag, boolean processing) {
public CraftingExtractor(INetwork network, NBTTagList tag, boolean processing) throws CraftingTaskReadException {
this.network = network;
this.processing = processing;
@@ -40,6 +41,11 @@ public class CraftingExtractor {
NBTTagCompound itemTag = tag.getCompoundTagAt(i);
ItemStack stack = new ItemStack(itemTag.getCompoundTag(NBT_ITEM));
if (stack.isEmpty()) {
throw new CraftingTaskReadException("Extractor stack is empty");
}
CraftingExtractorItemStatus status = CraftingExtractorItemStatus.values()[itemTag.getInteger(NBT_STATUS)];
this.items.add(stack);

View File

@@ -1,5 +1,6 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.inserter;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
@@ -20,13 +21,18 @@ public class CraftingInserter {
this.network = network;
}
public CraftingInserter(INetwork network, NBTTagList list) {
public CraftingInserter(INetwork network, NBTTagList list) throws CraftingTaskReadException {
this(network);
for (int i = 0; i < list.tagCount(); ++i) {
NBTTagCompound itemTag = list.getCompoundTagAt(i);
ItemStack stack = new ItemStack(itemTag.getCompoundTag(NBT_ITEM));
if (stack.isEmpty()) {
throw new CraftingTaskReadException("Inserter has empty stack");
}
CraftingInserterItemStatus status = CraftingInserterItemStatus.values()[itemTag.getInteger(NBT_STATUS)];
items.push(new CraftingInserterItem(stack, status));

View File

@@ -1,7 +1,10 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.step;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.CraftingTask;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.inserter.CraftingInserter;
import net.minecraft.nbt.NBTTagCompound;
public abstract class CraftingStep {
@@ -43,4 +46,29 @@ public abstract class CraftingStep {
return tag;
}
public static CraftingStep readFromNbt(INetwork network, CraftingInserter inserter, NBTTagCompound tag) throws CraftingTaskReadException {
ICraftingPattern pattern = CraftingTask.readPatternFromNbt(tag.getCompoundTag(NBT_PATTERN), network.world());
boolean completed = tag.getBoolean(NBT_COMPLETED);
String type = tag.getString(NBT_TYPE);
CraftingStep step;
switch (type) {
case CraftingStepCraft.TYPE:
step = new CraftingStepCraft(pattern, inserter, network, tag);
break;
case CraftingStepProcess.TYPE:
step = new CraftingStepProcess(pattern, network, tag);
break;
default:
throw new CraftingTaskReadException("Unknown crafting step type");
}
if (completed) {
step.setCompleted();
}
return step;
}
}

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.step;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.extractor.CraftingExtractor;
import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.inserter.CraftingInserter;
@@ -8,10 +9,13 @@ import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.util.Constants;
import java.util.List;
public class CraftingStepCraft extends CraftingStep {
public static final String TYPE = "craft";
private static final String NBT_EXTRACTOR = "Extractor";
private static final String NBT_TOOK = "Took";
@@ -32,6 +36,24 @@ public class CraftingStepCraft extends CraftingStep {
this.took = took;
}
public CraftingStepCraft(ICraftingPattern pattern, CraftingInserter inserter, INetwork network, NBTTagCompound tag) throws CraftingTaskReadException {
super(pattern);
if (pattern.isProcessing()) {
throw new IllegalArgumentException("Cannot pass processing pattern to craft handler");
}
this.inserter = inserter;
this.extractor = new CraftingExtractor(network, tag.getTagList(NBT_EXTRACTOR, Constants.NBT.TAG_COMPOUND), false);
this.took = NonNullList.create();
NBTTagList tookList = tag.getTagList(NBT_TOOK, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tookList.tagCount(); ++i) {
took.add(new ItemStack(tookList.getCompoundTagAt(i)));
}
}
@Override
public boolean canExecute() {
extractor.updateStatus(null);
@@ -58,7 +80,7 @@ public class CraftingStepCraft extends CraftingStep {
@Override
public String getType() {
return "craft";
return TYPE;
}
@Override

View File

@@ -1,6 +1,7 @@
package com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.step;
import com.raoulvdberge.refinedstorage.api.autocrafting.ICraftingPattern;
import com.raoulvdberge.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.raoulvdberge.refinedstorage.api.network.INetwork;
import com.raoulvdberge.refinedstorage.api.util.IStackList;
import com.raoulvdberge.refinedstorage.apiimpl.API;
@@ -8,10 +9,13 @@ import com.raoulvdberge.refinedstorage.apiimpl.autocrafting.task.extractor.Craft
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraftforge.common.util.Constants;
import java.util.List;
public class CraftingStepProcess extends CraftingStep {
public static final String TYPE = "process";
private static final String NBT_EXTRACTOR = "Extractor";
private static final String NBT_TO_RECEIVE = "ToReceive";
@@ -32,6 +36,27 @@ public class CraftingStepProcess extends CraftingStep {
}
}
public CraftingStepProcess(ICraftingPattern pattern, INetwork network, NBTTagCompound tag) throws CraftingTaskReadException {
super(pattern);
if (!pattern.isProcessing()) {
throw new IllegalArgumentException("Cannot pass non-processing pattern to processing handler");
}
this.extractor = new CraftingExtractor(network, tag.getTagList(NBT_EXTRACTOR, Constants.NBT.TAG_COMPOUND), true);
NBTTagList toReceiveList = tag.getTagList(NBT_TO_RECEIVE, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < toReceiveList.tagCount(); ++i) {
ItemStack toReceive = new ItemStack(toReceiveList.getCompoundTagAt(i));
if (toReceive.isEmpty()) {
throw new CraftingTaskReadException("Item to receive is empty");
}
this.itemsToReceive.add(toReceive);
}
}
@Override
public boolean canExecute() {
extractor.updateStatus(pattern.getContainer().getConnectedInventory());
@@ -67,7 +92,7 @@ public class CraftingStepProcess extends CraftingStep {
@Override
public String getType() {
return "process";
return TYPE;
}
@Override

View File

@@ -45,8 +45,8 @@ public class TabList {
public void init(int width) {
this.width = width;
this.left = gui.addButton(gui.getGuiLeft(), gui.getGuiTop() - 20, 20, 20, "<", true, pages.get() > 0);
this.right = gui.addButton(gui.getGuiLeft() + width - 22, gui.getGuiTop() - 20, 20, 20, ">", true, pages.get() > 0);
this.left = gui.addButton(gui.getGuiLeft(), gui.getGuiTop() - 22, 20, 20, "<", true, pages.get() > 0);
this.right = gui.addButton(gui.getGuiLeft() + width - 22, gui.getGuiTop() - 22, 20, 20, ">", true, pages.get() > 0);
}
public void addListener(ITabListListener listener) {
@@ -151,7 +151,7 @@ public class TabList {
if (pages.get() > 0) {
String text = (page.get() + 1) + " / " + (pages.get() + 1);
gui.drawString((int) ((width - (float) fontRenderer.getStringWidth(text)) / 2F), -14, text, 0xFFFFFF);
gui.drawString((int) ((width - (float) fontRenderer.getStringWidth(text)) / 2F), -16, text, 0xFFFFFF);
}
}

View File

@@ -482,7 +482,7 @@ public class TileController extends TileBase implements ITickable, INetwork, IRe
redstoneMode = RedstoneMode.read(tag);
craftingManager.readFromNBT(tag);
craftingManager.readFromNbt(tag);
readerWriterManager.readFromNBT(tag);