package net.minecraft.client.main;

import org.apache.logging.log4j.LogManager;
import joptsimple.ArgumentAcceptingOptionSpec;
import javax.annotation.Nullable;
import com.google.gson.Gson;
import java.util.OptionalInt;
import java.util.List;
import joptsimple.OptionSet;
import net.minecraft.client.Options;
import net.minecraft.client.resources.language.LanguageManager;
import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.pipeline.RenderPipeline;
import net.minecraft.DefaultUncaughtExceptionHandler;
import net.minecraft.client.server.IntegratedServer;
import net.minecraft.client.Minecraft;
import com.mojang.blaze3d.platform.DisplayData;
import net.minecraft.client.User;
import net.minecraft.CrashReport;
import net.minecraft.world.entity.player.Player;
import joptsimple.OptionSpec;
import net.minecraft.util.GsonHelper;
import java.lang.reflect.Type;
import com.mojang.authlib.properties.PropertyMap;
import com.google.gson.GsonBuilder;
import java.net.PasswordAuthentication;
import java.net.Authenticator;
import java.net.SocketAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import net.minecraft.Util;
import java.io.File;
import joptsimple.OptionParser;
import org.apache.logging.log4j.Logger;

public class Main {
    private static final Logger LOGGER;
    
    public static void main(final String[] arr) {
        final OptionParser optionParser2 = new OptionParser();
        optionParser2.allowsUnrecognizedOptions();
        optionParser2.accepts("demo");
        optionParser2.accepts("fullscreen");
        optionParser2.accepts("checkGlErrors");
        final OptionSpec<String> optionSpec3 = optionParser2.accepts("server").withRequiredArg();
        final OptionSpec<Integer> optionSpec4 = optionParser2.accepts("port").withRequiredArg().<Integer>ofType(Integer.class).defaultsTo(25565, new Integer[0]);
        final OptionSpec<File> optionSpec5 = optionParser2.accepts("gameDir").withRequiredArg().<File>ofType(File.class).defaultsTo(new File("."), new File[0]);
        final OptionSpec<File> optionSpec6 = optionParser2.accepts("assetsDir").withRequiredArg().<File>ofType(File.class);
        final OptionSpec<File> optionSpec7 = optionParser2.accepts("resourcePackDir").withRequiredArg().<File>ofType(File.class);
        final OptionSpec<String> optionSpec8 = optionParser2.accepts("proxyHost").withRequiredArg();
        final OptionSpec<Integer> optionSpec9 = optionParser2.accepts("proxyPort").withRequiredArg().defaultsTo("8080", new String[0]).<Integer>ofType(Integer.class);
        final OptionSpec<String> optionSpec10 = optionParser2.accepts("proxyUser").withRequiredArg();
        final OptionSpec<String> optionSpec11 = optionParser2.accepts("proxyPass").withRequiredArg();
        final OptionSpec<String> optionSpec12 = optionParser2.accepts("username").withRequiredArg().defaultsTo("Player" + Util.getMillis() % 1000L, new String[0]);
        final OptionSpec<String> optionSpec13 = optionParser2.accepts("uuid").withRequiredArg();
        final OptionSpec<String> optionSpec14 = optionParser2.accepts("accessToken").withRequiredArg().required();
        final OptionSpec<String> optionSpec15 = optionParser2.accepts("version").withRequiredArg().required();
        final OptionSpec<Integer> optionSpec16 = optionParser2.accepts("width").withRequiredArg().<Integer>ofType(Integer.class).defaultsTo(854, new Integer[0]);
        final OptionSpec<Integer> optionSpec17 = optionParser2.accepts("height").withRequiredArg().<Integer>ofType(Integer.class).defaultsTo(480, new Integer[0]);
        final OptionSpec<Integer> optionSpec18 = optionParser2.accepts("fullscreenWidth").withRequiredArg().<Integer>ofType(Integer.class);
        final OptionSpec<Integer> optionSpec19 = optionParser2.accepts("fullscreenHeight").withRequiredArg().<Integer>ofType(Integer.class);
        final OptionSpec<String> optionSpec20 = optionParser2.accepts("userProperties").withRequiredArg().defaultsTo("{}", new String[0]);
        final OptionSpec<String> optionSpec21 = optionParser2.accepts("profileProperties").withRequiredArg().defaultsTo("{}", new String[0]);
        final OptionSpec<String> optionSpec22 = optionParser2.accepts("assetIndex").withRequiredArg();
        final OptionSpec<String> optionSpec23 = optionParser2.accepts("userType").withRequiredArg().defaultsTo("legacy", new String[0]);
        final OptionSpec<String> optionSpec24 = optionParser2.accepts("versionType").withRequiredArg().defaultsTo("release", new String[0]);
        final OptionSpec<String> optionSpec25 = optionParser2.nonOptions();
        final OptionSet optionSet26 = optionParser2.parse(arr);
        final List<String> list27 = optionSet26.<String>valuesOf(optionSpec25);
        if (!list27.isEmpty()) {
            System.out.println("Completely ignored arguments: " + list27);
        }
        final String string28 = Main.<String>parseArgument(optionSet26, optionSpec8);
        Proxy proxy29 = Proxy.NO_PROXY;
        if (string28 != null) {
            try {
                proxy29 = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(string28, Main.<Integer>parseArgument(optionSet26, optionSpec9)));
            }
            catch (Exception ex) {}
        }
        final String string29 = Main.<String>parseArgument(optionSet26, optionSpec10);
        final String string30 = Main.<String>parseArgument(optionSet26, optionSpec11);
        if (!proxy29.equals(Proxy.NO_PROXY) && stringHasValue(string29) && stringHasValue(string30)) {
            Authenticator.setDefault(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(string29, string30.toCharArray());
                }
            });
        }
        final int integer32 = Main.<Integer>parseArgument(optionSet26, optionSpec16);
        final int integer33 = Main.<Integer>parseArgument(optionSet26, optionSpec17);
        final OptionalInt optionalInt34 = ofNullable(Main.<Integer>parseArgument(optionSet26, optionSpec18));
        final OptionalInt optionalInt35 = ofNullable(Main.<Integer>parseArgument(optionSet26, optionSpec19));
        final boolean boolean36 = optionSet26.has("fullscreen");
        final boolean boolean37 = optionSet26.has("demo");
        final String string31 = Main.<String>parseArgument(optionSet26, optionSpec15);
        final Gson gson39 = new GsonBuilder().registerTypeAdapter(PropertyMap.class, new PropertyMap.Serializer()).create();
        final PropertyMap propertyMap40 = GsonHelper.<PropertyMap>fromJson(gson39, Main.<String>parseArgument(optionSet26, optionSpec20), PropertyMap.class);
        final PropertyMap propertyMap41 = GsonHelper.<PropertyMap>fromJson(gson39, Main.<String>parseArgument(optionSet26, optionSpec21), PropertyMap.class);
        final String string32 = Main.<String>parseArgument(optionSet26, optionSpec24);
        final File file43 = Main.<File>parseArgument(optionSet26, optionSpec5);
        final File file44 = optionSet26.has(optionSpec6) ? Main.<File>parseArgument(optionSet26, optionSpec6) : new File(file43, "assets/");
        final File file45 = optionSet26.has(optionSpec7) ? Main.<File>parseArgument(optionSet26, optionSpec7) : new File(file43, "resourcepacks/");
        final String string33 = optionSet26.has(optionSpec13) ? optionSpec13.value(optionSet26) : Player.createPlayerUUID(optionSpec12.value(optionSet26)).toString();
        final String string34 = optionSet26.has(optionSpec22) ? optionSpec22.value(optionSet26) : null;
        final String string35 = Main.<String>parseArgument(optionSet26, optionSpec3);
        final Integer integer34 = Main.<Integer>parseArgument(optionSet26, optionSpec4);
        CrashReport.preload();
        final User dbz50 = new User(optionSpec12.value(optionSet26), string33, optionSpec14.value(optionSet26), optionSpec23.value(optionSet26));
        final GameConfig djy51 = new GameConfig(new GameConfig.UserData(dbz50, propertyMap40, propertyMap41, proxy29), new DisplayData(integer32, integer33, optionalInt34, optionalInt35, boolean36), new GameConfig.FolderData(file43, file45, file44, string34), new GameConfig.GameData(boolean37, string31, string32), new GameConfig.ServerData(string35, integer34));
        final Thread thread52 = new Thread("Client Shutdown Thread") {
            @Override
            public void run() {
                final Minecraft dbn2 = Minecraft.getInstance();
                if (dbn2 == null) {
                    return;
                }
                final IntegratedServer edm3 = dbn2.getSingleplayerServer();
                if (edm3 != null) {
                    edm3.halt(true);
                }
            }
        };
        thread52.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(Main.LOGGER));
        Runtime.getRuntime().addShutdownHook(thread52);
        final RenderPipeline cxd53 = new RenderPipeline();
        Minecraft dbn54;
        try {
            Thread.currentThread().setName("Render thread");
            RenderSystem.initRenderThread();
            RenderSystem.beginInitialization();
            dbn54 = new Minecraft(djy51);
            RenderSystem.finishInitialization();
        }
        catch (SilentInitException djz55) {
            Main.LOGGER.warn("Failed to create window: ", (Throwable)djz55);
            return;
        }
        catch (Throwable throwable55) {
            final CrashReport h56 = CrashReport.forThrowable(throwable55, "Initializing game");
            h56.addCategory("Initialization");
            Minecraft.fillReport(null, djy51.game.launchVersion, null, h56);
            Minecraft.crash(h56);
            return;
        }
        Thread thread53;
        if (dbn54.renderOnThread()) {
            thread53 = new Thread("Game thread") {
                @Override
                public void run() {
                    try {
                        RenderSystem.initGameThread(true);
                        dbn54.run();
                    }
                    catch (Throwable throwable2) {
                        Main.LOGGER.error("Exception in client thread", throwable2);
                    }
                }
            };
            thread53.start();
            while (dbn54.isRunning()) {}
        }
        else {
            thread53 = null;
            try {
                RenderSystem.initGameThread(false);
                dbn54.run();
            }
            catch (Throwable throwable56) {
                Main.LOGGER.error("Unhandled game exception", throwable56);
            }
        }
        try {
            dbn54.stop();
            if (thread53 != null) {
                thread53.join();
            }
        }
        catch (InterruptedException interruptedException56) {
            Main.LOGGER.error("Exception during client thread shutdown", (Throwable)interruptedException56);
        }
        finally {
            dbn54.destroy();
        }
    }
    
    private static OptionalInt ofNullable(@Nullable final Integer integer) {
        return (integer != null) ? OptionalInt.of(integer) : OptionalInt.empty();
    }
    
    @Nullable
    private static <T> T parseArgument(final OptionSet optionSet, final OptionSpec<T> optionSpec) {
        try {
            return optionSet.<T>valueOf(optionSpec);
        }
        catch (Throwable throwable3) {
            if (optionSpec instanceof ArgumentAcceptingOptionSpec) {
                final ArgumentAcceptingOptionSpec<T> argumentAcceptingOptionSpec4 = (ArgumentAcceptingOptionSpec<T>)(ArgumentAcceptingOptionSpec)optionSpec;
                final List<T> list5 = argumentAcceptingOptionSpec4.defaultValues();
                if (!list5.isEmpty()) {
                    return list5.get(0);
                }
            }
            throw throwable3;
        }
    }
    
    private static boolean stringHasValue(@Nullable final String string) {
        return string != null && !string.isEmpty();
    }
    
    static {
        LOGGER = LogManager.getLogger();
        System.setProperty("java.awt.headless", "true");
    }
}
