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);
}
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();
});
}
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);
}
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);
}
}
Aggregations