Fixed console input preview not showing up

This commit is contained in:
RaphiMC 2023-11-02 21:20:34 +01:00
parent e2c712239c
commit 368e86d374
No known key found for this signature in database
GPG key ID: 0F6BB0657A03AC94
2 changed files with 4 additions and 99 deletions

View file

@ -24,10 +24,9 @@ import net.raphimc.viaproxy.protocolhack.viaproxy.ConsoleCommandSender;
import net.raphimc.viaproxy.util.ArrayHelper; import net.raphimc.viaproxy.util.ArrayHelper;
import net.raphimc.viaproxy.util.logging.Logger; import net.raphimc.viaproxy.util.logging.Logger;
import java.io.BufferedReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays; import java.util.Arrays;
import java.util.Scanner;
public class ConsoleHandler { public class ConsoleHandler {
@ -43,10 +42,10 @@ public class ConsoleHandler {
} }
private static void listen() { private static void listen() {
final BufferedReader reader = new BufferedReader(new InputStreamReader(new DelayedStream(System.in, 500))); final Scanner scanner = new Scanner(System.in);
try { try {
String line; while (scanner.hasNextLine()) {
while ((line = reader.readLine()) != null) { final String line = scanner.nextLine();
try { try {
final String[] parts = line.split(" "); final String[] parts = line.split(" ");
if (parts.length == 0) continue; if (parts.length == 0) continue;

View file

@ -1,94 +0,0 @@
/*
* This file is part of ViaProxy - https://github.com/RaphiMC/ViaProxy
* Copyright (C) 2023 RK_01/RaphiMC and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.raphimc.viaproxy.cli;
import java.io.IOException;
import java.io.InputStream;
public class DelayedStream extends InputStream {
private final InputStream parent;
private final long sleepDelay;
public DelayedStream(final InputStream parent, final long sleepDelay) {
this.parent = parent;
this.sleepDelay = sleepDelay;
}
public InputStream getParent() {
return this.parent;
}
public long getSleepDelay() {
return this.sleepDelay;
}
public void waitForInput() throws IOException {
while (this.available() <= 0) {
try {
Thread.sleep(this.sleepDelay);
} catch (InterruptedException e) {
throw new IOException("Interrupted", e);
}
}
}
@Override
public int read() throws IOException {
this.waitForInput();
return this.parent.read();
}
@Override
public int read(byte[] b) throws IOException {
this.waitForInput();
return this.parent.read(b);
}
@Override
public int read(byte[] b, int off, int len) throws IOException {
this.waitForInput();
return this.parent.read(b, off, len);
}
@Override
public int available() throws IOException {
return this.parent.available();
}
@Override
public void close() throws IOException {
this.parent.close();
}
@Override
public synchronized void mark(int readlimit) {
this.parent.mark(readlimit);
}
@Override
public synchronized void reset() throws IOException {
this.parent.reset();
}
@Override
public boolean markSupported() {
return this.parent.markSupported();
}
}