mirror of
https://github.com/Minestom/Minestom.git
synced 2025-01-23 08:31:26 +01:00
Improve ArgumentEntity to support entity UUID & pre-check for player name
This commit is contained in:
parent
200b244224
commit
78e37258ed
@ -1,10 +1,12 @@
|
|||||||
package net.minestom.server.command.builder.arguments.minecraft;
|
package net.minestom.server.command.builder.arguments.minecraft;
|
||||||
|
|
||||||
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.builder.NodeMaker;
|
import net.minestom.server.command.builder.NodeMaker;
|
||||||
import net.minestom.server.command.builder.arguments.Argument;
|
import net.minestom.server.command.builder.arguments.Argument;
|
||||||
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
|
||||||
import net.minestom.server.entity.EntityType;
|
import net.minestom.server.entity.EntityType;
|
||||||
import net.minestom.server.entity.GameMode;
|
import net.minestom.server.entity.GameMode;
|
||||||
|
import net.minestom.server.entity.Player;
|
||||||
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
import net.minestom.server.network.packet.server.play.DeclareCommandsPacket;
|
||||||
import net.minestom.server.registry.Registries;
|
import net.minestom.server.registry.Registries;
|
||||||
import net.minestom.server.utils.entity.EntityFinder;
|
import net.minestom.server.utils.entity.EntityFinder;
|
||||||
@ -13,6 +15,7 @@ import org.jetbrains.annotations.NotNull;
|
|||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Represents the target selector argument.
|
* Represents the target selector argument.
|
||||||
@ -88,11 +91,25 @@ public class ArgumentEntity extends Argument<EntityFinder> {
|
|||||||
@NotNull
|
@NotNull
|
||||||
public static EntityFinder staticParse(@NotNull String input,
|
public static EntityFinder staticParse(@NotNull String input,
|
||||||
boolean onlySingleEntity, boolean onlyPlayers) throws ArgumentSyntaxException {
|
boolean onlySingleEntity, boolean onlyPlayers) throws ArgumentSyntaxException {
|
||||||
// Check for raw player name
|
// Check for raw player name or UUID
|
||||||
if (input.length() <= 16 && !input.contains(SELECTOR_PREFIX)) {
|
if (!input.contains(SELECTOR_PREFIX)) {
|
||||||
return new EntityFinder()
|
|
||||||
.setTargetSelector(EntityFinder.TargetSelector.ALL_PLAYERS)
|
// Check if the input is a valid UUID
|
||||||
.setName(input, EntityFinder.ToggleableType.INCLUDE);
|
try {
|
||||||
|
UUID uuid = UUID.fromString(input);
|
||||||
|
return new EntityFinder()
|
||||||
|
.setTargetSelector(EntityFinder.TargetSelector.ALL_ENTITIES)
|
||||||
|
.setUuid(uuid);
|
||||||
|
} catch (IllegalArgumentException ignored) {
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the input is a valid player name
|
||||||
|
final Player player = MinecraftServer.getConnectionManager().getPlayer(input);
|
||||||
|
if (player != null) {
|
||||||
|
return new EntityFinder()
|
||||||
|
.setTargetSelector(EntityFinder.TargetSelector.ALL_PLAYERS)
|
||||||
|
.setName(input);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// The minimum size is always 2 (for the selector variable, ex: @p)
|
// The minimum size is always 2 (for the selector variable, ex: @p)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
package net.minestom.server.utils.entity;
|
package net.minestom.server.utils.entity;
|
||||||
|
|
||||||
|
import it.unimi.dsi.fastutil.objects.Object2BooleanMap;
|
||||||
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
|
||||||
import net.minestom.server.MinecraftServer;
|
import net.minestom.server.MinecraftServer;
|
||||||
import net.minestom.server.command.CommandSender;
|
import net.minestom.server.command.CommandSender;
|
||||||
@ -38,7 +39,8 @@ public class EntityFinder {
|
|||||||
// By traits
|
// By traits
|
||||||
private Integer limit;
|
private Integer limit;
|
||||||
private final ToggleableMap<EntityType> entityTypes = new ToggleableMap<>();
|
private final ToggleableMap<EntityType> entityTypes = new ToggleableMap<>();
|
||||||
private final ToggleableMap<String> names = new ToggleableMap<>();
|
private String name;
|
||||||
|
private UUID uuid;
|
||||||
|
|
||||||
// Players specific
|
// Players specific
|
||||||
private final ToggleableMap<GameMode> gameModes = new ToggleableMap<>();
|
private final ToggleableMap<GameMode> gameModes = new ToggleableMap<>();
|
||||||
@ -79,8 +81,13 @@ public class EntityFinder {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public EntityFinder setName(@NotNull String name, @NotNull ToggleableType toggleableType) {
|
public EntityFinder setName(@NotNull String name) {
|
||||||
this.names.put(name, toggleableType.getValue());
|
this.name = name;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public EntityFinder setUuid(@NotNull UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -154,7 +161,6 @@ public class EntityFinder {
|
|||||||
|
|
||||||
// GameMode
|
// GameMode
|
||||||
if (!gameModes.isEmpty()) {
|
if (!gameModes.isEmpty()) {
|
||||||
final GameMode requirement = gameModes.requirement;
|
|
||||||
result = result.stream().filter(entity -> {
|
result = result.stream().filter(entity -> {
|
||||||
if (!(entity instanceof Player))
|
if (!(entity instanceof Player))
|
||||||
return false;
|
return false;
|
||||||
@ -176,15 +182,21 @@ public class EntityFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Name
|
// Name
|
||||||
if (!names.isEmpty()) {
|
if (name != null) {
|
||||||
// TODO entity name
|
|
||||||
result = result.stream().filter(entity -> {
|
result = result.stream().filter(entity -> {
|
||||||
if (!(entity instanceof Player))
|
if (!(entity instanceof Player))
|
||||||
return false;
|
return false;
|
||||||
return filterToggleableMap(entity, ((Player) entity).getUsername(), names);
|
return ((Player) entity).getUsername().equals(name);
|
||||||
}).collect(Collectors.toList());
|
}).collect(Collectors.toList());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// UUID
|
||||||
|
if (uuid != null) {
|
||||||
|
result = result.stream()
|
||||||
|
.filter(entity -> entity.getUuid().equals(uuid))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// Sort & limit
|
// Sort & limit
|
||||||
if (entitySort != EntitySort.ARBITRARY || limit != null) {
|
if (entitySort != EntitySort.ARBITRARY || limit != null) {
|
||||||
@ -298,10 +310,6 @@ public class EntityFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static class ToggleableMap<T> extends Object2BooleanOpenHashMap<T> {
|
private static class ToggleableMap<T> extends Object2BooleanOpenHashMap<T> {
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private T requirement;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@NotNull
|
@NotNull
|
||||||
@ -343,10 +351,16 @@ public class EntityFinder {
|
|||||||
if (!(entity instanceof Player))
|
if (!(entity instanceof Player))
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
final T requirement = map.requirement;
|
for (Object2BooleanMap.Entry<T> entry : map.object2BooleanEntrySet()) {
|
||||||
// true if the entity type has not been mentioned or if is accepted
|
final T key = entry.getKey();
|
||||||
return (!map.containsKey(value) && requirement == null) ||
|
final boolean include = entry.getBooleanValue();
|
||||||
Objects.equals(requirement, value) ||
|
|
||||||
map.getBoolean(value);
|
final boolean equals = Objects.equals(value, key);
|
||||||
|
if (include && !equals || !include && equals) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user