Skip to content

Commit

Permalink
feat: new paginated help menu
Browse files Browse the repository at this point in the history
- lamp v4 changes

Closes #5
  • Loading branch information
HappyAreaBean committed Dec 22, 2024
1 parent 1eeef67 commit 5059695
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 41 deletions.
24 changes: 6 additions & 18 deletions src/main/java/cc/happyareabean/sjm/SimpleJoinMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import cc.happyareabean.sjm.listener.PlayerJoinListener;
import cc.happyareabean.sjm.listener.UpdateNotifyListener;
import cc.happyareabean.sjm.utils.AdventureWebEditorAPI;
import cc.happyareabean.sjm.utils.Constants;
import lombok.Getter;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.minimessage.MiniMessage;
Expand All @@ -17,7 +16,10 @@
import org.inventivetalent.update.spiget.SpigetUpdate;
import org.inventivetalent.update.spiget.UpdateCallback;
import org.inventivetalent.update.spiget.comparator.VersionComparator;
import revxrsal.commands.bukkit.BukkitCommandHandler;
import revxrsal.commands.Lamp;
import revxrsal.commands.bukkit.BukkitLamp;
import revxrsal.commands.bukkit.BukkitLampConfig;
import revxrsal.commands.bukkit.actor.BukkitCommandActor;

