Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Autocrafting monitor support for tasks #780

Merged
merged 13 commits into from
Jan 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
- Autocrafting now handles multiple patterns with the same output correctly by trying to use the pattern with the
highest priority first. If there are missing resources, lower priority patterns are checked.
- The Autocrafter now faces the block you're clicking when placing it, like the other cable blocks (like the Exporter or Importer).
- You can no longer cancel autocrafting tasks if there is not enough space in storage to return the intermediate task storage.
- The Autocrafting Monitor now shows the machine in which a resource is processing.

### Fixed

Expand Down
6 changes: 4 additions & 2 deletions config/checkstyle/checkstyle.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<property name="ignorePattern" value="^package.*|^import.*|a href|href|http://|https://|ftp://"/>
</module>
<module name="FileLength">
<property name="max" value="800"/>
<property name="max" value="1600"/>
</module>
<module name="NewlineAtEndOfFile"/>
<module name="JavadocPackage"/>
Expand Down Expand Up @@ -106,7 +106,9 @@
<module name="IllegalIdentifierName"/>
<module name="AnonInnerLength"/>
<module name="MethodCount"/>
<module name="MethodLength"/>
<module name="MethodLength">
<property name="max" value="200"/>
</module>
<module name="OuterTypeNumber"/>
<module name="RecordComponentNumber">
<property name="max" value="20"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.refinedmods.refinedstorage.api.autocrafting.preview;

import com.refinedmods.refinedstorage.api.autocrafting.task.TaskId;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.storage.Actor;

