mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-06 00:17:58 +01:00
Added ArgumentIntRange & ArgumentFloatRange
This commit is contained in:
parent
e4ad66fcde
commit
17d638d460
@ -5,6 +5,8 @@ import net.minestom.server.command.builder.CommandDispatcher;
|
||||
import net.minestom.server.command.builder.CommandSyntax;
|
||||
import net.minestom.server.command.builder.arguments.*;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentColor;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentFloatRange;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentIntRange;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentTime;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEnchantment;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType;
|
||||
@ -333,7 +335,7 @@ public class CommandManager {
|
||||
List<DeclareCommandsPacket.Node> nodes = new ArrayList<>();
|
||||
|
||||
/*DeclareCommandsPacket.Node testNode = simpleArgumentNode(nodes, argument, executable);
|
||||
testNode.parser = "minecraft:entity_summon";
|
||||
testNode.parser = "minecraft:range";
|
||||
|
||||
if (true) {
|
||||
return nodes;
|
||||
@ -440,6 +442,12 @@ public class CommandManager {
|
||||
} else if (argument instanceof ArgumentEntityType) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable);
|
||||
argumentNode.parser = "minecraft:entity_summon";
|
||||
} else if (argument instanceof ArgumentIntRange) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable);
|
||||
argumentNode.parser = "minecraft:int_range";
|
||||
} else if (argument instanceof ArgumentFloatRange) {
|
||||
DeclareCommandsPacket.Node argumentNode = simpleArgumentNode(nodes, argument, executable);
|
||||
argumentNode.parser = "minecraft:float_range";
|
||||
}
|
||||
|
||||
return nodes;
|
||||
|
@ -6,6 +6,8 @@ import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.item.Enchantment;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import net.minestom.server.potion.PotionType;
|
||||
import net.minestom.server.utils.math.FloatRange;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import net.minestom.server.utils.time.UpdateOption;
|
||||
|
||||
import java.util.HashMap;
|
||||
@ -75,6 +77,14 @@ public class Arguments {
|
||||
return (EntityType) getObject(id);
|
||||
}
|
||||
|
||||
public IntRange getIntRange(String id) {
|
||||
return (IntRange) getObject(id);
|
||||
}
|
||||
|
||||
public FloatRange getFloatRange(String id) {
|
||||
return (FloatRange) getObject(id);
|
||||
}
|
||||
|
||||
public Object getObject(String id) {
|
||||
return args.getOrDefault(id, null);
|
||||
}
|
||||
|
@ -1,6 +1,8 @@
|
||||
package net.minestom.server.command.builder.arguments;
|
||||
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentColor;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentFloatRange;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentIntRange;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentTime;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEnchantment;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.registry.ArgumentEntityType;
|
||||
@ -75,4 +77,12 @@ public class ArgumentType {
|
||||
return new ArgumentEntityType(id);
|
||||
}
|
||||
|
||||
public static ArgumentIntRange IntRange(String id) {
|
||||
return new ArgumentIntRange(id);
|
||||
}
|
||||
|
||||
public static ArgumentFloatRange FloatRange(String id) {
|
||||
return new ArgumentFloatRange(id);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,54 @@
|
||||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.utils.math.FloatRange;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ArgumentFloatRange extends Argument<FloatRange> {
|
||||
|
||||
public static final int FORMAT_ERROR = -1;
|
||||
|
||||
public ArgumentFloatRange(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCorrectionResult(String value) {
|
||||
try {
|
||||
Float.valueOf(value);
|
||||
return SUCCESS; // Is a single number
|
||||
} catch (NumberFormatException e) {
|
||||
String[] split = value.split(Pattern.quote(".."));
|
||||
if (split.length == 2) {
|
||||
try {
|
||||
Float.valueOf(split[0]); // min
|
||||
Float.valueOf(split[1]); // max
|
||||
return SUCCESS;
|
||||
} catch (NumberFormatException e2) {
|
||||
return FORMAT_ERROR;
|
||||
}
|
||||
} else {
|
||||
return FORMAT_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public FloatRange parse(String value) {
|
||||
if (value.contains("..")) {
|
||||
String[] split = value.split(Pattern.quote(".."));
|
||||
final float min = Float.valueOf(split[0]);
|
||||
final float max = Float.valueOf(split[1]);
|
||||
return new FloatRange(min, max);
|
||||
} else {
|
||||
final float number = Float.valueOf(value);
|
||||
return new FloatRange(number, number);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConditionResult(FloatRange value) {
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class ArgumentIntRange extends Argument<IntRange> {
|
||||
|
||||
public static final int FORMAT_ERROR = -1;
|
||||
|
||||
public ArgumentIntRange(String id) {
|
||||
super(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getCorrectionResult(String value) {
|
||||
try {
|
||||
Integer.valueOf(value);
|
||||
return SUCCESS; // Is a single number
|
||||
} catch (NumberFormatException e) {
|
||||
String[] split = value.split(Pattern.quote(".."));
|
||||
if (split.length == 2) {
|
||||
try {
|
||||
Integer.valueOf(split[0]); // min
|
||||
Integer.valueOf(split[1]); // max
|
||||
return SUCCESS;
|
||||
} catch (NumberFormatException e2) {
|
||||
return FORMAT_ERROR;
|
||||
}
|
||||
} else {
|
||||
return FORMAT_ERROR;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public IntRange parse(String value) {
|
||||
if (value.contains("..")) {
|
||||
String[] split = value.split(Pattern.quote(".."));
|
||||
final int min = Integer.valueOf(split[0]);
|
||||
final int max = Integer.valueOf(split[1]);
|
||||
return new IntRange(min, max);
|
||||
} else {
|
||||
final int number = Integer.valueOf(value);
|
||||
return new IntRange(number, number);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getConditionResult(IntRange value) {
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
@ -1,34 +0,0 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
public class GroupedCollections<E> implements Iterable<E> {
|
||||
|
||||
private Collection<Collection<E>> collections;
|
||||
|
||||
public GroupedCollections(Collection<Collection<E>> collection) {
|
||||
this.collections = collection;
|
||||
}
|
||||
|
||||
public int size() {
|
||||
return collections.stream().mapToInt(es -> es.size()).sum();
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return collections.stream().allMatch(es -> es.isEmpty());
|
||||
}
|
||||
|
||||
public boolean contains(Object o) {
|
||||
return collections.stream().anyMatch(es -> es.contains(o));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return collections.stream().flatMap(Collection::stream).iterator();
|
||||
}
|
||||
|
||||
public void addCollection(Collection<E> list) {
|
||||
this.collections.add(list);
|
||||
}
|
||||
}
|
@ -1,13 +0,0 @@
|
||||
package net.minestom.server.utils;
|
||||
|
||||
public class HexUtils {
|
||||
|
||||
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
public static char byteToHex(byte b) {
|
||||
int v = b & 0xFF;
|
||||
char hexChar = HEX_ARRAY[v >>> 4];
|
||||
return hexChar;
|
||||
}
|
||||
|
||||
}
|
28
src/main/java/net/minestom/server/utils/math/FloatRange.java
Normal file
28
src/main/java/net/minestom/server/utils/math/FloatRange.java
Normal file
@ -0,0 +1,28 @@
|
||||
package net.minestom.server.utils.math;
|
||||
|
||||
public class FloatRange {
|
||||
|
||||
private float min, max;
|
||||
|
||||
public FloatRange(float min, float max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public float getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public void setMin(float min) {
|
||||
this.min = min;
|
||||
}
|
||||
|
||||
public float getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void setMax(float max) {
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
}
|
27
src/main/java/net/minestom/server/utils/math/IntRange.java
Normal file
27
src/main/java/net/minestom/server/utils/math/IntRange.java
Normal file
@ -0,0 +1,27 @@
|
||||
package net.minestom.server.utils.math;
|
||||
|
||||
public class IntRange {
|
||||
|
||||
private int min, max;
|
||||
|
||||
public IntRange(int min, int max) {
|
||||
this.min = min;
|
||||
this.max = max;
|
||||
}
|
||||
|
||||
public int getMin() {
|
||||
return min;
|
||||
}
|
||||
|
||||
public void setMin(int min) {
|
||||
this.min = min;
|
||||
}
|
||||
|
||||
public int getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
public void setMax(int max) {
|
||||
this.max = max;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user