package com.elmakers.mine.bukkit.action.builtin;

import org.bukkit.Location;
import org.bukkit.configuration.ConfigurationSection;

import com.elmakers.mine.bukkit.action.BaseSpellAction;
import com.elmakers.mine.bukkit.api.action.CastContext;
import com.elmakers.mine.bukkit.api.magic.MageController;
import com.elmakers.mine.bukkit.api.spell.SpellResult;

public class LightAction extends BaseSpellAction {
    private int level;
    private boolean async;
    private boolean update;
    private MageController controller;

    private class LightUndoAction implements Runnable
    {
        private final Location location;
        private final boolean update;

        public LightUndoAction(Location location, boolean update) {
            this.location = location;
            this.update = update;
        }

        @Override
        public void run() {
            if (location != null) {
                controller.deleteLight(location, async);
                if (update) {
                    controller.updateLight(location);
                }
            }
        }
    }

    @Override
    public void prepare(CastContext context, ConfigurationSection parameters)
    {
        super.prepare(context, parameters);
        controller = context.getController();
        async = parameters.getBoolean("async", false);
        update = parameters.getBoolean("update", true);
        level = parameters.getInt("level", 15);
    }

    @Override
    public SpellResult perform(CastContext context)
    {
        Location location = context.getTargetLocation();
        context.addWork(5);
        if (!controller.createLight(location, level, async)) {
            return SpellResult.FAIL;
        }
        if (update) {
            controller.updateLight(location);
            context.addWork(10);
        }
        context.registerForUndo(new LightUndoAction(location, update));
        return SpellResult.CAST;
    }

    @Override
    public boolean isUndoable()
    {
        return true;
    }

    @Override
    public boolean requiresTarget()
    {
        return true;
    }
}

@@ -477,24 +477,55 @@
 ean update;%0A
+    private boolean forceUndo;%0A
     private 
@@ -932,32 +932,50 @@
 %0A               
+ boolean removed =
  controller.dele
@@ -1018,32 +1018,43 @@
       if (update
+ && removed
 ) %7B%0A            
@@ -1084,32 +1084,39 @@
 teLight(location
+, false
 );%0A             
@@ -1494,24 +1494,91 @@
 evel%22, 15);%0A
+        forceUndo = parameters.getBoolean(%22undo_previous%22, false);%0A
     %7D%0A%0A    @
@@ -1636,32 +1636,141 @@
  context)%0A    %7B%0A
+        if (!context.getController().isLightingAvailable()) %7B%0A            return SpellResult.FAIL;%0A        %7D%0A
         Location
@@ -1846,25 +1846,24 @@
         if (
-!
 controller.c
@@ -1900,71 +1900,12 @@
 ync)
-) %7B%0A            return SpellResult.FAIL;%0A        %7D%0A        if (
+ && 
 upda
@@ -1991,32 +1991,159 @@
 (10);%0A        %7D%0A
+        if (forceUndo) %7B%0A            context.getMage().undoScheduled(context.getSpell().getSpellKey().getBaseKey());%0A        %7D%0A
         context.

Add undo_previous parameter to Light action, this did not work out for some reason