/*
 * 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.mixin.core.world.storage;

import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.datafix.DataFixer;
import net.minecraft.world.storage.SaveFormatOld;
import net.minecraft.world.storage.SaveHandler;
import net.minecraft.world.storage.WorldInfo;
import net.minecraftforge.fml.common.FMLCommonHandler;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;
import org.spongepowered.common.interfaces.IMixinSaveHandler;

import java.io.File;

@Mixin(value = SaveHandler.class, priority = 1001)
public abstract class MixinSaveHandler {

    @Shadow @Final protected DataFixer dataFixer;
    @Shadow @Final private File worldDirectory;

    @Redirect(method = "saveWorldInfoWithPlayer", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/fml/common/FMLCommonHandler;handleWorldDataSave"
            + "(Lnet/minecraft/world/storage/SaveHandler;Lnet/minecraft/world/storage/WorldInfo;Lnet/minecraft/nbt/NBTTagCompound;)V", remap = false))
    public void redirectHandleWorldDataSaveWithPlayer(FMLCommonHandler fml, SaveHandler handler, WorldInfo worldInformation, NBTTagCompound
            compound) {
        if (fml.getSavesDirectory().equals(this.worldDirectory.getParentFile())) {
            fml.handleWorldDataSave(handler, worldInformation, compound);
        }
    }

    @Redirect(method = "loadWorldInfo", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/storage/SaveFormatOld;loadAndFix(Ljava/io/File;Lnet/minecraft/util/datafix/DataFixer;Lnet/minecraft/world/storage/SaveHandler;)Lnet/minecraft/world/storage/WorldInfo;", remap = false))
    private WorldInfo onLoadWorldInfo(File file, DataFixer fixer, SaveHandler handler) {
        final WorldInfo worldInfo = SaveFormatOld.loadAndFix(file, fixer, handler);
        if (worldInfo != null) {
            try {
                ((IMixinSaveHandler) handler).loadSpongeDatData(worldInfo);
            } catch (Exception e) {
                throw new RuntimeException("derp", e);
            }
        }
        return worldInfo;
    }

}

@@ -1579,24 +1579,157 @@
 monHandler;%0A
+import net.minecraftforge.fml.common.Loader;%0Aimport net.minecraftforge.fml.common.ModContainer;%0Aimport org.spongepowered.api.Sponge;%0A
 import org.s
@@ -1758,16 +1758,16 @@
 .Final;%0A
-
 import o
@@ -1892,16 +1892,69 @@
 ion.At;%0A
+import org.spongepowered.asm.mixin.injection.Inject;%0A
 import o
@@ -2029,40 +2029,329 @@
 red.
-common.interfaces.IMixinSaveHand
+asm.mixin.injection.callback.CallbackInfoReturnable;%0Aimport org.spongepowered.common.SpongeImpl;%0Aimport org.spongepowered.common.interfaces.IMixinSaveHandler;%0Aimport org.spongepowered.common.plugin.PluginContainerExtension;%0Aimport org.spongepowered.mod.SpongeMod;%0Aimport org.spongepowered.mod.interfaces.IMixinLoadControl
 ler;
@@ -3942,10 +3942,714 @@
 %0A    %7D%0A%0A
+    @Inject(method = %22getWorldDirectory%22, at = @At(%22HEAD%22), cancellable = true)%0A    public void onGetWorldDirectory(CallbackInfoReturnable%3CFile%3E cir) %7B%0A        final ModContainer activeContainer = Loader.instance().activeModContainer();%0A        // Since Forge uses a single save handler mods will expect this method to return overworld's world directory%0A        // Fixes mods such as ComputerCraft and FuturePack%0A        if ((activeContainer != null && activeContainer != SpongeMod.instance && !(activeContainer instanceof PluginContainerExtension))) %7B%0A            final File directory = new File(%22.%22, Sponge.getServer().getDefaultWorldName());%0A            cir.setReturnValue(directory);%0A        %7D%0A    %7D%0A
 %7D%0A

Handle getWorldDirectory properly for mods. Fixes #1656