Skip to content
This repository has been archived by the owner on Jan 7, 2025. It is now read-only.

Commit

Permalink
Improve help system in general and add translatable messages
Browse files Browse the repository at this point in the history
  • Loading branch information
lokka30 committed Jan 6, 2023
1 parent ca06499 commit c8bbba3
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 42 deletions.
2 changes: 1 addition & 1 deletion bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
<parent>
<artifactId>arcaneframework-parent</artifactId>
<groupId>io.github.arcaneplugins.arcaneframework</groupId>
<version>0.2.4</version>
<version>0.2.5</version>
</parent>
<dependencies>
<!--suppress VulnerableLibrariesLocal -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,42 @@
@SuppressWarnings("unused")
public final class HelpSystem {

private final HomeChapter homeChapter;

public HelpSystem() {
homeChapter = new HomeChapter(this);
private HelpSystem() throws IllegalAccessException {
throw new IllegalAccessException("Illegal instantiation of utility class");
}

public @Nonnull HomeChapter getHomeChapter() {
return homeChapter;
public static String msgLeadingHelpCommand = "/???ReplaceMe??? help";

public static String msgHeading =
"""
&8&l┌ &f&lAF &fHelp Menu &8• %breadcrumbs%
&r%page-contents%&r
&8&l└ &8&m-&8{%previous-page%&8}&7 Page %page-current%&7 of %page-max%&8 &8{%next-page%&8}&m-""";

public static String msgHeadingPreviousPage = """
[«««](color=blue formatting=bold run_command=%leading-help-command% %chapter-path% %previous-index%)""";

public static String msgHeadingNextPage = """
[»»»](color=blue formatting=bold run_command=%leading-help-command% %chapter-path% %next-index%)""";

public static String msgHeadingPageMax = """
[%max-index%](color=gray format=italic run_command=%leading-help-command% %chapter-path% %max-index%)""";

public static String msgHeadingPageCurrent = """
[%current-index%](color=gray format=italic run_command=%leading-help-command% %chapter-path% %current-index%)""";

public static String msgBreadcrumb = """
[%chapter-id%](color=white format=underline run_command=%leading-help-command% %chapter-path%)""";

public static String msgBreadcrumbSeparator = """
&8 »\s""";

private static final HomeChapter HOME_CHAPTER = new HomeChapter();

public static @Nonnull HomeChapter getHomeChapter() {
return HOME_CHAPTER;
}

public static sealed class Chapter permits HomeChapter, SubChapter {
Expand All @@ -41,20 +69,19 @@ public Chapter(
public @Nonnull String getFormattedPage(
final int index
) {
return """
&8&m-------&7 LM Help: &b&n%s&8 &m-------&r
&r%s&r
&8&m-------&8 (&b&l««&8)&r &7Page %s of %s&8 (&b&l»»&8) &m-------&r"""
.formatted(
getBreadcrumbs(),

getPages().get(index - 1),

index,
getPages().size()
);
return msgHeading
.replace("%breadcrumbs%", getBreadcrumbs())
.replace("%page-contents%", getPages().get(index - 1))
.replace("%previous-page%", msgHeadingPreviousPage)
.replace("%page-current%", msgHeadingPageCurrent)
.replace("%current-index%", Integer.toString(index))
.replace("%page-max%", msgHeadingPageMax)
.replace("%next-page%", msgHeadingNextPage)
.replace("%max-index%", Integer.toString(getPages().size()))
.replace("%next-index%", Integer.toString(index + 1))
.replace("%previous-index%", Integer.toString(index - 1))
.replace("%chapter-path%", getChapterPath())
.replace("%leading-help-command%", msgLeadingHelpCommand);
}

public void addPages(
Expand All @@ -66,17 +93,54 @@ public void addPages(
));
}

public @Nonnull String getChapterPath() {
List<String> reversePath = new ArrayList<>();

final Consumer<Chapter> consumer = chapter -> {
if(reversePath.isEmpty()) {
reversePath.add(chapter.getId());
} else {
reversePath.add(chapter.getId() + " ");
}
};

//noinspection DuplicatedCode
Chapter c = this;
while(true) {
consumer.accept(c);
if(c instanceof SubChapter sc) {
c = sc.getParentChapter();
} else if(c instanceof HomeChapter hc) {
break;
} else {
throw new IllegalStateException(c.getClass().getName());
}
}

Collections.reverse(reversePath);

final StringBuilder sb = new StringBuilder();
for(final String s : reversePath) { sb.append(s); }
return sb.toString();
}

public @Nonnull String getBreadcrumbs() {
final List<String> reversedBreadcrumb = new ArrayList<>();

final Consumer<Chapter> consumer = chapter -> {
final String breadcrumb = msgBreadcrumb
.replace("%chapter-id%", chapter.getId())
.replace("%leading-help-command%", msgLeadingHelpCommand)
.replace("%chapter-path%", getChapterPath());

if(reversedBreadcrumb.isEmpty()) {
reversedBreadcrumb.add("&b&n" + chapter.getId());
reversedBreadcrumb.add(breadcrumb);
} else {
reversedBreadcrumb.add("&b&n" + chapter.getId() + "&8 « ");
reversedBreadcrumb.add(breadcrumb + msgBreadcrumbSeparator);
}
};

//noinspection DuplicatedCode
Chapter c = this;
while(true) {
consumer.accept(c);
Expand All @@ -92,7 +156,7 @@ public void addPages(
Collections.reverse(reversedBreadcrumb);

final StringBuilder sb = new StringBuilder();
for(final String s : reversedBreadcrumb) { sb.append(s); }
for(final String breadcrumb : reversedBreadcrumb) { sb.append(breadcrumb); }
return sb.toString();
}

Expand All @@ -108,21 +172,10 @@ public void addPages(

public static final class HomeChapter extends Chapter {

private final HelpSystem helpSystem;

public HomeChapter(
final @Nonnull HelpSystem helpSystem
) {
super("Home");

this.helpSystem = Objects.requireNonNull(
helpSystem,
"helpSystem"
);
HomeChapter() {
super("home");
}

public @Nonnull HelpSystem getHelpSystem() { return helpSystem; }

}

public static final class SubChapter extends Chapter {
Expand Down
8 changes: 5 additions & 3 deletions bukkit/src/test/java/HelpSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ public class HelpSystemTest {

@Test
void helpSystemTest() {
final HelpSystem hs = new HelpSystem();

// Breadcrumb: `Home`
final HomeChapter home = hs.getHomeChapter();
final HomeChapter home = HelpSystem.getHomeChapter();

home.getPages().clear();
home.getSubChapters().clear();

home.addPages("""
• Click here to view command help.
• Click here to view documentation.
Expand Down
2 changes: 1 addition & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
<parent>
<artifactId>arcaneframework-parent</artifactId>
<groupId>io.github.arcaneplugins.arcaneframework</groupId>
<version>0.2.4</version>
<version>0.2.5</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.

<groupId>io.github.arcaneplugins.arcaneframework</groupId>
<artifactId>arcaneframework-parent</artifactId>
<version>0.2.4</version>
<version>0.2.5</version>
<modules>
<module>bukkit</module>
<module>core</module>
Expand Down

0 comments on commit c8bbba3

Please sign in to comment.