Skip to content

Commit 741ffdd

Browse files
committed
Added a cancel function to the craft job, changed the event to only return the id, if it errored out and a status message. Moved to an enum for any debug constants
1 parent b1bf168 commit 741ffdd

9 files changed

Lines changed: 136 additions & 58 deletions

File tree

src/main/java/de/srendi/advancedperipherals/common/addons/ae2/AECraftJob.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import dan200.computercraft.api.peripheral.IComputerAccess;
2020
import de.srendi.advancedperipherals.AdvancedPeripherals;
2121
import de.srendi.advancedperipherals.common.blocks.blockentities.MeBridgeEntity;
22+
import de.srendi.advancedperipherals.common.util.StatusConstants;
2223
import de.srendi.advancedperipherals.common.util.inventory.BasicCraftJob;
2324
import net.minecraft.world.level.Level;
2425
import org.jetbrains.annotations.Nullable;
@@ -99,15 +100,15 @@ public long getElapsedTime() {
99100
if (getJobStatus() == null) {
100101
return -1;
101102
}
102-
return getJobStatus() .elapsedTimeNanos();
103+
return getJobStatus().elapsedTimeNanos();
103104
}
104105

105106
@Override
106107
public long getTotalItems() {
107108
if (getJobStatus() == null) {
108109
return -1;
109110
}
110-
return getJobStatus() .totalItems();
111+
return getJobStatus().totalItems();
111112
}
112113

113114
@Override
@@ -158,6 +159,17 @@ public Object getFinalOutput() {
158159
return AppEngApi.parseGenericStack(currentJob.finalOutput());
159160
}
160161

