Search in sources :

Example 1 with UserManager

use of org.spongepowered.api.user.UserManager in project SpongeCommon by SpongePowered.

the class SpongeUserValueParameter method parseValue.

@Override
@NonNull
public Optional<? extends UUID> parseValue(@NonNull final CommandCause cause, final ArgumentReader.@NonNull Mutable reader) throws ArgumentParseException {
    final String peek = reader.peekString();
    if (peek.startsWith("@")) {
        try {
            final ServerPlayer entity = (ServerPlayer) (this.selectorArgumentType.parse((StringReader) reader).findSingleEntity(((CommandSourceStack) cause)));
            return Optional.of(entity.uniqueId());
        } catch (final CommandSyntaxException e) {
            throw reader.createException(Component.text(e.getContext()));
        }
    }
    final UserManager userManager = SpongeCommon.game().server().userManager();
    try {
        final UUID uuid = UUID.fromString(reader.parseString());
        if (userManager.exists(uuid)) {
            return Optional.of(uuid);
        }
    } catch (final Exception ignored) {
    }
    // if no UUID, get the name. We've already advanced the reader at this point.
    final Optional<UUID> result = Sponge.server().gameProfileManager().cache().findByName(peek).map(GameProfile::uuid).filter(userManager::exists);
    if (result.isPresent()) {
        return result;
    }
    throw reader.createException(Component.text("Could not find user with user name \"" + peek + "\""));
}
Also used : UserManager(org.spongepowered.api.user.UserManager) ServerPlayer(org.spongepowered.api.entity.living.player.server.ServerPlayer) StringReader(com.mojang.brigadier.StringReader) UUID(java.util.UUID) CommandSourceStack(net.minecraft.commands.CommandSourceStack) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) ArgumentParseException(org.spongepowered.api.command.exception.ArgumentParseException) CommandSyntaxException(com.mojang.brigadier.exceptions.CommandSyntaxException) NonNull(org.checkerframework.checker.nullness.qual.NonNull)

Aggregations

StringReader (com.mojang.brigadier.StringReader)1 CommandSyntaxException (com.mojang.brigadier.exceptions.CommandSyntaxException)1 UUID (java.util.UUID)1 CommandSourceStack (net.minecraft.commands.CommandSourceStack)1 NonNull (org.checkerframework.checker.nullness.qual.NonNull)1 ArgumentParseException (org.spongepowered.api.command.exception.ArgumentParseException)1 ServerPlayer (org.spongepowered.api.entity.living.player.server.ServerPlayer)1 UserManager (org.spongepowered.api.user.UserManager)1