use of org.spongepowered.api.world.weather.WeatherUniverse in project LanternServer by LanternPowered.
the class CommandWeather method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), new PatternMatchingCommandElement(Text.of("type")) {
@Override
protected Iterable<String> getChoices(CommandSource source) {
Collection<Weather> weathers = Sponge.getRegistry().getAllOf(Weather.class);
ImmutableList.Builder<String> builder = ImmutableList.builder();
for (Weather weather : weathers) {
builder.add(weather.getId());
builder.addAll(((LanternWeather) weather).getAliases());
}
return builder.build();
}
@Override
protected Object getValue(String choice) throws IllegalArgumentException {
final Optional<Weather> optWeather = Sponge.getRegistry().getType(Weather.class, choice);
if (!optWeather.isPresent()) {
return Sponge.getRegistry().getAllOf(Weather.class).stream().filter(weather -> {
for (String alias : ((LanternWeather) weather).getAliases()) {
if (alias.equalsIgnoreCase(choice)) {
return true;
}
}
return false;
}).findAny().orElseThrow(() -> new IllegalArgumentException("Invalid input " + choice + " was found"));
}
return optWeather.get();
}
}, GenericArguments.optional(GenericArguments.integer(Text.of("duration")))).executor((src, args) -> {
LanternWorldProperties world = CommandHelper.getWorldProperties(src, args);
WeatherUniverse weatherUniverse = world.getWorld().get().getWeatherUniverse().orElse(null);
Weather type = args.<Weather>getOne("type").get();
if (weatherUniverse != null) {
if (args.hasAny("duration")) {
weatherUniverse.setWeather(type, args.<Integer>getOne("duration").get() * 20);
} else {
weatherUniverse.setWeather(type);
}
}
src.sendMessage(t("Changing to " + type.getName() + " weather"));
return CommandResult.success();
});
}
Aggregations