--- a/net/minecraft/server/Main.java
+++ b/net/minecraft/server/Main.java
@@ -20,6 +20,7 @@
 import joptsimple.OptionSet;
 import joptsimple.OptionSpec;
 import joptsimple.util.PathConverter;
+import joptsimple.util.PathProperties;
 import net.minecraft.CrashReport;
 import net.minecraft.DefaultUncaughtExceptionHandler;
 import net.minecraft.SharedConstants;
@@ -36,6 +37,7 @@
 import net.minecraft.server.dedicated.DedicatedServerProperties;
 import net.minecraft.server.dedicated.DedicatedServerSettings;
 import net.minecraft.server.level.progress.LoggerChunkProgressListener;
+import net.minecraft.server.packs.PackType;
 import net.minecraft.server.packs.repository.PackRepository;
 import net.minecraft.server.packs.repository.ServerPacksSource;
 import net.minecraft.util.Mth;
@@ -53,292 +55,374 @@
 import net.minecraft.world.level.levelgen.WorldOptions;
 import net.minecraft.world.level.levelgen.presets.WorldPresets;
 import net.minecraft.world.level.storage.LevelDataAndDimensions;
+import net.minecraft.world.level.storage.LevelResource;
 import net.minecraft.world.level.storage.LevelStorageSource;
 import net.minecraft.world.level.storage.LevelSummary;
 import net.minecraft.world.level.storage.PrimaryLevelData;
-import net.minecraft.world.level.storage.WorldData;
 import org.slf4j.Logger;
 
