sooooo here’s part of some Code i have…now what i want to do is add in some advance arguments so that all of the builder is customizable… some help please
public CommandResult execute(CommandSource src, CommandContext args)
throws CommandException
{
String name = (String)args.getOne(“worldName”).get();
if ((src instanceof Player))
{
Player player = (Player)src;
WorldBuilder builder = (WorldBuilder)this.game.getRegistry().createBuilder(WorldBuilder.class);
builder.dimensionType(DimensionTypes.OVERWORLD);
builder.name(name);
builder.enabled(true);
builder.hardcore(false);
builder.keepsSpawnLoaded(true);
builder.loadsOnStartup(true);
builder.usesMapFeatures(false);
builder.generator(GeneratorTypes.DEFAULT);
Optional optWorld = builder.build();
if (optWorld.isPresent())
{
player.setLocation(((World)optWorld.get()).getSpawnLocation());
player.sendMessage(Texts.of("succesfully created world and teleported to world: " + name));
return CommandResult.success();
Check out the SpongeDocs page on Command arguments here: https://docs.spongepowered.org/en/plugin/commands/arguments.html
Basically, when you create your command using the CommandSpec, you can specify any of the various argument types on that page. So you could use for one argument optional(catalogedElement(of("dimension"), Sponge.getGame(), DimensionType.class), DimensionTypes.OVERWORLD)
to get an optional DimensionType argument in your executor.
You can also use flags if you want to be even fancier, so you could have /createworld -h true -g DEFAULT worldname
, however a documentation page on command flags isn’t written yet.
ok i understand that…but how would i add the option to the actual builder? look i already have the arg ready! but how do i implement it in the actual builder?
Args:
.arguments(GenericArguments.remainingJoinedStrings(Texts.of(“dimensionalType”)))
String dimension = (String)args.getOne("dimensionalType").get();
Builder:
builder.dimensionType(DimensionTypes.OVERWORLD);
I want to change the builder property of Overworld through the command please
Take a look at the first section of the documentation again. You can use catalogedElement
along with onlyOne
argument types to get a catalog type directly as an argument.
That way you don’t have do any of the work finding the catalog types in the registry - the arguments parser will do that for you.
This can be used to get the generator and dimension types for the builder. The other types are simple java types, and can be added with string
and bool
in the arguments list.
So for example, to load the dimension type as an argument:
// Somewhere in your command spec
.arguments(GenericArguments.onlyOne(GenericArguments.catalogedElement(Texts.of("dimension"), Sponge.getGame(), DimensionType.class)
// Somewhere in command executor
builder.dimensionType(args.<DimensionType>getOne("dimension").get());
In this example, a few key things:
Use of onlyOne: because the argument parser supports partial matches (ie using “Zeph” as an argument could match both “ZephireNZ” and “Zephyr” as a player). The documentation will say whether an parser creates a single or multiple results.
Use of catalogedElement: This makes the parser automatically find the matching catalog type using the DataManager
, rather than leaving that to the command.
I hope that solves your problems.
1 Like
YES omg!!! yes!!! thank you
something like this yea…however that’s flagging errors all across the board in eclipse
the porblem is that that argument is not my only one and when i put it with the other which is a remaining joine strings arguement it flags the executor of the next line breaks and then wants me to add a method to the other already working argument.
I don’t understand. Can you post the full code for your CommandSpec?
yea here it is
/*---disabled till code for teleportation light is found
logger.info("testing-------------------------------------");
CommandSpec portal = CommandSpec.builder()
.description(Texts.of("Made for Teleportation"))
.arguments(GenericArguments.remainingJoinedStrings(Texts.of("worldName")))
.executor(new PortalCommand(game))
.build();
game.getCommandManager().register(this, portal, new String[] { "dest"});
---*/
I think I see your problem now. Were you trying to do “arguments()” twice? arguments takes either a single argument, or a list of arguments, you can’t just run the function twice.
i tried both i was trying to do the one you suggested followed by a comma then mine the first time then i tried to do .arguements twice…help? please