Search in sources :

Example 1 with WeatherUniverse

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();
    });
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) PatternMatchingCommandElement(org.spongepowered.api.command.args.PatternMatchingCommandElement) ImmutableList(com.google.common.collect.ImmutableList) WeatherUniverse(org.spongepowered.api.world.weather.WeatherUniverse) CommandSource(org.spongepowered.api.command.CommandSource) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 LanternWorldProperties (org.lanternpowered.server.world.LanternWorldProperties)1 LanternWeather (org.lanternpowered.server.world.weather.LanternWeather)1 CommandSource (org.spongepowered.api.command.CommandSource)1 PatternMatchingCommandElement (org.spongepowered.api.command.args.PatternMatchingCommandElement)1 Weather (org.spongepowered.api.world.weather.Weather)1 WeatherUniverse (org.spongepowered.api.world.weather.WeatherUniverse)1