From 6fd401589c81c64241b18c2fe5e1b35ab7d6fda5 Mon Sep 17 00:00:00 2001 From: Eoghanmc22 Date: Fri, 20 Nov 2020 08:07:09 -0500 Subject: [PATCH] slightly optimize CommandManager --- .../server/command/CommandManager.java | 32 +++++++++++++------ .../net/minestom/server/entity/Player.java | 2 +- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/minestom/server/command/CommandManager.java b/src/main/java/net/minestom/server/command/CommandManager.java index 2b23eaa72..667cd3413 100644 --- a/src/main/java/net/minestom/server/command/CommandManager.java +++ b/src/main/java/net/minestom/server/command/CommandManager.java @@ -2,7 +2,6 @@ package net.minestom.server.command; import it.unimi.dsi.fastutil.ints.IntArrayList; import it.unimi.dsi.fastutil.ints.IntList; -import net.minestom.server.MinecraftServer; import net.minestom.server.command.builder.Command; import net.minestom.server.command.builder.CommandDispatcher; import net.minestom.server.command.builder.CommandSyntax; @@ -30,6 +29,9 @@ import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; import java.util.*; import java.util.function.Consumer; @@ -55,25 +57,35 @@ public final class CommandManager { running = true; // Setup console thread Thread consoleThread = new Thread(() -> { - final Scanner scanner = new Scanner(System.in); + BufferedReader bi = new BufferedReader(new InputStreamReader(System.in)); while (running) { - if (scanner.hasNext()) { - String command = scanner.nextLine(); - if (!command.startsWith(COMMAND_PREFIX)) - continue; - command = command.replaceFirst(COMMAND_PREFIX, ""); - execute(consoleSender, command); + + try { + if (bi.ready()) { + String command = bi.readLine(); + if (!command.startsWith(COMMAND_PREFIX)) + continue; + command = command.replaceFirst(COMMAND_PREFIX, ""); + execute(consoleSender, command); + } + } catch (IOException e) { + e.printStackTrace(); + continue; } // Prevent permanent looping try { - Thread.sleep(MinecraftServer.TICK_MS); + Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } - scanner.close(); + try { + bi.close(); + } catch (IOException e) { + e.printStackTrace(); + } }, "ConsoleCommand-Thread"); consoleThread.setDaemon(true); consoleThread.start(); diff --git a/src/main/java/net/minestom/server/entity/Player.java b/src/main/java/net/minestom/server/entity/Player.java index 11313f224..b4de068c8 100644 --- a/src/main/java/net/minestom/server/entity/Player.java +++ b/src/main/java/net/minestom/server/entity/Player.java @@ -345,7 +345,7 @@ public class Player extends LivingEntity implements CommandSender { // Flush all pending packets if (PlayerUtils.isNettyClient(this)) { Channel channel = ((NettyPlayerConnection) playerConnection).getChannel(); - channel.eventLoop().execute(() -> channel.flush()); + channel.eventLoop().execute(channel::flush); } // Network tick verification