/*
 * 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.common.mixin.blockcapturing;

import net.minecraft.block.Block;
import net.minecraft.world.World;
import org.spongepowered.api.block.BlockType;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.common.config.SpongeConfig;
import org.spongepowered.common.config.category.BlockCapturingCategory;
import org.spongepowered.common.config.category.BlockCapturingModCategory;
import org.spongepowered.common.config.type.GeneralConfigBase;
import org.spongepowered.common.interfaces.world.IMixinWorldServer;
import org.spongepowered.common.mixin.plugin.blockcapturing.IModData_BlockCapturing;

@Mixin(Block.class)
public abstract class MixinBlock_BlockCapturing implements IModData_BlockCapturing, BlockType {

    private boolean processTickChangesImmediately;
    private boolean refreshCache = true;

    @Override
    public boolean processTickChangesImmediately() {
        return this.processTickChangesImmediately;
    }

    @Override
    public void initializeBlockCapturingState(World worldIn) {
        SpongeConfig<? extends GeneralConfigBase> activeConfig = ((IMixinWorldServer) worldIn).getActiveConfig();
        BlockCapturingCategory blockCapturing = activeConfig.getConfig().getBlockCapturing();
        String[] ids = this.getId().split(":");
        String modId = ids[0];
        String name = ids[1];

        BlockCapturingModCategory modCapturing = blockCapturing.getModMappings().get(modId);
        if (modCapturing == null && blockCapturing.autoPopulateData()) {
            modCapturing = new BlockCapturingModCategory();
            blockCapturing.getModMappings().put(modId.toLowerCase(), modCapturing);
            modCapturing.getBlockMap().put(name.toLowerCase(), false);
            if (blockCapturing.autoPopulateData()) {
                activeConfig.save();
            }
            return;
        } else if (modCapturing != null) {
            if (!modCapturing.isEnabled()) {
                this.processTickChangesImmediately = false;
                return;
            }

            Boolean processImmediately = modCapturing.getBlockMap().get(name.toLowerCase());
            if (processImmediately != null) {
                this.processTickChangesImmediately = processImmediately;
            }
        }

        if (blockCapturing.autoPopulateData()) {
            activeConfig.save();
        }
    }

    @Override
    public boolean requiresBlockCapturingRefresh() {
        return this.refreshCache;
    }

    @Override
    public void requiresBlockCapturingRefresh(boolean refresh) {
        this.refreshCache = refresh;
    }

}

@@ -1365,16 +1365,55 @@
 .World;%0A
+import org.apache.logging.log4j.Level;%0A
 import o
@@ -1492,16 +1492,109 @@
 .Mixin;%0A
+import org.spongepowered.asm.util.PrettyPrinter;%0Aimport org.spongepowered.common.SpongeImpl;%0A
 import o
@@ -2670,16 +2670,1186 @@
 t(%22:%22);%0A
+        if (ids.length != 2) %7B%0A            final PrettyPrinter printer = new PrettyPrinter(60).add(%22Malformatted Block ID discovered!%22).centre().hr()%0A                .addWrapped(60, %22Sponge has found a malformatted block id when trying to%22%0A                                + %22 load configurations for the block id. The printed out block id%22%0A                                + %22is not originally from sponge, and should be brought up with the%22%0A                                + %22mod developer as the registration for this block is not likely%22%0A                                + %22to work with other systems and assumptions of having a properly%22%0A                                + %22formatted block id.%22)%0A                .add(%22%25s : %25s%22, %22Malformed ID%22, this.getId())%0A                .add(%22%25s : %25s%22, %22Discovered id array%22, ids)%0A                .add();%0A            final String id = ids%5B0%5D;%0A            ids = new String%5B%5D%7B%22unknown%22, id%7D;%0A            printer%0A                .add(%22Sponge will attempt to work around this by using the provided generated id:%22)%0A                .add(%22%25s : %25s%22, %22Generated ID%22, ids)%0A                .log(SpongeImpl.getLogger(), Level.WARN);%0A%0A        %7D%0A
         

Add failsafe for malformed block id registrations causing a crash when loading configs. This fixes #1845.;