Giter VIP home page Giter VIP logo

Comments (6)

Pharap avatar Pharap commented on September 3, 2024 2

This should be closed as 'not an issue' or the equivalent.

Technically it's not a bug because : is not part of the syntax for an unquoted string (though this is undocumented):

public static boolean isAllowedInUnquotedString(final char c) {
return c >= '0' && c <= '9'
|| c >= 'A' && c <= 'Z'
|| c >= 'a' && c <= 'z'
|| c == '_' || c == '-'
|| c == '.' || c == '+';
}

The solution is (as was suggested) to create a custom argument that can parse values with : in them.

from brigadier.

Dinnerbone avatar Dinnerbone commented on September 3, 2024 1

The best solution here would be a custom argument so you can validate input quicker and do the splitting of port at parse-time instead of execute-time.

If you need to use string, you'll have to quote it. checkit "127.0.0.1:25566" - unquoted strings can only contain a limited set of characters.

from brigadier.

ryantheleach avatar ryantheleach commented on September 3, 2024

@DeStilleGast I don't think there's enough information here to help you effectively.

Can you post a code example of the command registration that you were attempting to parse, and the full command that you were entering?

As well as what you expected to happen, (clearly in this case, not throw errors) but it looks like this is only one small piece of the puzzle.

from brigadier.

DeStilleGast avatar DeStilleGast commented on September 3, 2024

@ryantheleach yeah sure, makes sense

the code for the command is:

commandDispatcher.register(
	literal("checkit")
	.then(
		argument("server", word())
			.executes(c -> {
				String server = c.getArgument("server", String.class);
				int port = 25565;

				if(server.contains(":")) {
					String[] split = server.split(":");
					server = split[0];
					port = Integer.parseInt(split[1]);
				}

				checkWhitelisted(c.getSource(), server, port, c.getSource().getUsername());
				return 1;
			})
			.then(argument("user", string())
				.executes(c -> {
					String server = c.getArgument("server", String.class);
					int port = 25565;

					if(server.contains(":")) {
						String[] split = server.split(":");
						server = split[0];
						port = Integer.parseInt(split[1]);
					}

					checkWhitelisted(c.getSource(), server, port, c.getArgument("user", String.class));
					return 1;
				})))
);

and the command that I try to execute (command prefix removed): checkit 127.0.0.1:25566
what this command basicly should do is that is checks the database if a user is whitelisted or not (reply is done in the function itself)

current workaround is to use checkit "127.0.0.1:25566" but that can get annoying and confusing

from brigadier.

DeStilleGast avatar DeStilleGast commented on September 3, 2024

@Dinnerbone I really don't need a string for it, I just parse a ip and port from a string with optional port option. I tried to make a custom argument, thought first it was going to be hard, but it is easier then it looks.

Created my own IpArgumentType and it works pretty.

My code for future people (it's maybe not pretty, but it works well):
Class IpArgumentType

public class IpArgumentType implements ArgumentType<Ip> {

    @Override
    public <S> Ip parse(StringReader reader) throws CommandSyntaxException {
        String input = reader.getRemaining().split(" ")[0];
        reader.setCursor(reader.getCursor() + input.length());

        return new Ip(input);
    }

    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        return null;
    }

    @Override
    public Collection<String> getExamples() {
        return Arrays.asList("127.0.0.1", "127.0.0.1:25567");
    }
}

Class Ip:

public class Ip {

    private String ip;
    private int port = 25565; //default port

    public Ip(String ip) {
        this.ip = ip;

        if(ip.contains(":")) {
            String[] split = ip.split(":");
            this.ip = split[0];
            try {
                port = Integer.parseInt(split[1]);
            }catch (NumberFormatException ex){
                throw new NumberFormatException(String.format("%s is not a number !!", split[1]));
            }
        }
    }

    public Ip(String ip, int port) {
        this.ip = ip;
        this.port = port;
    }

    public String getIp() {
        return ip;
    }

    public int getPort() {
        return port;
    }
}

from brigadier.

DeStilleGast avatar DeStilleGast commented on September 3, 2024

but my issue is fixed, keep this bug open or can it be closed ?

from brigadier.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.