66import com .refinedmods .refinedstorage .api .autocrafting .status .TaskStatus ;
77import com .refinedmods .refinedstorage .api .autocrafting .task .TaskId ;
88import com .refinedmods .refinedstorage .api .network .autocrafting .AutocraftingNetworkComponent ;
9+ import com .refinedmods .refinedstorage .api .network .impl .autocrafting .TimeoutableCancellationToken ;
910import com .refinedmods .refinedstorage .api .resource .ResourceKey ;
1011import com .refinedmods .refinedstorage .api .storage .Actor ;
1112import dan200 .computercraft .api .peripheral .IComputerAccess ;
@@ -28,7 +29,7 @@ public class RSCraftJob extends BasicCraftJob {
2829 private TaskStatus craftingTask ;
2930 private Preview preview ;
3031 private Future <Optional <Preview >> futureCalculationResult ;
31- private Future < Optional <TaskId >> futureTask ;
32+ private Optional <TaskId > futureTask = Optional . empty () ;
3233
3334 public RSCraftJob (IComputerAccess computer , Level world , long amount , ResourceKey toCraft , AutocraftingNetworkComponent calculationResult ) {
3435 super (computer , "rs" , world , amount );
@@ -139,26 +140,11 @@ public void tick() {
139140 }
140141 }
141142 // The following is to get the TaskStatus from the issued task
142- if (futureTask == null || ! futureTask . isDone () || craftingTask != null ) {
143+ if (futureTask . isEmpty () || craftingTask != null ) {
143144 return ;
144145 }
145- Optional <TaskId > optionalId ;
146146
147- try {
148- optionalId = futureTask .get ();
149- } catch (InterruptedException | ExecutionException ex ) {
150- AdvancedPeripherals .debug ("Tried to get the task but the task was not done. Should be done" , ex );
151- fireEvent (true , StatusConstants .UNKNOWN_ERROR );
152- return ;
153- }
154-
155- if (optionalId .isEmpty ()) {
156- // This indicates that the second calculation was not successful. Well I guess. There is no java doc and I currently
157- // don't get an answer from the maintainer. So maybe we want to fire the crafting event
158- return ;
159- }
160-
161- TaskId id = optionalId .get ();
147+ TaskId id = futureTask .get ();
162148 for (TaskStatus status : autocraftingComponent .getStatuses ()) {
163149 if (status .info ().id ().equals (id )) {
164150 this .craftingTask = status ;
@@ -171,7 +157,7 @@ public void tick() {
171157
172158 @ Override
173159 protected void maybeCraft () {
174- if (startedCrafting || futureTask != null || futureCalculationResult == null || !futureCalculationResult .isDone ()) {
160+ if (startedCrafting || futureTask . isPresent () || futureCalculationResult == null || !futureCalculationResult .isDone ()) {
175161 return ;
176162 }
177163
@@ -209,9 +195,9 @@ protected void maybeCraft() {
209195 return ;
210196 }
211197
212- // How RS2 handles crafting is a bit cursed. We first create a preview which calculates the recipes, and then we check if the preview was successful
198+ // We first create a preview which calculates the recipes, and then we check if the preview was successful
213199 // If it was, we again start a task which again calculates the recipes, and then we hope nothing changed from the first calculation
214- futureTask = autocraftingComponent .startTask (toCraft , amount , Actor .EMPTY , false , CancellationToken . NONE );
200+ futureTask = autocraftingComponent .startTask (toCraft , amount , Actor .EMPTY , false , new TimeoutableCancellationToken () );
215201 }
216202
217203 @ Override
@@ -226,7 +212,7 @@ protected void startCalculation() {
226212 return ;
227213 }
228214
229- futureCalculationResult = autocraftingComponent .getPreview (toCraft , amount , CancellationToken . NONE );
215+ futureCalculationResult = autocraftingComponent .getPreview (toCraft , amount , new TimeoutableCancellationToken () );
230216 fireEvent (false , StatusConstants .CALCULATION_STARTED );
231217 }
232218
0 commit comments