99import it .unimi .dsi .fastutil .objects .Object2IntOpenHashMap ;
1010import net .earthcomputer .clientcommands .Configs ;
1111import net .earthcomputer .clientcommands .command .ClientCommandHelper ;
12+ import net .earthcomputer .clientcommands .event .ClientLevelEvents ;
1213import net .earthcomputer .clientcommands .util .MultiVersionCompat ;
1314import net .earthcomputer .clientcommands .task .ItemThrowTask ;
1415import net .earthcomputer .clientcommands .task .LongTask ;
2324import net .minecraft .client .gui .components .ChatComponent ;
2425import net .minecraft .client .multiplayer .ClientLevel ;
2526import net .minecraft .client .player .LocalPlayer ;
26- import net .minecraft .client .resources .language .I18n ;
2727import net .minecraft .core .BlockPos ;
2828import net .minecraft .core .Holder ;
2929import net .minecraft .core .IdMap ;
@@ -118,8 +118,14 @@ public class EnchantmentCracker {
118118
119119 private static WeakReference <LongTask > currentEnchantingTask = null ;
120120 private static boolean isCurrentlyThrowingItems = false ;
121+ private static int expectedNumBookshelves = -1 ;
121122
122123 public static void registerEvents () {
124+ ClientLevelEvents .UNLOAD_LEVEL .register (isDisconnect -> {
125+ if (isDisconnect ) {
126+ expectedNumBookshelves = -1 ;
127+ }
128+ });
123129 PlayerRandCracker .RNG_CALLED_EVENT .register (EnchantmentCracker ::onRNGCallEvent );
124130 }
125131
@@ -134,46 +140,60 @@ public static void drawEnchantmentGUIOverlay(GuiGraphics graphics) {
134140
135141 CrackState crackState = Configs .enchCrackState ;
136142
137- List <String > lines = new ArrayList <>();
143+ List <Component > lines = new ArrayList <>();
138144
139- lines .add (I18n . get ("enchCrack.state" , I18n . get ("enchCrack.state." + crackState .getSerializedName ())));
140- lines .add (I18n . get ("playerManip.state" , I18n . get ("playerManip.state." + Configs .playerCrackState .getSerializedName ())));
145+ lines .add (Component . translatable ("enchCrack.state" , Component . translatable ("enchCrack.state." + crackState .getSerializedName ())));
146+ lines .add (Component . translatable ("playerManip.state" , Component . translatable ("playerManip.state." + Configs .playerCrackState .getSerializedName ())));
141147
142- lines .add ("" );
148+ lines .add (Component . empty () );
143149
144150 if (crackState == CrackState .CRACKED ) {
145- lines .add (I18n . get ("enchCrack.xpSeed.one" , String .format ("%08X" , possibleXPSeeds .iterator ().next ())));
151+ lines .add (Component . translatable ("enchCrack.xpSeed.one" , String .format ("%08X" , possibleXPSeeds .iterator ().next ())));
146152 } else if (crackState == CrackState .CRACKING ) {
147- lines .add (I18n . get ("enchCrack.xpSeed.many" , possibleXPSeeds .size ()));
153+ lines .add (Component . translatable ("enchCrack.xpSeed.many" , possibleXPSeeds .size ()));
148154 }
149155
150- lines .add ("" );
156+ lines .add (Component . empty () );
151157
152158 if (enchantingTablePos != null ) {
153- lines .add (I18n .get ("enchCrack.bookshelfCount" , getEnchantPower (level , enchantingTablePos )));
154- lines .add ("" );
159+ int numBookshelves = getEnchantPower (level , enchantingTablePos );
160+ if (expectedNumBookshelves == -1 ) {
161+ lines .add (Component .translatable ("enchCrack.bookshelfCount" , numBookshelves ));
162+ } else {
163+ boolean bookshelfCountMatches = numBookshelves == expectedNumBookshelves || (numBookshelves > 15 && expectedNumBookshelves == 15 );
164+ lines .add (Component .translatable ("enchCrack.bookshelfCount.expected" , expectedNumBookshelves ));
165+ lines .add (Component .translatable (
166+ "enchCrack.bookshelfCount.actual" ,
167+ Component .literal (String .valueOf (numBookshelves ))
168+ .withStyle (bookshelfCountMatches ? ChatFormatting .GREEN : ChatFormatting .RED )
169+ ));
170+ if (!bookshelfCountMatches ) {
171+ lines .add (Component .translatable ("enchCrack.bookshelfCount.incorrect" ).withStyle (ChatFormatting .RED ));
172+ }
173+ }
174+ lines .add (Component .empty ());
155175 }
156176
157177 if (crackState == CrackState .CRACKED ) {
158- lines .add (I18n . get ("enchCrack.enchantments" ));
178+ lines .add (Component . translatable ("enchCrack.enchantments" ));
159179 } else {
160- lines .add (I18n . get ("enchCrack.clues" ));
180+ lines .add (Component . translatable ("enchCrack.clues" ));
161181 }
162182
163183 for (int slot = 0 ; slot < 3 ; slot ++) {
164- lines .add (I18n . get ("enchCrack.slot" , slot + 1 ));
184+ lines .add (Component . translatable ("enchCrack.slot" , slot + 1 ));
165185 List <EnchantmentInstance > enchs = getEnchantmentsInTable (slot );
166186 if (enchs != null ) {
167187 sortIntoTooltipOrder (level .registryAccess ().lookupOrThrow (Registries .ENCHANTMENT ), enchs );
168188 for (EnchantmentInstance ench : enchs ) {
169- lines .add (" " + Enchantment .getFullname (ench .enchantment (), ench .level ()). getString ( ));
189+ lines .add (Component . literal ( " " ). append ( Enchantment .getFullname (ench .enchantment (), ench .level ())));
170190 }
171191 }
172192 }
173193
174194 Font font = Minecraft .getInstance ().font ;
175195 int y = 0 ;
176- for (String line : lines ) {
196+ for (Component line : lines ) {
177197 graphics .drawString (font , line , 0 , y , 0xffffff , false );
178198 y += font .lineHeight ;
179199 }
@@ -357,6 +377,7 @@ private static void onRNGCallEvent(PlayerRandCracker.RNGCallEvent event) {
357377 event .setMaintainedEvenIfSeedUnknown ();
358378 }
359379 doneEnchantment = true ;
380+ expectedNumBookshelves = -1 ;
360381 }
361382
362383 // ENCHANTMENT MANIPULATION
@@ -569,6 +590,7 @@ public void run() {
569590 chat .addMessage (Component .translatable ("enchCrack.insn.ready" ).withStyle (ChatFormatting .BOLD ));
570591 chat .addMessage (Component .translatable ("enchCrack.insn.bookshelves" , finalResult .bookshelves ));
571592 chat .addMessage (Component .translatable ("enchCrack.insn.slot" , finalResult .slot + 1 ));
593+ expectedNumBookshelves = finalResult .bookshelves ;
572594 }
573595 }
574596
0 commit comments