package net.fabricmc.base.launch;

import com.google.common.base.Charsets;
import com.google.common.io.ByteStreams;
import net.fabricmc.api.Side;
import net.fabricmc.base.loader.MixinLoader;
import net.fabricmc.base.util.mixin.MixinDevRemapper;
import net.fabricmc.base.util.mixin.MixinPrebaker;
import org.spongepowered.asm.launch.MixinBootstrap;
import org.spongepowered.asm.mixin.MixinEnvironment;
import org.spongepowered.asm.mixin.Mixins;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FabricMixinBootstrap {
    public static final String APPLIED_MIXIN_CONFIGS_FILENAME = ".fabric-applied-mixin-configs";
    public static final String MAPPINGS_FILENAME = ".fabric-dev-mappings.tiny";
    private static List<String> appliedMixinConfigs;
    private static boolean initialized = false;
    private static File mappingFile;

    public static void setMappingFile(File value) {
        mappingFile = value;
    }

    public static void addConfiguration(String configuration) {
        if (appliedMixinConfigs == null || !appliedMixinConfigs.contains(configuration)) {
            Mixins.addConfiguration(configuration);
        }
    }

    public static void init(Side side, MixinLoader mixinLoader) {
        if (initialized) {
            throw new RuntimeException("FabricMixinBootstrap has already been initialized!");
        }

        InputStream appliedMixinsStream = MixinPrebaker.class.getClassLoader().getResourceAsStream(APPLIED_MIXIN_CONFIGS_FILENAME);
        if (appliedMixinsStream != null) {
            try {
                byte[] data = ByteStreams.toByteArray(appliedMixinsStream);
                appliedMixinConfigs = Arrays.asList(new String(data, Charsets.UTF_8).split("\n"));
                appliedMixinsStream.close();
            } catch (IOException e) {
                System.err.println("Fabric development environment setup error - the game will probably crash soon!");
                e.printStackTrace();
            }
        }

        try {
            InputStream mappingStream = mappingFile != null
                    ? new FileInputStream(mappingFile)
                    : MixinPrebaker.class.getClassLoader().getResourceAsStream(MAPPINGS_FILENAME);

            if (mappingStream != null) {
                try {
                    MixinDevRemapper remapper = new MixinDevRemapper();
                    remapper.readMapping(new BufferedReader(new InputStreamReader(mappingStream)), "mojang", "pomf");
                    mappingStream.close();

                    MixinEnvironment.getDefaultEnvironment().getRemappers().add(remapper);
                } catch (IOException e) {
                    System.err.println("Fabric development environment setup error - the game will probably crash soon!");
                    e.printStackTrace();
                }
            }
        } catch (FileNotFoundException e) {
            // Ignore
        }

        MixinBootstrap.init();

        appliedMixinConfigs = new ArrayList<>();
        appliedMixinConfigs.add("fabricmc.mixins.common.json");
        if (side.hasClient()) {
            appliedMixinConfigs.add("fabricmc.mixins.client.json");
        }
        if (side.hasServer()) {
            appliedMixinConfigs.add("fabricmc.mixins.server.json");
        }

        appliedMixinConfigs.addAll(mixinLoader.getCommonMixinConfigs());
        if (side.hasClient()) {
            appliedMixinConfigs.addAll(mixinLoader.getClientMixinConfigs());
        }
        if (side.hasServer()) {
            appliedMixinConfigs.addAll(mixinLoader.getServerMixinConfigs());
        }

        appliedMixinConfigs.forEach(Mixins::addConfiguration);

        initialized = true;
    }

    public static List<String> getAppliedMixinConfigs() {
        return appliedMixinConfigs;
    }
}

@@ -509,16 +509,45 @@
 Arrays;%0A
+import java.util.Collection;%0A
 import j
@@ -3033,79 +3033,23 @@
    a
-ppliedMixinConfigs = new ArrayList%3C%3E();%0A        appliedMixinConfigs.add
+ddConfiguration
 (%22fa
@@ -3122,38 +3122,31 @@
        a
-ppliedMixinConfigs.add
+ddConfiguration
 (%22fabric
@@ -3225,38 +3225,31 @@
        a
-ppliedMixinConfigs.add
+ddConfiguration
 (%22fabric
@@ -3296,35 +3296,8 @@
     
-appliedMixinConfigs.addAll(
 mixi
@@ -3319,32 +3319,79 @@
 onMixinConfigs()
+.forEach(FabricMixinBootstrap::addConfiguration
 );%0A        if (s
@@ -3425,35 +3425,8 @@
     
-appliedMixinConfigs.addAll(
 mixi
@@ -3448,32 +3448,79 @@
 ntMixinConfigs()
+.forEach(FabricMixinBootstrap::addConfiguration
 );%0A        %7D%0A   
@@ -3564,99 +3564,29 @@
     
-appliedMixinConfigs.addAll(mixinLoader.getServerMixinConfigs());%0A        %7D%0A%0A        applied
+mixinLoader.getClient
 Mixi
@@ -3593,16 +3593,18 @@
 nConfigs
+()
 .forEach
@@ -3604,22 +3604,36 @@
 forEach(
-Mixins
+FabricMixinBootstrap
 ::addCon
@@ -3644,16 +3644,26 @@
 ration);
+%0A        %7D
 %0A%0A      

fix very bad mistake