diff --git a/Brigadier/src/main/java/me/fixeddev/commandflow/brigadier/CommandBrigadierConverter.java b/Brigadier/src/main/java/me/fixeddev/commandflow/brigadier/CommandBrigadierConverter.java index 957b2888..555f47a6 100644 --- a/Brigadier/src/main/java/me/fixeddev/commandflow/brigadier/CommandBrigadierConverter.java +++ b/Brigadier/src/main/java/me/fixeddev/commandflow/brigadier/CommandBrigadierConverter.java @@ -6,6 +6,7 @@ import com.mojang.brigadier.arguments.BoolArgumentType; import com.mojang.brigadier.arguments.DoubleArgumentType; import com.mojang.brigadier.arguments.IntegerArgumentType; +import com.mojang.brigadier.arguments.LongArgumentType; import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.builder.ArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder; @@ -28,6 +29,7 @@ import me.fixeddev.commandflow.part.defaults.DoublePart; import me.fixeddev.commandflow.part.defaults.FirstMatchPart; import me.fixeddev.commandflow.part.defaults.IntegerPart; +import me.fixeddev.commandflow.part.defaults.LongPart; import me.fixeddev.commandflow.part.defaults.OptionalPart; import me.fixeddev.commandflow.part.defaults.StringPart; import me.fixeddev.commandflow.part.defaults.SubCommandPart; @@ -161,6 +163,8 @@ private CommandNode toArgumentBuilder(ArgumentPart part) { return RequiredArgumentBuilder.argument(part.getName(), IntegerArgumentType.integer()).build(); } else if (part instanceof DoublePart) { return RequiredArgumentBuilder.argument(part.getName(), DoubleArgumentType.doubleArg()).build(); + } else if (part instanceof LongPart) { + return RequiredArgumentBuilder.argument(part.getName(), LongArgumentType.longArg()).build(); } else { if (part instanceof StringPart) { StringPart stringPart = (StringPart) part; diff --git a/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/DefaultsModule.java b/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/DefaultsModule.java index bbbf1f2d..c420e54c 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/DefaultsModule.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/DefaultsModule.java @@ -12,6 +12,7 @@ import me.fixeddev.commandflow.annotated.part.defaults.factory.BooleanPartFactory; import me.fixeddev.commandflow.annotated.part.defaults.factory.ContextFactory; import me.fixeddev.commandflow.annotated.part.defaults.factory.DoublePartFactory; +import me.fixeddev.commandflow.annotated.part.defaults.factory.LongPartFactory; import me.fixeddev.commandflow.annotated.part.defaults.factory.SwitchPartFactory; import me.fixeddev.commandflow.annotated.part.defaults.factory.FloatPartFactory; import me.fixeddev.commandflow.annotated.part.defaults.factory.IntegerPartFactory; @@ -41,6 +42,10 @@ public void configure() { bindFactory(int.class, partFactory); bindFactory(Integer.class, partFactory); + LongPartFactory longPartFactory = new LongPartFactory(); + bindFactory(long.class, longPartFactory); + bindFactory(Long.class, longPartFactory); + bindFactory(String.class, new StringPartFactory()); bindFactory(new Key(String.class, Text.class), new StringTextPartFactory()); diff --git a/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/factory/LongPartFactory.java b/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/factory/LongPartFactory.java new file mode 100644 index 00000000..cafcfffe --- /dev/null +++ b/Universal/src/main/java/me/fixeddev/commandflow/annotated/part/defaults/factory/LongPartFactory.java @@ -0,0 +1,25 @@ +package me.fixeddev.commandflow.annotated.part.defaults.factory; + +import me.fixeddev.commandflow.annotated.annotation.Range; +import me.fixeddev.commandflow.annotated.part.PartFactory; +import me.fixeddev.commandflow.part.CommandPart; +import me.fixeddev.commandflow.part.defaults.FloatPart; +import me.fixeddev.commandflow.part.defaults.LongPart; + +import java.lang.annotation.Annotation; +import java.util.List; + +public class LongPartFactory implements PartFactory { + + @Override + public CommandPart createPart(String name, List modifiers) { + Range range = getAnnotation(modifiers, Range.class); + + if (range != null) { + return new LongPart(name, range.min(), range.max()); + } + + return new FloatPart(name); + } + +} diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/Parts.java b/Universal/src/main/java/me/fixeddev/commandflow/part/Parts.java index bc730c02..0dd8cc7f 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/part/Parts.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/Parts.java @@ -6,6 +6,7 @@ import me.fixeddev.commandflow.part.defaults.DoublePart; import me.fixeddev.commandflow.part.defaults.EnumPart; import me.fixeddev.commandflow.part.defaults.FirstMatchPart; +import me.fixeddev.commandflow.part.defaults.LongPart; import me.fixeddev.commandflow.part.defaults.SwitchPart; import me.fixeddev.commandflow.part.defaults.FloatPart; import me.fixeddev.commandflow.part.defaults.IntegerPart; @@ -22,6 +23,7 @@ import java.util.Collection; import java.util.List; +// TODO: Fix some unknown references in some methods of this class. /** * An utility class to ease the use of {@link CommandPart} and his sub classes. */ @@ -136,6 +138,30 @@ public static CommandPart string(String name) { return new StringPart(name); } + /** + * Returns a non-ranged {@link LongPart} with the + * given {@code name}. + * + * @param name The name for the part. + * @return A {@link LongPart} with the provided {@code name}. + */ + public static CommandPart longPart(String name) { + return new LongPart(name); + } + + /** + * Returns a ranged {@link LongPart}, with the given + * {@code name}, {@code min} and {@code max} range. + * + * @param name The name for the part. + * @param min The minimum range for the part. + * @param max The maximum range for the part. + * @return A {@link LongPart} with the given {@code name}, {@code min} and {@code max} range. + */ + public static CommandPart longPart(String name, long min, long max) { + return new LongPart(name, min, max); + } + /** * A basic {@link CommandPart} that takes a string from the {@link me.fixeddev.commandflow.stack.ArgumentStack} * diff --git a/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/LongPart.java b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/LongPart.java new file mode 100644 index 00000000..eb13fb28 --- /dev/null +++ b/Universal/src/main/java/me/fixeddev/commandflow/part/defaults/LongPart.java @@ -0,0 +1,62 @@ +package me.fixeddev.commandflow.part.defaults; + +import me.fixeddev.commandflow.CommandContext; +import me.fixeddev.commandflow.exception.ArgumentParseException; +import me.fixeddev.commandflow.part.CommandPart; +import me.fixeddev.commandflow.stack.ArgumentStack; +import net.kyori.text.TranslatableComponent; + +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.List; + +import static net.kyori.text.TextComponent.of; + +public class LongPart extends PrimitivePart { + + private final long max; + private final long min; + private final boolean ranged; + + /** + * Creates a PrimitivePart instance with the given name. + * @param name The name for this part. + */ + private LongPart(String name, long min, long max, boolean ranged) { + super(name); + this.min = min; + this.max = max; + this.ranged = ranged; + } + + public LongPart(String name, long min, long max) { + this(name, min, max, true); + } + + public LongPart(String name) { + this(name, 0L, 0L, false); + } + + @Override + public List parseValue(CommandContext context, ArgumentStack stack, CommandPart caller) throws ArgumentParseException { + long next = stack.nextLong(); + + if (ranged && (next > max || next < min)) { + throw new ArgumentParseException( + TranslatableComponent.of( + "number.out-range", + of(next), + of(min), + of(max) + ) + ); + } + + return Collections.singletonList(next); + } + + @Override + public Type getType() { + return long.class; + } +} diff --git a/Universal/src/main/java/me/fixeddev/commandflow/stack/ArgumentStack.java b/Universal/src/main/java/me/fixeddev/commandflow/stack/ArgumentStack.java index 6a991a3d..5c8cb0db 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/stack/ArgumentStack.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/stack/ArgumentStack.java @@ -34,6 +34,8 @@ public interface ArgumentStack { boolean nextBoolean() throws ArgumentParseException; + long nextLong() throws ArgumentParseException; + void markAsConsumed(); List getBacking(); diff --git a/Universal/src/main/java/me/fixeddev/commandflow/stack/SimpleArgumentStack.java b/Universal/src/main/java/me/fixeddev/commandflow/stack/SimpleArgumentStack.java index 2e910672..065bd22f 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/stack/SimpleArgumentStack.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/stack/SimpleArgumentStack.java @@ -243,6 +243,22 @@ public boolean nextBoolean() throws ArgumentParseException { return Boolean.parseBoolean(next); } + @Override + public long nextLong() throws ArgumentParseException { + String next = next(); + + try { + return Long.parseLong(next); + } catch (NumberFormatException e) { + throw new ArgumentParseException( + TranslatableComponent.of( + "invalid.long", + TextComponent.of(next) + ) + ); + } + } + @Override public void markAsConsumed() { int oldPosition = position; diff --git a/Universal/src/main/java/me/fixeddev/commandflow/translator/DefaultMapTranslationProvider.java b/Universal/src/main/java/me/fixeddev/commandflow/translator/DefaultMapTranslationProvider.java index 1a35ecdf..1a155053 100644 --- a/Universal/src/main/java/me/fixeddev/commandflow/translator/DefaultMapTranslationProvider.java +++ b/Universal/src/main/java/me/fixeddev/commandflow/translator/DefaultMapTranslationProvider.java @@ -21,6 +21,7 @@ public DefaultMapTranslationProvider() { translations.put("invalid.double", "The number %s is not a valid double!"); translations.put("invalid.boolean", "The string %s is not a valid boolean!"); translations.put("invalid.enum-value", "The value %s is not valid, the valid values are: %s"); + translations.put("invalid.long", "The number %s is not a valid long!"); } public String getTranslation(String key) {