Work on commands a bit more
This commit is contained in:
@@ -4,9 +4,29 @@ import io.dico.dicore.command.CommandException;
|
||||
import io.dico.dicore.command.EMessageType;
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import io.dico.dicore.command.chat.help.HelpPages;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class AbstractChatController implements IChatController {
|
||||
private @NotNull HelpPages helpPages;
|
||||
|
||||
public AbstractChatController(@NotNull HelpPages helpPages) {
|
||||
this.helpPages = helpPages;
|
||||
}
|
||||
|
||||
public AbstractChatController() {
|
||||
this(HelpPages.newDefaultHelpPages());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public HelpPages getHelpPages() {
|
||||
return helpPages;
|
||||
}
|
||||
|
||||
public void setHelpPages(@NotNull HelpPages helpPages) {
|
||||
this.helpPages = helpPages;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(ExecutionContext context, EMessageType type, String message) {
|
||||
@@ -37,12 +57,12 @@ public class AbstractChatController implements IChatController {
|
||||
|
||||
@Override
|
||||
public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
|
||||
sendMessage(sender, EMessageType.INSTRUCTION, HelpCache.getHelpCache(address).getHelpPage(page));
|
||||
sender.sendMessage(helpPages.getHelpPage(sender, context, address, page));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
|
||||
sendMessage(sender, EMessageType.INSTRUCTION, HelpCache.getHelpCache(address).getSyntax());
|
||||
sender.sendMessage(helpPages.getSyntax(sender, context, address));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -83,4 +103,9 @@ public class AbstractChatController implements IChatController {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String filterMessage(String message) {
|
||||
return message;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,17 +1,5 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import io.dico.dicore.command.chat.help.IHelpComponent;
|
||||
import io.dico.dicore.command.chat.help.IHelpTopic;
|
||||
import io.dico.dicore.command.chat.help.IPageBuilder;
|
||||
import io.dico.dicore.command.chat.help.IPageLayout;
|
||||
import io.dico.dicore.command.chat.help.defaults.*;
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Static factory methods for {@link IChatController}
|
||||
*/
|
||||
@@ -27,26 +15,6 @@ public class ChatControllers {
|
||||
}
|
||||
|
||||
static {
|
||||
defaultChat = new AbstractChatController() {
|
||||
IPageBuilder pageBuilder = new DefaultPageBuilder();
|
||||
IPageLayout pageLayout = new DefaultPageLayout();
|
||||
List<IHelpTopic> topics = Arrays.asList(new DescriptionHelpTopic(), new SyntaxHelpTopic(), new SubcommandsHelpTopic());
|
||||
|
||||
@Override
|
||||
public void sendHelpMessage(CommandSender sender, ExecutionContext context, ICommandAddress address, int page) {
|
||||
sender.sendMessage(pageBuilder.getPage(topics, pageLayout, address, sender, context, page, 12));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendSyntaxMessage(CommandSender sender, ExecutionContext context, ICommandAddress address) {
|
||||
List<IHelpComponent> components = topics.get(1).getComponents(address, sender, context);
|
||||
if (components.isEmpty()) {
|
||||
sendHelpMessage(sender, context, address, 1);
|
||||
} else {
|
||||
sender.sendMessage(DefaultPageBuilder.combine(components));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
defaultChat = new AbstractChatController();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,186 +0,0 @@
|
||||
package io.dico.dicore.command.chat;
|
||||
|
||||
import io.dico.dicore.command.Command;
|
||||
import io.dico.dicore.command.EMessageType;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import io.dico.dicore.command.parameter.Parameter;
|
||||
import io.dico.dicore.command.parameter.ParameterList;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class HelpCache {
|
||||
private static Map<ICommandAddress, HelpCache> caches = new IdentityHashMap<>();
|
||||
private ICommandAddress address;
|
||||
private String shortSyntax;
|
||||
private String[] lines;
|
||||
private int[] pageStarts;
|
||||
|
||||
public static HelpCache getHelpCache(ICommandAddress address) {
|
||||
return caches.computeIfAbsent(address, HelpCache::new);
|
||||
}
|
||||
|
||||
private HelpCache(ICommandAddress address) {
|
||||
this.address = address;
|
||||
}
|
||||
|
||||
private void loadHelp() {
|
||||
List<String> lines = new ArrayList<>();
|
||||
List<Integer> potentialPageStarts = new ArrayList<>();
|
||||
int curLineIdx = 0;
|
||||
potentialPageStarts.add(curLineIdx);
|
||||
|
||||
String curLine = address.getChatController().getMessagePrefixForType(EMessageType.INSTRUCTION);
|
||||
curLine += address.getChatController().getChatFormatForType(EMessageType.INSTRUCTION);
|
||||
curLine += getSyntax();
|
||||
lines.add(curLine);
|
||||
curLineIdx++;
|
||||
|
||||
if (address.hasCommand()) {
|
||||
Command command = address.getCommand();
|
||||
String[] description = command.getDescription();
|
||||
if (description != null && description.length > 0) {
|
||||
for (String line : description) {
|
||||
curLine = address.getChatController().getChatFormatForType(EMessageType.INFORMATIVE).toString();
|
||||
curLine += line;
|
||||
lines.add(curLine);
|
||||
curLineIdx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
List<ICommandAddress> children = address.getChildren().values().stream()
|
||||
.distinct()
|
||||
.sorted(Comparator.comparing(ICommandAddress::getMainKey))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
for (ICommandAddress address : children) {
|
||||
potentialPageStarts.add(curLineIdx);
|
||||
|
||||
curLine = this.address.getChatController().getChatFormatForType(EMessageType.INSTRUCTION) + "/";
|
||||
if (address.isDepthLargerThan(2)) {
|
||||
curLine += "... ";
|
||||
}
|
||||
curLine += address.getMainKey();
|
||||
curLine += getHelpCache(address).getShortSyntax();
|
||||
lines.add(curLine);
|
||||
curLineIdx++;
|
||||
|
||||
if (address.hasCommand()) {
|
||||
String shortDescription = address.getCommand().getShortDescription();
|
||||
if (shortDescription != null) {
|
||||
curLine = this.address.getChatController().getChatFormatForType(EMessageType.INFORMATIVE).toString();
|
||||
curLine += shortDescription;
|
||||
lines.add(curLine);
|
||||
curLineIdx++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.lines = lines.toArray(new String[lines.size()]);
|
||||
|
||||
// compute where the pages start with a maximum page size of 10
|
||||
List<Integer> pageStarts = new ArrayList<>();
|
||||
pageStarts.add(0);
|
||||
int maxLength = 10;
|
||||
int curPageEndTarget = maxLength;
|
||||
for (int i = 1, n = potentialPageStarts.size(); i < n; i++) {
|
||||
int index = potentialPageStarts.get(i);
|
||||
if (index == curPageEndTarget) {
|
||||
pageStarts.add(curPageEndTarget);
|
||||
curPageEndTarget += maxLength;
|
||||
} else if (index > curPageEndTarget) {
|
||||
curPageEndTarget = potentialPageStarts.get(i - 1);
|
||||
pageStarts.add(curPageEndTarget);
|
||||
curPageEndTarget += maxLength;
|
||||
}
|
||||
}
|
||||
|
||||
int[] pageStartsArray = new int[pageStarts.size()];
|
||||
for (int i = 0, n = pageStartsArray.length; i < n; i++) {
|
||||
pageStartsArray[i] = pageStarts.get(i);
|
||||
}
|
||||
this.pageStarts = pageStartsArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a help page
|
||||
*
|
||||
* @param page the 0-bound page number (first page is page 0)
|
||||
* @return the help page
|
||||
*/
|
||||
public String getHelpPage(int page) {
|
||||
if (lines == null) {
|
||||
loadHelp();
|
||||
}
|
||||
|
||||
//System.out.println(Arrays.toString(lines));
|
||||
|
||||
if (page >= pageStarts.length) {
|
||||
//System.out.println("page >= pageStarts.length: " + Arrays.toString(pageStarts));
|
||||
return "";
|
||||
} else if (page < 0) {
|
||||
throw new IllegalArgumentException("Page number is negative");
|
||||
}
|
||||
|
||||
int start = pageStarts[page];
|
||||
int end = page + 1 == pageStarts.length ? lines.length : pageStarts[page + 1];
|
||||
//System.out.println("start = " + start);
|
||||
//System.out.println("end = " + end);
|
||||
return String.join("\n", Arrays.copyOfRange(lines, start, end));
|
||||
}
|
||||
|
||||
public int getTotalPageCount() {
|
||||
return pageStarts.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* The latter syntax of the command, prefixed by a space.
|
||||
*
|
||||
* @return The latter part of the syntax for this command. That is, without the actual command name.
|
||||
*/
|
||||
public String getShortSyntax() {
|
||||
if (shortSyntax != null) {
|
||||
return shortSyntax;
|
||||
}
|
||||
|
||||
StringBuilder syntax = new StringBuilder();
|
||||
if (address.hasCommand()) {
|
||||
Command command = address.getCommand();
|
||||
ParameterList list = command.getParameterList();
|
||||
Parameter<?, ?> repeated = list.getRepeatedParameter();
|
||||
|
||||
int requiredCount = list.getRequiredCount();
|
||||
List<Parameter<?, ?>> indexedParameters = list.getIndexedParameters();
|
||||
for (int i = 0, n = indexedParameters.size(); i < n; i++) {
|
||||
syntax.append(i < requiredCount ? " <" : " [");
|
||||
Parameter<?, ?> param = indexedParameters.get(i);
|
||||
syntax.append(param.getName());
|
||||
if (param == repeated) {
|
||||
syntax.append("...");
|
||||
}
|
||||
syntax.append(i < requiredCount ? '>' : ']');
|
||||
}
|
||||
|
||||
Map<String, Parameter<?, ?>> parametersByName = list.getParametersByName();
|
||||
for (Parameter<?, ?> param : parametersByName.values()) {
|
||||
if (param.isFlag()) {
|
||||
syntax.append(" [").append(param.getName());
|
||||
if (param.expectsInput()) {
|
||||
syntax.append(" <>");
|
||||
}
|
||||
syntax.append(']');
|
||||
}
|
||||
}
|
||||
} else {
|
||||
syntax.append(' ');
|
||||
}
|
||||
this.shortSyntax = syntax.toString();
|
||||
return this.shortSyntax;
|
||||
}
|
||||
|
||||
public String getSyntax() {
|
||||
return '/' + address.getAddress() + getShortSyntax();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -25,4 +25,6 @@ public interface IChatController {
|
||||
|
||||
String getMessagePrefixForType(EMessageType type);
|
||||
|
||||
String filterMessage(String message);
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,92 @@
|
||||
package io.dico.dicore.command.chat.help;
|
||||
|
||||
import io.dico.dicore.command.ExecutionContext;
|
||||
import io.dico.dicore.command.ICommandAddress;
|
||||
import io.dico.dicore.command.chat.help.defaults.*;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
public class HelpPages {
|
||||
private @NotNull IPageBuilder pageBuilder;
|
||||
private @NotNull IPageLayout pageLayout;
|
||||
private int pageLength;
|
||||
private @NotNull List<IHelpTopic> helpTopics;
|
||||
private @NotNull IHelpTopic syntaxTopic;
|
||||
|
||||
public HelpPages(@NotNull IPageBuilder pageBuilder, @NotNull IPageLayout pageLayout, int pageLength, @NotNull IHelpTopic syntaxTopic, @NotNull List<IHelpTopic> helpTopics) {
|
||||
this.pageBuilder = pageBuilder;
|
||||
this.pageLayout = pageLayout;
|
||||
this.pageLength = pageLength;
|
||||
this.syntaxTopic = syntaxTopic;
|
||||
this.helpTopics = helpTopics;
|
||||
}
|
||||
|
||||
public HelpPages(IPageBuilder pageBuilder, IPageLayout pageLayout, int pageLength, IHelpTopic syntaxTopic, IHelpTopic... helpTopics) {
|
||||
this(pageBuilder, pageLayout, pageLength, syntaxTopic, new ArrayList<>(Arrays.asList(helpTopics)));
|
||||
}
|
||||
|
||||
@SuppressWarnings("RedundantArrayCreation")
|
||||
public static HelpPages newDefaultHelpPages() {
|
||||
IHelpTopic syntaxTopic = new SyntaxHelpTopic();
|
||||
return new HelpPages(new DefaultPageBuilder(), new DefaultPageLayout(), 12,
|
||||
syntaxTopic, new IHelpTopic[]{new DescriptionHelpTopic(), syntaxTopic, new SubcommandsHelpTopic()});
|
||||
}
|
||||
|
||||
public @NotNull IPageBuilder getPageBuilder() {
|
||||
return pageBuilder;
|
||||
}
|
||||
|
||||
public void setPageBuilder(@NotNull IPageBuilder pageBuilder) {
|
||||
this.pageBuilder = pageBuilder;
|
||||
}
|
||||
|
||||
public @NotNull IPageLayout getPageLayout() {
|
||||
return pageLayout;
|
||||
}
|
||||
|
||||
public void setPageLayout(@NotNull IPageLayout pageLayout) {
|
||||
this.pageLayout = pageLayout;
|
||||
}
|
||||
|
||||
public int getPageLength() {
|
||||
return pageLength;
|
||||
}
|
||||
|
||||
public void setPageLength(int pageLength) {
|
||||
this.pageLength = pageLength;
|
||||
}
|
||||
|
||||
public @NotNull IHelpTopic getSyntaxTopic() {
|
||||
return syntaxTopic;
|
||||
}
|
||||
|
||||
public void setSyntaxTopic(@NotNull IHelpTopic syntaxTopic) {
|
||||
this.syntaxTopic = syntaxTopic;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<IHelpTopic> getHelpTopics() {
|
||||
return helpTopics;
|
||||
}
|
||||
|
||||
public void setHelpTopics(@NotNull List<IHelpTopic> helpTopics) {
|
||||
this.helpTopics = helpTopics;
|
||||
}
|
||||
|
||||
public @NotNull String getHelpPage(Permissible viewer, ExecutionContext context, ICommandAddress address, int page) {
|
||||
return pageBuilder.getPage(helpTopics, pageLayout, address, viewer, context, page, pageLength);
|
||||
}
|
||||
|
||||
public @NotNull String getSyntax(Permissible viewer, ExecutionContext context, ICommandAddress address) {
|
||||
List<IHelpComponent> components = syntaxTopic.getComponents(address, viewer, context);
|
||||
if (components.isEmpty()) {
|
||||
return getHelpPage(viewer, context, address, 1);
|
||||
}
|
||||
return DefaultPageBuilder.combine(components);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user