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!