Is this the correct layout for executing commands through the console?

Hi, I’m using this code to execute a give command through the console. The itemname and quantity arguments are both set in the initialization command. Whenever I run my plugin, everything works fine except for this command. Any ideas on why it does not work???

game.getCommandManager().process(game.getServer().getConsole(), "give @a “+“itemname”+” "+“quantity”);

The command would resolve to give @a itemname quantity - I assume you’re wanting to insert variables into the string instead?

um lol, ya I think I have been pretty stupid.
Whenever I assign them as variables they come up with an error message. Here is the full code, any ideas are greatly appreciated:

.arguments (
.executor((src, args) -> {
game.getCommandManager().process(game.getServer().getConsole(), "give @a “+“itemname”+” "+“quantity”);

retracted comment

As I linked in nearly a duplicate thread (are you guys clones?), Java’s still Java. Local variables don’t appear out of thin air, no matter what function you call. In this case, the CommandContext object, which you have named args, is what you’re looking for here.

    int quantity = args.<Integer>getOne("quantity").get();
    String itemname = args.<String>getOne("itemname").get();
    game.getCommandManager().process(game.getServer().getConsole(), "give @a "+itemname+" "+quantity);
    return CommandResult.success();

And by the way, if you’re literally looking for item names, instead of GenericArguments.string, try GenericArguments.catalogedElement with ItemType.class. Sponge will parse it, provide relevant auto completes, and you can then retrieve it with args.<ItemType>getOne("itemname").get().

1 Like

Give this a read, either you two are working together on a problem and posting duplicate results, or are following the same flawed tutorial, or are deliberately wasting time.

I’d really appreciate knowing how you and the other thread are linked, because at this stage I’m starting to get suspicious.

inb4 sponge equivalent of BcBroz