Skip to content

Commit

Permalink
Merge pull request #13 from iAlexT/master
Browse files Browse the repository at this point in the history
Add support for longs
  • Loading branch information
FixedDev authored Jul 10, 2021
2 parents 887f2ec + dc22920 commit 5e6236b
Show file tree
Hide file tree
Showing 8 changed files with 141 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -161,6 +163,8 @@ private CommandNode<Object> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
@@ -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<? extends Annotation> modifiers) {
Range range = getAnnotation(modifiers, Range.class);

if (range != null) {
return new LongPart(name, range.min(), range.max());
}

return new FloatPart(name);
}

}
26 changes: 26 additions & 0 deletions Universal/src/main/java/me/fixeddev/commandflow/part/Parts.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down Expand Up @@ -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}
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Long> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public interface ArgumentStack {

boolean nextBoolean() throws ArgumentParseException;

long nextLong() throws ArgumentParseException;

void markAsConsumed();

List<String> getBacking();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5e6236b

Please sign in to comment.