-
Notifications
You must be signed in to change notification settings - Fork 6
Using MCOpts
The main part of MCOpts consists of two things:
Parameters.of(args, expect()::declare) , in which you handle execution of your parameters, and Parameters.expect(), where you describe what your command expects from the user.
To get the easiest approach, your command should inherit from either SimpleCommand or CommandExpecting.
Parameters is the base class from which you extract your command arguments. Call get with either an index int or a named parameter string to get an instance of Parameter. From there, you can choose to map or filter it, and finally extract it by either require(), optional() or tryGet() (ignore all exceptions).
Example code:
Parameters parameters = Parameters.of(args, expect()::declare);
String structureID = parameters.get(0).require();
Structure<?> structure = parameters.get(0).to(RCP::structure).require();
WorldServer world = parameters.get("dimension").to(MCP.dimension(server, sender)).require();
AxisAlignedTransform2D transform = parameters.get(IvP.transform("rotation", "mirror")).optional().orElse(null);
GenerationType generationType = parameters.get("gen").to(RCP.generationType(structure)).require();
BlockSurfacePos pos = parameters.get(IvP.surfacePos("x", "z", sender.getPosition(), false)).require();
String seed = parameters.get("seed").optional().orElse(null);
boolean select = parameters.has("select");
If you want to add your own helper methods, they should either be easy to call via method handle, or include by returning a Function<Parameter, Parameter>.
public static Parameter<Structure<?>> structure(Parameter<String> p)
{
return p.map(StructureRegistry.INSTANCE::get,
t -> RecurrentComplex.translations.commandException("commands.strucGen.noStructure", p.get()));
}
public static Function<Parameter<String>, Parameter<GenerationType>> generationType(Structure<?> structure)
{
return p -> p.map(structure::generationType, t -> RecurrentComplex.translations.commandException("No Generation by this ID"))
.orElseGet(() -> structure.<GenerationType>generationTypes(NaturalGeneration.class).stream().findFirst()
.orElse(structure.generationTypes(GenerationType.class).stream().findFirst().orElse(null)));
}
With expect, you start by adding all the ordered parameters, and later named arguments or flags.
Example:
return Parameters.expect()
.then(RCE::structure).required()
.then(IvE.surfacePos("x", "z"))
.named("dimension", "d").then(MCE::dimension)
.named("gen").then(RCE.generationType(p -> p.get(0)))
.named("rotation", "r").then(MCE::rotation)
.named("seed").then(RCE::randomString).descriptionU("seed")
.flag("mirror", "m")
.flag("select", "s");
If you want to add your own helper methods, they should either be easy to call via method handle, or include by returning a Consumer.
public static void structure(Expect e)
{
e.next(StructureRegistry.INSTANCE.ids()).descriptionU("structure");
}
public static Consumer<Expect> generationType(Function<Parameters, Parameter<String>> fun)
{
return e -> e.next(params -> fun.apply(params).to(RCP::structure).tryGet()
.map(structure -> structure.generationTypes(GenerationType.class).stream().map(GenerationType::id)))
.descriptionU("generation type id");
}