Implement Chat Formatting per platform

This commit is contained in:
Aikar
2017-06-27 00:24:05 -04:00
parent 2e493fcb49
commit cd109eb266
17 changed files with 250 additions and 45 deletions
@@ -26,15 +26,16 @@ package co.aikar.commands;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.text.serializer.TextSerializers;
public class SpongeCommandIssuer implements CommandIssuer {
private final SpongeCommandManager manager;
private final CommandSource source;
SpongeCommandIssuer(final CommandSource source) {
SpongeCommandIssuer(SpongeCommandManager manager, final CommandSource source) {
this.manager = manager;
this.source = source;
}
@@ -51,14 +52,8 @@ public class SpongeCommandIssuer implements CommandIssuer {
@Override
public void sendMessage(MessageType type, String message) {
switch (type) {
case ERROR:
case SYNTAX:
this.source.sendMessage(Text.of(TextColors.RED, TextSerializers.LEGACY_FORMATTING_CODE.stripCodes(message)));
break;
default:
this.source.sendMessage(Text.of(TextColors.YELLOW, TextSerializers.LEGACY_FORMATTING_CODE.stripCodes(message)));
}
message = format(manager, type, message);
this.source.sendMessage(TextSerializers.LEGACY_FORMATTING_CODE.deserialize(message));
}
@Override
@@ -30,6 +30,7 @@ import org.slf4j.Logger;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.text.format.TextColors;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
@@ -49,6 +50,10 @@ public class SpongeCommandManager extends CommandManager {
public SpongeCommandManager(PluginContainer plugin) {
this.plugin = plugin;
this.commandTiming = Timings.of(plugin, "Commands");
this.formatters.put(MessageType.ERROR, new SpongeMessageFormatter(TextColors.RED, TextColors.YELLOW, TextColors.RED));
this.formatters.put(MessageType.SYNTAX, new SpongeMessageFormatter(TextColors.YELLOW, TextColors.GREEN, TextColors.WHITE));
this.formatters.put(MessageType.INFO, new SpongeMessageFormatter(TextColors.BLUE, TextColors.DARK_GREEN, TextColors.GREEN));
}
@Override
@@ -106,11 +111,12 @@ public class SpongeCommandManager extends CommandManager {
if (!(issuer instanceof CommandSource)) {
throw new IllegalArgumentException(issuer.getClass().getName() + " is not a Command Issuer.");
}
return new SpongeCommandIssuer((CommandSource) issuer);
return new SpongeCommandIssuer(this, (CommandSource) issuer);
}
@Override
public <R extends CommandExecutionContext> R createCommandContext(RegisteredCommand command, Parameter parameter, CommandIssuer sender, List<String> args, int i, Map<String, Object> passedArgs) {
//noinspection unchecked
return (R) new SpongeCommandExecutionContext(command, parameter, sender, args, i, passedArgs);
}
@@ -0,0 +1,42 @@
package co.aikar.commands;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColor;
import org.spongepowered.api.text.serializer.TextSerializers;
public class SpongeMessageFormatter implements MessageFormatter {
private final TextColor color1;
private final TextColor color2;
private final TextColor color3;
public SpongeMessageFormatter(TextColor color1) {
this(color1, color1);
}
public SpongeMessageFormatter(TextColor color1, TextColor color2) {
this(color1, color2, color2);
}
public SpongeMessageFormatter(TextColor color1, TextColor color2, TextColor color3) {
this.color1 = color1;
this.color2 = color2;
this.color3 = color3;
}
@Override
public String c1(String message) {
return convert(color1, message);
}
@Override
public String c2(String message) {
return convert(color2, message);
}
@Override
public String c3(String message) {
return convert(color3, message);
}
private String convert(TextColor color, String message) {
return TextSerializers.LEGACY_FORMATTING_CODE.serialize(Text.of(color, message));
}
}
@@ -58,7 +58,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
@Override
public CommandResult process(CommandSource source, String arguments) throws CommandException {
if(this.execute(new SpongeCommandIssuer(source), this.name, arguments.split(" "))) {
if(this.execute(new SpongeCommandIssuer(manager, source), this.name, arguments.split(" "))) {
return CommandResult.success();
}
return CommandResult.empty();
@@ -66,7 +66,7 @@ public class SpongeRootCommand implements CommandCallable, RootCommand {
@Override
public List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> location) throws CommandException {
return tabComplete(new SpongeCommandIssuer(source), this.name, arguments.split(" "));
return tabComplete(new SpongeCommandIssuer(manager, source), this.name, arguments.split(" "));
}
@Override