8
8
import it .unimi .dsi .fastutil .objects .Object2IntMap ;
9
9
import it .unimi .dsi .fastutil .objects .Object2IntOpenHashMap ;
10
10
import net .earthcomputer .clientcommands .Configs ;
11
+ import net .earthcomputer .clientcommands .command .ClientCommandHelper ;
11
12
import net .earthcomputer .clientcommands .util .MultiVersionCompat ;
12
13
import net .earthcomputer .clientcommands .task .ItemThrowTask ;
13
14
import net .earthcomputer .clientcommands .task .LongTask ;
50
51
import org .jetbrains .annotations .Nullable ;
51
52
import org .slf4j .Logger ;
52
53
54
+ import java .lang .ref .WeakReference ;
53
55
import java .util .ArrayList ;
54
56
import java .util .Collections ;
55
57
import java .util .Comparator ;
@@ -114,6 +116,13 @@ public class EnchantmentCracker {
114
116
public static final Logger LOGGER = LogUtils .getLogger ();
115
117
private static final int PROGRESS_BAR_WIDTH = 50 ;
116
118
119
+ private static WeakReference <LongTask > currentEnchantingTask = null ;
120
+ private static boolean isCurrentlyThrowingItems = false ;
121
+
122
+ public static void registerEvents () {
123
+ PlayerRandCracker .RNG_CALLED_EVENT .register (EnchantmentCracker ::onRNGCallEvent );
124
+ }
125
+
117
126
// RENDERING
118
127
/*
119
128
* This section is in charge of rendering the overlay on the enchantment GUI
@@ -309,28 +318,43 @@ private static void addPlayerRNGInfo(int enchantmentSeed) {
309
318
}
310
319
}
311
320
312
- public static void onEnchantedItem () {
321
+ private static void onRNGCallEvent (PlayerRandCracker .RNGCallEvent event ) {
322
+ if (event .getType () != PlayerRandCracker .RNGCallType .ENCHANTING ) {
323
+ LongTask enchantingTask = currentEnchantingTask == null ? null : currentEnchantingTask .get ();
324
+ if (enchantingTask != null ) {
325
+ // if currently throwing items, unexpected RNG calls are handled by the item throw task
326
+ if (!isCurrentlyThrowingItems ) {
327
+ ClientCommandHelper .sendError (Component .translatable ("commands.cenchant.unexpectedCall" , event .getType ().getResetMessage ()));
328
+ enchantingTask ._break ();
329
+ }
330
+ }
331
+ return ;
332
+ }
333
+
313
334
if (Configs .playerCrackState == PlayerRandCracker .CrackState .UNCRACKED && !isEnchantingPredictionEnabled ()) {
314
335
return ;
315
336
}
316
337
if (Configs .playerCrackState .knowsSeed ()) {
338
+ long prevSeed = PlayerRandCracker .getSeed ();
339
+ int xpSeed = PlayerRandCracker .nextInt ();
340
+ PlayerRandCracker .setSeed (prevSeed );
317
341
possibleXPSeeds .clear ();
318
- possibleXPSeeds .add (PlayerRandCracker . nextInt () );
342
+ possibleXPSeeds .add (xpSeed );
319
343
Configs .playerCrackState = PlayerRandCracker .CrackState .CRACKED ;
320
344
Configs .enchCrackState = CrackState .CRACKED ;
345
+ event .setMaintained ();
321
346
} else if (Configs .playerCrackState == PlayerRandCracker .CrackState .HALF_CRACKED ) {
322
347
possibleXPSeeds .clear ();
323
348
Configs .playerCrackState = PlayerRandCracker .CrackState .ENCH_CRACKING_2 ;
324
349
Configs .enchCrackState = CrackState .UNCRACKED ;
350
+ event .setMaintainedEvenIfSeedUnknown ();
325
351
} else if ((Configs .playerCrackState == PlayerRandCracker .CrackState .UNCRACKED
326
352
|| Configs .playerCrackState == PlayerRandCracker .CrackState .ENCH_CRACKING_1
327
353
|| Configs .playerCrackState == PlayerRandCracker .CrackState .ENCH_CRACKING_2 )) {
328
354
possibleXPSeeds .clear ();
329
355
Configs .playerCrackState = PlayerRandCracker .CrackState .ENCH_CRACKING_1 ;
330
356
Configs .enchCrackState = CrackState .UNCRACKED ;
331
- } else {
332
- PlayerRandCracker .onUnexpectedItemEnchant ();
333
- Configs .enchCrackState = CrackState .UNCRACKED ;
357
+ event .setMaintainedEvenIfSeedUnknown ();
334
358
}
335
359
doneEnchantment = true ;
336
360
}
@@ -405,9 +429,16 @@ public static String manipulateEnchantments(Item item, Predicate<List<Enchantmen
405
429
LongTaskList taskList = new LongTaskList () {
406
430
@ Override
407
431
public Set <Object > getMutexKeys () {
408
- return simulate ? Set .of () : Set .of (ItemThrowTask .class );
432
+ return simulate ? Set .of (EnchantmentCracker .class ) : Set .of (EnchantmentCracker .class , ItemThrowTask .class );
433
+ }
434
+
435
+ @ Override
436
+ public void onCompleted () {
437
+ super .onCompleted ();
438
+ currentEnchantingTask = null ;
409
439
}
410
440
};
441
+ currentEnchantingTask = new WeakReference <>(taskList );
411
442
412
443
taskList .addTask (new SimpleTask () {
413
444
private int index = 0 ;
@@ -454,28 +485,36 @@ public void onCompleted() {
454
485
}
455
486
456
487
if (!simulate && finalResult != null ) {
488
+ doneEnchantment = false ;
457
489
int timesNeeded = finalResult .itemThrows ();
458
490
if (timesNeeded != ManipulateResult .NO_DUMMY ) {
459
491
if (timesNeeded != 0 ) {
460
492
player .snapTo (player .getX (), player .getY (), player .getZ (), player .getYRot (), 90 );
461
493
// sync rotation to server before we throw any items
462
494
player .connection .send (new ServerboundMovePlayerPacket .Rot (player .getYRot (), 90 , player .onGround (), player .horizontalCollision ));
463
- Configs .playerCrackState = PlayerRandCracker .CrackState .MANIPULATING_ENCHANTMENTS ;
464
495
}
465
496
if (timesNeeded > 0 ) {
497
+ isCurrentlyThrowingItems = true ;
466
498
taskList .addTask (new ItemThrowTask (timesNeeded , ItemThrowTask .FLAG_WAIT_FOR_ITEMS ) {
467
499
@ Override
468
- public boolean condition () {
469
- if (Configs .playerCrackState != PlayerRandCracker .CrackState .MANIPULATING_ENCHANTMENTS ) {
500
+ public void onCompleted () {
501
+ super .onCompleted ();
502
+ Minecraft .getInstance ().player .playNotifySound (SoundEvents .NOTE_BLOCK_PLING .value (), SoundSource .PLAYERS , 1.0f , 2.0f );
503
+ isCurrentlyThrowingItems = false ;
504
+ }
505
+
506
+ @ Override
507
+ protected void onFailedToThrowItem (PlayerRandCracker .ThrowItemsResult throwItemsResult ) {
508
+ super .onFailedToThrowItem (throwItemsResult );
509
+ if (throwItemsResult .getType () != PlayerRandCracker .ThrowItemsResult .Type .NOT_ENOUGH_ITEMS ) {
470
510
taskList ._break ();
471
- return false ;
472
511
}
473
- return super .condition ();
474
512
}
475
513
476
514
@ Override
477
- public void onCompleted () {
478
- Minecraft .getInstance ().player .playNotifySound (SoundEvents .NOTE_BLOCK_PLING .value (), SoundSource .PLAYERS , 1.0f , 2.0f );
515
+ protected void onUnexpectedRNGCall (PlayerRandCracker .RNGCallType callType ) {
516
+ ClientCommandHelper .sendError (Component .translatable ("commands.cenchant.unexpectedCall" , callType .getResetMessage ()));
517
+ taskList ._break ();
479
518
}
480
519
481
520
@ Override
@@ -498,14 +537,12 @@ protected void onItemThrown(int current, int total) {
498
537
taskList .addTask (new LongTask () {
499
538
@ Override
500
539
public void initialize () {
501
- Configs .playerCrackState = PlayerRandCracker .CrackState .WAITING_DUMMY_ENCHANT ;
502
540
Minecraft .getInstance ().gui .getChat ().addMessage (Component .translatable ("enchCrack.insn.dummy" ));
503
- doneEnchantment = false ;
504
541
}
505
542
506
543
@ Override
507
544
public boolean condition () {
508
- return Configs . playerCrackState == PlayerRandCracker . CrackState . WAITING_DUMMY_ENCHANT ;
545
+ return ! doneEnchantment ;
509
546
}
510
547
511
548
@ Override
@@ -524,11 +561,10 @@ public String toString() {
524
561
});
525
562
}
526
563
527
- doneEnchantment = true ;
528
564
taskList .addTask (new OneTickTask () {
529
565
@ Override
530
566
public void run () {
531
- if (Configs .enchCrackState == CrackState .CRACKED && doneEnchantment ) {
567
+ if (Configs .enchCrackState == CrackState .CRACKED ) {
532
568
ChatComponent chat = Minecraft .getInstance ().gui .getChat ();
533
569
chat .addMessage (Component .translatable ("enchCrack.insn.ready" ).withStyle (ChatFormatting .BOLD ));
534
570
chat .addMessage (Component .translatable ("enchCrack.insn.bookshelves" , finalResult .bookshelves ));
0 commit comments