Search in sources :

Example 1 with ChangeWeatherEvent

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

the class ServerLevelMixin method impl$onSetWeatherParameters.

@Inject(method = "tick", locals = LocalCapture.CAPTURE_FAILEXCEPTION, at = @At(value = "FIELD", target = "Lnet/minecraft/server/level/ServerLevel;oRainLevel:F", shift = At.Shift.BEFORE, ordinal = 1))
public void impl$onSetWeatherParameters(final BooleanSupplier param0, final CallbackInfo ci, final ProfilerFiller var0, final boolean wasRaining) {
    final boolean isRaining = this.shadow$isRaining();
    if (this.oRainLevel != this.rainLevel || this.oThunderLevel != this.thunderLevel || wasRaining != isRaining) {
        Weather newWeather = ((ServerWorld) this).properties().weather();
        final Cause currentCause = Sponge.server().causeStackManager().currentCause();
        final Transaction<Weather> weatherTransaction = new Transaction<>(this.impl$prevWeather, newWeather);
        final ChangeWeatherEvent event = SpongeEventFactory.createChangeWeatherEvent(currentCause, ((ServerWorld) this), weatherTransaction);
        if (Sponge.eventManager().post(event)) {
            newWeather = event.weather().original();
        } else {
            newWeather = event.weather().finalReplacement();
        }
        // Set event results
        this.impl$prevWeather = newWeather;
        if (newWeather.type() == WeatherTypes.CLEAR.get()) {
            this.serverLevelData.setThunderTime(0);
            this.serverLevelData.setRainTime(0);
            this.serverLevelData.setClearWeatherTime((int) newWeather.remainingDuration().ticks());
            this.serverLevelData.setThundering(false);
            this.serverLevelData.setRaining(false);
        } else {
            final int newTime = (int) newWeather.remainingDuration().ticks();
            this.serverLevelData.setRaining(true);
            this.serverLevelData.setClearWeatherTime(0);
            this.serverLevelData.setRainTime(newTime);
            if (newWeather.type() == WeatherTypes.THUNDER.get()) {
                this.serverLevelData.setThunderTime(newTime);
                this.serverLevelData.setThundering(true);
            } else {
                this.serverLevelData.setThunderTime(0);
                this.serverLevelData.setThundering(false);
            }
        }
    }
}
Also used : Weather(org.spongepowered.api.world.weather.Weather) ChangeWeatherEvent(org.spongepowered.api.event.world.ChangeWeatherEvent) ServerWorld(org.spongepowered.api.world.server.ServerWorld) Transaction(org.spongepowered.api.data.Transaction) Cause(org.spongepowered.api.event.Cause) Inject(org.spongepowered.asm.mixin.injection.Inject)

Aggregations

Transaction (org.spongepowered.api.data.Transaction)1 Cause (org.spongepowered.api.event.Cause)1 ChangeWeatherEvent (org.spongepowered.api.event.world.ChangeWeatherEvent)1 ServerWorld (org.spongepowered.api.world.server.ServerWorld)1 Weather (org.spongepowered.api.world.weather.Weather)1 Inject (org.spongepowered.asm.mixin.injection.Inject)1