/*
 * This file is part of Nucleus, licensed under the MIT License (MIT). See the LICENSE.txt file
 * at the root of this project for more details.
 */
package io.github.nucleuspowered.nucleus.modules.warp.commands;

import io.github.nucleuspowered.nucleus.api.module.warp.NucleusWarpService;
import io.github.nucleuspowered.nucleus.api.module.warp.data.Warp;
import io.github.nucleuspowered.nucleus.modules.warp.WarpPermissions;
import io.github.nucleuspowered.nucleus.modules.warp.event.DeleteWarpEvent;
import io.github.nucleuspowered.nucleus.modules.warp.services.WarpService;
import io.github.nucleuspowered.nucleus.scaffold.command.ICommandContext;
import io.github.nucleuspowered.nucleus.scaffold.command.ICommandExecutor;
import io.github.nucleuspowered.nucleus.scaffold.command.ICommandResult;
import io.github.nucleuspowered.nucleus.scaffold.command.annotation.Command;
import io.github.nucleuspowered.nucleus.scaffold.command.annotation.EssentialsEquivalent;
import io.github.nucleuspowered.nucleus.services.INucleusServiceCollection;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandElement;
import org.spongepowered.api.event.CauseStackManager;
import org.spongepowered.api.util.annotation.NonnullByDefault;

@EssentialsEquivalent({"delwarp", "remwarp", "rmwarp"})
@NonnullByDefault
@Command(
        aliases = {"delete", "del"},
        basePermission = WarpPermissions.BASE_WARP_LIST,
        commandDescriptionKey = "warp.list",
        async = true,
        parentCommand = WarpCommand.class
)
public class DeleteWarpCommand implements ICommandExecutor<CommandSource> {

    @Override
    public CommandElement[] parameters(INucleusServiceCollection serviceCollection) {
        return new CommandElement[] {
                serviceCollection.getServiceUnchecked(WarpService.class).warpElement(false)
        };
    }

    @Override public ICommandResult execute(ICommandContext<? extends CommandSource> context) throws CommandException {
        Warp warp = context.requireOne(WarpService.WARP_KEY, Warp.class);
        NucleusWarpService qs = Sponge.getServiceManager().provideUnchecked(NucleusWarpService.class);

        try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) {
            frame.pushCause(context.getCommandSource());
            DeleteWarpEvent event = new DeleteWarpEvent(frame.getCurrentCause(), warp);
            if (Sponge.getEventManager().post(event)) {
                return event.getCancelMessage().map(context::errorResultLiteral)
                        .orElseGet(() -> context.errorResult("nucleus.eventcancelled"));
            }

            if (qs.removeWarp(warp.getName())) {
                // Worked. Tell them.
                context.sendMessage("command.warps.del", warp.getName());
                return context.successResult();
            }

            // Didn't work. Tell them.
            return context.errorResult("command.warps.delerror");
        }
    }

}

@@ -1276,33 +1276,27 @@
 i.event.
-C
+c
 ause
-StackManager
+.Cause
 ;%0Aimport
@@ -1980,16 +1980,20 @@
 Override
+%0A   
  public 
@@ -2281,127 +2281,198 @@
     
-try (CauseStackManager.StackFrame frame = Sponge.getCauseStackManager().pushCauseFrame()) %7B%0A            frame.pushCause
+final Cause cause;%0A        if (context.getCause().root() == context.getCommandSource()) %7B%0A            cause = context.getCause();%0A        %7D else %7B%0A            cause = context.getCause().with
 (con
@@ -2493,32 +2493,39 @@
 dSource());%0A    
+    %7D%0A%0A
         DeleteWa
@@ -2564,31 +2564,13 @@
 ent(
-frame.getCurrentC
+c
 ause
-()
 , wa
@@ -2574,20 +2574,16 @@
  warp);%0A
-    
         
@@ -2634,28 +2634,24 @@
             
-  
-  
 return event
@@ -2703,20 +2703,16 @@
 iteral)%0A
-    
         
@@ -2792,35 +2792,27 @@
 ));%0A        
-  
-  
 %7D%0A%0A
-    
         if (
@@ -2856,20 +2856,16 @@
         
-  
-  
 // Worke
@@ -2870,36 +2870,32 @@
 ked. Tell them.%0A
-    
             cont
@@ -2952,36 +2952,32 @@
 ));%0A            
-    
 return context.s
@@ -2996,28 +2996,24 @@
 ();%0A        
-    
 %7D%0A%0A         
@@ -3011,20 +3011,16 @@
         
-  
-  
 // Didn'
@@ -3030,36 +3030,32 @@
 ork. Tell them.%0A
-    
         return c
@@ -3100,26 +3100,16 @@
 rror%22);%0A
-        %7D%0A
     %7D%0A%0A%7D

Fix warp deletion not firing.