Add Mson support to HelpCommand

This commit is contained in:
ulumulu1510 2015-09-04 18:15:47 +02:00 committed by Olof Larsson
parent a364395042
commit 43b0e32e5e
6 changed files with 104 additions and 64 deletions

View File

@ -1,5 +1,10 @@
package com.massivecraft.massivecore; package com.massivecraft.massivecore;
import org.bukkit.ChatColor;
import com.massivecraft.massivecore.mson.Mson;
import static com.massivecraft.massivecore.mson.Mson.mson;
public class Lang public class Lang
{ {
public static final String PERM_DEFAULT_DENIED_FORMAT = "<b>You don't have permission to <h>%s<b>."; public static final String PERM_DEFAULT_DENIED_FORMAT = "<b>You don't have permission to <h>%s<b>.";
@ -11,8 +16,9 @@ public class Lang
public static final String COMMAND_TOO_FEW_ARGS = "<b>Not enough command input. <i>You should use it like this:"; public static final String COMMAND_TOO_FEW_ARGS = "<b>Not enough command input. <i>You should use it like this:";
public static final String COMMAND_TOO_MANY_ARGS = "<b>Too much command input %s<b>."; public static final String COMMAND_TOO_MANY_ARGS = "<b>Too much command input %s<b>.";
public static final String COMMAND_TOO_MANY_ARGS2 = "<i>You should use the command like this:"; public static final String COMMAND_TOO_MANY_ARGS2 = "<i>You should use the command like this:";
public static final String COMMAND_NO_SUCH_SUB = "<b>Couldn't find the command <c>%s"; public static final Mson COMMAND_REPLACEMENT = mson("REPLACEMENT");
public static final String COMMAND_SUGGEST_SUB = "<i>Maybe you could try %s"; public static final Mson COMMAND_NO_SUCH_SUB = mson("Couldn't find the command ", COMMAND_REPLACEMENT).color(ChatColor.YELLOW);
public static final String COMMAND_GET_HELP = "<i>Use %s <i>to see commands."; public static final Mson COMMAND_SUGGEST_SUB = mson("Maybe you could try ", COMMAND_REPLACEMENT).color(ChatColor.YELLOW);
public static final Mson COMMAND_GET_HELP = mson("Use ", COMMAND_REPLACEMENT, " to see commands.").color(ChatColor.YELLOW);
public static final String COMMAND_TOO_MANY_TAB_SUGGESTIONS = "<h>%d <b>tab completions available. Be more specific and try again."; public static final String COMMAND_TOO_MANY_TAB_SUGGESTIONS = "<h>%d <b>tab completions available. Be more specific and try again.";
} }

View File

@ -1,10 +1,13 @@
package com.massivecraft.massivecore.cmd; package com.massivecraft.massivecore.cmd;
import static com.massivecraft.massivecore.mson.Mson.mson;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.massivecraft.massivecore.cmd.arg.AR; import com.massivecraft.massivecore.cmd.arg.AR;
import com.massivecraft.massivecore.cmd.arg.ARInteger; import com.massivecraft.massivecore.cmd.arg.ARInteger;
import com.massivecraft.massivecore.mson.Mson;
public class ArgSetting<T> public class ArgSetting<T>
{ {
@ -176,13 +179,22 @@ public class ArgSetting<T>
return ! this.isRequiredFor(sender); return ! this.isRequiredFor(sender);
} }
public String getUseageTemplateDisplayFor(CommandSender sender) public Mson getUseageTemplateDisplayFor(CommandSender sender)
{ {
if (this.isRequiredFor(sender)) return "<" + this.getName() + ">"; Mson ret;
String def = this.getDefaultDesc(); if (this.isRequiredFor(sender))
def = (def != null ? "=" + def : ""); {
return "[" + this.getName() + def + "]"; ret = mson("<" + this.getName() + ">");
}
else
{
String def = this.getDefaultDesc();
def = (def != null ? "=" + def : "");
ret = mson("[" + this.getName() + def + "]");
}
return ret;
} }
// -------------------------------------------- // // -------------------------------------------- //

