Would anyone be willing to look over my basic test command - thanks if you do!

Hi all, Im pretty new to sponge, but do have some knowledge of Java. After reading through the sponge docs, I decided to try make a command that uses the console to change the weather based on a command input and then send a broadcast of the source, some text and then the weather change. This is the code that I am using, if someone would be willing to take a look at it, they would make my day!

package betterweathercommand;

import com.google.inject.Inject; 
import org.spongepowered.api.text.format.TextColors;
import org.slf4j.Logger;  
import org.spongepowered.api.Game;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.event.Listener;  
import org.spongepowered.api.event.game.state.GameInitializationEvent;  
import org.spongepowered.api.plugin.Plugin;    
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.entity.living.player.Player;
import com.google.common.base.Function;


@Plugin(id = "weathercmd", name = "WeatherCMD", version = "1.0-SNAPSHOT", description = "Runs weather update based upon input string and broadcasts the change in weather and the executor",
        authors = {"BobtheCan"}, url = "nul")
public class Main {

    @Inject
    private Game game;

    @Inject Logger logger;

    @Listener
    public void onGameInitializationEvent(GameInitializationEvent event) {
    	
		CommandSpec WeatherCMD = CommandSpec.builder()
                .permission("weathercmd.execute")
                .description(Text.of("Runs weather update based upon input string and broadcasts the change in weather and the executor of command"))
                .arguments (
                        GenericArguments.string(Text.of("weathercondition")))
                .executor(new CommandExecutor()  {
            		@Override
            		public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
            			String weathercondition = args.<String>getOne("weathercondition").get()
                	Sponge.getCommandManager().process(Sponge.getServer().getConsole(), ("weather" + weathercondition));
                    Sponge.getServer()
                    .getBroadcastChannel()
                    .send(Text.of(TextColors.DARK_PURPLE, (src + "has changed the weather to" + weathercondition)));
                    return CommandResult.success();
                }})
                .build();
    		    
    		    

    		game.getCommandManager().register(this, setweather, "weatherchange", "changeweather", "cw"); } }

Thanks for looking at this!

For the most part looks good. I don’t know how smart it would be the use console as the executor of the command though. Also, for your arguments, if you want to do more validation easily, replace the GenericArguments.string, with GenericArguments.catalogedElement. There exists a Catalog for weather. Also try setting the weather yourself without relying on another command.

Also, you aren’t including a space between the the “weather”, and the argument.

3 Likes

Thanks for the help!!! I will get on this now.

Checked out stuff a bit more, and updated my answer to reflect that. Sorry for not seeing it at first.

Um, for the broadcast, would that say:
[executor of command] has changed the weather to [weather condition]
eg BobbytheTrashcan has changed the weather to clear

Also, whenever I run the command in my server, I get the error: No value present.

Any ideas?

Make sure you’re changing the argument retrieval too, it’s not a String anymore.
Also, just an extra bit, make sure to pad your strings with spaces - right now it’s more likely to say “BobbytheTrashcanhas changed the weather toclear”
If you’ve changed the argument type to the cataloged type instead of String, you should comma-separate the ‘weathercondition’ piece instead of using +, since it will then translate it appropriately.

You should change the weather command variable to lowerCamelCase. You don’t need the Game injection, remove it and use Sponge. You already did that to broadcast a message.