update arguments to reflect new color options

This commit is contained in:
Kieran Wallbanks 2021-03-04 14:03:53 +00:00
parent 8b82d61c0c
commit e3d135e1f2
6 changed files with 148 additions and 21 deletions

View File

@ -107,11 +107,12 @@ public class ArgumentType {
// Minecraft specific arguments
/**
* @deprecated Use {@link #TextColor} for colors and {@link #TextDecoration} for styles.
* @deprecated Use {@link ArgumentTextColor} for colors, {@link ArgumentTextDecoration} for styles, {@link ArgumentColor} for raw colors,
* {@link ArgumentDyeColor} for dye colors and {@link ArgumentTeamFormat} for team formats
*/
@Deprecated
public static ArgumentColor Color(@NotNull String id) {
return new ArgumentColor(id);
public static ArgumentChatColor ChatColor(@NotNull String id) {
return new ArgumentChatColor(id);
}
public static ArgumentTextColor TextColor(@NotNull String id) {
@ -122,6 +123,18 @@ public class ArgumentType {
return new ArgumentTextDecoration(id);
}
public static ArgumentColor Color(@NotNull String id) {
return new ArgumentColor(id);
}
public static ArgumentDyeColor DyeColor(@NotNull String id) {
return new ArgumentDyeColor(id);
}
public static ArgumentTeamFormat TeamFormat(@NotNull String id) {
return new ArgumentTeamFormat(id);
}
/**
* @see ArgumentTime
*/

View File

@ -0,0 +1,43 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.chat.ChatColor;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import org.jetbrains.annotations.NotNull;
/**
* Represents an argument which will give you a {@link ChatColor}.
* <p>
* Example: red, white, reset
* @deprecated Use {@link ArgumentTextColor} for colors, {@link ArgumentTextDecoration} for styles, {@link ArgumentColor} for raw colors,
* {@link ArgumentDyeColor} for dye colors and {@link ArgumentTeamFormat} for team formats
*/
@Deprecated
public class ArgumentChatColor extends Argument<ChatColor> {
public static final int UNDEFINED_COLOR = -2;
public ArgumentChatColor(String id) {
super(id);
}
@NotNull
@Override
public ChatColor parse(@NotNull String input) throws ArgumentSyntaxException {
final ChatColor color = ChatColor.fromName(input);
if (color == ChatColor.NO_COLOR)
throw new ArgumentSyntaxException("Undefined color", input, UNDEFINED_COLOR);
return color;
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = "minecraft:chat_color";
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
}

View File

@ -1,6 +1,7 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.chat.ChatColor;
import net.kyori.adventure.text.format.NamedTextColor;
import net.minestom.server.color.Color;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
@ -8,28 +9,26 @@ import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import org.jetbrains.annotations.NotNull;
/**
* Represents an argument which will give you a {@link ChatColor}.
* <p>
* Example: red, white, reset
* @deprecated Use {@link ArgumentTextColor} for colors and {@link ArgumentTextDecoration} for styles.
* Represents an argument that will give you a {@link Color}. Input is parsed
* first as a hex string ({@code #int}), then as a CSS hex string ({@code #rrggbb} or
* {@code #rgb}), then as an integer and finally as a named text colour. The values for
* the named text colours can be found in {@link NamedTextColor}.
* <br><br>
* This class is essentially a wrapper around {@link ArgumentTextColor}.
*/
@Deprecated
public class ArgumentColor extends Argument<ChatColor> {
public class ArgumentColor extends Argument<Color> {
private final ArgumentTextColor argumentTextColor;
public static final int UNDEFINED_COLOR = -2;
public static int UNDEFINED_COLOR = ArgumentTextColor.UNDEFINED_COLOR;
public ArgumentColor(String id) {
public ArgumentColor(@NotNull String id) {
super(id);
argumentTextColor = new ArgumentTextColor(id);
}
@NotNull
@Override
public ChatColor parse(@NotNull String input) throws ArgumentSyntaxException {
final ChatColor color = ChatColor.fromName(input);
if (color == ChatColor.NO_COLOR)
throw new ArgumentSyntaxException("Undefined color", input, UNDEFINED_COLOR);
return color;
public @NotNull Color parse(@NotNull String input) throws ArgumentSyntaxException {
return new Color(this.argumentTextColor.parse(input));
}
@Override

View File

@ -0,0 +1,32 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.color.DyeColor;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import org.jetbrains.annotations.NotNull;
/**
* An argument that returns a {@link DyeColor} from the name of the dye color.
*/
public class ArgumentDyeColor extends Argument<DyeColor> {
public static int UNDEFINED_DYE_COLOR = -2;
public ArgumentDyeColor(@NotNull String id) {
super(id);
}
@Override
public @NotNull DyeColor parse(@NotNull String input) throws ArgumentSyntaxException {
try {
return DyeColor.valueOf(input.toUpperCase().replace(' ', '_').trim());
} catch (IllegalArgumentException ignored) {
throw new ArgumentSyntaxException("Undefined dye color", input, UNDEFINED_DYE_COLOR);
}
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
}
}

View File

@ -0,0 +1,40 @@
package net.minestom.server.command.builder.arguments.minecraft;
import net.minestom.server.color.TeamFormat;
import net.minestom.server.command.builder.NodeMaker;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
import org.jetbrains.annotations.NotNull;
/**
* An argument that will give you a {@link TeamFormat} from it's name or the int code.
*/
public class ArgumentTeamFormat extends Argument<TeamFormat> {
public static final int UNDEFINED_TEAM_FORMAT = -2;
public ArgumentTeamFormat(@NotNull String id) {
super(id);
}
@Override
public @NotNull TeamFormat parse(@NotNull String input) throws ArgumentSyntaxException {
try {
return TeamFormat.valueOf(input.toUpperCase().trim().replace(' ', '_'));
} catch (IllegalArgumentException ignored) {
try {
return TeamFormat.values()[Integer.parseInt(input)];
} catch (NumberFormatException | ArrayIndexOutOfBoundsException alsoIgnored) {
throw new ArgumentSyntaxException("Undefined team format!", input, UNDEFINED_TEAM_FORMAT);
}
}
}
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = "minecraft:team_format";
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}
}

View File

@ -25,7 +25,7 @@ public class ArgumentTextColor extends Argument<TextColor> {
@Override
public @NotNull TextColor parse(@NotNull String input) throws ArgumentSyntaxException {
TextColor textColor = null;
TextColor textColor;
// first try standard hex
textColor = TextColor.fromHexString(input);
@ -58,7 +58,7 @@ public class ArgumentTextColor extends Argument<TextColor> {
@Override
public void processNodes(@NotNull NodeMaker nodeMaker, boolean executable) {
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(this, executable, false, false);
argumentNode.parser = "minecraft:text_decoration";
argumentNode.parser = "minecraft:text_color";
nodeMaker.addNodes(new DeclareCommandsPacket.Node[]{argumentNode});
}