+// CraftBukkit start
+import com.google.common.base.Charsets;
+import java.io.InputStreamReader;
+import java.util.concurrent.atomic.AtomicReference;
+import net.minecraft.SharedConstants;
+import org.bukkit.configuration.file.YamlConfiguration;
+// CraftBukkit end
+
 public class Main {
+
     private static final Logger LOGGER = LogUtils.getLogger();
 
+    public Main() {}
+
     @DontObfuscate
-    public static void main(String[] args) {
+    public static void main(final OptionSet optionset) { // CraftBukkit - replaces main(String[] astring)
         SharedConstants.tryDetectVersion();
-        OptionParser optionParser = new OptionParser();
-        OptionSpec<Void> optionSpec = optionParser.accepts("nogui");
-        OptionSpec<Void> optionSpec2 = optionParser.accepts("initSettings", "Initializes 'server.properties' and 'eula.txt', then quits");
-        OptionSpec<Void> optionSpec3 = optionParser.accepts("demo");
-        OptionSpec<Void> optionSpec4 = optionParser.accepts("bonusChest");
-        OptionSpec<Void> optionSpec5 = optionParser.accepts("forceUpgrade");
-        OptionSpec<Void> optionSpec6 = optionParser.accepts("eraseCache");
-        OptionSpec<Void> optionSpec7 = optionParser.accepts("recreateRegionFiles");
-        OptionSpec<Void> optionSpec8 = optionParser.accepts("safeMode", "Loads level with vanilla datapack only");
-        OptionSpec<Void> optionSpec9 = optionParser.accepts("help").forHelp();
-        OptionSpec<String> optionSpec10 = optionParser.accepts("universe").withRequiredArg().defaultsTo(".");
-        OptionSpec<String> optionSpec11 = optionParser.accepts("world").withRequiredArg();
-        OptionSpec<Integer> optionSpec12 = optionParser.accepts("port").withRequiredArg().ofType(Integer.class).defaultsTo(-1);
-        OptionSpec<String> optionSpec13 = optionParser.accepts("serverId").withRequiredArg();
-        OptionSpec<Void> optionSpec14 = optionParser.accepts("jfrProfile");
-        OptionSpec<Path> optionSpec15 = optionParser.accepts("pidFile").withRequiredArg().withValuesConvertedBy(new PathConverter());
-        OptionSpec<String> optionSpec16 = optionParser.nonOptions();
+        /* CraftBukkit start - Replace everything
+        OptionParser optionparser = new OptionParser();
+        OptionSpec<Void> optionspec = optionparser.accepts("nogui");
+        OptionSpec<Void> optionspec1 = optionparser.accepts("initSettings", "Initializes 'server.properties' and 'eula.txt', then quits");
+        OptionSpec<Void> optionspec2 = optionparser.accepts("demo");
+        OptionSpec<Void> optionspec3 = optionparser.accepts("bonusChest");
+        OptionSpec<Void> optionspec4 = optionparser.accepts("forceUpgrade");
+        OptionSpec<Void> optionspec5 = optionparser.accepts("eraseCache");
+        OptionSpec<Void> optionspec6 = optionparser.accepts("recreateRegionFiles");
+        OptionSpec<Void> optionspec7 = optionparser.accepts("safeMode", "Loads level with vanilla datapack only");
+        OptionSpec<Void> optionspec8 = optionparser.accepts("help").forHelp();
+        OptionSpec<String> optionspec9 = optionparser.accepts("universe").withRequiredArg().defaultsTo(".", new String[0]);
+        OptionSpec<String> optionspec10 = optionparser.accepts("world").withRequiredArg();
+        OptionSpec<Integer> optionspec11 = optionparser.accepts("port").withRequiredArg().ofType(Integer.class).defaultsTo(-1, new Integer[0]);
+        OptionSpec<String> optionspec12 = optionparser.accepts("serverId").withRequiredArg();
+        OptionSpec<Void> optionspec13 = optionparser.accepts("jfrProfile");
+        OptionSpec<Path> optionspec14 = optionparser.accepts("pidFile").withRequiredArg().withValuesConvertedBy(new PathConverter(new PathProperties[0]));
+        OptionSpec<String> optionspec15 = optionparser.nonOptions();
 
         try {
-            OptionSet optionSet = optionParser.parse(args);
-            if (optionSet.has(optionSpec9)) {
-                optionParser.printHelpOn(System.err);
+            OptionSet optionset = optionparser.parse(astring);
+
+            if (optionset.has(optionspec8)) {
+                optionparser.printHelpOn(System.err);
                 return;
             }
+            */ // CraftBukkit end
+
+        try {
+
+            Path path = (Path) optionset.valueOf("pidFile"); // CraftBukkit
 
-            Path path = optionSet.valueOf(optionSpec15);
             if (path != null) {
-                writePidFile(path);
+                Main.writePidFile(path);
             }
 
             CrashReport.preload();
-            if (optionSet.has(optionSpec14)) {
+            if (optionset.has("jfrProfile")) { // CraftBukkit
                 JvmProfiler.INSTANCE.start(Environment.SERVER);
             }
 
+            io.papermc.paper.plugin.PluginInitializerManager.load(optionset); // Paper
             Bootstrap.bootStrap();
             Bootstrap.validate();
             Util.startTimerHackThread();
-            Path path2 = Paths.get("server.properties");
-            DedicatedServerSettings dedicatedServerSettings = new DedicatedServerSettings(path2);
-            dedicatedServerSettings.forceSave();
-            RegionFileVersion.configure(dedicatedServerSettings.getProperties().regionFileComression);
-            Path path3 = Paths.get("eula.txt");
-            Eula eula = new Eula(path3);
-            if (optionSet.has(optionSpec2)) {
-                LOGGER.info("Initialized '{}' and '{}'", path2.toAbsolutePath(), path3.toAbsolutePath());
+            Path path1 = Paths.get("server.properties");
+            DedicatedServerSettings dedicatedserversettings = new DedicatedServerSettings(optionset); // CraftBukkit - CLI argument support
+
+            dedicatedserversettings.forceSave();
+            RegionFileVersion.configure(dedicatedserversettings.getProperties().regionFileComression);
+            Path path2 = Paths.get("eula.txt");
+            Eula eula = new Eula(path2);
+            // Paper start - load config files early for access below if needed
+            org.bukkit.configuration.file.YamlConfiguration bukkitConfiguration = io.papermc.paper.configuration.PaperConfigurations.loadLegacyConfigFile((File) optionset.valueOf("bukkit-settings"));
+            org.bukkit.configuration.file.YamlConfiguration spigotConfiguration = io.papermc.paper.configuration.PaperConfigurations.loadLegacyConfigFile((File) optionset.valueOf("spigot-settings"));
+            // Paper end - load config files early for access below if needed
+
+            if (optionset.has("initSettings")) { // CraftBukkit
+                // CraftBukkit start - SPIGOT-5761: Create bukkit.yml and commands.yml if not present
+                File configFile = (File) optionset.valueOf("bukkit-settings");
+                YamlConfiguration configuration = YamlConfiguration.loadConfiguration(configFile);
+                configuration.options().copyDefaults(true);
+                configuration.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(Main.class.getClassLoader().getResourceAsStream("configurations/bukkit.yml"), Charsets.UTF_8)));
+                configuration.save(configFile);
+
+                File commandFile = (File) optionset.valueOf("commands-settings");
+                YamlConfiguration commandsConfiguration = YamlConfiguration.loadConfiguration(commandFile);
+                commandsConfiguration.options().copyDefaults(true);
+                commandsConfiguration.setDefaults(YamlConfiguration.loadConfiguration(new InputStreamReader(Main.class.getClassLoader().getResourceAsStream("configurations/commands.yml"), Charsets.UTF_8)));
+                commandsConfiguration.save(commandFile);
+                // CraftBukkit end
+                Main.LOGGER.info("Initialized '{}' and '{}'", path1.toAbsolutePath(), path2.toAbsolutePath());
                 return;
             }
 
-            if (!eula.hasAgreedToEULA()) {
-                LOGGER.info("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.");
+            // Spigot Start
+            boolean eulaAgreed = Boolean.getBoolean( "com.mojang.eula.agree" );
+            if ( eulaAgreed )
+            {
+                System.err.println( "You have used the Spigot command line EULA agreement flag." );
+                System.err.println( "By using this setting you are indicating your agreement to Mojang's EULA (https://account.mojang.com/documents/minecraft_eula)." );
+                System.err.println( "If you do not agree to the above EULA please stop your server and remove this flag immediately." );
+            }
+            // Spigot End
+            if (!eula.hasAgreedToEULA() && !eulaAgreed) { // Spigot
+                Main.LOGGER.info("You need to agree to the EULA in order to run the server. Go to eula.txt for more info.");
                 return;
             }
 
-            File file = new File(optionSet.valueOf(optionSpec10));
-            Services services = Services.create(new YggdrasilAuthenticationService(Proxy.NO_PROXY), file);
-            String string = Optional.ofNullable(optionSet.valueOf(optionSpec11)).orElse(dedicatedServerSettings.getProperties().levelName);
-            LevelStorageSource levelStorageSource = LevelStorageSource.createDefault(file.toPath());
-            LevelStorageSource.LevelStorageAccess levelStorageAccess = levelStorageSource.validateAndCreateAccess(string);
-            Dynamic<?> dynamic;
-            if (levelStorageAccess.hasWorldData()) {
-                LevelSummary levelSummary;
+            // Paper start - Detect headless JRE
+            String awtException = io.papermc.paper.util.ServerEnvironment.awtDependencyCheck();
+            if (awtException != null) {
+                Main.LOGGER.error("You are using a headless JRE distribution.");
+                Main.LOGGER.error("This distribution is missing certain graphic libraries that the Minecraft server needs to function.");
+                Main.LOGGER.error("For instructions on how to install the non-headless JRE, see https://docs.papermc.io/misc/java-install");
+                Main.LOGGER.error("");
+                Main.LOGGER.error(awtException);
+                return;
+            }
+            // Paper end - Detect headless JRE
+
+            org.spigotmc.SpigotConfig.disabledAdvancements = spigotConfiguration.getStringList("advancements.disabled"); // Paper - fix SPIGOT-5885, must be set early in init
+            // Paper start - fix SPIGOT-5824
+            File file;
+            File userCacheFile = new File(Services.USERID_CACHE_FILE);
+            if (optionset.has("universe")) {
+                file = (File) optionset.valueOf("universe"); // CraftBukkit
+                userCacheFile = new File(file, Services.USERID_CACHE_FILE);
+            } else {
+                file = new File(bukkitConfiguration.getString("settings.world-container", "."));
+            }
+            // Paper end - fix SPIGOT-5824
+            Services services = Services.create(new com.destroystokyo.paper.profile.PaperAuthenticationService(Proxy.NO_PROXY), file, userCacheFile, optionset); // Paper - pass OptionSet to load paper config files; override authentication service; fix world-container
+            // CraftBukkit start
+            String s = (String) Optional.ofNullable((String) optionset.valueOf("world")).orElse(dedicatedserversettings.getProperties().levelName);
+            LevelStorageSource convertable = LevelStorageSource.createDefault(file.toPath());
+            LevelStorageSource.LevelStorageAccess convertable_conversionsession = convertable.validateAndCreateAccess(s, LevelStem.OVERWORLD);
+            // CraftBukkit end
+            Dynamic dynamic;
+
+            if (convertable_conversionsession.hasWorldData()) {
+                LevelSummary worldinfo;
+
                 try {
-                    dynamic = levelStorageAccess.getDataTag();
-                    levelSummary = levelStorageAccess.getSummary(dynamic);
-                } catch (NbtException | ReportedNbtException | IOException var41) {
-                    LevelStorageSource.LevelDirectory levelDirectory = levelStorageAccess.getLevelDirectory();
-                    LOGGER.warn("Failed to load world data from {}", levelDirectory.dataFile(), var41);
-                    LOGGER.info("Attempting to use fallback");
+                    dynamic = convertable_conversionsession.getDataTag();
+                    worldinfo = convertable_conversionsession.getSummary(dynamic);
+                } catch (NbtException | ReportedNbtException | IOException ioexception) {
+                    LevelStorageSource.LevelDirectory convertable_b = convertable_conversionsession.getLevelDirectory();
+
+                    Main.LOGGER.warn("Failed to load world data from {}", convertable_b.dataFile(), ioexception);
+                    Main.LOGGER.info("Attempting to use fallback");
 
                     try {
-                        dynamic = levelStorageAccess.getDataTagFallback();
-                        levelSummary = levelStorageAccess.getSummary(dynamic);
-                    } catch (NbtException | ReportedNbtException | IOException var40) {
-                        LOGGER.error("Failed to load world data from {}", levelDirectory.oldDataFile(), var40);
-                        LOGGER.error(
-                            "Failed to load world data from {} and {}. World files may be corrupted. Shutting down.",
-                            levelDirectory.dataFile(),
-                            levelDirectory.oldDataFile()
-                        );
+                        dynamic = convertable_conversionsession.getDataTagFallback();
+                        worldinfo = convertable_conversionsession.getSummary(dynamic);
+                    } catch (NbtException | ReportedNbtException | IOException ioexception1) {
+                        Main.LOGGER.error("Failed to load world data from {}", convertable_b.oldDataFile(), ioexception1);
+                        Main.LOGGER.error("Failed to load world data from {} and {}. World files may be corrupted. Shutting down.", convertable_b.dataFile(), convertable_b.oldDataFile());
                         return;
                     }
 
-                    levelStorageAccess.restoreLevelDataFromOld();
+                    convertable_conversionsession.restoreLevelDataFromOld();
                 }
 
-                if (levelSummary.requiresManualConversion()) {
-                    LOGGER.info("This world must be opened in an older version (like 1.6.4) to be safely converted");
+                if (worldinfo.requiresManualConversion()) {
+                    Main.LOGGER.info("This world must be opened in an older version (like 1.6.4) to be safely converted");
                     return;
                 }
 
-                if (!levelSummary.isCompatible()) {
-                    LOGGER.info("This world was created by an incompatible version.");
+                if (!worldinfo.isCompatible()) {
+                    Main.LOGGER.info("This world was created by an incompatible version.");
                     return;
                 }
             } else {
                 dynamic = null;
             }
 
-            Dynamic<?> dynamic5 = dynamic;
-            boolean bl = optionSet.has(optionSpec8);
-            if (bl) {
-                LOGGER.warn("Safe mode active, only vanilla datapack will be loaded");
+            Dynamic<?> dynamic1 = dynamic;
+            boolean flag = optionset.has("safeMode"); // CraftBukkit
+
+            if (flag) {
+                Main.LOGGER.warn("Safe mode active, only vanilla datapack will be loaded");
             }
 
-            PackRepository packRepository = ServerPacksSource.createPackRepository(levelStorageAccess);
+            PackRepository resourcepackrepository = ServerPacksSource.createPackRepository(convertable_conversionsession);
+            // CraftBukkit start
+            File bukkitDataPackFolder = new File(convertable_conversionsession.getLevelPath(LevelResource.DATAPACK_DIR).toFile(), "bukkit");
+            if (!bukkitDataPackFolder.exists()) {
+                bukkitDataPackFolder.mkdirs();
+            }
+            File mcMeta = new File(bukkitDataPackFolder, "pack.mcmeta");
+            try {
+                com.google.common.io.Files.write("{\n"
+                        + "    \"pack\": {\n"
+                        + "        \"description\": \"Data pack for resources provided by Bukkit plugins\",\n"
+                        + "        \"pack_format\": " + SharedConstants.getCurrentVersion().getPackVersion(PackType.SERVER_DATA) + "\n"
+                        + "    }\n"
+                        + "}\n", mcMeta, com.google.common.base.Charsets.UTF_8);
+            } catch (java.io.IOException ex) {
+                throw new RuntimeException("Could not initialize Bukkit datapack", ex);
+            }
+            AtomicReference<WorldLoader.DataLoadContext> worldLoader = new AtomicReference<>();
+            // CraftBukkit end
+
+            WorldStem worldstem;
 
-            WorldStem worldStem;
             try {
-                WorldLoader.InitConfig initConfig = loadOrCreateConfig(dedicatedServerSettings.getProperties(), dynamic5, bl, packRepository);
-                worldStem = Util.<WorldStem>blockUntilDone(
-                        applyExecutor -> WorldLoader.load(
-                                initConfig,
-                                context -> {
-                                    Registry<LevelStem> registry = context.datapackDimensions().registryOrThrow(Registries.LEVEL_STEM);
-                                    if (dynamic5 != null) {
-                                        LevelDataAndDimensions levelDataAndDimensions = LevelStorageSource.getLevelDataAndDimensions(
-                                            dynamic5, context.dataConfiguration(), registry, context.datapackWorldgen()
-                                        );
-                                        return new WorldLoader.DataLoadOutput<>(
-                                            levelDataAndDimensions.worldData(), levelDataAndDimensions.dimensions().dimensionsRegistryAccess()
-                                        );
-                                    } else {
-                                        LOGGER.info("No existing world data, creating new world");
-                                        LevelSettings levelSettings;
-                                        WorldOptions worldOptions;
-                                        WorldDimensions worldDimensions;
-                                        if (optionSet.has(optionSpec3)) {
-                                            levelSettings = MinecraftServer.DEMO_SETTINGS;
-                                            worldOptions = WorldOptions.DEMO_OPTIONS;
-                                            worldDimensions = WorldPresets.createNormalWorldDimensions(context.datapackWorldgen());
-                                        } else {
-                                            DedicatedServerProperties dedicatedServerProperties = dedicatedServerSettings.getProperties();
-                                            levelSettings = new LevelSettings(
-                                                dedicatedServerProperties.levelName,
-                                                dedicatedServerProperties.gamemode,
-                                                dedicatedServerProperties.hardcore,
-                                                dedicatedServerProperties.difficulty,
-                                                false,
-                                                new GameRules(),
-                                                context.dataConfiguration()
-                                            );
-                                            worldOptions = optionSet.has(optionSpec4)
-                                                ? dedicatedServerProperties.worldOptions.withBonusChest(true)
-                                                : dedicatedServerProperties.worldOptions;
-                                            worldDimensions = dedicatedServerProperties.createDimensions(context.datapackWorldgen());
-                                        }
+                WorldLoader.InitConfig worldloader_c = Main.loadOrCreateConfig(dedicatedserversettings.getProperties(), dynamic1, flag, resourcepackrepository);
 
-                                        WorldDimensions.Complete complete = worldDimensions.bake(registry);
-                                        Lifecycle lifecycle = complete.lifecycle().add(context.datapackWorldgen().allRegistriesLifecycle());
-                                        return new WorldLoader.DataLoadOutput<>(
-                                            new PrimaryLevelData(levelSettings, worldOptions, complete.specialWorldProperty(), lifecycle),
-                                            complete.dimensionsRegistryAccess()
-                                        );
-                                    }
-                                },
-                                WorldStem::new,
-                                Util.backgroundExecutor(),
-                                applyExecutor
-                            )
-                    )
-                    .get();
-            } catch (Exception var39) {
-                LOGGER.warn(
-                    "Failed to load datapacks, can't proceed with server load. You can either fix your datapacks or reset to vanilla with --safeMode",
-                    (Throwable)var39
-                );
+                worldstem = (WorldStem) Util.blockUntilDone((executor) -> {
+                    return WorldLoader.load(worldloader_c, (worldloader_a) -> {
+                        worldLoader.set(worldloader_a); // CraftBukkit
+                        Registry<LevelStem> iregistry = worldloader_a.datapackDimensions().registryOrThrow(Registries.LEVEL_STEM);
+
+                        if (dynamic1 != null) {
+                            LevelDataAndDimensions leveldataanddimensions = LevelStorageSource.getLevelDataAndDimensions(dynamic1, worldloader_a.dataConfiguration(), iregistry, worldloader_a.datapackWorldgen());
+
+                            return new WorldLoader.DataLoadOutput<>(leveldataanddimensions.worldData(), leveldataanddimensions.dimensions().dimensionsRegistryAccess());
+                        } else {
+                            Main.LOGGER.info("No existing world data, creating new world");
+                            LevelSettings worldsettings;
+                            WorldOptions worldoptions;
+                            WorldDimensions worlddimensions;
+
+                            if (optionset.has("demo")) { // CraftBukkit
+                                worldsettings = MinecraftServer.DEMO_SETTINGS;
+                                worldoptions = WorldOptions.DEMO_OPTIONS;
+                                worlddimensions = WorldPresets.createNormalWorldDimensions(worldloader_a.datapackWorldgen());
+                            } else {
+                                DedicatedServerProperties dedicatedserverproperties = dedicatedserversettings.getProperties();
+
+                                worldsettings = new LevelSettings(dedicatedserverproperties.levelName, dedicatedserverproperties.gamemode, dedicatedserverproperties.hardcore, dedicatedserverproperties.difficulty, false, new GameRules(), worldloader_a.dataConfiguration());
+                                worldoptions = optionset.has("bonusChest") ? dedicatedserverproperties.worldOptions.withBonusChest(true) : dedicatedserverproperties.worldOptions; // CraftBukkit
+                                worlddimensions = dedicatedserverproperties.createDimensions(worldloader_a.datapackWorldgen());
+                            }
+
+                            WorldDimensions.Complete worlddimensions_b = worlddimensions.bake(iregistry);
+                            Lifecycle lifecycle = worlddimensions_b.lifecycle().add(worldloader_a.datapackWorldgen().allRegistriesLifecycle());
+
+                            return new WorldLoader.DataLoadOutput<>(new PrimaryLevelData(worldsettings, worldoptions, worlddimensions_b.specialWorldProperty(), lifecycle), worlddimensions_b.dimensionsRegistryAccess());
+                        }
+                    }, WorldStem::new, Util.backgroundExecutor(), executor);
+                }).get();
+            } catch (Exception exception) {
+                Main.LOGGER.warn("Failed to load datapacks, can't proceed with server load. You can either fix your datapacks or reset to vanilla with --safeMode", exception);
                 return;
             }
 
-            RegistryAccess.Frozen frozen = worldStem.registries().compositeAccess();
-            boolean bl2 = optionSet.has(optionSpec7);
-            if (optionSet.has(optionSpec5) || bl2) {
-                forceUpgrade(levelStorageAccess, DataFixers.getDataFixer(), optionSet.has(optionSpec6), () -> true, frozen, bl2);
+            /*
+            IRegistryCustom.Dimension iregistrycustom_dimension = worldstem.registries().compositeAccess();
+            boolean flag1 = optionset.has(optionspec6);
+
+            if (optionset.has(optionspec4) || flag1) {
+                forceUpgrade(convertable_conversionsession, DataConverterRegistry.getDataFixer(), optionset.has(optionspec5), () -> {
+                    return true;
+                }, iregistrycustom_dimension, flag1);
             }
 
-            WorldData worldData = worldStem.worldData();
-            levelStorageAccess.saveDataTag(frozen, worldData);
-            final DedicatedServer dedicatedServer = MinecraftServer.spin(
-                threadx -> {
-                    DedicatedServer dedicatedServerx = new DedicatedServer(
-                        threadx,
-                        levelStorageAccess,
-                        packRepository,
-                        worldStem,
-                        dedicatedServerSettings,
-                        DataFixers.getDataFixer(),
-                        services,
-                        LoggerChunkProgressListener::createFromGameruleRadius
-                    );
-                    dedicatedServerx.setPort(optionSet.valueOf(optionSpec12));
-                    dedicatedServerx.setDemo(optionSet.has(optionSpec3));
-                    dedicatedServerx.setId(optionSet.valueOf(optionSpec13));
-                    boolean blx = !optionSet.has(optionSpec) && !optionSet.valuesOf(optionSpec16).contains("nogui");
-                    if (blx && !GraphicsEnvironment.isHeadless()) {
-                        dedicatedServerx.showGui();
-                    }
+            SaveData savedata = worldstem.worldData();
 
-                    return dedicatedServerx;
+            convertable_conversionsession.saveDataTag(iregistrycustom_dimension, savedata);
+            */
+            Class.forName(net.minecraft.world.entity.npc.VillagerTrades.class.getName()); // Paper - load this sync so it won't fail later async
+            final DedicatedServer dedicatedserver = (DedicatedServer) MinecraftServer.spin((thread) -> {
+                DedicatedServer dedicatedserver1 = new DedicatedServer(optionset, worldLoader.get(), thread, convertable_conversionsession, resourcepackrepository, worldstem, dedicatedserversettings, DataFixers.getDataFixer(), services, LoggerChunkProgressListener::createFromGameruleRadius);
+
+                /*
+                dedicatedserver1.setPort((Integer) optionset.valueOf(optionspec11));
+                */
+                dedicatedserver1.setDemo(optionset.has("demo")); // Paper
+                /*
+                dedicatedserver1.setId((String) optionset.valueOf(optionspec12));
+                */
+                boolean flag2 = !optionset.has("nogui") && !optionset.nonOptionArguments().contains("nogui");
+
+                if(!Boolean.parseBoolean(System.getenv().getOrDefault("PAPER_DISABLE_SERVER_GUI", String.valueOf(false)))) // Paper - Add environment variable to disable server gui
+                if (flag2 && !GraphicsEnvironment.isHeadless()) {
+                    dedicatedserver1.showGui();
                 }
-            );
+
+                if (optionset.has("port")) {
+                    int port = (Integer) optionset.valueOf("port");
+                    if (port > 0) {
+                        dedicatedserver1.setPort(port);
+                    }
+                }
+
+                return dedicatedserver1;
+            });
+            /* CraftBukkit start
             Thread thread = new Thread("Server Shutdown Thread") {
-                @Override
                 public void run() {
-                    dedicatedServer.halt(true);
+                    dedicatedserver.halt(true);
                 }
             };
-            thread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(LOGGER));
+
+            thread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(Main.LOGGER));
             Runtime.getRuntime().addShutdownHook(thread);
-        } catch (Exception var42) {
-            LOGGER.error(LogUtils.FATAL_MARKER, "Failed to start the minecraft server", (Throwable)var42);
+            */ // CraftBukkit end
+        } catch (Exception exception1) {
+            Main.LOGGER.error(LogUtils.FATAL_MARKER, "Failed to start the minecraft server", exception1);
         }
+
     }
 
     private static void writePidFile(Path path) {
         try {
-            long l = ProcessHandle.current().pid();
-            Files.writeString(path, Long.toString(l));
-        } catch (IOException var3) {
-            throw new UncheckedIOException(var3);
+            long i = ProcessHandle.current().pid();
+
+            Files.writeString(path, Long.toString(i));
+        } catch (IOException ioexception) {
+            throw new UncheckedIOException(ioexception);
         }
     }
 
-    private static WorldLoader.InitConfig loadOrCreateConfig(
-        DedicatedServerProperties serverPropertiesHandler, @Nullable Dynamic<?> dynamic, boolean safeMode, PackRepository dataPackManager
-    ) {
-        boolean bl;
-        WorldDataConfiguration worldDataConfiguration2;
+    private static WorldLoader.InitConfig loadOrCreateConfig(DedicatedServerProperties serverPropertiesHandler, @Nullable Dynamic<?> dynamic, boolean safeMode, PackRepository dataPackManager) {
+        boolean flag1;
+        WorldDataConfiguration worlddataconfiguration;
+
         if (dynamic != null) {
-            WorldDataConfiguration worldDataConfiguration = LevelStorageSource.readDataConfig(dynamic);
-            bl = false;
-            worldDataConfiguration2 = worldDataConfiguration;
+            WorldDataConfiguration worlddataconfiguration1 = LevelStorageSource.readDataConfig(dynamic);
+
+            flag1 = false;
+            worlddataconfiguration = worlddataconfiguration1;
         } else {
-            bl = true;
-            worldDataConfiguration2 = new WorldDataConfiguration(serverPropertiesHandler.initialDataPackConfiguration, FeatureFlags.DEFAULT_FLAGS);
+            flag1 = true;
+            worlddataconfiguration = new WorldDataConfiguration(serverPropertiesHandler.initialDataPackConfiguration, FeatureFlags.DEFAULT_FLAGS);
         }
 
-        WorldLoader.PackConfig packConfig = new WorldLoader.PackConfig(dataPackManager, worldDataConfiguration2, safeMode, bl);
-        return new WorldLoader.InitConfig(packConfig, Commands.CommandSelection.DEDICATED, serverPropertiesHandler.functionPermissionLevel);
+        WorldLoader.PackConfig worldloader_d = new WorldLoader.PackConfig(dataPackManager, worlddataconfiguration, safeMode, flag1);
+
+        return new WorldLoader.InitConfig(worldloader_d, Commands.CommandSelection.DEDICATED, serverPropertiesHandler.functionPermissionLevel);
     }
 
-    public static void forceUpgrade(
-        LevelStorageSource.LevelStorageAccess session,
-        DataFixer dataFixer,
-        boolean eraseCache,
-        BooleanSupplier continueCheck,
-        RegistryAccess dynamicRegistryManager,
-        boolean recreateRegionFiles
-    ) {
-        LOGGER.info("Forcing world upgrade!");
-        WorldUpgrader worldUpgrader = new WorldUpgrader(session, dataFixer, dynamicRegistryManager, eraseCache, recreateRegionFiles);
-        Component component = null;
+    public static void forceUpgrade(LevelStorageSource.LevelStorageAccess session, DataFixer dataFixer, boolean eraseCache, BooleanSupplier continueCheck, RegistryAccess dynamicRegistryManager, boolean recreateRegionFiles) {
+        Main.LOGGER.info("Forcing world upgrade! {}", session.getLevelId()); // CraftBukkit
+        WorldUpgrader worldupgrader = new WorldUpgrader(session, dataFixer, dynamicRegistryManager, eraseCache, recreateRegionFiles);
+        Component ichatbasecomponent = null;
 
-        while (!worldUpgrader.isFinished()) {
-            Component component2 = worldUpgrader.getStatus();
-            if (component != component2) {
-                component = component2;
-                LOGGER.info(worldUpgrader.getStatus().getString());
+        while (!worldupgrader.isFinished()) {
+            Component ichatbasecomponent1 = worldupgrader.getStatus();
+
+            if (ichatbasecomponent != ichatbasecomponent1) {
+                ichatbasecomponent = ichatbasecomponent1;
+                Main.LOGGER.info(worldupgrader.getStatus().getString());
             }
 
-            int i = worldUpgrader.getTotalChunks();
+            int i = worldupgrader.getTotalChunks();
+
             if (i > 0) {
-                int j = worldUpgrader.getConverted() + worldUpgrader.getSkipped();
-                LOGGER.info("{}% completed ({} / {} chunks)...", Mth.floor((float)j / (float)i * 100.0F), j, i);
+                int j = worldupgrader.getConverted() + worldupgrader.getSkipped();
+
+                Main.LOGGER.info("{}% completed ({} / {} chunks)...", new Object[]{Mth.floor((float) j / (float) i * 100.0F), j, i});
             }
 
             if (!continueCheck.getAsBoolean()) {
-                worldUpgrader.cancel();
+                worldupgrader.cancel();
             } else {
                 try {
                     Thread.sleep(1000L);
-                } catch (InterruptedException var11) {
+                } catch (InterruptedException interruptedexception) {
+                    ;
                 }
             }
         }
+
     }
 }
