Hi, would anyone be willing to check over this small plugin that I have been fiddling around with?

I’ve been going over the basics of sponge and wrote a basic command plugin. Would someone be able to check over it quickly, particularly my command arguments?

Main:

package me.thunder.lunadp;

import org.spongepowered.api.event.Subscribe;
import org.spongepowered.api.event.state.InitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.util.command.spec.CommandSpec;
 
import static org.spongepowered.api.util.command.args.GenericArguments.*;
 
@Plugin(id="me.thunder.lunadp", name="LunaDP", version="0.0.1")
public class lunadp {
 
        @Subscribe
        public void onInitialization(InitializationEvent e) {
                CommandSpec startdp = CommandSpec.builder()
                                .description(Texts.of("Starts DP based upon input of item id and displays custom message."))
                                .permission("lunadp.startdp")
                                .executor(new quickdp())
                                .arguments(optional(player(Texts.of("target"), e.getGame())))
                                .arguments(GenericArguments.integer("itemid"), GenericArguments.string("message"))
                                .build();
               
                e.getGame().getCommandDispatcher().register(this, startdp, "startdp");
        }
}

Command:

package me.thunder.dpcommand;

import org.spongepowered.api.data.DataTransactionResult;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.text.Texts;
import org.spongepowered.api.util.command.CommandException;
import org.spongepowered.api.util.command.CommandResult;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.util.command.args.CommandContext;
import org.spongepowered.api.util.command.spec.CommandExecutor;
 
public class dpcommand implements CommandExecutor {
 
        public CommandResult execute(CommandSource commandSource, CommandContext commandContext) throws CommandException {
                CommandResult r = Sponge.getCommandManager().proccess(Sponge.getServer().getConsole(), "give @a " + itemid);
                game.getServer().getBroadcastChannel().send(message);
                
                
               
                return CommandResult.success();
        }
}

Particularly the arguments, I am worried about, in the main class are meant to then be used in the outputs in the command class, but I am not particularly sure if I have structured that correctly.

Thanks!

For your command builder:
You are passing two different sets of arguments. The second one overrides the first one.
I have no idea why you are passing in the game when asking for a player.

For you CommandExecutor:
I don’t see the message or itemid field anywhere, where are they?
You are also returning a CommandResult.success() even though you might have gotten a different CommandResult from executing the command.

In addition to all of that I would reccomend that you pay a bit attention to the coding style. Normally class names are in UpperCamelCase. Also, if you are going to use static imports for the arguments (I would recommend you against it, as it makes it harder to read the code and seeing where something comes from), be consistent about actually using it.

Your Sponge version is really really really old. This looks to be, what, 2.0? The latest supported version is 4.1.0, and the version that’s just around the corner is 5.0.0. Use these if you want any help whatsoever.

Also, before and after the code, when posting, put three backticks on a line by themselves (```). Makes the whole code nice and formatted.

2 Likes