Search in sources :

Example 6 with LanternWeather

use of org.lanternpowered.server.world.weather.LanternWeather in project LanternServer by LanternPowered.

the class LanternWeatherUniverse method pulseWeather.

private void pulseWeather(CauseStack causeStack) {
    if (!this.world.getOrCreateRule(RuleTypes.DO_WEATHER_CYCLE).getValue()) {
        return;
    }
    this.weatherData.setRunningDuration(this.weatherData.getRunningDuration() + 1);
    final long remaining = this.weatherData.getRemainingDuration() - 1;
    if (remaining <= 0) {
        final LanternWeather next = this.nextWeather();
        if (setWeather(causeStack, next, next.getRandomTicksDuration(), true)) {
            // If the event is cancelled, continue the current weather for
            // a random amount of time, maybe more luck next time
            final LanternWeather current = this.weatherData.getWeather();
            this.weatherData.setRemainingDuration(current.getRandomTicksDuration());
        }
    } else {
        this.weatherData.setRemainingDuration(remaining);
    }
    this.weatherData.getWeather().getAction().run(this.context);
}
Also used : LanternWeather(org.lanternpowered.server.world.weather.LanternWeather)

Example 7 with LanternWeather

use of org.lanternpowered.server.world.weather.LanternWeather 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)

Example 8 with LanternWeather

use of org.lanternpowered.server.world.weather.LanternWeather in project LanternServer by LanternPowered.

the class LanternWeatherUniverse method nextWeather.

/**
 * Gets the next possible {@link LanternWeather}, ignoring
 * the last weather type.
 *
 * @return The next weather type
 */
@SuppressWarnings("unchecked")
private LanternWeather nextWeather() {
    final List<LanternWeather> weathers = new ArrayList(this.world.game.getRegistry().getAllOf(Weather.class));
    final LanternWeather current = this.weatherData.getWeather();
    weathers.remove(current);
    if (weathers.isEmpty()) {
        return current;
    }
    final WeightedTable<LanternWeather> table = new WeightedTable<>();
    weathers.forEach(weather -> table.add(weather, weather.getWeight()));
    return table.get(this.random).get(0);
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ArrayList(java.util.ArrayList)

Example 9 with LanternWeather

use of org.lanternpowered.server.world.weather.LanternWeather in project LanternServer by LanternPowered.

the class LanternWorldProperties method setThundering.

@Override
public void setThundering(boolean state) {
    LanternWeather weather = this.weatherData.getWeather();
    final boolean thunderStorm = weather == Weathers.THUNDER_STORM;
    if (thunderStorm != state) {
        weather = (LanternWeather) (state ? Weathers.THUNDER_STORM : Weathers.CLEAR);
        this.weatherData.setWeather(weather);
        this.weatherData.setRemainingDuration(weather.getRandomTicksDuration());
        this.weatherData.setRunningDuration(0);
    }
}
Also used : LanternWeather(org.lanternpowered.server.world.weather.LanternWeather)

Aggregations

LanternWeather (org.lanternpowered.server.world.weather.LanternWeather)9 Weather (org.spongepowered.api.world.weather.Weather)4 ArrayList (java.util.ArrayList)2 LanternWorldProperties (org.lanternpowered.server.world.LanternWorldProperties)2 Vector3i (com.flowpowered.math.vector.Vector3i)1 ImmutableList (com.google.common.collect.ImmutableList)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 List (java.util.List)1 UUID (java.util.UUID)1 WeatherUniverse (org.lanternpowered.api.world.weather.WeatherUniverse)1 LanternDimensionType (org.lanternpowered.server.world.dimension.LanternDimensionType)1 AbstractFlatGeneratorType (org.lanternpowered.server.world.gen.flat.AbstractFlatGeneratorType)1 CommandSource (org.spongepowered.api.command.CommandSource)1 PatternMatchingCommandElement (org.spongepowered.api.command.args.PatternMatchingCommandElement)1 DataContainer (org.spongepowered.api.data.DataContainer)1 DataQuery (org.spongepowered.api.data.DataQuery)1 DataView (org.spongepowered.api.data.DataView)1 ChangeWorldWeatherEvent (org.spongepowered.api.event.world.ChangeWorldWeatherEvent)1 WeightedTable (org.spongepowered.api.util.weighted.WeightedTable)1