import java.io.File;
import java.net.URI;
Expand All @@ -37,7 +39,7 @@ public class SimpleJoinMessage extends JavaPlugin {
@Getter public static final LegacyComponentSerializer LEGACY_SERIALIZER = LegacyComponentSerializer.legacy('&');
@Getter private static BukkitAudiences adventure;
@Getter private AdventureWebEditorAPI adventureWebEditorAPI;
@Getter private BukkitCommandHandler commandHandler;
@Getter private Lamp<BukkitCommandActor> commandHandler;
@Getter private SJMConfig SJMConfig;
@Getter private SJMMisc miscConfig;

Expand All @@ -55,21 +57,7 @@ public void onEnable() {
getServer().getPluginManager().registerEvents(new UpdateNotifyListener(), this);

getLogger().info("Registering commands...");
commandHandler = BukkitCommandHandler.create(this);
commandHandler.enableAdventure(adventure);
commandHandler.setMessagePrefix(LEGACY_SERIALIZER.serialize(Constants.PREFIX));
commandHandler.setHelpWriter((command, actor) -> {
StringBuilder sb = new StringBuilder();
sb.append("&8• &e/");
sb.append(command.getPath().toRealString());
if (!command.getUsage().isEmpty()) {
sb.append(" ");
sb.append(command.getUsage());
}
sb.append(" &7- &f");
sb.append(command.getDescription());
return sb.toString();
});
commandHandler = BukkitLamp.builder(BukkitLampConfig.builder(this).audiences(adventure).build()).build();
commandHandler.register(new SJMCommand());

adventureWebEditorAPI = new AdventureWebEditorAPI(URI.create(this.SJMConfig.getAdventureWebURL()));
Expand Down
33 changes: 10 additions & 23 deletions src/main/java/cc/happyareabean/sjm/commands/SJMCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,27 @@
import cc.happyareabean.sjm.SimpleJoinMessage;
import cc.happyareabean.sjm.config.SJMConfig;
import cc.happyareabean.sjm.utils.AdventureWebEditorAPI;
import cc.happyareabean.sjm.utils.CommandUtils;
import cc.happyareabean.sjm.utils.Constants;
import cc.happyareabean.sjm.utils.MessageUtil;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import revxrsal.commands.annotation.Command;
import revxrsal.commands.annotation.CommandPlaceholder;
import revxrsal.commands.annotation.Default;
import revxrsal.commands.annotation.DefaultFor;
import revxrsal.commands.annotation.Description;
import revxrsal.commands.annotation.Named;
import revxrsal.commands.annotation.Range;
import revxrsal.commands.annotation.Subcommand;
import revxrsal.commands.bukkit.BukkitCommandActor;
import revxrsal.commands.bukkit.actor.BukkitCommandActor;
import revxrsal.commands.bukkit.annotation.CommandPermission;
import revxrsal.commands.help.CommandHelp;
import revxrsal.commands.help.Help;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static cc.happyareabean.sjm.SimpleJoinMessage.LEGACY_SERIALIZER;
import static cc.happyareabean.sjm.SimpleJoinMessage.MINIMESSAGE;
import static net.kyori.adventure.text.Component.newline;
import static net.kyori.adventure.text.Component.space;
Expand All @@ -36,22 +35,10 @@
@CommandPermission("sjm.admin")
public class SJMCommand {

@DefaultFor({"~"})
@CommandPlaceholder
@Description("SimpleJoinMessage commands list")
public void help(BukkitCommandActor actor, CommandHelp<String> helpEntries, @Default("1") int page) {
List<Component> list = new ArrayList<>();
list.add(Constants.HEADER);
list.add(MINIMESSAGE.deserialize(String.format("<gradient:#f7ff00:#db36a4><b>SimpleJoinMessage</b></gradient> <white>%s</white>", Constants.VERSION))
.clickEvent(openUrl("https://go.happyareabean.cc/sjm"))
.hoverEvent(MINIMESSAGE.deserialize("<rainbow>click me!")));
list.add(text()
.content("By ")
.color(NamedTextColor.GRAY)
.append(text("HappyAreaBean", NamedTextColor.GREEN)).build());
list.add(Constants.HEADER);
helpEntries.paginate(page, 10).forEach(s -> list.add(LEGACY_SERIALIZER.deserialize(s)));
list.add(Constants.HEADER);
list.forEach(actor::reply);
public void help(BukkitCommandActor actor, @Range(min = 1) @Default("1") int page, Help.RelatedCommands<BukkitCommandActor> helpEntries) {
CommandUtils.handleHelpMenu(actor, page, helpEntries, 5, "sjm");
}

@Subcommand("reload")
Expand All @@ -69,14 +56,14 @@ public void reloadShow(BukkitCommandActor actor) {
actor.reply(Constants.PREFIX.append(text("The settings have been reloaded, but you're in console mode, so the message won't be sent.", NamedTextColor.RED)));
return;
}
MessageUtil.sendMessageDelay(actor.getAsPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0);
MessageUtil.sendMessageDelay(actor.asPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0);
}

@Subcommand({"show"})
@Description("Send a join message in your chat")
public void show(BukkitCommandActor actor) {
actor.requirePlayer();
MessageUtil.sendMessageDelay(actor.getAsPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0);
MessageUtil.sendMessageDelay(actor.asPlayer(), SimpleJoinMessage.getInstance().getSJMConfig().getJoinMessage(), 0);
}

@Subcommand({"editor"})
Expand Down
105 changes: 105 additions & 0 deletions src/main/java/cc/happyareabean/sjm/utils/CommandUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package cc.happyareabean.sjm.utils;

import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.TextComponent;
import net.kyori.adventure.text.event.ClickEvent;
import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.format.TextDecoration;
import org.jetbrains.annotations.Nullable;
import revxrsal.commands.bukkit.actor.BukkitCommandActor;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.help.Help;

import java.util.ArrayList;
import java.util.List;

import static cc.happyareabean.sjm.SimpleJoinMessage.LEGACY_SERIALIZER;
import static cc.happyareabean.sjm.SimpleJoinMessage.MINIMESSAGE;
import static cc.happyareabean.sjm.utils.Constants.PAGE_TEXT;
import static net.kyori.adventure.text.Component.text;
import static net.kyori.adventure.text.event.ClickEvent.openUrl;

public class CommandUtils {

public static void handleHelpMenu(BukkitCommandActor actor,
int page,
Help.CommandList<BukkitCommandActor> commands,
int elementsPerPage,
@Nullable String filterFor) {
List<ExecutableCommand<BukkitCommandActor>> list = new ArrayList<>();

if (filterFor != null) {
for (ExecutableCommand<BukkitCommandActor> command : commands.all()) {
String usage = command.usage();
if (!usage.startsWith(filterFor)) continue;
list.add(command);
}
} else {
list = commands.all();
}

List<ExecutableCommand<BukkitCommandActor>> commandList = Help.paginate(list, page, elementsPerPage);

TextComponent.Builder builder = Component.text();

builder.append(Constants.HEADER);
builder.appendNewline();
builder.append(MINIMESSAGE.deserialize(String.format("<gradient:#f7ff00:#db36a4><b>SimpleJoinMessage</b></gradient> <white>%s</white>",
Constants.VERSION))
.clickEvent(openUrl("https://go.happyareabean.cc/sjm"))
.hoverEvent(MINIMESSAGE.deserialize("<rainbow>click me!")));
builder.appendNewline();
builder.append(text()
.content("By ")
.color(NamedTextColor.GRAY)
.append(text("HappyAreaBean", NamedTextColor.GREEN)).build());
builder.appendNewline();
builder.append(Constants.HEADER);
builder.appendNewline();

commandList.forEach(command -> {
builder.append(LEGACY_SERIALIZER.deserialize(String.format(" &8• &e/%s %s", command.usage(), (command.description() != null ? "&7- &f" + command.description() : ""))));
builder.appendNewline();
});

int numberOfPages = Help.numberOfPages(list.size(), elementsPerPage);
if (numberOfPages > 1) {
builder.appendNewline();
builder.append(paginateNavigation(page, numberOfPages, Constants.HELP_COMMAND_FORMAT));
builder.appendNewline();
}

builder.append(Constants.HEADER);

actor.reply(builder.build());
}

public static void handleHelpMenu(BukkitCommandActor actor, int page, Help.CommandList<BukkitCommandActor> commands, int elementsPerPage) {
handleHelpMenu(actor, page, commands, elementsPerPage, null);
}

public static Component paginateNavigation(int currentPage, int maxPage, String commandFormat) {
int previousPage = currentPage - 1;
int nextPage = currentPage + 1;

boolean havePreviousPage = previousPage != 0;
boolean haveNextPage = maxPage != currentPage;

TextComponent.Builder pageText = text()
.decorate(TextDecoration.BOLD)
.color(NamedTextColor.YELLOW);

pageText.append(text("«", !havePreviousPage ? NamedTextColor.DARK_GRAY : null)
.clickEvent(havePreviousPage ? ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, String.format(commandFormat, previousPage)) : null)
.hoverEvent(havePreviousPage ? text(String.format(PAGE_TEXT, previousPage)).color(NamedTextColor.GOLD) : null));

pageText.append(text(" ▍ "));

pageText.append(text("»", !haveNextPage ? NamedTextColor.DARK_GRAY : null)
.clickEvent(haveNextPage ? ClickEvent.clickEvent(ClickEvent.Action.RUN_COMMAND, String.format(commandFormat, nextPage)) : null)
.hoverEvent(haveNextPage ? text(String.format(PAGE_TEXT, nextPage)).color(NamedTextColor.GOLD) : null));

return pageText.build();
}

}
3 changes: 3 additions & 0 deletions src/main/java/cc/happyareabean/sjm/utils/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ public class Constants {

public static final String VERSION = SimpleJoinMessage.getInstance().getDescription().getVersion();
public static final String USER_AGENT = "SimpleJoinMessage/" + VERSION;

public static final String HELP_COMMAND_FORMAT = "/sjm %s";
public static final String PAGE_TEXT = "Page %s";
}

0 comments on commit 5059695

Please sign in to comment.