Hi, throughout this entire block, I am getting a massive error, but eclipse is illegible as to what the error is.
CommandSpec myCommandSpec = CommandSpec.builder()
.permission("myplugin.full")
.description(Text.of("description"))
.arguments (
GenericArguments.integer(Text.of("int")),
GenericArguments.integer(Text.of("int")),
GenericArguments.remainingJoinedStrings(Text.of("joinedstrings")))
.executor(new Command() );
.build();
game.getCommandManager().register(this, myCommandSpec, "command", "run", "c");
Thanks for the help. I honestly have no idea at this point.
There is a semicolon after .executor()
1 Like
Ok, thanks. Where after .executor( )?
I’d assume that’s the only issue, but you haven’t given us the error (and I’m on mobile).
Edit: You edited before I could post
Right at the end of .executor(new Command() );
I’m getting a type mismatch for the first line,
syntax error for the second line,
syntax error for .build line
and unresolved variable for last line
Hmmm, I am getting this error which seems to be causing the problem:
Code: CommandSpec Main = CommandSpec.builder()
Error message: Type mismatch: cannot convert from CommandSpec.Builder to CommandSpec.
Any ideas?
Back home now, this code works for me:
CommandSpec myCommandSpec = CommandSpec.builder()
.permission("myplugin.full")
.description(Text.of("description"))
.arguments (
GenericArguments.integer(Text.of("int")),
GenericArguments.integer(Text.of("int")),
GenericArguments.remainingJoinedStrings(Text.of("joinedstrings")))
.executor(new Command())
.build();
Make sure that you are building your CommandSpec.Builder
using build()
.
Ok, thanks. That works for me as well…
Um, my next issue is, I have a separate class for executing the command, however none of the variables defined in the main class are being transferred into the executor class. Any ideas?
I get the error, [variable] cannot be resolved into a variable.
You’d have to either public statically declare your variables or pass them through the Command()
constructor.
Ex:
.arguments(new Command(myVariable, myOtherVariable))
and
public Command(String someString, int someInt) {
// Store these variables class-locally
}
I’d recommend learning more about Java before digging into plugin development. Then you likely wouldn’t be running into these issues while developing.
Edit: Or of course publicly declare the variables and pass the main public object. Then it’s just a matter of making use of myPluginObject.myVariable
.
Sorry, I think I have worded this wrong. I mean the variables that represent the arguments of a command.
I have declared:
GenericArguments.integer(Text.of(“itemid”)),
However, when I go to use these in the executing class, they do not register. I was wondering how you declare them so they have a value that is set in a different class.
Ah, okay. The argument parsing page on the SpongeDocs has a quick rundown of this, but the code would essentially look like this:
@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
int itemId = args.<Integer>getOne("itemid").get();
return CommandResult.success();
}
The CommandContext
object will store the arguments for the command. From there, it is a matter of calling getOne(String)
to retrieve the argument. Unless your argument is optional, it is safe to directly call .get()
, and the <Integer>
is there to avoid unnecessary casting to int.
Ok, thanks dude. That should be it. 10/10 review.