Very rough code for the new crafting system
This commit is contained in:
109
src/main/java/refinedstorage/apiimpl/autocrafting/v2/CraftingTask.java
Executable file
109
src/main/java/refinedstorage/apiimpl/autocrafting/v2/CraftingTask.java
Executable file
@@ -0,0 +1,109 @@
|
|||||||
|
package refinedstorage.apiimpl.autocrafting.v2;
|
||||||
|
|
||||||
|
import com.google.common.collect.ArrayListMultimap;
|
||||||
|
import com.google.common.collect.Multimap;
|
||||||
|
import net.minecraft.item.Item;
|
||||||
|
import net.minecraft.item.ItemStack;
|
||||||
|
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||||
|
import refinedstorage.api.network.INetworkMaster;
|
||||||
|
import refinedstorage.api.network.NetworkUtils;
|
||||||
|
import refinedstorage.api.storage.CompareUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayDeque;
|
||||||
|
import java.util.Deque;
|
||||||
|
|
||||||
|
public class CraftingTask {
|
||||||
|
private INetworkMaster network;
|
||||||
|
private ICraftingPattern pattern;
|
||||||
|
private int quantity;
|
||||||
|
private Deque<ItemStack> toTake = new ArrayDeque<>();
|
||||||
|
private Multimap<Item, ItemStack> toCraft = ArrayListMultimap.create();
|
||||||
|
private Multimap<Item, ItemStack> missing = ArrayListMultimap.create();
|
||||||
|
|
||||||
|
public CraftingTask(INetworkMaster network, ICraftingPattern pattern, int quantity) {
|
||||||
|
this.network = network;
|
||||||
|
this.pattern = pattern;
|
||||||
|
this.quantity = quantity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void calculate() {
|
||||||
|
calculate(pattern, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void calculate(ICraftingPattern pattern, boolean basePattern) {
|
||||||
|
for (int i = 0; i < quantity; ++i) {
|
||||||
|
for (ItemStack input : pattern.getInputs()) {
|
||||||
|
ItemStack inputInNetwork = network.getItemStorage().get(input, CompareUtils.COMPARE_DAMAGE | CompareUtils.COMPARE_NBT);
|
||||||
|
|
||||||
|
if (inputInNetwork == null || inputInNetwork.stackSize == 0) {
|
||||||
|
ICraftingPattern inputPattern = NetworkUtils.getPattern(network, input);
|
||||||
|
|
||||||
|
if (inputPattern != null) {
|
||||||
|
addToCraft(input);
|
||||||
|
|
||||||
|
calculate(inputPattern, false);
|
||||||
|
} else {
|
||||||
|
addMissing(input);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
toTake.push(input);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "quantity=" + quantity + ",toTake=" + toTake.toString() + ",toCraft=" + toCraft.toString() + ",missing=" + missing.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean update() {
|
||||||
|
if (!toTake.isEmpty()) {
|
||||||
|
ItemStack took = NetworkUtils.extractItem(network, toTake.peek(), 1);
|
||||||
|
|
||||||
|
if (took != null) {
|
||||||
|
toTake.pop();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (toTake.isEmpty() && missing.isEmpty()) {
|
||||||
|
for (ItemStack output : pattern.getOutputs()) {
|
||||||
|
// @TODO: Handle remainder
|
||||||
|
network.insertItem(output, output.stackSize, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ItemStack byproduct : pattern.getByproducts()) {
|
||||||
|
// @TODO: Handle remainder
|
||||||
|
network.insertItem(byproduct, byproduct.stackSize, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addMissing(ItemStack stack) {
|
||||||
|
for (ItemStack m : missing.get(stack.getItem())) {
|
||||||
|
if (CompareUtils.compareStackNoQuantity(m, stack)) {
|
||||||
|
m.stackSize += stack.stackSize;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
missing.put(stack.getItem(), stack.copy());
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addToCraft(ItemStack stack) {
|
||||||
|
for (ItemStack m : toCraft.get(stack.getItem())) {
|
||||||
|
if (CompareUtils.compareStackNoQuantity(m, stack)) {
|
||||||
|
m.stackSize += stack.stackSize;
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
toCraft.put(stack.getItem(), stack.copy());
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,8 +12,8 @@ import refinedstorage.api.network.INetworkMaster;
|
|||||||
import refinedstorage.api.network.NetworkUtils;
|
import refinedstorage.api.network.NetworkUtils;
|
||||||
import refinedstorage.api.network.grid.IItemGridHandler;
|
import refinedstorage.api.network.grid.IItemGridHandler;
|
||||||
import refinedstorage.api.storage.CompareUtils;
|
import refinedstorage.api.storage.CompareUtils;
|
||||||
import refinedstorage.apiimpl.autocrafting.preview.CraftingPreviewData;
|
import refinedstorage.apiimpl.autocrafting.v2.CraftingTask;
|
||||||
import refinedstorage.network.MessageGridCraftingPreviewResponse;
|
import refinedstorage.tile.TileController;
|
||||||
|
|
||||||
public class ItemGridHandler implements IItemGridHandler {
|
public class ItemGridHandler implements IItemGridHandler {
|
||||||
private INetworkMaster network;
|
private INetworkMaster network;
|
||||||
@@ -125,11 +125,16 @@ public class ItemGridHandler implements IItemGridHandler {
|
|||||||
ItemStack stack = network.getItemStorage().get(hash);
|
ItemStack stack = network.getItemStorage().get(hash);
|
||||||
|
|
||||||
if (stack != null) {
|
if (stack != null) {
|
||||||
CraftingPreviewData previewData = new CraftingPreviewData(network);
|
CraftingTask t = new CraftingTask(network, NetworkUtils.getPattern(network, stack), quantity);
|
||||||
|
t.calculate();
|
||||||
|
System.out.println(t.toString());
|
||||||
|
((TileController) network).craftingTasksV2.add(t);
|
||||||
|
|
||||||
|
/*CraftingPreviewData previewData = new CraftingPreviewData(network);
|
||||||
|
|
||||||
previewData.calculate(stack, quantity);
|
previewData.calculate(stack, quantity);
|
||||||
|
|
||||||
RefinedStorage.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(previewData.values(), hash, quantity), player);
|
RefinedStorage.INSTANCE.network.sendTo(new MessageGridCraftingPreviewResponse(previewData.values(), hash, quantity), player);*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,6 @@ import net.minecraft.item.ItemStack;
|
|||||||
import net.minecraft.nbt.NBTTagCompound;
|
import net.minecraft.nbt.NBTTagCompound;
|
||||||
import net.minecraft.nbt.NBTTagList;
|
import net.minecraft.nbt.NBTTagList;
|
||||||
import net.minecraft.network.datasync.DataSerializers;
|
import net.minecraft.network.datasync.DataSerializers;
|
||||||
import net.minecraft.tileentity.TileEntity;
|
|
||||||
import net.minecraft.util.EnumFacing;
|
import net.minecraft.util.EnumFacing;
|
||||||
import net.minecraft.util.math.BlockPos;
|
import net.minecraft.util.math.BlockPos;
|
||||||
import net.minecraft.world.World;
|
import net.minecraft.world.World;
|
||||||
@@ -21,12 +20,8 @@ import net.minecraftforge.fluids.FluidStack;
|
|||||||
import net.minecraftforge.items.ItemHandlerHelper;
|
import net.minecraftforge.items.ItemHandlerHelper;
|
||||||
import refinedstorage.RefinedStorage;
|
import refinedstorage.RefinedStorage;
|
||||||
import refinedstorage.RefinedStorageBlocks;
|
import refinedstorage.RefinedStorageBlocks;
|
||||||
import refinedstorage.api.RefinedStorageAPI;
|
|
||||||
import refinedstorage.api.autocrafting.ICraftingPattern;
|
import refinedstorage.api.autocrafting.ICraftingPattern;
|
||||||
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
|
import refinedstorage.api.autocrafting.ICraftingPatternContainer;
|
||||||
import refinedstorage.api.autocrafting.ICraftingPatternProvider;
|
|
||||||
import refinedstorage.api.autocrafting.registry.ICraftingTaskFactory;
|
|
||||||
import refinedstorage.api.autocrafting.task.CraftingTask;
|
|
||||||
import refinedstorage.api.autocrafting.task.ICraftingTask;
|
import refinedstorage.api.autocrafting.task.ICraftingTask;
|
||||||
import refinedstorage.api.network.*;
|
import refinedstorage.api.network.*;
|
||||||
import refinedstorage.api.network.grid.IFluidGridHandler;
|
import refinedstorage.api.network.grid.IFluidGridHandler;
|
||||||
@@ -37,6 +32,7 @@ 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.task.CraftingTaskProcessing;
|
import refinedstorage.apiimpl.autocrafting.task.CraftingTaskProcessing;
|
||||||
|
import refinedstorage.apiimpl.autocrafting.v2.CraftingTask;
|
||||||
import refinedstorage.apiimpl.network.NetworkNodeGraph;
|
import refinedstorage.apiimpl.network.NetworkNodeGraph;
|
||||||
import refinedstorage.apiimpl.network.WirelessGridHandler;
|
import refinedstorage.apiimpl.network.WirelessGridHandler;
|
||||||
import refinedstorage.apiimpl.network.grid.FluidGridHandler;
|
import refinedstorage.apiimpl.network.grid.FluidGridHandler;
|
||||||
@@ -180,6 +176,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
|
|
||||||
private List<ICraftingPattern> patterns = new ArrayList<>();
|
private List<ICraftingPattern> patterns = new ArrayList<>();
|
||||||
|
|
||||||
|
public List<CraftingTask> craftingTasksV2 = new ArrayList<>();
|
||||||
private List<ICraftingTask> craftingTasks = new ArrayList<>();
|
private List<ICraftingTask> craftingTasks = new ArrayList<>();
|
||||||
private List<ICraftingTask> craftingTasksToAdd = new ArrayList<>();
|
private List<ICraftingTask> craftingTasksToAdd = new ArrayList<>();
|
||||||
private List<ICraftingTask> craftingTasksToCancel = new ArrayList<>();
|
private List<ICraftingTask> craftingTasksToCancel = new ArrayList<>();
|
||||||
@@ -275,7 +272,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
|
|
||||||
craftingTasksToAdd.clear();
|
craftingTasksToAdd.clear();
|
||||||
|
|
||||||
Iterator<ICraftingTask> craftingTaskIterator = craftingTasks.iterator();
|
/*Iterator<ICraftingTask> craftingTaskIterator = craftingTasks.iterator();
|
||||||
|
|
||||||
while (craftingTaskIterator.hasNext()) {
|
while (craftingTaskIterator.hasNext()) {
|
||||||
ICraftingTask task = craftingTaskIterator.next();
|
ICraftingTask task = craftingTaskIterator.next();
|
||||||
@@ -291,6 +288,16 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
markDirty();
|
markDirty();
|
||||||
|
|
||||||
updateCraftingTasks();
|
updateCraftingTasks();
|
||||||
|
}*/
|
||||||
|
|
||||||
|
Iterator<CraftingTask> craftingTaskIterator = craftingTasksV2.iterator();
|
||||||
|
|
||||||
|
while (craftingTaskIterator.hasNext()) {
|
||||||
|
CraftingTask task = craftingTaskIterator.next();
|
||||||
|
|
||||||
|
if (task.update()) {
|
||||||
|
craftingTaskIterator.remove();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -704,7 +711,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static ICraftingTask readCraftingTask(World world, int depth, NBTTagCompound tag) {
|
public static ICraftingTask readCraftingTask(World world, int depth, NBTTagCompound tag) {
|
||||||
ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(CraftingTask.NBT_PATTERN_STACK));
|
/* ItemStack stack = ItemStack.loadItemStackFromNBT(tag.getCompoundTag(CraftingTask.NBT_PATTERN_STACK));
|
||||||
|
|
||||||
if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) {
|
if (stack != null && stack.getItem() instanceof ICraftingPatternProvider) {
|
||||||
TileEntity container = world.getTileEntity(BlockPos.fromLong(tag.getLong(CraftingTask.NBT_PATTERN_CONTAINER)));
|
TileEntity container = world.getTileEntity(BlockPos.fromLong(tag.getLong(CraftingTask.NBT_PATTERN_CONTAINER)));
|
||||||
@@ -718,7 +725,7 @@ public class TileController extends TileBase implements INetworkMaster, IEnergyR
|
|||||||
return factory.create(world, depth, tag, pattern);
|
return factory.create(world, depth, tag, pattern);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user