mirror of
https://github.com/Minestom/Minestom.git
synced 2024-12-27 11:38:03 +01:00
WIP entity selector argument
This commit is contained in:
parent
2c2a691af7
commit
8f99753235
@ -2,13 +2,13 @@ package net.minestom.server.command.builder;
|
||||
|
||||
import net.minestom.server.chat.ChatColor;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.instance.block.Block;
|
||||
import net.minestom.server.item.Enchantment;
|
||||
import net.minestom.server.item.ItemStack;
|
||||
import net.minestom.server.particle.Particle;
|
||||
import net.minestom.server.potion.PotionEffect;
|
||||
import net.minestom.server.utils.entity.EntityFinder;
|
||||
import net.minestom.server.utils.location.RelativeBlockPosition;
|
||||
import net.minestom.server.utils.location.RelativeVec;
|
||||
import net.minestom.server.utils.math.FloatRange;
|
||||
@ -20,7 +20,6 @@ import org.jglrxavpok.hephaistos.nbt.NBT;
|
||||
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@ -122,8 +121,8 @@ public final class Arguments {
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<Entity> getEntities(@NotNull String id) {
|
||||
return (List<Entity>) getObject(id);
|
||||
public EntityFinder getEntities(@NotNull String id) {
|
||||
return (EntityFinder) getObject(id);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
|
@ -100,7 +100,6 @@ public class ArgumentType {
|
||||
return new ArgumentFloatRange(id);
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public static ArgumentEntities Entities(@NotNull String id) {
|
||||
return new ArgumentEntities(id);
|
||||
}
|
||||
|
@ -2,8 +2,13 @@ package net.minestom.server.command.builder.arguments.minecraft;
|
||||
|
||||
import net.minestom.server.MinecraftServer;
|
||||
import net.minestom.server.command.builder.arguments.Argument;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||
import net.minestom.server.entity.EntityType;
|
||||
import net.minestom.server.entity.GameMode;
|
||||
import net.minestom.server.network.ConnectionManager;
|
||||
import net.minestom.server.registry.Registries;
|
||||
import net.minestom.server.utils.entity.EntityFinder;
|
||||
import net.minestom.server.utils.math.IntRange;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
@ -15,21 +20,30 @@ import java.util.List;
|
||||
* Represents the target selector argument.
|
||||
* https://minecraft.gamepedia.com/Commands#Target_selectors
|
||||
*/
|
||||
public class ArgumentEntities extends Argument<List<Entity>> {
|
||||
public class ArgumentEntities extends Argument<EntityFinder> {
|
||||
|
||||
private static final int SUCCESS = 0;
|
||||
|
||||
public static final int INVALID_SYNTAX = -2;
|
||||
public static final int ONLY_SINGLE_ENTITY_ERROR = -3;
|
||||
public static final int ONLY_PLAYERS_ERROR = -4;
|
||||
public static final int INVALID_ARGUMENT_NAME = -5;
|
||||
public static final int INVALID_ARGUMENT_VALUE = -6;
|
||||
private static final ConnectionManager CONNECTION_MANAGER = MinecraftServer.getConnectionManager();
|
||||
private static final List<String> selectorVariables = Arrays.asList("@p", "@r", "@a", "@e", "@s");
|
||||
private static final List<String> playersOnlySelector = Arrays.asList("@p", "@r", "@a", "@s");
|
||||
private static final List<String> singleOnlySelector = Arrays.asList("@p", "@r", "@s");
|
||||
// List with all the valid arguments
|
||||
private static final List<String> validArguments = Arrays.asList("x", "y", "z",
|
||||
"distance", "dx", "dy", "dz",
|
||||
"scores", "tag", "team", "limit", "sort", "level", "gamemode", "name",
|
||||
"scores", "tag", "team", "limit", "sort", "level", "gamemode",
|
||||
"x_rotation", "y_rotation", "type", "nbt", "advancements", "predicate");
|
||||
|
||||
// List with all the easily parsable arguments which only require reading until a specific character (comma)
|
||||
private static final List<String> simpleArguments = Arrays.asList("x", "y", "z",
|
||||
"distance", "dx", "dy", "dz",
|
||||
"scores", "tag", "team", "limit", "sort", "level", "gamemode", "name",
|
||||
"x_rotation", "y_rotation", "type", "advancements", "predicate");
|
||||
private boolean onlySingleEntity;
|
||||
private boolean onlyPlayers;
|
||||
|
||||
@ -47,78 +61,164 @@ public class ArgumentEntities extends Argument<List<Entity>> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getCorrectionResult(@NotNull String value) {
|
||||
System.out.println("check: " + value);
|
||||
@NotNull
|
||||
@Override
|
||||
public EntityFinder parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
System.out.println("check: " + input);
|
||||
|
||||
// Check for raw player name
|
||||
if (value.length() <= 16) {
|
||||
if (CONNECTION_MANAGER.getPlayer(value) != null)
|
||||
return SUCCESS;
|
||||
if (input.length() <= 16) {
|
||||
if (CONNECTION_MANAGER.getPlayer(input) != null) {
|
||||
// TODO success
|
||||
}
|
||||
}
|
||||
|
||||
// The minimum size is always 0 (for the selector variable, ex: @p)
|
||||
if (value.length() < 2)
|
||||
return INVALID_SYNTAX;
|
||||
if (input.length() < 2)
|
||||
throw new ArgumentSyntaxException("Length needs to be > 1", input, INVALID_SYNTAX);
|
||||
|
||||
// The target selector variable always start by '@'
|
||||
if (!value.startsWith("@"))
|
||||
return INVALID_SYNTAX;
|
||||
if (!input.startsWith("@"))
|
||||
throw new ArgumentSyntaxException("Target selector needs to start with @", input, INVALID_SYNTAX);
|
||||
|
||||
final String selectorVariable = value.substring(0, 2);
|
||||
final String selectorVariable = input.substring(0, 2);
|
||||
|
||||
// Check if the selector variable used exists
|
||||
if (!selectorVariables.contains(selectorVariable))
|
||||
return INVALID_SYNTAX;
|
||||
throw new ArgumentSyntaxException("Invalid selector variable", input, INVALID_SYNTAX);
|
||||
|
||||
// Check if it should only select single entity and if the selector variable valid the condition
|
||||
if (onlySingleEntity && !singleOnlySelector.contains(selectorVariable))
|
||||
return ONLY_SINGLE_ENTITY_ERROR;
|
||||
throw new ArgumentSyntaxException("Argument requires only a single entity", input, ONLY_SINGLE_ENTITY_ERROR);
|
||||
|
||||
// Check if it should only select players and if the selector variable valid the condition
|
||||
if (onlyPlayers && !playersOnlySelector.contains(selectorVariable))
|
||||
return ONLY_PLAYERS_ERROR;
|
||||
throw new ArgumentSyntaxException("Argument requires only players", input, ONLY_PLAYERS_ERROR);
|
||||
|
||||
// Create the EntityFinder which will be used for the rest of the parsing
|
||||
final EntityFinder entityFinder = new EntityFinder()
|
||||
.setTargetSelector(toTargetSelector(selectorVariable));
|
||||
|
||||
// The selector is a single selector variable which verify all the conditions
|
||||
if (value.length() == 2)
|
||||
return SUCCESS;
|
||||
if (input.length() == 2)
|
||||
return entityFinder;
|
||||
|
||||
// START PARSING THE STRUCTURE
|
||||
final String structure = value.substring(2);
|
||||
final String structure = input.substring(2);
|
||||
return parseStructure(input, entityFinder, structure);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private EntityFinder parseStructure(@NotNull String input,
|
||||
@NotNull EntityFinder entityFinder,
|
||||
@NotNull String structure) throws ArgumentSyntaxException {
|
||||
// The structure isn't opened or closed properly
|
||||
if (!structure.startsWith("[") || !structure.endsWith("]"))
|
||||
return INVALID_SYNTAX;
|
||||
throw new ArgumentSyntaxException("Target selector needs to start and end with brackets", input, INVALID_SYNTAX);
|
||||
|
||||
// Remove brackets
|
||||
final String structureData = structure.substring(1, structure.length() - 1);
|
||||
System.out.println("structure data: " + structureData);
|
||||
|
||||
String currentArgument = "";
|
||||
for (int i = 0; i < structureData.length(); i++) {
|
||||
final char c = structureData.charAt(i);
|
||||
if (c == '=') {
|
||||
i = retrieveArgument(structureData, currentArgument, i);
|
||||
System.out.println("type: " + currentArgument);
|
||||
|
||||
if (!validArguments.contains(currentArgument))
|
||||
throw new ArgumentSyntaxException("Argument name '" + currentArgument + "' does not exist", input, INVALID_ARGUMENT_NAME);
|
||||
|
||||
i = parseArgument(entityFinder, currentArgument, input, structureData, i);
|
||||
currentArgument = ""; // Reset current argument
|
||||
} else {
|
||||
currentArgument += c;
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return entityFinder;
|
||||
}
|
||||
|
||||
private int retrieveArgument(String structureData, String argument, int index) {
|
||||
int finalIndex = index;
|
||||
for (int i = index + 1; i < structureData.length(); i++) {
|
||||
System.out.println("char: " + structureData.charAt(i));
|
||||
System.out.println("retrieve: " + argument);
|
||||
private int parseArgument(@NotNull EntityFinder entityFinder,
|
||||
@NotNull String argumentName,
|
||||
@NotNull String input,
|
||||
@NotNull String structureData, int beginIndex) throws ArgumentSyntaxException {
|
||||
final char comma = ',';
|
||||
final boolean isSimple = simpleArguments.contains(argumentName);
|
||||
|
||||
int finalIndex = beginIndex + 1;
|
||||
StringBuilder valueBuilder = new StringBuilder();
|
||||
for (; finalIndex < structureData.length(); finalIndex++) {
|
||||
final char c = structureData.charAt(finalIndex);
|
||||
|
||||
// Command is parsed
|
||||
if (isSimple && c == comma)
|
||||
break;
|
||||
|
||||
valueBuilder.append(c);
|
||||
}
|
||||
|
||||
final String value = valueBuilder.toString().trim();
|
||||
|
||||
//System.out.println("value: " + value);
|
||||
switch (argumentName) {
|
||||
case "type": {
|
||||
final boolean include = !value.startsWith("!");
|
||||
final String entityName = include ? value : value.substring(1);
|
||||
final EntityType entityType = Registries.getEntityType(entityName);
|
||||
if (entityType == null)
|
||||
throw new ArgumentSyntaxException("Invalid entity name", input, INVALID_ARGUMENT_VALUE);
|
||||
entityFinder.setEntity(entityType, include ? EntityFinder.ToggleableType.INCLUDE : EntityFinder.ToggleableType.EXCLUDE);
|
||||
break;
|
||||
}
|
||||
case "gamemode": {
|
||||
final boolean include = !value.startsWith("!");
|
||||
final String gameModeName = include ? value : value.substring(1);
|
||||
try {
|
||||
final GameMode gameMode = GameMode.valueOf(gameModeName);
|
||||
entityFinder.setGameMode(gameMode, include ? EntityFinder.ToggleableType.INCLUDE : EntityFinder.ToggleableType.EXCLUDE);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ArgumentSyntaxException("Invalid entity game mode", input, INVALID_ARGUMENT_VALUE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "limit":
|
||||
try {
|
||||
final int limit = Integer.parseInt(value);
|
||||
entityFinder.setLimit(limit);
|
||||
} catch (NumberFormatException e) {
|
||||
throw new ArgumentSyntaxException("Invalid limit number", input, INVALID_ARGUMENT_VALUE);
|
||||
}
|
||||
break;
|
||||
case "sort":
|
||||
try {
|
||||
EntityFinder.EntitySort entitySort = EntityFinder.EntitySort.valueOf(value.toUpperCase());
|
||||
entityFinder.setEntitySort(entitySort);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new ArgumentSyntaxException("Invalid entity sort", input, INVALID_ARGUMENT_VALUE);
|
||||
}
|
||||
break;
|
||||
case "level":
|
||||
try {
|
||||
final IntRange level = ArgumentIntRange.staticParse(value);
|
||||
entityFinder.setLevel(level);
|
||||
} catch (ArgumentSyntaxException e) {
|
||||
throw new ArgumentSyntaxException("Invalid level number", input, INVALID_ARGUMENT_VALUE);
|
||||
}
|
||||
break;
|
||||
case "distance":
|
||||
try {
|
||||
final IntRange distance = ArgumentIntRange.staticParse(value);
|
||||
entityFinder.setDistance(distance);
|
||||
} catch (ArgumentSyntaxException e) {
|
||||
throw new ArgumentSyntaxException("Invalid level number", input, INVALID_ARGUMENT_VALUE);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
return finalIndex;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public List<Entity> parse(@NotNull String value) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean isOnlySingleEntity() {
|
||||
return onlySingleEntity;
|
||||
}
|
||||
@ -126,4 +226,18 @@ public class ArgumentEntities extends Argument<List<Entity>> {
|
||||
public boolean isOnlyPlayers() {
|
||||
return onlyPlayers;
|
||||
}
|
||||
|
||||
private static EntityFinder.TargetSelector toTargetSelector(@NotNull String selectorVariable) {
|
||||
if (selectorVariable.equals("@p"))
|
||||
return EntityFinder.TargetSelector.NEAREST_PLAYER;
|
||||
if (selectorVariable.equals("@r"))
|
||||
return EntityFinder.TargetSelector.RANDOM_PLAYER;
|
||||
if (selectorVariable.equals("@a"))
|
||||
return EntityFinder.TargetSelector.ALL_PLAYERS;
|
||||
if (selectorVariable.equals("@e"))
|
||||
return EntityFinder.TargetSelector.ALL_ENTITIES;
|
||||
if (selectorVariable.equals("@s"))
|
||||
return EntityFinder.TargetSelector.SELF;
|
||||
throw new IllegalStateException("Weird selector variable: " + selectorVariable);
|
||||
}
|
||||
}
|
||||
|
@ -20,6 +20,11 @@ public class ArgumentIntRange extends ArgumentRange<IntRange> {
|
||||
@NotNull
|
||||
@Override
|
||||
public IntRange parse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
return staticParse(input);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static IntRange staticParse(@NotNull String input) throws ArgumentSyntaxException {
|
||||
try {
|
||||
if (input.contains("..")) {
|
||||
final int index = input.indexOf('.');
|
||||
|
@ -9,6 +9,9 @@ import org.jetbrains.annotations.NotNull;
|
||||
* Exception triggered when an {@link Argument} is wrongly parsed.
|
||||
* <p>
|
||||
* Retrieved in {@link ArgumentCallback} defined in {@link Command#setArgumentCallback(ArgumentCallback, Argument)}.
|
||||
* <p>
|
||||
* Be aware that the message returned by {@link #getMessage()} is only here for debugging purpose,
|
||||
* you should refer to {@link #getErrorCode()} to identify the exceptions.
|
||||
*/
|
||||
public class ArgumentSyntaxException extends Exception {
|
||||
|
||||
|
@ -35,10 +35,10 @@ public class EntityFinder {
|
||||
|
||||
// By traits
|
||||
private OptionalInt limit;
|
||||
private ToggleableMap<EntityType> entityTypes;
|
||||
private final ToggleableMap<EntityType> entityTypes = new ToggleableMap<>();
|
||||
|
||||
// Players specific
|
||||
private ToggleableMap<GameMode> gameModes;
|
||||
private final ToggleableMap<GameMode> gameModes = new ToggleableMap<>();
|
||||
private IntRange level;
|
||||
|
||||
public EntityFinder setTargetSelector(@NotNull TargetSelector targetSelector) {
|
||||
@ -71,6 +71,16 @@ public class EntityFinder {
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityFinder setEntity(@NotNull EntityType entityType, @NotNull ToggleableType toggleableType) {
|
||||
this.entityTypes.put(entityType, toggleableType.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityFinder setGameMode(@NotNull GameMode gameMode, @NotNull ToggleableType toggleableType) {
|
||||
this.gameModes.put(gameMode, toggleableType.getValue());
|
||||
return this;
|
||||
}
|
||||
|
||||
public EntityFinder setDifference(double dx, double dy, double dz) {
|
||||
this.dx = OptionalDouble.of(dx);
|
||||
this.dy = OptionalDouble.of(dy);
|
||||
@ -125,7 +135,7 @@ public class EntityFinder {
|
||||
}
|
||||
|
||||
// Entity type
|
||||
if (entityTypes != null && !entityTypes.isEmpty()) {
|
||||
if (!entityTypes.isEmpty()) {
|
||||
final EntityType requirement = entityTypes.requirement;
|
||||
result = result.stream().filter(entity -> {
|
||||
final EntityType entityType = entity.getEntityType();
|
||||
@ -137,7 +147,7 @@ public class EntityFinder {
|
||||
}
|
||||
|
||||
// GameMode
|
||||
if (gameModes != null && !gameModes.isEmpty()) {
|
||||
if (!gameModes.isEmpty()) {
|
||||
final GameMode requirement = gameModes.requirement;
|
||||
result = result.stream().filter(entity -> {
|
||||
if (!(entity instanceof Player))
|
||||
@ -205,6 +215,20 @@ public class EntityFinder {
|
||||
ARBITRARY, FURTHEST, NEAREST, RANDOM
|
||||
}
|
||||
|
||||
public enum ToggleableType {
|
||||
INCLUDE(true), EXCLUDE(false);
|
||||
|
||||
private final boolean value;
|
||||
|
||||
ToggleableType(boolean value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
private static class ToggleableMap<T> extends Object2BooleanOpenHashMap<T> {
|
||||
|
||||
@Nullable
|
||||
|
@ -6,6 +6,7 @@ import net.minestom.server.command.builder.Command;
|
||||
import net.minestom.server.command.builder.arguments.ArgumentType;
|
||||
import net.minestom.server.command.builder.arguments.minecraft.ArgumentEntities;
|
||||
import net.minestom.server.entity.Entity;
|
||||
import net.minestom.server.utils.entity.EntityFinder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@ -14,13 +15,18 @@ public class EntitySelectorCommand extends Command {
|
||||
public EntitySelectorCommand() {
|
||||
super("ent");
|
||||
|
||||
setDefaultExecutor((sender, args) -> System.out.println("DEFAULT"));
|
||||
|
||||
ArgumentEntities argumentEntities = ArgumentType.Entities("entities");
|
||||
|
||||
setArgumentCallback((sender, exception) -> exception.printStackTrace(), argumentEntities);
|
||||
|
||||
addSyntax(this::executor, argumentEntities);
|
||||
|
||||
}
|
||||
|
||||
private void executor(CommandSender commandSender, Arguments arguments) {
|
||||
List<Entity> entities = arguments.getEntities("entities");
|
||||
EntityFinder query = arguments.getEntities("entities");
|
||||
System.out.println("SUCCESS COMMAND");
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user