/*
 * 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.nbt;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fml.common.FMLCommonHandler;
import org.apache.logging.log4j.Level;
import org.spongepowered.api.util.PEBKACException;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.util.PrettyPrinter;
import org.spongepowered.common.SpongeImpl;

import java.lang.reflect.Method;

@Mixin(NBTTagCompound.class)
public class MixinNBTTagCompound {

    @Inject(method = "setTag(Ljava/lang/String;Lnet/minecraft/nbt/NBTBase;)V", at = @At("HEAD"))
    private void checkNullTag(String key, NBTBase value, CallbackInfo callbackInfo) {
        if (value == null) {
            final PrettyPrinter printer = new PrettyPrinter(60);
            printer.add("Null being stored in NBT!!!").centre().hr();
            printer.addWrapped("Sponge is forcing a shutdown of the game because someone is storing nulls in an NBTTagCompound. "
                               + "Our implementation and Minecraft's strictly prevents this from happening, however, certain mods are"
                               + "not null checking. Please provide this report in a report to the associated mod!");
            printer.add("NBT Key: %s", key);
            printer.add("Exception!");
            final PEBKACException pebkacException = new PEBKACException("Someone is trying to store a null to an NBTTagCompound!");
            printer.add(pebkacException);
            printer.log(SpongeImpl.getLogger(), Level.ERROR);
            try {
                final Class<?> terminateVm = Class.forName("org.spongepowered.mixin.handler.TerminateVM");
                final Method terminate = terminateVm.getMethod("terminate");
                terminate.setAccessible(true);
                terminate.invoke(null, "net.minecraftforge.fml", -2);
            } catch (Exception e) {
                FMLCommonHandler.instance().exitJava(-2, true);
            }
        }
    }

}

@@ -1414,24 +1414,120 @@
 monHandler;%0A
+import net.minecraftforge.fml.common.Loader;%0Aimport net.minecraftforge.fml.common.ModContainer;%0A
 import org.a
@@ -2336,18 +2336,16 @@
  in NBT!
-!!
 %22).centr
@@ -2486,44 +2486,8 @@
 nd. 
-%22%0A                               + %22
 Our 
@@ -2501,16 +2501,35 @@
 ntation 
+%22%0A              + %22
 and Mine
@@ -2604,44 +2604,9 @@
  are
-%22%0A                               + %22
+ 
 not 
@@ -2650,16 +2650,35 @@
 port in 
+%22%0A              + %22
 a report
@@ -2700,24 +2700,327 @@
 ted mod!%22);%0A
+            final ModContainer mod = Loader.instance().activeModContainer();%0A            if (mod != null) %7B%0A                printer.add(%22Potential Culprit: %22 + mod.getModId());%0A            %7D else %7B%0A                printer.add(%22Potential Culprit: Not known, analyze stacktrace for clues%22);%0A            %7D%0A
             

Print possible culprit that is setting null nbt tags.