-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Order command entries in
/sb help
by smallest to largest
- Loading branch information
Showing
7 changed files
with
174 additions
and
67 deletions.
There are no files selected for viewing
91 changes: 89 additions & 2 deletions
91
...bukkit/src/main/java/com/github/tofpu/speedbridge2/bukkit/command/MinimalCommandHelp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} | ||
} |
61 changes: 0 additions & 61 deletions
61
...rc/main/java/com/github/tofpu/speedbridge2/bukkit/command/MinimalCommandHelpResolver.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...bukkit/src/main/java/com/github/tofpu/speedbridge2/bukkit/command/RelatedCommandHelp.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
...m/bukkit/src/main/java/com/github/tofpu/speedbridge2/bukkit/command/util/CommandUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |