--- a/net/minecraft/server/DedicatedServer.java
+++ b/net/minecraft/server/DedicatedServer.java
@@ -26,6 +26,17 @@
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 
+// CraftBukkit start
+import java.io.PrintStream;
+import org.apache.logging.log4j.Level;
+
+import org.bukkit.command.CommandSender;
+import org.bukkit.craftbukkit.LoggerOutputStream;
+import org.bukkit.event.server.ServerCommandEvent;
+import org.bukkit.craftbukkit.util.Waitable;
+import org.bukkit.event.server.RemoteServerCommandEvent;
+// CraftBukkit end
+
 public class DedicatedServer extends MinecraftServer implements IMinecraftServer {
 
     private static final Logger LOGGER = LogManager.getLogger();
@@ -39,8 +50,10 @@
     @Nullable
     private ServerGUI p;
 
-    public DedicatedServer(File file, DedicatedServerSettings dedicatedserversettings, DataFixer datafixer, YggdrasilAuthenticationService yggdrasilauthenticationservice, MinecraftSessionService minecraftsessionservice, GameProfileRepository gameprofilerepository, UserCache usercache, WorldLoadListenerFactory worldloadlistenerfactory, String s) {
-        super(file, Proxy.NO_PROXY, datafixer, new CommandDispatcher(true), yggdrasilauthenticationservice, minecraftsessionservice, gameprofilerepository, usercache, worldloadlistenerfactory, s);
+    // CraftBukkit start - Signature changed
+    public DedicatedServer(joptsimple.OptionSet options, DedicatedServerSettings dedicatedserversettings, DataFixer datafixer, YggdrasilAuthenticationService yggdrasilauthenticationservice, MinecraftSessionService minecraftsessionservice, GameProfileRepository gameprofilerepository, UserCache usercache, WorldLoadListenerFactory worldloadlistenerfactory, String s) {
+        super(options, Proxy.NO_PROXY, datafixer, new CommandDispatcher().init(true), yggdrasilauthenticationservice, minecraftsessionservice, gameprofilerepository, usercache, worldloadlistenerfactory, s);
+        // CraftBukkit end
         this.propertyManager = dedicatedserversettings;
         this.remoteControlCommandListener = new RemoteControlCommandListener(this);
         Thread thread = new Thread("Server Infinisleeper") {
@@ -66,13 +79,44 @@
     public boolean init() throws IOException {
         Thread thread = new Thread("Server console handler") {
             public void run() {
-                BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(System.in, StandardCharsets.UTF_8));
+                // CraftBukkit start
+                if (!org.bukkit.craftbukkit.Main.useConsole) {
+                    return;
+                }
+                jline.console.ConsoleReader bufferedreader = reader;
+
+                // MC-33041, SPIGOT-5538: if System.in is not valid due to javaw, then return
+                try {
+                    System.in.available();
+                } catch (IOException ex) {
+                    return;
+                }
+                // CraftBukkit end
 
                 String s;
 
                 try {
-                    while (!DedicatedServer.this.isStopped() && DedicatedServer.this.isRunning() && (s = bufferedreader.readLine()) != null) {
-                        DedicatedServer.this.issueCommand(s, DedicatedServer.this.getServerCommandListener());
+                    // CraftBukkit start - JLine disabling compatibility
+                    while (!DedicatedServer.this.isStopped() && DedicatedServer.this.isRunning()) {
+                        if (org.bukkit.craftbukkit.Main.useJline) {
+                            s = bufferedreader.readLine(">", null);
+                        } else {
+                            s = bufferedreader.readLine();
+                        }
+
+                        // SPIGOT-5220: Throttle if EOF (ctrl^d) or stdin is /dev/null
+                        if (s == null) {
+                            try {
+                                Thread.sleep(50L);
+                            } catch (InterruptedException ex) {
+                                Thread.currentThread().interrupt();
+                            }
+                            continue;
+                        }
+                        if (s.trim().length() > 0) { // Trim to filter lines which are just spaces
+                            DedicatedServer.this.issueCommand(s, DedicatedServer.this.getServerCommandListener());
+                        }
+                        // CraftBukkit end
                     }
                 } catch (IOException ioexception) {
                     DedicatedServer.LOGGER.error("Exception handling console input", ioexception);
@@ -81,6 +125,27 @@
             }
         };
 
+        // CraftBukkit start - TODO: handle command-line logging arguments
+        java.util.logging.Logger global = java.util.logging.Logger.getLogger("");
+        global.setUseParentHandlers(false);
+        for (java.util.logging.Handler handler : global.getHandlers()) {
+            global.removeHandler(handler);
+        }
+        global.addHandler(new org.bukkit.craftbukkit.util.ForwardLogHandler());
+
+        final org.apache.logging.log4j.core.Logger logger = ((org.apache.logging.log4j.core.Logger) LogManager.getRootLogger());
+        for (org.apache.logging.log4j.core.Appender appender : logger.getAppenders().values()) {
+            if (appender instanceof org.apache.logging.log4j.core.appender.ConsoleAppender) {
+                logger.removeAppender(appender);
+            }
+        }
+
+        new org.bukkit.craftbukkit.util.TerminalConsoleWriterThread(System.out, this.reader).start();
+
+        System.setOut(new PrintStream(new LoggerOutputStream(logger, Level.INFO), true));
+        System.setErr(new PrintStream(new LoggerOutputStream(logger, Level.WARN), true));
+        // CraftBukkit end
+
         thread.setDaemon(true);
         thread.setUncaughtExceptionHandler(new DefaultUncaughtExceptionHandler(DedicatedServer.LOGGER));
         thread.start();
@@ -134,6 +199,12 @@
             return false;
         }
 
+        // CraftBukkit start
+        this.a((PlayerList) (new DedicatedPlayerList(this)));
+        server.loadPlugins();
+        server.enablePlugins(org.bukkit.plugin.PluginLoadOrder.STARTUP);
+        // CraftBukkit end
+
         if (!this.getOnlineMode()) {
             DedicatedServer.LOGGER.warn("**** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!");
             DedicatedServer.LOGGER.warn("The server will make no attempt to authenticate usernames. Beware.");
@@ -148,7 +219,7 @@
         if (!NameReferencingFileConverter.e(this)) {
             return false;
         } else {
-            this.a((PlayerList) (new DedicatedPlayerList(this)));
+            this.convertable = new Convertable(server.getWorldContainer().toPath(), server.getWorldContainer().toPath().resolve("../backups"), this.dataConverterManager); // CraftBukkit - moved from MinecraftServer constructor
             long i = SystemUtils.getMonotonicNanos();
             String s = dedicatedserverproperties.levelSeed;
             String s1 = dedicatedserverproperties.generatorSettings;
@@ -178,7 +249,13 @@
             if (worldtype == WorldType.FLAT) {
                 jsonobject.addProperty("flat_world_options", s1);
             } else if (!s1.isEmpty()) {
-                jsonobject = ChatDeserializer.a(s1);
+                // CraftBukkit start
+                try {
+                    jsonobject = ChatDeserializer.a(s1);
+                } catch (Exception ex) {
+                    DedicatedServer.LOGGER.warn("Invalid generator-settings, ignoring", ex);
+                }
+                // CraftBukkit end
             }
 
             this.a(this.getWorld(), this.getWorld(), j, worldtype, jsonobject);
@@ -200,6 +277,7 @@
                 DedicatedServer.LOGGER.info("Starting remote control listener");
                 this.remoteControlListener = new RemoteControlListener(this);
                 this.remoteControlListener.a();
+                this.remoteConsole = new org.bukkit.craftbukkit.command.CraftRemoteConsoleCommandSender(this.remoteControlCommandListener); // CraftBukkit
             }
 
             if (this.getMaxTickTime() > 0L) {
@@ -307,6 +385,7 @@
             this.remoteStatusListener.b();
         }
 
+        System.exit(0); // CraftBukkit
     }
 
     @Override
@@ -340,7 +419,15 @@
         while (!this.serverCommandQueue.isEmpty()) {
             ServerCommand servercommand = (ServerCommand) this.serverCommandQueue.remove(0);
 
-            this.getCommandDispatcher().a(servercommand.source, servercommand.command);
+            // CraftBukkit start - ServerCommand for preprocessing
+            ServerCommandEvent event = new ServerCommandEvent(console, servercommand.command);
+            server.getPluginManager().callEvent(event);
+            if (event.isCancelled()) continue;
+            servercommand = new ServerCommand(event.getCommand(), servercommand.source);
+
+            // this.getCommandDispatcher().a(servercommand.source, servercommand.command); // Called in dispatchServerCommand
+            server.dispatchServerCommand(console, servercommand);
+            // CraftBukkit end
         }
 
     }
@@ -540,14 +627,45 @@
 
     @Override
     public String getPlugins() {
-        return "";
+        // CraftBukkit start - Whole method
+        StringBuilder result = new StringBuilder();
+        org.bukkit.plugin.Plugin[] plugins = server.getPluginManager().getPlugins();
+
+        result.append(server.getName());
+        result.append(" on Bukkit ");
+        result.append(server.getBukkitVersion());
+
+        if (plugins.length > 0 && server.getQueryPlugins()) {
+            result.append(": ");
+
+            for (int i = 0; i < plugins.length; i++) {
+                if (i > 0) {
+                    result.append("; ");
+                }
+
+                result.append(plugins[i].getDescription().getName());
+                result.append(" ");
+                result.append(plugins[i].getDescription().getVersion().replaceAll(";", ","));
+            }
+        }
+
+        return result.toString();
+        // CraftBukkit end
     }
 
     @Override
     public String executeRemoteCommand(String s) {
         this.remoteControlCommandListener.clearMessages();
         this.executeSync(() -> {
-            this.getCommandDispatcher().a(this.remoteControlCommandListener.getWrapper(), s);
+            // CraftBukkit start - fire RemoteServerCommandEvent
+            RemoteServerCommandEvent event = new RemoteServerCommandEvent(remoteConsole, s);
+            server.getPluginManager().callEvent(event);
+            if (event.isCancelled()) {
+                return;
+            }
+            ServerCommand serverCommand = new ServerCommand(event.getCommand(), remoteControlCommandListener.getWrapper());
+            server.dispatchServerCommand(remoteConsole, serverCommand);
+            // CraftBukkit end
         });
         return this.remoteControlCommandListener.getMessages();
     }
@@ -568,4 +686,16 @@
     public boolean a(GameProfile gameprofile) {
         return false;
     }
+
+    // CraftBukkit start
+    @Override
+    public boolean isDebugging() {
+        return this.getDedicatedServerProperties().debug;
+    }
+
+    @Override
+    public CommandSender getBukkitSender(CommandListenerWrapper wrapper) {
+        return console;
+    }
+    // CraftBukkit end
 }