/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.common.event.tracking.context.transaction.effect;

import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.block.state.BlockState;
import org.spongepowered.common.bridge.world.level.block.state.BlockStateBridge;
import org.spongepowered.common.event.tracking.context.transaction.pipeline.BlockPipeline;
import org.spongepowered.common.event.tracking.context.transaction.pipeline.PipelineCursor;
import org.spongepowered.common.world.SpongeBlockChangeFlag;

public final class UpdateLightSideEffect implements ProcessingSideEffect {

    private static final class Holder {
        static final UpdateLightSideEffect INSTANCE = new UpdateLightSideEffect();
    }

    public static UpdateLightSideEffect getInstance() {
        return UpdateLightSideEffect.Holder.INSTANCE;
    }

    UpdateLightSideEffect() {}

    @Override
    public EffectResult processSideEffect(final BlockPipeline pipeline, final PipelineCursor oldState,
        final BlockState newState, final SpongeBlockChangeFlag flag, final int limit
    ) {
        if (!flag.updateLighting()) {
            return EffectResult.NULL_PASS;
        }
        final int originalOpactiy = oldState.opacity;
        final ServerLevel serverWorld = pipeline.getServerWorld();
        final BlockState currentState = pipeline.getAffectedChunk().getBlockState(oldState.pos);
        if (oldState.state != currentState && (((BlockStateBridge) currentState).bridge$getLightValue(serverWorld, oldState.pos) != originalOpactiy || currentState.useShapeForLightOcclusion() || oldState.state.useShapeForLightOcclusion())) {
            // this.profiler.startSection("queueCheckLight");
            serverWorld.getProfiler().push("queueCheckLight");
            // this.getChunkProvider().getLightManager().checkBlock(pos);
            serverWorld.getChunkSource().getLightEngine().checkBlock(oldState.pos);
            // this.profiler.endSection();
            serverWorld.getProfiler().pop();
        }
        return EffectResult.NULL_PASS;
    }

}

@@ -2100,18 +2100,23 @@
 fect() %7B
+%0A    
 %7D%0A
-
 %0A    @Ov
@@ -2164,16 +2164,25 @@
 eEffect(
+%0A        
 final Bl
@@ -2648,96 +2648,647 @@
     
-if (oldState.state != currentState && (((BlockStateBridge) currentState).bridge$
+// local variable notes:%0A        // var2 = oldState.state%0A        // var3 = currentState%0A        // if (%0A        //      (param2 & 128) == 0 // this is handled above as flag.updateLighting()%0A        //      && var3 != var2%0A        //      && (%0A        //          var3.getLightBlock(this, param0) != var2.getLightBlock(this, param0)%0A        //          %7C%7C var3.getLightEmission() != var2.getLightEmission()%0A        //          %7C%7C var3.useShapeForLightOcclusion()%0A        //          %7C%7C var2.useShapeForLightOcclusion()%0A        //         )%0A        //     ) %7B%0A        if (oldState.state != currentState%0A            && (currentState.
 getLight
 Valu
@@ -3287,13 +3287,13 @@
 ight
-Value
+Block
 (ser
@@ -3334,16 +3334,112 @@
 lOpactiy
+%0A            %7C%7C currentState.getLightEmission() != oldState.state.getLightEmission()%0A           
  %7C%7C curr
@@ -3474,16 +3474,28 @@
 lusion()
+%0A           
  %7C%7C oldS
@@ -3532,16 +3532,25 @@
 lusion()
+%0A        
 )) %7B%0A   

Fix UpdateLightSideEffect