Created
February 11, 2025 14:59
-
-
Save TheMeinerLP/612efee7136dd5a9d3bdd10703b6efaa to your computer and use it in GitHub Desktop.
Combine multiple argument types seperated with comma
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minestom.demo.test; | |
import net.minestom.server.command.ArgumentParserType; | |
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.instance.block.Block; | |
import org.jetbrains.annotations.NotNull; | |
public class ArgumentBlock extends Argument<Block> { | |
public ArgumentBlock(@NotNull String id, boolean allowSpace, boolean useRemaining) { | |
super(id, allowSpace, useRemaining); | |
} | |
public ArgumentBlock(@NotNull String id, boolean allowSpace) { | |
super(id, allowSpace); | |
} | |
public ArgumentBlock(@NotNull String id) { | |
super(id); | |
} | |
@Override | |
public @NotNull Block parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException { | |
Block block = Block.fromNamespaceId(input); | |
if (block != null) { | |
return block; | |
} | |
throw new ArgumentSyntaxException("Undefined block", input, -2); | |
} | |
@Override | |
public ArgumentParserType parser() { | |
return ArgumentParserType.RESOURCE; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minestom.demo.test; | |
import net.minestom.server.command.ArgumentParserType; | |
import net.minestom.server.command.CommandSender; | |
import net.minestom.server.command.builder.CommandContext; | |
import net.minestom.server.command.builder.arguments.Argument; | |
import net.minestom.server.command.builder.exception.ArgumentSyntaxException; | |
import net.minestom.server.command.builder.parser.CommandParser; | |
import net.minestom.server.command.builder.parser.ValidSyntaxHolder; | |
import net.minestom.server.utils.StringUtils; | |
import org.jetbrains.annotations.NotNull; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.List; | |
public class ArgumentDynamicLoop<T> extends Argument<List<T>> { | |
public static final int INVALID_INPUT_ERROR = 1; | |
private final List<Argument<T>> arguments = new ArrayList<>(); | |
private final String splitter; | |
@SafeVarargs | |
public ArgumentDynamicLoop(@NotNull String id, String splitter, @NotNull Argument<T>... arguments) { | |
super(id, true, true); | |
this.splitter = splitter; | |
this.arguments.addAll(Arrays.asList(arguments)); | |
} | |
@NotNull | |
@Override | |
public List<T> parse(@NotNull CommandSender sender, @NotNull String input) throws ArgumentSyntaxException { | |
List<T> result = new ArrayList<>(); | |
final String[] split = input.split(this.splitter); | |
final StringBuilder builder = new StringBuilder(); | |
boolean success = false; | |
for (String s : split) { | |
builder.append(s); | |
for (Argument<T> argument : arguments) { | |
try { | |
final String inputString = builder.toString(); | |
final T value = argument.parse(sender, inputString); | |
success = true; | |
result.add(value); | |
break; | |
} catch (ArgumentSyntaxException ignored) { | |
success = false; | |
} | |
} | |
if (success) { | |
builder.setLength(0); // Clear | |
} else { | |
builder.append(StringUtils.SPACE); | |
} | |
} | |
if (result.isEmpty() || !success) { | |
throw new ArgumentSyntaxException("Invalid loop, there is no valid argument found", input, INVALID_INPUT_ERROR); | |
} | |
return result; | |
} | |
public List<Argument<T>> arguments() { | |
return arguments; | |
} | |
@Override | |
public ArgumentParserType parser() { | |
return null; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.minestom.demo.test; | |
import net.minestom.server.command.builder.Command; | |
public class ExampleCommand extends Command { | |
public ExampleCommand() { | |
super("/set"); | |
var possibleArguments = new ArgumentDynamicLoop<>("input", ",", new ArgumentBlock("block")); | |
addSyntax((sender, context) -> { | |
sender.sendMessage("Block: " + context.get(possibleArguments).getFirst()); | |
}, possibleArguments); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment