Search in sources :

Example 1 with ChangeWorldWeatherEvent

use of org.spongepowered.api.event.world.ChangeWorldWeatherEvent in project LanternServer by LanternPowered.

the class LanternWeatherUniverse method setWeather.

private boolean setWeather(CauseStack causeStack, Weather weather, long duration, boolean doEvent) {
    checkNotNull(weather, "weather");
    final LanternWeather current = this.weatherData.getWeather();
    if (doEvent) {
        final ChangeWorldWeatherEvent event = SpongeEventFactory.createChangeWorldWeatherEvent(causeStack.getCurrentCause(), (int) duration, (int) duration, current, weather, weather, this.world);
        Sponge.getEventManager().post(event);
        if (event.isCancelled()) {
            return true;
        }
        weather = event.getWeather();
        duration = event.getDuration();
    }
    final LanternWeather weather0 = (LanternWeather) weather;
    this.weatherData.setRemainingDuration(duration);
    if (current != weather) {
        this.weatherData.setRunningDuration(0);
        this.weatherData.setWeather(weather0);
    }
    this.darknessTarget = weather0.getOptions().getOrDefault(WeatherOptions.SKY_DARKNESS).get().floatValue();
    this.rainStrengthTarget = weather0.getOptions().getOrDefault(WeatherOptions.RAIN_STRENGTH).get().floatValue();
    return false;
}
Also used : LanternWeather(org.lanternpowered.server.world.weather.LanternWeather) ChangeWorldWeatherEvent(org.spongepowered.api.event.world.ChangeWorldWeatherEvent)

Example 2 with ChangeWorldWeatherEvent

use of org.spongepowered.api.event.world.ChangeWorldWeatherEvent in project SpongeCommon by SpongePowered.

the class MixinWorldServer method onUpdateWeatherReturn.

@Inject(method = "updateWeather", at = @At(value = "RETURN"))
private void onUpdateWeatherReturn(CallbackInfo ci) {
    final Weather weather = getWeather();
    int duration = (int) getRemainingDuration();
    if (this.prevWeather != weather && duration > 0) {
        try (StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(this);
            final ChangeWorldWeatherEvent event = SpongeEventFactory.createChangeWorldWeatherEvent(frame.getCurrentCause(), duration, duration, weather, weather, this.prevWeather, this);
            if (Sponge.getEventManager().post(event)) {
                this.setWeather(this.prevWeather);
            } else {
                if (!weather.equals(event.getWeather()) || duration != event.getDuration()) {
                    this.setWeather(event.getWeather(), event.getDuration());
                    this.prevWeather = event.getWeather();
                    this.weatherStartTime = this.worldInfo.getWorldTotalTime();
                }
            }
        }
    }
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) StackFrame(org.spongepowered.api.event.CauseStackManager.StackFrame) ChangeWorldWeatherEvent(org.spongepowered.api.event.world.ChangeWorldWeatherEvent) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

ChangeWorldWeatherEvent (org.spongepowered.api.event.world.ChangeWorldWeatherEvent)2 LanternWeather (org.lanternpowered.server.world.weather.LanternWeather)1 StackFrame (org.spongepowered.api.event.CauseStackManager.StackFrame)1 Weather (org.spongepowered.api.world.weather.Weather)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1