View File

@ -1,9 +1,10 @@
package com.massivecraft.massivecore.cmd; package com.massivecraft.massivecore.cmd;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List;
import com.massivecraft.massivecore.MassiveException; import com.massivecraft.massivecore.MassiveException;
import com.massivecraft.massivecore.cmd.MassiveCommand; import com.massivecraft.massivecore.mson.Mson;
import com.massivecraft.massivecore.util.Txt; import com.massivecraft.massivecore.util.Txt;
public class HelpCommand extends MassiveCommand public class HelpCommand extends MassiveCommand
@ -41,18 +42,16 @@ public class HelpCommand extends MassiveCommand
MassiveCommand parentCommand = this.getParentCommand(); MassiveCommand parentCommand = this.getParentCommand();
// Create Lines // Create Lines
ArrayList<String> lines = new ArrayList<String>(); List<Mson> lines = new ArrayList<Mson>();
for (String helpline : parentCommand.getHelp()) for (String helpline : parentCommand.getHelp())
{ {
lines.add(Txt.parse("<a>#<i> " + helpline)); lines.add(Mson.parse("<a>#<i> " + helpline));
} }
for (MassiveCommand subCommand : parentCommand.getSubCommands()) for (MassiveCommand subCommand : parentCommand.getSubCommands())
{ {
if (subCommand.isVisibleTo(sender)) if ( ! subCommand.isVisibleTo(sender)) continue;
{ lines.add(subCommand.getUseageTemplate(this.getCommandChain(), true, true, sender));
lines.add(subCommand.getUseageTemplate(this.getCommandChain(), true, true, sender));
}
} }
// Send Lines // Send Lines

View File

