Skip to content

Commit

Permalink
Order command entries in /sb help by smallest to largest
Browse files Browse the repository at this point in the history
  • Loading branch information
Tofpu committed Jan 28, 2024
1 parent d71ec5c commit 3fbb589
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,93 @@
package com.github.tofpu.speedbridge2.bukkit.command;

import java.util.List;
import com.github.tofpu.speedbridge2.bukkit.command.util.CommandUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Range;
import revxrsal.commands.CommandHandler;
import revxrsal.commands.command.CommandCategory;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.core.CommandPath;
import revxrsal.commands.exception.InvalidHelpPageException;
import revxrsal.commands.help.CommandHelp;
import revxrsal.commands.help.CommandHelpWriter;
import revxrsal.commands.process.ContextResolver;

public interface MinimalCommandHelp<T> extends List<T> {
import java.util.*;

/**
* Sort the command help entries by the smallest path to the largest.
*
* @param <T> type must match with {@link revxrsal.commands.help.CommandHelpWriter<T>}
* @see CommandHelp
*/
public interface MinimalCommandHelp<T> extends CommandHelp<T> {
class Resolver implements ContextResolver<MinimalCommandHelp<?>> {
private final CommandHandler handler;

public Resolver(CommandHandler handler) {
this.handler = handler;
}

@Override
public MinimalCommandHelp<?> resolve(@NotNull ContextResolver.ContextResolverContext context) {
if (handler.getHelpWriter() == null)
throw new IllegalArgumentException("No help writer is registered!");

ExecutableCommand helpCommand = context.command();
CommandHelpWriter<?> writer = handler.getHelpWriter();
BaseMinimalCommandHelp<Object> entries = new BaseMinimalCommandHelp<>();
CommandCategory parent = helpCommand.getParent();
CommandPath fullCommandPath = helpCommand.getPath();
CommandPath parentPath = parent == null ? null : parent.getPath();

if (parentPath == null) {
return entries;
}

CommandCategory commandCategory = handler.getCategory(parentPath);
if (commandCategory == null) {
return entries;
}

LinkedList<CommandPath> qualifiedPaths = new LinkedList<>();
Map<CommandPath, ExecutableCommand> pathExecutableCommandMap = new HashMap<>();

handler.getCommands().forEach((commandPath, executableCommand) -> {
if (!CommandUtil.isSameRoot(fullCommandPath, commandPath)) {
System.out.println("first: " + fullCommandPath.getFirst() + " != " + commandPath.getFirst());
return;
}
qualifiedPaths.add(commandPath);
pathExecutableCommandMap.put(commandPath, executableCommand);
});

qualifiedPaths.sort(Comparator.comparingInt(CommandPath::size));
qualifiedPaths.forEach(commandPath -> entries.add(writer.generate(pathExecutableCommandMap.get(commandPath), context.actor())));
return entries;
}

static final class BaseMinimalCommandHelp<T> extends ArrayList<T> implements MinimalCommandHelp<T> {
@Override
public MinimalCommandHelp<T> paginate(int page, int elementsPerPage) throws InvalidHelpPageException {
if (isEmpty()) return new BaseMinimalCommandHelp<>();
BaseMinimalCommandHelp<T> list = new BaseMinimalCommandHelp<>();
int size = getPageSize(elementsPerPage);
if (page > size)
throw new InvalidHelpPageException(this, page, elementsPerPage);
int listIndex = page - 1;
int l = Math.min(page * elementsPerPage, size());
for (int i = listIndex * elementsPerPage; i < l; ++i) {
list.add(get(i));
}
return list;
}

@Override
public @Range(from = 1, to = Long.MAX_VALUE) int getPageSize(int elementsPerPage) {
if (elementsPerPage < 1)
throw new IllegalArgumentException("Elements per page cannot be less than 1! (Found " + elementsPerPage + ")");
return (size() / elementsPerPage) + (size() % elementsPerPage == 0 ? 0 : 1);
}
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void init(final BukkitPlugin plugin) {

registerExceptions(commandHandler);

commandHandler.registerContextResolver((Class) MinimalCommandHelp.class, new MinimalCommandHelpResolver(commandHandler));
commandHandler.registerContextResolver((Class) RelatedCommandHelp.class, new RelatedCommandHelp.Resolver(commandHandler));
commandHandler.registerContextResolver((Class) MinimalCommandHelp.class, new MinimalCommandHelp.Resolver(commandHandler));
commandHandler.setHelpWriter((command, actor) -> {
String description = command.getDescription();
if (description == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void score(final OnlinePlayer player, @Optional Integer islandSlot) {

@Subcommand("help")
@Description("View the available commands")
public void help(final OnlinePlayer player, final CommandHelp<String> helpEntries, final @Default("1") int page) {
public void help(final OnlinePlayer player, final MinimalCommandHelp<String> helpEntries, final @Default("1") int page) {
MessageBuilder messageBuilder = new MessageBuilder()
.appendNewLine()
.append("&8&l|>&r &e&lSpeedBridge3 &fv2.0-dev").appendNewLine()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.github.tofpu.speedbridge2.bukkit.command;

import com.github.tofpu.speedbridge2.bukkit.command.util.CommandUtil;
import org.jetbrains.annotations.NotNull;
import revxrsal.commands.CommandHandler;
import revxrsal.commands.command.CommandCategory;
import revxrsal.commands.command.ExecutableCommand;
import revxrsal.commands.core.CommandPath;
import revxrsal.commands.help.CommandHelp;
import revxrsal.commands.help.CommandHelpWriter;
import revxrsal.commands.process.ContextResolver;

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

/**
* Aggregates direct related command entries only.
*
* @param <T> type must match with {@link revxrsal.commands.help.CommandHelpWriter<T>}
* @see CommandHelp
*/
public interface RelatedCommandHelp<T> extends List<T> {
class Resolver implements ContextResolver<RelatedCommandHelp<?>> {
private final CommandHandler handler;

public Resolver(CommandHandler handler) {
this.handler = handler;
}

@Override
public RelatedCommandHelp<?> resolve(@NotNull ContextResolver.ContextResolverContext context) {
if (handler.getHelpWriter() == null)
throw new IllegalArgumentException("No help writer is registered!");

ExecutableCommand helpCommand = context.command();
CommandHelpWriter<?> writer = handler.getHelpWriter();
BaseRelatedCommandHelp<Object> entries = new BaseRelatedCommandHelp<>();
CommandCategory parent = helpCommand.getParent();
CommandPath fullCommandPath = helpCommand.getPath();
CommandPath parentPath = parent == null ? null : parent.getPath();

if (parentPath == null) {
return entries;
}

CommandCategory commandCategory = handler.getCategory(parentPath);
if (commandCategory == null) {
return entries;
}

handler.getCommands().forEach((commandPath, executableCommand) -> {
if (!CommandUtil.isSameRoot(fullCommandPath, commandPath)) {
System.out.println("first: " + fullCommandPath.getFirst() + " != " + commandPath.getFirst());
return;
}

if (!commandPath.isChildOf(parentPath)) {
System.out.println("different child");
return;
}

entries.add(writer.generate(executableCommand, context.actor()));
});
return entries;
}

static final class BaseRelatedCommandHelp<T> extends ArrayList<T> implements RelatedCommandHelp<T> {}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.github.tofpu.speedbridge2.bukkit.command.subcommand;

import com.github.tofpu.speedbridge2.bukkit.command.MinimalCommandHelp;
import com.github.tofpu.speedbridge2.bukkit.command.RelatedCommandHelp;
import com.github.tofpu.speedbridge2.bukkit.util.MessageBuilder;
import com.github.tofpu.speedbridge2.object.player.OnlinePlayer;
import org.apache.commons.lang.WordUtils;
Expand All @@ -15,7 +15,7 @@ public abstract class CoreCommand implements OrphanCommand {

@Subcommand("help")
@Description("View the available commands")
public void help(final OnlinePlayer player, final MinimalCommandHelp<String> helpEntries) {
public void help(final OnlinePlayer player, final RelatedCommandHelp<String> helpEntries) {
MessageBuilder messageBuilder = new MessageBuilder()
.appendNewLine()
.append("&8&l|>&r &e&lSpeedBridge3 &fv2.0-dev &8| &e&l%s Help".replace("%s", capitalizedName())).appendNewLine()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.github.tofpu.speedbridge2.bukkit.command.util;

import revxrsal.commands.core.CommandPath;

import java.util.Objects;

public class CommandUtil {
public static boolean isSameRoot(CommandPath that, CommandPath other) {
return Objects.equals(that.getFirst(), other.getFirst());
}
}

0 comments on commit 3fbb589

Please sign in to comment.