Skip to content

Custom Parameters

Berke Akçen edited this page Mar 30, 2024 · 3 revisions

Table of contents

Custom Parameters without @Param or @Default

Same type of the objects registered as a custom parameters by only the simple name of classes will result as the same outputs.

public class ExampleClass extends JavaPlugin {

	@Override
	public void onEnable() {
		CommandFramework commandFramework = new CommandFramework(this);
		commandFramework.registerCommands(this);
		commandFramework.addCustomParameter("String", arguments -> arguments.getArgument(0));
	}

        // /example test - Output will be "Value: test" if the first argument is given in the command.
	@Command(name = "example")
	public void exampleCommand(CommandArguments arguments, String arg) {
                // If we add more String typed parameters all of them will be the same.
                // To add multiple parameters with the same type but different outputs check 'Custom Parameters with @Param annotation' part below.
		arguments.sendMessage("Value: " + arg);
	}
}

Custom Parameters using @Param annotation

This annotation provides user to use same type of objects multiple times.

public class ExampleClass extends JavaPlugin {

	@Override
	public void onEnable() {
		CommandFramework commandFramework = new CommandFramework(this);
		commandFramework.registerCommands(this);
		commandFramework.addCustomParameter("firstArg", arguments -> arguments.getArgument(0));
                commandFramework.addCustomParameter("second arg", arguments -> arguments.getArgument(1));
	}

        // /example firstArg secondArg - Output will be "First argument is 'firstArg' and the second is secondArg'."
        // We ensure that we have at least 2 arguments given. For default values check the part below.
        @Command(name = "example", min = 2)
	public void exampleCommand(CommandArguments arguments, @Param("firstArg") String first, @Param("second arg") String second) {
		arguments.sendMessage("First argument is '" + first + "' and the second is '" + second + "'.");
	}
}

Custom Parameters using @Param and @Default annotations

This annotation provides user to use same type of objects multiple times but this time with default values.
Primitive types such as long, double, int, short, float, byte, char, boolean and additionally Strings are supported directly.

public class ExampleClass extends JavaPlugin {

	@Override
	public void onEnable() {
		CommandFramework commandFramework = new CommandFramework(this);
		commandFramework.registerCommands(this);
		commandFramework.addCustomParameter("arg", arguments -> arguments.getArgument(0));
		commandFramework.addCustomParameter("secondAsInt", arguments -> arguments.getLength() > 1 ? arguments.getArgumentAsInt(1) : null);
	}

        // /example - Output will be "Value: default value of the argument"
        // /example test - Output will be "Value: test"
        @Command(name = "example")
	public void exampleCommand(CommandArguments arguments, @Default("default value of the argument") @Param("arg") String value) {
		arguments.sendMessage("Value: " + value);
	}

        // /example firstArg 123 - Output will be "Second argument as int is 123"
        // /example firstArg - Output will be "Second argument as int is 100" (100 is the value from default annotation)
        @Command(name = "intExample")
	public void exampleCommand(CommandArguments arguments, @Default("100") @Param("secondAsInt") int secondArg) {
		arguments.sendMessage("Second argument as int is " + secondArg);
	}
}

Custom Parameters using @Param and @Default annotations for non-primitive classes

For non-primitive types, the class itself must contain a static method that returns class type called valueOf(String value).

For instance, assume that our class is named TestValue like below.
The class must include a static method like TestValue#valueOf(String).

public static TestValue valueOf(String value) {
	return new TestValue(value);
}
public class Main extends JavaPlugin {

	@Override
	public void onEnable() {
		CommandFramework commandFramework = new CommandFramework(this);
		commandFramework.registerCommands(this);
		commandFramework.addCustomParameter("test", arguments -> arguments.getLength() != 1 ? null : new TestValue(arguments.getArgument(0)));
	}

        // /test somRandomTextAsFirstArg - Output will be "Value: somRandomTextAsFirstArg"
        // /test - Output will be "Value: Default value"
	@Command(name = "test")
	public void test(CommandArguments arguments,
					 @Default("Default value")
					 @Param("test")
					 TestValue testValue) {
		arguments.sendMessage("Value:" + testValue.value);
	}

	public static class TestValue {

		private final String value;

		public TestValue(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}

		public static TestValue valueOf(String value) {
			return new TestValue(value);
		}
	}
}