@ -12,6 +12,7 @@ import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
@ -131,8 +132,7 @@ public class MassiveCommand
public void addSubCommand(MassiveCommand subCommand, int index) public void addSubCommand(MassiveCommand subCommand, int index)
{ {
subCommand.commandChain.addAll(this.commandChain); subCommand.addToCommandChain(this);
subCommand.commandChain.add(this);
this.subCommands.add(index, subCommand); this.subCommands.add(index, subCommand);
} }
@ -465,6 +465,19 @@ public class MassiveCommand
public List<MassiveCommand> getCommandChain() { return this.commandChain; } public List<MassiveCommand> getCommandChain() { return this.commandChain; }
public void setCommandChain(List<MassiveCommand> commandChain) { this.commandChain = commandChain; } public void setCommandChain(List<MassiveCommand> commandChain) { this.commandChain = commandChain; }
// Adds command to tree structure
public void addToCommandChain(MassiveCommand command)
{
this.commandChain.add(0, command);
List<MassiveCommand> cmds = this.getSubCommands();
for (MassiveCommand cmd : cmds)
{
cmd.addToCommandChain(command);
}
}
public MassiveCommand getParentCommand() public MassiveCommand getParentCommand()
{ {
List<MassiveCommand> commandChain = this.getCommandChain(); List<MassiveCommand> commandChain = this.getCommandChain();
@ -772,15 +785,15 @@ public class MassiveCommand
// Try Levenshtein // Try Levenshtein
List<String> matches = this.getSimilarSubcommandAliases(arg, this.getMaxLevenshteinDistanceForArg(arg)); List<String> matches = this.getSimilarSubcommandAliases(arg, this.getMaxLevenshteinDistanceForArg(arg));
Mixin.msgOne(sender, Lang.COMMAND_NO_SUCH_SUB, this.getUseageTemplate() + " " + arg); Mixin.messageOne(sender, Lang.COMMAND_NO_SUCH_SUB.replaceAll(Lang.COMMAND_REPLACEMENT, this.getUseageTemplate(false).suggest(this, arg)));
if ( ! matches.isEmpty()) if ( ! matches.isEmpty())
{ {
String suggest = Txt.parse(Txt.implodeCommaAnd(matches, "<i>, <c>", " <i>or <c>")); String suggest = Txt.parse(Txt.implodeCommaAnd(matches, "<i>, <c>", " <i>or <c>"));
Mixin.msgOne(sender, Lang.COMMAND_SUGGEST_SUB, this.getUseageTemplate() + " " + suggest); Mixin.messageOne(sender, Lang.COMMAND_SUGGEST_SUB.replaceAll(Lang.COMMAND_REPLACEMENT, this.getUseageTemplate(false).suggest(this, suggest)));
} }
else else
{ {
Mixin.msgOne(sender, Lang.COMMAND_GET_HELP, this.getUseageTemplate()); Mixin.messageOne(sender, Lang.COMMAND_GET_HELP.replaceAll(Lang.COMMAND_REPLACEMENT, this.getUseageTemplate(false).suggest(this)));
} }
} }
@ -852,72 +865,80 @@ public class MassiveCommand
// HELP AND USAGE INFORMATION // HELP AND USAGE INFORMATION
// -------------------------------------------- // // -------------------------------------------- //
public String getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc, boolean onlyFirstAlias, CommandSender sender) public static final Mson USAGE_TEMPLATE_CORE = Mson.mson("/").color(ChatColor.AQUA);
{
StringBuilder ret = new StringBuilder();
public Mson getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc, boolean onlyFirstAlias, CommandSender sender)
{
// Create Ret
Mson ret = USAGE_TEMPLATE_CORE;
List<Mson> extra = new ArrayList<Mson>();
ret = ret.tooltip(Txt.parse("<i>Click to <c>%s<i>", this.getCommandLine()));
// Get commandchain
List<MassiveCommand> commands = new ArrayList<MassiveCommand>(commandChain); List<MassiveCommand> commands = new ArrayList<MassiveCommand>(commandChain);
commands.add(this); commands.add(this);
String commandGoodColor = Txt.parse("<c>"); // Add commands
String commandBadColor = Txt.parse("<bad>");
ret.append(commandGoodColor);
ret.append('/');
boolean first = true; boolean first = true;
Iterator<MassiveCommand> iter = commands.iterator(); for (MassiveCommand command : commands)
while(iter.hasNext())
{ {
MassiveCommand mc = iter.next(); Mson mson = null;
if (sender != null && !mc.isRequirementsMet(sender, false))
{
ret.append(commandBadColor);
}
else
{
ret.append(commandGoodColor);
}
if (first && onlyFirstAlias) if (first && onlyFirstAlias)
{ {
ret.append(mc.getAliases().get(0)); mson = mson(command.getAliases().get(0));
} }
else else
{ {
ret.append(Txt.implode(mc.getAliases(), ",")); mson = mson(Txt.implode(command.getAliases(), ","));
} }
if (iter.hasNext()) if (sender != null && ! command.isRequirementsMet(sender, false))
{ {
ret.append(' '); mson = mson.color(ChatColor.RED);
}
else
{
mson = mson.color(ChatColor.AQUA);
} }
if ( ! first) extra.add(Mson.SPACE);
extra.add(mson);
first = false; first = false;
} }
List<String> args = this.getArgUseagesFor(sender); // Check if last command is parentCommand and make command suggestable/clickable
if (commands.get(commands.size() - 1).isParentCommand())
if (args.size() > 0)
{ {
ret.append(Txt.parse("<p>")); ret = ret.command(this);
ret.append(' '); }
ret.append(Txt.implode(args, " ")); else
{
ret = ret.suggest(this);
} }
// Add args
for (Mson arg : this.getArgUseagesFor(sender))
{
extra.add(Mson.SPACE);
extra.add(arg.color(ChatColor.DARK_AQUA));
}
// Add desc
if (addDesc) if (addDesc)
{ {
ret.append(' '); extra.add(Mson.SPACE);
ret.append(Txt.parse("<i>")); extra.add(mson(this.getDesc()).color(ChatColor.YELLOW));
ret.append(this.getDesc());
} }
return ret.toString(); // Return Ret
return ret.extra(extra);
} }
protected List<String> getArgUseagesFor(CommandSender sender) protected List<Mson> getArgUseagesFor(CommandSender sender)
{ {
List<String> ret = new MassiveList<String>(); List<Mson> ret = new MassiveList<Mson>();
if (this.isUsingNewArgSystem()) if (this.isUsingNewArgSystem())
{ {
for (ArgSetting<?> setting : this.getArgSettings()) for (ArgSetting<?> setting : this.getArgSettings())
@ -929,7 +950,7 @@ public class MassiveCommand
{ {
for (String requiredArg : this.getRequiredArgs()) for (String requiredArg : this.getRequiredArgs())
{ {
ret.add("<" + requiredArg + ">"); ret.add(mson("<" + requiredArg + ">"));
} }
for (Entry<String, String> optionalArg : this.getOptionalArgs().entrySet()) for (Entry<String, String> optionalArg : this.getOptionalArgs().entrySet())
@ -943,29 +964,29 @@ public class MassiveCommand
{ {
val = "=" + val; val = "=" + val;
} }
ret.add("[" + optionalArg.getKey() + val + "]"); ret.add(mson("[" + optionalArg.getKey() + val + "]"));
} }
} }
return ret; return ret;
} }
public String getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc, boolean onlyFirstAlias) public Mson getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc, boolean onlyFirstAlias)
{ {
return getUseageTemplate(commandChain, addDesc, onlyFirstAlias, sender); return getUseageTemplate(commandChain, addDesc, onlyFirstAlias, sender);
} }
public String getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc) public Mson getUseageTemplate(List<MassiveCommand> commandChain, boolean addDesc)
{ {
return getUseageTemplate(commandChain, addDesc, false); return getUseageTemplate(commandChain, addDesc, false);
} }
public String getUseageTemplate(boolean addDesc) public Mson getUseageTemplate(boolean addDesc)
{ {
return getUseageTemplate(this.getCommandChain(), addDesc); return getUseageTemplate(this.getCommandChain(), addDesc);
} }
public String getUseageTemplate() public Mson getUseageTemplate()
{ {
return getUseageTemplate(false); return getUseageTemplate(false);
} }
@ -1015,7 +1036,7 @@ public class MassiveCommand
public List<String> getTabCompletions(List<String> args, CommandSender sender) public List<String> getTabCompletions(List<String> args, CommandSender sender)
{ {
if (args == null) throw new IllegalArgumentException("args was mull"); if (args == null) throw new NullPointerException("args");
if (sender == null) throw new IllegalArgumentException("sender was null"); if (sender == null) throw new IllegalArgumentException("sender was null");
if (args.isEmpty()) throw new IllegalArgumentException("args was empty"); if (args.isEmpty()) throw new IllegalArgumentException("args was empty");

View File

@ -34,7 +34,7 @@ public class MassiveCoreBukkitCommand extends Command implements PluginIdentifia
super( super(
name, name,
massiveCommand.getDesc(), massiveCommand.getDesc(),
massiveCommand.getUseageTemplate(), massiveCommand.getUseageTemplate().toPlain(true),
Collections.<String>emptyList() // We don't use aliases Collections.<String>emptyList() // We don't use aliases
); );
this.massiveCommand = massiveCommand; this.massiveCommand = massiveCommand;

View File

@ -40,6 +40,8 @@ public class Mson implements Serializable
public static final LowercaseEnumAdapter<ChatColor> ADAPTER_LOWERCASE_CHAT_COLOR = LowercaseEnumAdapter.get(ChatColor.class); public static final LowercaseEnumAdapter<ChatColor> ADAPTER_LOWERCASE_CHAT_COLOR = LowercaseEnumAdapter.get(ChatColor.class);
public static final LowercaseEnumAdapter<MsonEventAction> ADAPTER_LOWERCASE_MSON_EVENT_ACTION = LowercaseEnumAdapter.get(MsonEventAction.class); public static final LowercaseEnumAdapter<MsonEventAction> ADAPTER_LOWERCASE_MSON_EVENT_ACTION = LowercaseEnumAdapter.get(MsonEventAction.class);
public static final transient Mson SPACE = mson(" ");
// -------------------------------------------- // // -------------------------------------------- //
// GSON // GSON
// -------------------------------------------- // // -------------------------------------------- //