/*
 * 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.mod.service.permission;

import com.google.common.base.Optional;
import org.spongepowered.api.entity.player.Player;
import org.spongepowered.api.service.permission.Subject;
import org.spongepowered.api.service.permission.context.Context;
import org.spongepowered.api.service.permission.context.ContextCalculator;
import org.spongepowered.api.util.command.CommandSource;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.extent.Extent;

import java.util.Set;

/**
 * A context calculator handling world contexts.
 */
public class SpongeContextCalculator implements ContextCalculator {
    @Override
    public void accumulateContexts(Subject subject, Set<Context> accumulator) {
        Optional<CommandSource> subjSource = subject.getCommandSource();
        if (subjSource.isPresent() && subjSource.get() instanceof Player) {
            Extent currentExt = ((Player) subjSource.get()).getLocation().getExtent();
            if (currentExt instanceof World) {
                accumulator.add(((World) currentExt).getContext());
                accumulator.add((((World) currentExt).getDimension().getContext()));
            }
        }
    }

    @Override
    public boolean matches(Context context, Subject subject) {
        Optional<CommandSource> subjSource = subject.getCommandSource();
        if (subjSource.isPresent() && subjSource.get() instanceof Player && context.getType().equals(Context.WORLD_KEY)) {
            if (context.getType().equals(Context.WORLD_KEY)) {
                Extent currentExt = ((Player) subjSource.get()).getLocation().getExtent();
                if (currentExt instanceof World) {
                    return ((World) currentExt).getContext().equals(context);
                }
            } else if (context.getType().equals(Context.DIMENSION_KEY)) {
                Extent currentExt = ((Player) subjSource.get()).getLocation().getExtent();
                if (currentExt instanceof World) {
                    return ((World) currentExt).getDimension().getContext().equals(context);
                }
            }
        }
        return false;
    }
}

@@ -1336,59 +1336,8 @@
 al;%0A
-import org.spongepowered.api.entity.player.Player;%0A
 impo
@@ -1619,19 +1619,41 @@
 api.
-world.World
+util.command.source.LocatedSource
 ;%0Aim
@@ -1689,21 +1689,13 @@
 rld.
-extent.Extent
+World
 ;%0A%0Ai
@@ -2074,22 +2074,29 @@
 tanceof 
-Player
+LocatedSource
 ) %7B%0A    
@@ -2095,38 +2095,37 @@
 ) %7B%0A            
-Extent
+World
  currentExt = ((
@@ -2116,38 +2116,45 @@
  currentExt = ((
-Player
+LocatedSource
 ) subjSource.get
@@ -2164,81 +2164,17 @@
 .get
-Location().getExtent();%0A            if (currentExt instanceof World) %7B%0A  
+World();%0A
     
@@ -2173,34 +2173,32 @@
 ();%0A            
-  
 accumulator.add(
@@ -2197,25 +2197,16 @@
 tor.add(
-((World) 
 currentE
@@ -2199,33 +2199,32 @@
 r.add(currentExt
-)
 .getContext());%0A
@@ -2227,36 +2227,32 @@
 ));%0A            
-    
 accumulator.add(
@@ -2252,25 +2252,16 @@
 or.add((
-((World) 
 currentE
@@ -2254,33 +2254,32 @@
 .add((currentExt
-)
 .getDimension().
@@ -2294,30 +2294,16 @@
 xt()));%0A
-            %7D%0A
         
@@ -2527,22 +2527,29 @@
 tanceof 
-Player
+LocatedSource
  && cont
@@ -2607,153 +2607,67 @@
     
-if (context.getType().equals(Context.WORLD_KEY)) %7B%0A                Extent currentExt = ((Player) subjSource.get()).getLocation().getExtent();%0A   
+LocatedSource source = ((LocatedSource) subjSource.get());%0A
     
@@ -2678,48 +2678,59 @@
     
- 
 if (c
-urrentExt instanceof World) %7B%0A    
+ontext.getType().equals(Context.WORLD_KEY)) %7B%0A
     
@@ -2748,35 +2748,32 @@
  return 
-((World) currentExt
+source.getWorld(
 ).getCon
@@ -2796,34 +2796,16 @@
 ntext);%0A
-                %7D%0A
         
@@ -2890,180 +2890,31 @@
     
-Extent currentExt = ((Player) subjSource.get()).getLocation().getExtent();%0A                if (currentExt instanceof World) %7B%0A                    return ((World) currentExt
+return source.getWorld(
 ).ge
@@ -2960,26 +2960,8 @@
 t);%0A
-                %7D%0A
     

Update context calculator to work for any LocatedSubject