All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.minestom.server.command.builder.arguments.minecraft.ArgumentResource Maven / Gradle / Ivy

There is a newer version: 7320437640
Show newest version
package net.minestom.server.command.builder.arguments.minecraft;

import net.minestom.server.command.CommandSender;
import net.minestom.server.command.builder.arguments.Argument;
import net.minestom.server.command.builder.exception.ArgumentSyntaxException;
import net.minestom.server.utils.StringUtils;
import net.minestom.server.utils.binary.BinaryWriter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class ArgumentResource extends Argument {

    public static final int SPACE_ERROR = 1;

    private final String identifier;

    public ArgumentResource(@NotNull String id, @NotNull String identifier) {
        super(id);
        this.identifier = identifier;
    }

    @Override
    public @NotNull String parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException {
        if (input.contains(StringUtils.SPACE))
            throw new ArgumentSyntaxException("Resource location cannot contain space character", input, SPACE_ERROR);

        return input;
    }

    @Override
    public String parser() {
        return "minecraft:resource";
    }

    @Override
    public String toString() {
        return String.format("Resource<%s>", getId());
    }

    @Override
    public byte @Nullable [] nodeProperties() {
        return BinaryWriter.makeArray(packetWriter ->
                packetWriter.writeSizedString(this.identifier)
        );
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy