Remove v5 autocrafting engine, add CraftingTaskState.

This commit is contained in:
raoulvdberge
2020-06-27 21:21:03 +02:00
parent 42e9a664c5
commit 2933dee849
13 changed files with 42 additions and 1552 deletions

View File

@@ -0,0 +1,9 @@
package com.refinedmods.refinedstorage.api.autocrafting.task;
public enum CraftingTaskState {
UNKNOWN,
CALCULATING,
CALCULATED,
RUNNING,
DONE
}

View File

@@ -46,9 +46,7 @@ public interface ICraftingTask {
/**
* @return the completion percentage
*/
default int getCompletionPercentage() {
return 0;
}
int getCompletionPercentage();
/**
* @return the stack requested
@@ -122,4 +120,9 @@ public interface ICraftingTask {
* @return the id of this task
*/
UUID getId();
/**
* @return the state of this crafting task
*/
CraftingTaskState getState();
}

View File

@@ -1,11 +1,10 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting;
import com.refinedmods.refinedstorage.RS;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPatternContainer;
import com.refinedmods.refinedstorage.api.util.IComparer;
import com.refinedmods.refinedstorage.apiimpl.API;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5.CraftingTaskFactory;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTaskFactory;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.container.Container;
@@ -166,8 +165,7 @@ public class CraftingPattern implements ICraftingPattern {
@Override
public ResourceLocation getCraftingTaskFactoryId() {
return RS.SERVER_CONFIG.getAutocrafting().useExperimentalAutocrafting() ? com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTaskFactory.ID :
CraftingTaskFactory.ID;
return CraftingTaskFactory.ID;
}
@Override
@@ -281,7 +279,7 @@ public class CraftingPattern implements ICraftingPattern {
@Override
public boolean equals(Object obj) {
if(obj instanceof CraftingPattern) {
if (obj instanceof CraftingPattern) {
return canBeInChainWith((CraftingPattern) obj);
}
return false;

View File

@@ -1,5 +1,5 @@
# Autocrafting v5
This is the fifth iteration of the autocrafting code.
# Autocrafting
Autocrafting has had several refactors.
Some history:

View File

@@ -1,80 +0,0 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.refinedmods.refinedstorage.api.network.INetwork;
import com.refinedmods.refinedstorage.api.util.IStackList;
import com.refinedmods.refinedstorage.util.StackUtils;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.nbt.ListNBT;
import net.minecraft.util.NonNullList;
import net.minecraftforge.common.util.Constants;
class Crafting {
private static final String NBT_PATTERN = "Pattern";
private static final String NBT_TOOK = "Took";
private static final String NBT_TO_EXTRACT = "ToExtract";
private static final String NBT_ROOT = "Root";
private final ICraftingPattern pattern;
private final NonNullList<ItemStack> took;
private final IStackList<ItemStack> toExtract;
private final boolean root;
public Crafting(ICraftingPattern pattern, NonNullList<ItemStack> took, IStackList<ItemStack> toExtract, boolean root) {
this.pattern = pattern;
this.took = took;
this.toExtract = toExtract;
this.root = root;
}
public Crafting(INetwork network, CompoundNBT tag) throws CraftingTaskReadException {
this.pattern = CraftingTask.readPatternFromNbt(tag.getCompound(NBT_PATTERN), network.getWorld());
this.toExtract = CraftingTask.readItemStackList(tag.getList(NBT_TO_EXTRACT, Constants.NBT.TAG_COMPOUND));
this.root = tag.getBoolean(NBT_ROOT);
this.took = NonNullList.create();
ListNBT tookList = tag.getList(NBT_TOOK, Constants.NBT.TAG_COMPOUND);
for (int i = 0; i < tookList.size(); ++i) {
ItemStack stack = StackUtils.deserializeStackFromNbt(tookList.getCompound(i));
// Can be empty.
took.add(stack);
}
}
public boolean isRoot() {
return root;
}
public ICraftingPattern getPattern() {
return pattern;
}
public NonNullList<ItemStack> getTook() {
return took;
}
public IStackList<ItemStack> getToExtract() {
return toExtract;
}
public CompoundNBT writeToNbt() {
CompoundNBT tag = new CompoundNBT();
tag.put(NBT_PATTERN, CraftingTask.writePatternToNbt(pattern));
tag.put(NBT_TO_EXTRACT, CraftingTask.writeItemStackList(toExtract));
tag.putBoolean(NBT_ROOT, root);
ListNBT tookList = new ListNBT();
for (ItemStack took : this.took) {
tookList.add(StackUtils.serializeStackToNbt(took));
}
tag.put(NBT_TOOK, tookList);
return tag;
}
}

View File

@@ -1,32 +0,0 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.CraftingTaskErrorType;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTaskError;
import javax.annotation.Nullable;
public class CraftingTaskError implements ICraftingTaskError {
private final CraftingTaskErrorType type;
private ICraftingPattern recursedPattern;
public CraftingTaskError(CraftingTaskErrorType type) {
this.type = type;
}
public CraftingTaskError(CraftingTaskErrorType type, ICraftingPattern recursedPattern) {
this.type = type;
this.recursedPattern = recursedPattern;
}
@Override
public CraftingTaskErrorType getType() {
return type;
}
@Override
@Nullable
public ICraftingPattern getRecursedPattern() {
return recursedPattern;
}
}

View File

@@ -1,28 +0,0 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5;
import com.refinedmods.refinedstorage.RS;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingRequestInfo;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTask;
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTaskFactory;
import com.refinedmods.refinedstorage.api.network.INetwork;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.util.ResourceLocation;
import javax.annotation.Nonnull;
public class CraftingTaskFactory implements ICraftingTaskFactory {
public static final ResourceLocation ID = new ResourceLocation(RS.ID, "v5");
@Nonnull
@Override
public ICraftingTask create(INetwork network, ICraftingRequestInfo requested, int quantity, ICraftingPattern pattern) {
return new CraftingTask(network, requested, quantity, pattern);
}
@Override
public ICraftingTask createFromNbt(INetwork network, CompoundNBT tag) throws CraftingTaskReadException {
return new CraftingTask(network, tag);
}
}

View File

@@ -1,93 +0,0 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5;
import com.refinedmods.refinedstorage.api.autocrafting.ICraftingPattern;
import com.refinedmods.refinedstorage.api.autocrafting.task.CraftingTaskReadException;
import com.refinedmods.refinedstorage.api.network.INetwork;
import com.refinedmods.refinedstorage.api.util.IStackList;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompoundNBT;
import net.minecraftforge.common.util.Constants;
import net.minecraftforge.fluids.FluidStack;
class Processing {
private static final String NBT_PATTERN = "Pattern";
private static final String NBT_ITEMS_TO_RECEIVE = "ItemsToReceive";
private static final String NBT_FLUIDS_TO_RECEIVE = "FluidsToReceive";
private static final String NBT_ITEMS_TO_PUT = "ItemsToPut";
private static final String NBT_FLUIDS_TO_PUT = "FluidsToPut";
private static final String NBT_STATE = "State";
private static final String NBT_ROOT = "Root";
private final ICraftingPattern pattern;
private final IStackList<ItemStack> itemsToReceive;
private final IStackList<FluidStack> fluidsToReceive;
private final IStackList<ItemStack> itemsToPut;
private final IStackList<FluidStack> fluidsToPut;
private ProcessingState state = ProcessingState.READY;
private final boolean root;
public Processing(ICraftingPattern pattern, IStackList<ItemStack> itemsToReceive, IStackList<FluidStack> fluidsToReceive, IStackList<ItemStack> itemsToPut, IStackList<FluidStack> fluidsToPut, boolean root) {
this.pattern = pattern;
this.itemsToReceive = itemsToReceive;
this.fluidsToReceive = fluidsToReceive;
this.itemsToPut = itemsToPut;
this.fluidsToPut = fluidsToPut;
this.root = root;
}
public Processing(INetwork network, CompoundNBT tag) throws CraftingTaskReadException {
this.pattern = CraftingTask.readPatternFromNbt(tag.getCompound(NBT_PATTERN), network.getWorld());
this.itemsToReceive = CraftingTask.readItemStackList(tag.getList(NBT_ITEMS_TO_RECEIVE, Constants.NBT.TAG_COMPOUND));
this.fluidsToReceive = CraftingTask.readFluidStackList(tag.getList(NBT_FLUIDS_TO_RECEIVE, Constants.NBT.TAG_COMPOUND));
this.root = tag.getBoolean(NBT_ROOT);
this.itemsToPut = CraftingTask.readItemStackList(tag.getList(NBT_ITEMS_TO_PUT, Constants.NBT.TAG_COMPOUND));
this.fluidsToPut = CraftingTask.readFluidStackList(tag.getList(NBT_FLUIDS_TO_PUT, Constants.NBT.TAG_COMPOUND));
this.state = ProcessingState.values()[tag.getInt(NBT_STATE)];
}
public ICraftingPattern getPattern() {
return pattern;
}
public IStackList<ItemStack> getItemsToReceive() {
return itemsToReceive;
}
public IStackList<FluidStack> getFluidsToReceive() {
return fluidsToReceive;
}
public IStackList<ItemStack> getItemsToPut() {
return itemsToPut;
}
public IStackList<FluidStack> getFluidsToPut() {
return fluidsToPut;
}
public void setState(ProcessingState state) {
this.state = state;
}
public ProcessingState getState() {
return state;
}
public boolean isRoot() {
return root;
}
public CompoundNBT writeToNbt() {
CompoundNBT tag = new CompoundNBT();
tag.put(NBT_PATTERN, CraftingTask.writePatternToNbt(pattern));
tag.put(NBT_ITEMS_TO_RECEIVE, CraftingTask.writeItemStackList(itemsToReceive));
tag.put(NBT_FLUIDS_TO_RECEIVE, CraftingTask.writeFluidStackList(fluidsToReceive));
tag.putBoolean(NBT_ROOT, root);
tag.put(NBT_ITEMS_TO_PUT, CraftingTask.writeItemStackList(itemsToPut));
tag.put(NBT_FLUIDS_TO_PUT, CraftingTask.writeFluidStackList(fluidsToPut));
tag.putInt(NBT_STATE, state.ordinal());
return tag;
}
}

View File

@@ -1,10 +0,0 @@
package com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5;
enum ProcessingState {
READY,
EXTRACTED_ALL,
MACHINE_NONE,
MACHINE_DOES_NOT_ACCEPT,
PROCESSED,
LOCKED
}

View File

@@ -82,6 +82,7 @@ public class CraftingTask implements ICraftingTask {
private int totalSteps;
private int currentstep;
private final Set<ICraftingPattern> patternsUsed = new HashSet<>();
private CraftingTaskState state = CraftingTaskState.UNKNOWN;
private final IStorageDisk<ItemStack> internalStorage;
private final IStorageDisk<FluidStack> internalFluidStorage;
@@ -233,6 +234,7 @@ public class CraftingTask implements ICraftingTask {
throw new IllegalStateException("Task already started!");
}
this.state = CraftingTaskState.CALCULATING;
this.calculationStarted = System.currentTimeMillis();
IStackList<ItemStack> results = API.instance().createItemStackList();
@@ -244,10 +246,11 @@ public class CraftingTask implements ICraftingTask {
int qtyPerCraft = getQuantityPerCraft(requested.getItem(), requested.getFluid(), this.pattern);
int qty = ((this.quantity - 1) / qtyPerCraft) + 1; //CeilDiv
ICraftingTaskError result = calculateInternal(qty, storage, fluidStorage, results, fluidResults, this.pattern, true);
if (result != null) {
this.state = CraftingTaskState.CALCULATED;
return result;
}
@@ -260,15 +263,19 @@ public class CraftingTask implements ICraftingTask {
req.setAmount(qty);
this.toCraftFluids.add(req);
}
if(missing.isEmpty()){
if (missing.isEmpty()) {
crafts.values().forEach(c -> {
totalSteps += c.getQuantity();
if (c instanceof Processing) {
((Processing) c).finishCalculation();
}
});
}
this.state = CraftingTaskState.CALCULATED;
return null;
}
@@ -929,9 +936,9 @@ public class CraftingTask implements ICraftingTask {
++ticks;
extractInitial();
if (this.crafts.isEmpty()) {
this.state = CraftingTaskState.DONE;
List<Runnable> toPerform = new ArrayList<>();
for (ItemStack stack : internalStorage.getStacks()) {
@@ -951,6 +958,10 @@ public class CraftingTask implements ICraftingTask {
return internalStorage.getStacks().isEmpty() && internalFluidStorage.getStacks().isEmpty();
} else {
this.state = CraftingTaskState.RUNNING;
extractInitial();
for (Craft craft : crafts.values()) {
if (craft instanceof Crafting) {
updateCrafting((Crafting) craft);
@@ -1322,4 +1333,9 @@ public class CraftingTask implements ICraftingTask {
public UUID getId() {
return id;
}
@Override
public CraftingTaskState getState() {
return state;
}
}

View File

@@ -920,19 +920,15 @@ public class ServerConfig {
public class Autocrafting {
private final ForgeConfigSpec.IntValue calculationTimeoutMs;
private final ForgeConfigSpec.BooleanValue useExperimental;
public Autocrafting() {
builder.push("autocrafting");
useExperimental = builder.comment("Use the experimental autocrafting engine").define("useExperimental", true);
calculationTimeoutMs = builder.comment("The autocrafting calculation timeout in milliseconds, crafting tasks taking longer than this to calculate are cancelled to avoid server strain").defineInRange("calculationTimeoutMs", 5000, 5000, Integer.MAX_VALUE);
builder.pop();
}
public boolean useExperimentalAutocrafting(){
return useExperimental.get();
}
public int getCalculationTimeoutMs() {
return calculationTimeoutMs.get();
}

View File

@@ -13,7 +13,7 @@ import com.refinedmods.refinedstorage.apiimpl.autocrafting.craftingmonitor.ItemC
import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.ErrorCraftingPreviewElement;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.FluidCraftingPreviewElement;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.preview.ItemCraftingPreviewElement;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v5.CraftingTaskFactory;
import com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTaskFactory;
import com.refinedmods.refinedstorage.apiimpl.network.NetworkListener;
import com.refinedmods.refinedstorage.apiimpl.network.NetworkNodeListener;
import com.refinedmods.refinedstorage.apiimpl.network.grid.factory.*;
@@ -131,7 +131,6 @@ public class CommonSetup {
API.instance().getCraftingMonitorElementRegistry().add(ErrorCraftingMonitorElement.ID, ErrorCraftingMonitorElement::read);
API.instance().getCraftingTaskRegistry().add(CraftingTaskFactory.ID, new CraftingTaskFactory());
API.instance().getCraftingTaskRegistry().add(com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTaskFactory.ID, new com.refinedmods.refinedstorage.apiimpl.autocrafting.task.v6.CraftingTaskFactory());
LootFunctionManager.registerFunction(new StorageBlockLootFunctionSerializer());
LootFunctionManager.registerFunction(new PortableGridBlockLootFunctionSerializer());