Expand All @@ -14,5 +15,5 @@ public interface PreviewProvider {

CompletableFuture<Long> getMaxAmount(ResourceKey resource);

CompletableFuture<Boolean> startTask(ResourceKey resource, long amount, Actor actor, boolean notify);
CompletableFuture<Optional<TaskId>> startTask(ResourceKey resource, long amount, Actor actor, boolean notify);
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.refinedmods.refinedstorage.api.autocrafting.status;

import com.refinedmods.refinedstorage.api.autocrafting.task.ExternalPatternInputSinkKey;
import com.refinedmods.refinedstorage.api.autocrafting.task.TaskId;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;

import java.util.List;
import javax.annotation.Nullable;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.10")
public record TaskStatus(TaskInfo info, float percentageCompleted, List<Item> items) {
public record TaskStatus(TaskInfo info, double percentageCompleted, List<Item> items) {
public record TaskInfo(TaskId id, ResourceKey resource, long amount, long startTime) {
}

public record Item(
ItemType type,
ResourceKey resource,
ItemType type,
@Nullable ExternalPatternInputSinkKey sinkKey,
long stored,
long missing,
long processing,
long scheduled,
long crafting
Expand All @@ -25,8 +27,8 @@ public record Item(

public enum ItemType {
NORMAL,
MACHINE_DOES_NOT_ACCEPT_RESOURCE,
NO_MACHINE_FOUND,
AUTOCRAFTER_IS_LOCKED
REJECTED,
NONE_FOUND,
LOCKED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.refinedmods.refinedstorage.api.autocrafting.status;

import com.refinedmods.refinedstorage.api.autocrafting.task.ExternalPatternInputSinkKey;
import com.refinedmods.refinedstorage.api.autocrafting.task.TaskId;
import com.refinedmods.refinedstorage.api.core.CoreValidations;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;

import java.util.LinkedHashMap;
import java.util.Map;
import javax.annotation.Nullable;

public class TaskStatusBuilder {
private final TaskStatus.TaskInfo info;
private final Map<ResourceKey, MutableItem> items = new LinkedHashMap<>();

public TaskStatusBuilder(final TaskId id, final ResourceKey resource, final long amount, final long startTime) {
this.info = new TaskStatus.TaskInfo(id, resource, amount, startTime);
}

public TaskStatusBuilder stored(final ResourceKey resource, final long stored) {
CoreValidations.validateLargerThanZero(stored, "Stored");
get(resource).stored += stored;
return this;
}

public TaskStatusBuilder processing(final ResourceKey resource,
final long processing,
@Nullable final ExternalPatternInputSinkKey sinkKey) {
CoreValidations.validateLargerThanZero(processing, "Processing");
get(resource).processing += processing;
get(resource).sinkKey = sinkKey;
return this;
}

public TaskStatusBuilder scheduled(final ResourceKey resource, final long scheduled) {
CoreValidations.validateLargerThanZero(scheduled, "Crafting");
get(resource).scheduled += scheduled;
return this;
}

public TaskStatusBuilder crafting(final ResourceKey resource, final long crafting) {
CoreValidations.validateLargerThanZero(crafting, "Crafting");
get(resource).crafting += crafting;
return this;
}

public TaskStatusBuilder rejected(final ResourceKey resource) {
get(resource).type = TaskStatus.ItemType.REJECTED;
return this;
}

public TaskStatusBuilder noneFound(final ResourceKey resource) {
get(resource).type = TaskStatus.ItemType.NONE_FOUND;
return this;
}

public TaskStatusBuilder locked(final ResourceKey resource) {
get(resource).type = TaskStatus.ItemType.LOCKED;
return this;
}

private MutableItem get(final ResourceKey resource) {
return items.computeIfAbsent(resource, key -> new MutableItem(TaskStatus.ItemType.NORMAL));
}

public TaskStatus build(final double percentageCompleted) {
return new TaskStatus(info, percentageCompleted, items.entrySet().stream().map(entry -> new TaskStatus.Item(
entry.getKey(),
entry.getValue().type,
entry.getValue().sinkKey,
entry.getValue().stored,
entry.getValue().processing,
entry.getValue().scheduled,
entry.getValue().crafting
)).toList());
}

private static class MutableItem {
private TaskStatus.ItemType type;
private long stored;
private long processing;
@Nullable
private ExternalPatternInputSinkKey sinkKey;
private long scheduled;
private long crafting;

private MutableItem(final TaskStatus.ItemType type) {
this.type = type;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.refinedmods.refinedstorage.api.autocrafting.task;

import com.refinedmods.refinedstorage.api.autocrafting.Pattern;
import com.refinedmods.refinedstorage.api.autocrafting.status.TaskStatusBuilder;
import com.refinedmods.refinedstorage.api.core.Action;
import com.refinedmods.refinedstorage.api.resource.ResourceKey;
import com.refinedmods.refinedstorage.api.resource.list.MutableResourceList;
Expand Down Expand Up @@ -32,12 +33,18 @@ protected AbstractTaskPattern(final Pattern pattern, final TaskPlan.PatternPlan
}
}

abstract boolean step(MutableResourceList internalStorage,
RootStorage rootStorage,
ExternalPatternInputSink externalPatternInputSink);
abstract PatternStepResult step(MutableResourceList internalStorage,
RootStorage rootStorage,
ExternalPatternInputSink externalPatternInputSink);

abstract RootStorageListener.InterceptResult interceptInsertion(ResourceKey resource, long amount);

abstract void appendStatus(TaskStatusBuilder builder);

abstract long getWeight();

abstract double getPercentageCompleted();

protected final boolean extractAll(final ResourceList inputs,
final MutableResourceList internalStorage,
final Action action) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
import com.refinedmods.refinedstorage.api.resource.ResourceAmount;

import java.util.Collection;
import javax.annotation.Nullable;

import org.apiguardian.api.API;

@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.12")
@FunctionalInterface
public interface ExternalPatternInputSink {
boolean accept(Pattern pattern, Collection<ResourceAmount> resources, Action action);
Result accept(Pattern pattern, Collection<ResourceAmount> resources, Action action);

@Nullable
ExternalPatternInputSinkKey getKey(Pattern pattern);

enum Result {
ACCEPTED,
REJECTED,
SKIPPED,
LOCKED
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.refinedmods.refinedstorage.api.autocrafting.task;

import org.apiguardian.api.API;

@FunctionalInterface
@API(status = API.Status.STABLE, since = "2.0.0-milestone.4.12")
public interface ExternalPatternInputSinkKey {
String getName();
}
Loading
Loading