162+
@Override
163+
public boolean cancel() {
164+
if (targetCpu instanceof CraftingCPUCluster cluster) {
165+
if (cluster.isBusy()) {
166+
cluster.cancel();
167+
return true;
168+
}
169+
}
170+
return false;
171+
}
172+
161173
@LuaFunction
162174
public long getUsedBytes() {
163175
if (currentJob == null) {
@@ -189,13 +201,13 @@ public void startCalculation() {
189201
ICraftingService craftingService = grid.getService(ICraftingService.class);
190202

191203
if (!craftingService.isCraftable(toCraft)) {
192-
fireEvent(false, false, false, false, false, NOT_CRAFTABLE);
204+
fireEvent(true, StatusConstants.NOT_CRAFTABLE);
193205
calculationNotSuccessful = true;
194206
return;
195207
}
196208

197209
futureJob = craftingService.beginCraftingCalculation(world, this.simulationRequester, toCraft, amount, CalculationStrategy.REPORT_MISSING_ITEMS);
198-
fireEvent(true, false, false, false, false, CALCULATION_STARTED);
210+
fireEvent(false, StatusConstants.CALCULATION_STARTED);
199211
}
200212

201213
public void maybeCraft() {
@@ -209,20 +221,20 @@ public void maybeCraft() {
209221
} catch (ExecutionException | InterruptedException ex) {
210222
AdvancedPeripherals.debug("Tried to get job, but job calculation is not done. Should be done.", org.apache.logging.log4j.Level.ERROR);
211223
ex.printStackTrace();
212-
fireEvent(true, false, false, false, true, UNKNOWN_ERROR);
224+
fireEvent(true, StatusConstants.UNKNOWN_ERROR);
213225
return;
214226
}
215227

216228
if (job == null) {
217229
AdvancedPeripherals.debug("Job is null, should not be null.", org.apache.logging.log4j.Level.ERROR);
218-
fireEvent(true, false, false, false, true, UNKNOWN_ERROR);
230+
fireEvent(true, StatusConstants.UNKNOWN_ERROR);
219231
return;
220232
}
221233
this.currentJob = job;
222234

223235
KeyCounter missing = job.missingItems();
224236
if (!missing.isEmpty()) {
225-
fireEvent(true, false, false, false, true, MISSING_ITEMS);
237+
fireEvent(true, StatusConstants.MISSING_ITEMS);
226238
calculationNotSuccessful = true;
227239
return;
228240
}
@@ -233,7 +245,7 @@ public void maybeCraft() {
233245
ICraftingSubmitResult submitResult = craftingService.submitJob(job, requester, targetCpu, false, this.source);
234246
if (!submitResult.successful()) {
235247
calculationNotSuccessful = true;
236-
fireEvent(true, false, false, false, true, submitResult.errorCode().toString());
248+
fireEvent(true, submitResult.errorCode().toString());
237249
return;
238250
}
239251

@@ -246,18 +258,18 @@ public void maybeCraft() {
246258
public void jobStateChanged() {
247259
ICraftingLink jobLink = this.jobLink;
248260
if (jobLink == null) {
249-
fireEvent(true, true, true, false, true, UNKNOWN_ERROR);
261+
fireEvent(true, StatusConstants.UNKNOWN_ERROR);
250262
return;
251263
}
252264

253265
if (jobLink.isCanceled() && !isJobCanceled) {
254-
fireEvent(true, true, false, true, false, JOB_CANCELED);
266+
fireEvent(false, StatusConstants.JOB_CANCELED);
255267
setJobCanceled();
256268
return;
257269
}
258270

259271
if (jobLink.isDone() && !isJobDone) {
260-
fireEvent(true, true, true, false, false, JOB_DONE);
272+
fireEvent(false, StatusConstants.JOB_DONE);
261273
setJobDone();
262274
}
263275
}

src/main/java/de/srendi/advancedperipherals/common/addons/computercraft/peripheral/MeBridgePeripheral.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import de.srendi.advancedperipherals.common.blocks.blockentities.MeBridgeEntity;
2525
import de.srendi.advancedperipherals.common.configuration.APConfig;
2626
import de.srendi.advancedperipherals.common.util.Pair;
27+
import de.srendi.advancedperipherals.common.util.StatusConstants;
2728
import de.srendi.advancedperipherals.common.util.inventory.FluidFilter;
2829
import de.srendi.advancedperipherals.common.util.inventory.FluidUtil;
2930
import de.srendi.advancedperipherals.common.util.inventory.GenericFilter;
@@ -154,7 +155,7 @@ protected MethodResult importToME(@NotNull IArguments arguments, @Nullable IFlui
154155
}
155156

156157
private MethodResult notConnected() {
157-
return MethodResult.of(null, "NOT_CONNECTED");
158+
return MethodResult.of(null, StatusConstants.NOT_CONNECTED.asString());
158159
}
159160

160161
private boolean isAvailable() {
@@ -573,20 +574,20 @@ public final MethodResult craftItem(IComputerAccess computer, IArguments argumen
573574

574575
ItemFilter parsedFilter = filter.getLeft();
575576
if (parsedFilter.isEmpty())
576-
return MethodResult.of(null, "EMPTY_FILTER");
577+
return MethodResult.of(null, StatusConstants.EMPTY_FILTER.asString());
577578

578579
String cpuName = arguments.optString(1, "");
579580

580581
return new CraftJobCallback(computer, () -> {
581582
ICraftingCPU target = AppEngApi.getCraftingCPU(node, cpuName);
582583
if (!cpuName.isEmpty() && target == null) {
583-
return MethodResult.of(null, "CPU " + cpuName + " does not exists");
584+
return MethodResult.of(null, StatusConstants.CPU_DOES_NOT_EXIST.withInfo(cpuName).asString());
584585
}
585586

586587
ICraftingService craftingGrid = node.getGrid().getService(ICraftingService.class);
587588
Pair<Long, AEItemKey> stack = AppEngApi.findAEStackFromFilter(AppEngApi.getMonitor(bridge.getGridNode()), craftingGrid, parsedFilter);
588589
if (stack.getRight() == null && stack.getLeft() == 0) {
589-
return MethodResult.of(null, "NOT_CRAFTABLE");
590+
return MethodResult.of(null, StatusConstants.NOT_CRAFTABLE.asString());
590591
}
591592

592593
AECraftJob job = new AECraftJob(owner.getLevel(), computer, node, stack.getRight(), parsedFilter.getCount(), bridge, target);
@@ -607,18 +608,18 @@ public final MethodResult craftFluid(IComputerAccess computer, IArguments argume
607608

608609
FluidFilter parsedFilter = filter.getLeft();
609610
if (parsedFilter.isEmpty())
610-
return MethodResult.of(null, "EMPTY_FILTER");
611+
return MethodResult.of(null, StatusConstants.EMPTY_FILTER.asString());
611612

612613
String cpuName = arguments.optString(1, "");
613614
return new CraftJobCallback(computer, () -> {
614615
ICraftingCPU target = AppEngApi.getCraftingCPU(node, cpuName);
615616
if (!cpuName.isEmpty() && target == null)
616-
return MethodResult.of(null, "CPU " + cpuName + " does not exists");
617+
return MethodResult.of(null, StatusConstants.CPU_DOES_NOT_EXIST.withInfo(cpuName).asString());
617618

618619
ICraftingService craftingGrid = node.getGrid().getService(ICraftingService.class);
619620
Pair<Long, AEFluidKey> stack = AppEngApi.findAEFluidFromFilter(AppEngApi.getMonitor(bridge.getGridNode()), craftingGrid, parsedFilter);
620621
if (stack.getRight() == null && stack.getLeft() == 0)
621-
return MethodResult.of(false, "NOT_CRAFTABLE");
622+
return MethodResult.of(false, StatusConstants.NOT_CRAFTABLE.asString());
622623

623624
AECraftJob job = new AECraftJob(owner.getLevel(), computer, node, stack.getRight(), parsedFilter.getCount(), bridge, target);
624625
bridge.addJob(job);

src/main/java/de/srendi/advancedperipherals/common/addons/refinedstorage/RSCraftJob.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import com.refinedmods.refinedstorage.api.autocrafting.task.ICraftingTask;
77
import dan200.computercraft.api.peripheral.IComputerAccess;
88
import de.srendi.advancedperipherals.common.util.LuaConverter;
9+
import de.srendi.advancedperipherals.common.util.StatusConstants;
910
import de.srendi.advancedperipherals.common.util.inventory.BasicCraftJob;
1011
import net.minecraft.world.item.ItemStack;
1112
import net.minecraft.world.level.Level;
@@ -89,6 +90,15 @@ public Object getFinalOutput() {
8990
return null;
9091
}
9192

93+
@Override
94+
public boolean cancel() {
95+
if (isJobDone() || isJobCanceled()) {
96+
return false;
97+
}
98+
craftingManager.cancel(craftingTask.getId());
99+
return true;
100+
}
101+
92102
public ICraftingTask getCraftingTask() {
93103
return craftingTask;
94104
}
@@ -102,14 +112,14 @@ protected void maybeCraft() {
102112
CalculationResultType type = calculationResult.getType();
103113

104114
if (type == CalculationResultType.MISSING) {
105-
fireEvent(true, false, false, false, true, MISSING_ITEMS);
115+
fireEvent(true, StatusConstants.MISSING_ITEMS);
106116
calculationNotSuccessful = true;
107117
return;
108118
}
109119

110120
if (!calculationResult.isOk()) {
111121
calculationNotSuccessful = true;
112-
fireEvent(true, false, false, false, true, type.toString());
122+
fireEvent(true, type.toString());
113123
return;
114124
}
115125

@@ -134,7 +144,7 @@ private void maybeCalculateItem() {
134144
}
135145

136146
calculationResult = craftingManager.create(itemToCraft, (int) amount);
137-
fireEvent(true, false, false, false, false, CALCULATION_STARTED);
147+
fireEvent(false, StatusConstants.CALCULATION_STARTED);
138148
}
139149

140150
private void maybeCalculateFluid() {
@@ -143,29 +153,29 @@ private void maybeCalculateFluid() {
143153
}
144154

145155
if (craftingManager.getPattern(fluidToCraft) == null) {
146-
fireEvent(false, false, false, false, false, NOT_CRAFTABLE);
156+
fireEvent(true, StatusConstants.NOT_CRAFTABLE);
147157
return;
148158
}
149159

150160
calculationResult = craftingManager.create(fluidToCraft, (int) amount);
151-
fireEvent(true, false, false, false, false, CALCULATION_STARTED);
161+
fireEvent(false, StatusConstants.CALCULATION_STARTED);
152162
}
153163

154164
@Override
155165
public void jobStateChanged() {
156166
if (this.craftingTask == null) {
157-
fireEvent(true, true, true, false, true, UNKNOWN_ERROR);
167+
fireEvent(true, StatusConstants.UNKNOWN_ERROR);
158168
return;
159169
}
160170

161171
if (isJobCanceled() && !isJobCanceled) {
162-
fireEvent(true, true, false, true, false, JOB_CANCELED);
172+
fireEvent(false, StatusConstants.JOB_CANCELED);
163173
setJobCanceled();
164174
return;
165175
}
166176

167177
if (isJobDone() && !isJobDone) {
168-
fireEvent(true, true, true, false, false, JOB_DONE);
178+
fireEvent(true, StatusConstants.JOB_DONE);
169179
setJobDone();
170180
}
171181
}

src/main/java/de/srendi/advancedperipherals/common/blocks/blockentities/RsBridgeEntity.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class RsBridgeEntity extends NetworkNodeBlockEntity<RefinedStorageNode> i
4343
private boolean addedListener = false;
4444

4545
public RsBridgeEntity(BlockPos pos, BlockState state) {
46-
super(APBlockEntityTypes.RS_BRIDGE.get(), pos, state, SPEC);
46+
super(APBlockEntityTypes.RS_BRIDGE.get(), pos, state, SPEC, RefinedStorageNode.class);
4747
peripheralSettings = new CompoundTag();
4848
}
4949

@@ -122,6 +122,7 @@ public void onAttached() {
122122

123123
@Override
124124
public void onChanged() {
125+
// Not as perfect as we currently do it for our AE jobs. This is called for every job even if they aren't created from the bridge
125126
jobs.stream().filter(BasicCraftJob::isCraftingStarted).forEach(BasicCraftJob::jobStateChanged);
126127
}
127128
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package de.srendi.advancedperipherals.common.util;
2+
3+
/**
4+
* A collection of constants used as return types for several peripherals
5+
*/
6+
public enum StatusConstants {
7+
8+
// Crafting Jobs
9+
CALCULATION_STARTED,
10+
CRAFTING_STARTED,
11+
JOB_CANCELED,
12+
JOB_DONE,
13+
NOT_CRAFTABLE,
14+
MISSING_ITEMS,
15+
CPU_DOES_NOT_EXIST,
16+
// Filters
17+
EMPTY_FILTER,
18+
FLUID_NOT_FOUND,
19+
ITEM_NOT_FOUND,
20+
NO_VALID_FLUID,
21+
NO_VALID_ITEM,
22+
NO_VALID_FROMSLOT,
23+
NO_VALID_TOSLOT,
24+
NO_VALID_NBT_HASH,
25+
NO_VALID_NBT,
26+
NO_VALID_FINGERPRINT,
27+
NO_VALID_COUNT,
28+
NO_VALID_FILTER_TYPE,
29+
// Misc
30+
NOT_CONNECTED,
31+
UNKNOWN_ERROR;
32+
33+
private String info;
34+
35+
public StatusConstants withInfo(String info) {
36+
this.info = info;
37+
return this;
38+
}
39+
40+
public String asString() {
41+
return this + "_" + this.info;
42+
}
43+
44+
}

0 commit comments

Comments
 (0)