1414import java .util .Map ;
1515import java .util .stream .Collectors ;
1616
17+ import org .bukkit .ChatColor ;
1718import org .bukkit .Material ;
1819import org .bukkit .World ;
1920import org .bukkit .event .inventory .ClickType ;
2627import world .bentobox .aoneblock .oneblocks .OneBlockPhase ;
2728import world .bentobox .aoneblock .oneblocks .Requirement ;
2829import world .bentobox .bank .Bank ;
30+ import world .bentobox .bentobox .BentoBox ;
2931import world .bentobox .bentobox .api .addons .Addon ;
3032import world .bentobox .bentobox .api .localization .TextVariables ;
3133import world .bentobox .bentobox .api .panels .PanelItem ;
3638import world .bentobox .bentobox .api .user .User ;
3739import world .bentobox .bentobox .database .objects .Island ;
3840import world .bentobox .bentobox .hooks .LangUtilsHook ;
41+ import world .bentobox .bentobox .util .Util ;
3942import world .bentobox .level .Level ;
4043
4144
@@ -55,6 +58,8 @@ public class PhasesPanel
5558 private static final String LEVEL = "[level]" ;
5659 private static final String PHASE2 = "[phase]" ;
5760 private static final String INDEXING = "indexing" ;
61+ private static final String BLOCKS = "[blocks]" ;
62+ public static final String REFERENCE = "aoneblock.gui.buttons.phase." ;
5863
5964 // ---------------------------------------------------------------------
6065 // Section: Constructor
@@ -349,7 +354,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
349354 [level]
350355 [permission]
351356 */
352- final String reference = "aoneblock.gui.buttons.phase." ;
357+
353358
354359 // Get settings for island.
355360 PanelItemBuilder builder = new PanelItemBuilder ();
@@ -375,7 +380,7 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
375380 }
376381 else
377382 {
378- builder .name (this .user .getTranslation (reference + "name" ,
383+ builder .name (this .user .getTranslation (REFERENCE + "name" ,
379384 PHASE2 , phase .getPhaseName ()));
380385 }
381386
@@ -391,16 +396,16 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
391396 phase .getRequirements ().forEach (requirement -> {
392397 switch (requirement .getType ())
393398 {
394- case ECO -> economyText .append (this .user .getTranslationOrNothing (reference + "economy" ,
399+ case ECO -> economyText .append (this .user .getTranslationOrNothing (REFERENCE + "economy" ,
395400 TextVariables .NUMBER , String .valueOf (requirement .getEco ())));
396401
397- case BANK -> bankText .append (this .user .getTranslationOrNothing (reference + "bank" ,
402+ case BANK -> bankText .append (this .user .getTranslationOrNothing (REFERENCE + "bank" ,
398403 TextVariables .NUMBER , String .valueOf (requirement .getBank ())));
399404
400- case LEVEL -> levelText .append (this .user .getTranslationOrNothing (reference + "level" ,
405+ case LEVEL -> levelText .append (this .user .getTranslationOrNothing (REFERENCE + "level" ,
401406 TextVariables .NUMBER , String .valueOf (requirement .getLevel ())));
402407
403- case PERMISSION -> permissionText .append (this .user .getTranslationOrNothing (reference + "permission" ,
408+ case PERMISSION -> permissionText .append (this .user .getTranslationOrNothing (REFERENCE + "permission" ,
404409 PERMISSION , requirement .getPermission ()));
405410 case COOLDOWN -> {
406411 // do nothing
@@ -410,6 +415,29 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
410415 }
411416 });
412417
418+ // Blocks Text
419+ String blocksText = user .getTranslation (REFERENCE + "blocks-prefix" ) + phase .getBlocks ().keySet ().stream ()
420+ .map (m -> getMaterialName (user , m ))
421+ .map (string -> user .getTranslation (REFERENCE + "blocks" , TextVariables .NAME , string ))
422+ .collect (Collectors .joining ());
423+ // Removing the last newline character or comma if it exists
424+ blocksText = blocksText .trim ();
425+ if (blocksText .endsWith ("\n " ) || blocksText .endsWith ("," )) {
426+ blocksText = blocksText .substring (0 , blocksText .length () - 1 );
427+ }
428+ // Insert newlines every x characters
429+ int wrapAt = 50 ; // Set default value
430+ try {
431+ // Attempt to parse the value from getTranslation
432+ wrapAt = Integer .valueOf (user .getTranslation (REFERENCE + "wrap-at" ));
433+
434+ } catch (NumberFormatException e ) {
435+ // If parsing fails, keep default value of 40
436+ addon .logError ("Warning: Unable to parse 'wrap-at' value, using default of 50." );
437+ }
438+
439+ String formattedText = insertNewlines (blocksText , wrapAt );
440+
413441 if (template .description () != null )
414442 {
415443 String biomeText = phase .getPhaseBiome () == null ? "" : LangUtilsHook .getBiomeName (phase .getPhaseBiome (), this .user );
@@ -420,23 +448,24 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
420448 BANK , bankText .toString (),
421449 ECONOMY , economyText .toString (),
422450 LEVEL , levelText .toString (),
423- PERMISSION , permissionText .toString ());
451+ PERMISSION , permissionText .toString (), BLOCKS , formattedText );
424452 }
425453 else
426454 {
427455 // Null description, so we make our own
428- String blockText = this .user .getTranslationOrNothing (reference + "starting-block" ,
456+ String blockText = this .user .getTranslationOrNothing (REFERENCE + "starting-block" ,
429457 TextVariables .NUMBER , phase .getBlockNumber ());
430- String biomeText = phase .getPhaseBiome () == null ? "" : this .user .getTranslationOrNothing (reference + "biome" ,
431- BIOME , LangUtilsHook .getBiomeName (phase .getPhaseBiome (), this .user ));
458+ String biomeText = phase .getPhaseBiome () == null ? ""
459+ : this .user .getTranslationOrNothing (REFERENCE + "biome" ,
460+ BIOME , LangUtilsHook .getBiomeName (phase .getPhaseBiome (), this .user ));
432461
433- descriptionText = this .user .getTranslationOrNothing (reference + "description" ,
462+ descriptionText = this .user .getTranslationOrNothing (REFERENCE + "description" ,
434463 "[starting-block]" , biomeText ,
435464 BIOME , blockText ,
436465 BANK , bankText .toString (),
437466 ECONOMY , economyText .toString (),
438467 LEVEL , levelText .toString (),
439- PERMISSION , permissionText .toString ());
468+ PERMISSION , permissionText .toString (), BLOCKS , formattedText );
440469 }
441470
442471 // Strip out or replace formating
@@ -519,6 +548,46 @@ private PanelItem createPhaseButton(ItemTemplateRecord template, Map.Entry<Integ
519548 return builder .build ();
520549 }
521550
551+ private String getMaterialName (User user , Material m ) {
552+ return addon .getPlugin ().getHooks ().getHook ("LangUtils" ).map (hook -> LangUtilsHook .getMaterialName (m , user ))
553+ .orElse (Util .prettifyText (m .name ()));
554+ }
555+
556+ private static String insertNewlines (String input , int interval ) {
557+ StringBuilder result = new StringBuilder (input .length ());
558+ int index = 0 ;
559+ char activeColor = 'a' ;
560+ int lastAmpIndex = -2 ;
561+
562+ while (index < input .length ()) {
563+ if (input .charAt (index ) == ChatColor .COLOR_CHAR && index < (input .length () - 1 )) {
564+ lastAmpIndex = index ;
565+ activeColor = input .charAt (index + 1 );
566+ }
567+ if (input .length () < index + interval ) {
568+ result .append (input .substring (index ));
569+ break ;
570+ }
571+
572+ // Find the space near the interval to break the line without cutting a word
573+ int breakPoint = input .lastIndexOf (' ' , index + interval );
574+ if (breakPoint <= index ) {
575+ breakPoint = index + interval ; // In case there are no spaces, break at exact interval
576+ }
577+
578+ result .append (input .substring (index , breakPoint )).append ('\n' );
579+ if (lastAmpIndex >= 0 ) {
580+ // Append color code
581+ result .append (ChatColor .COLOR_CHAR );
582+ result .append (activeColor );
583+ result .append (" " );
584+ }
585+ index = breakPoint + 1 ; // Move past the last space
586+ }
587+
588+ return result .toString ();
589+ }
590+
522591
523592 /**
524593 * This method checks if phase requirements fails.
0 commit comments