completely re-do console

This commit is contained in:
EOT3000 2019-07-13 15:00:51 -04:00
parent 7c74df2aa2
commit 266d374167
15 changed files with 225 additions and 126 deletions

View File

@ -52,8 +52,8 @@ public class ChatColor {
public static final String RESET = ESCAPE + "r";
public static String toANSI(String string) {
string = string.replace(BOLD, "");
string = string.replace(OBFUSCATED, (char) 0x1b + "[6m");
string = string.replace(BOLD, (char) 0x1b + "[1m");
string = string.replace(OBFUSCATED, (char) 0x1b + "[5m");
string = string.replace(ITALIC, (char) 0x1b + "[3m");
string = string.replace(UNDERLINE, (char) 0x1b + "[4m");
string = string.replace(STRIKETHROUGH, (char) 0x1b + "[9m");

View File

@ -0,0 +1,69 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.api;
public class ConsoleColors {
public static final String RESET = "\u001b[0m";
public static final String BOLD = "\u001b[1m";
public static final String BLINK = "\u001b[5m";
public static final String WHITE = "\u001b[30m";
public static final String RED = "\u001b[31m";
public static final String GREEN = "\u001b[32m";
public static final String YELLOW = "\u001b[33m";
public static final String BLUE = "\u001b[34m";
public static final String MAGENTA = "\u001b[35m";
public static final String CYAN = "\u001b[36m";
public static final String LIGHT_GRAY = "\u001b[37m";
public static final String DARK_GRAY = "\u001b[90m";
public static final String BRIGHT_RED = "\u001b[91m";
public static final String BRIGHT_GREEN = "\u001b[92m";
public static final String BRIGHT_YELLOW = "\u001b[93m";
public static final String BRIGHT_BLUE = "\u001b[94m";
public static final String BRIGHT_MAGENTA = "\u001b[95m";
public static final String BRIGHT_CYAN = "\u001b[96m";
public static final String BLACK = "\u001b[97m";
public static final String BLACK_BACKGROUND = "\u001b[30m";
public static final String RED_BACKGROUND = "\u001b[31m";
public static final String GREEN_BACKGROUND = "\u001b[32m";
public static final String YELLOW_BACKGROUND = "\u001b[33m";
public static final String BLUE_BACKGROUND = "\u001b[34m";
public static final String MAGENTA_BACKGROUND = "\u001b[35m";
public static final String CYAN_BACKGROUND = "\u001b[36m";
public static final String WHITE_BACKGROUND = "\u001b[37m";
/*public static final String BLACK_BACKGROUND = "\u001b[30m";
public static final String RED_BACKGROUND = "\u001b[31m";
public static final String GREEN_BACKGROUND = "\u001b[32m";
public static final String YELLOW_BACKGROUND = "\u001b[33m";
public static final String BLUE_BACKGROUND = "\u001b[34m";
public static final String MAGENTA_BACKGROUND = "\u001b[35m";
public static final String CYAN_BACKGROUND = "\u001b[36m";
public static final String WHITE_BACKGROUND = "\u001b[37m";*/
}

View File

@ -0,0 +1,4 @@
package org.geysermc.api;
public class GeyserAPI {
}

View File

@ -27,13 +27,6 @@ package org.geysermc.api.logger;
public interface Logger {
/**
* Logs an info message to console
*
* @param message the message to log
*/
void info(String message);
/**
* Logs a severe message to console
*
@ -41,6 +34,13 @@ public interface Logger {
*/
void severe(String message);
/**
* Logs an error message to console
*
* @param message the message to log
*/
void error(String message);
/**
* Logs a warning message to console
*
@ -48,6 +48,13 @@ public interface Logger {
*/
void warning(String message);
/**
* Logs an info message to console
*
* @param message the message to log
*/
void info(String message);
/**
* Logs a debug message to console
*

View File

@ -28,6 +28,10 @@ package org.geysermc.api.plugin;
import lombok.Getter;
import lombok.Setter;
/**
* The class that any main plugin class should extend.
* The first init point is the constructor, followed by onLoad, and finally onEnable.
*/
public class Plugin {
@Getter
@ -54,4 +58,11 @@ public class Plugin {
public void onLoad() {
}
/**
* Called when th server is reloaded
*/
public void onReload() {
}
}

View File

@ -65,6 +65,12 @@
<version>2.1.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.fusesource.jansi</groupId>
<artifactId>jansi</artifactId>
<version>1.18</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.nukkitx.protocol</groupId>
<artifactId>bedrock-v354</artifactId>

View File

@ -27,12 +27,20 @@ package org.geysermc.connector;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.nukkitx.network.raknet.RakNetServer;
import com.nukkitx.network.raknet.RakNetServerListener;
import com.nukkitx.network.raknet.RakNetServerSession;
import com.nukkitx.protocol.bedrock.BedrockPacketCodec;
import com.nukkitx.protocol.bedrock.BedrockServer;
import com.nukkitx.protocol.bedrock.v361.Bedrock_v361;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.socket.DatagramPacket;
import lombok.Getter;
import org.apache.logging.log4j.core.filter.Filterable;
import org.fusesource.jansi.AnsiConsole;
import org.geysermc.api.ChatColor;
import org.geysermc.api.Connector;
import org.geysermc.api.ConsoleColors;
import org.geysermc.api.Geyser;
import org.geysermc.api.command.CommandMap;
import org.geysermc.api.logger.Logger;
@ -48,10 +56,8 @@ import org.geysermc.connector.plugin.GeyserPluginLoader;
import org.geysermc.connector.plugin.GeyserPluginManager;
import org.geysermc.connector.utils.Toolbox;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.lang.reflect.Field;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
@ -91,15 +97,21 @@ public class GeyserConnector implements Connector {
}
private GeyserConnector() {
if(!(System.console() == null) && System.getProperty("os.name", "Windows 10").toLowerCase().contains("windows")) {
AnsiConsole.systemInstall();
}
instance = this;
this.generalThreadPool = Executors.newScheduledThreadPool(32); //TODO: Make configurable value
this.logger = new GeyserLogger(this);
this.logger = GeyserLogger.DEFAULT;
ConsoleCommandReader consoleReader = new ConsoleCommandReader(this);
consoleReader.startConsole();
logger.info(ChatColor.AQUA + "******************************************");
logger.info("******************************************");
logger.info("");
logger.info("Loading " + NAME + " vesion " + VERSION);
logger.info("");

View File

@ -29,32 +29,19 @@ import org.geysermc.api.ChatColor;
import org.geysermc.connector.GeyserConnector;
import io.sentry.Sentry;
import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.io.*;
import java.lang.reflect.Field;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.*;
public class GeyserLogger implements org.geysermc.api.logger.Logger {
private Logger logger;
private boolean colored = true;
private boolean debug = true;
public GeyserLogger(GeyserConnector connector) {
this.logger = Logger.getLogger(connector.getClass().getName());
public static final GeyserLogger DEFAULT = new GeyserLogger();
logger.setUseParentHandlers(false);
logger.setLevel(Level.ALL);
System.setOut(new PrintStream(new LoggingOutputStream(this.logger, Level.INFO), true));
private GeyserLogger() {
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.INFO);
@ -71,15 +58,13 @@ public class GeyserLogger implements org.geysermc.api.logger.Logger {
}
});
logger.addHandler(consoleHandler);
try {
File logDir = new File("logs");
logDir.mkdir();
File logFile = new File(logDir, "latest.log");
int maxLogFileSize = 20;//Mo
if (logFile.exists() && (logFile.length()) > maxLogFileSize * 1024L * 1024L)
logger.warning("Your log file is larger than " + maxLogFileSize + "Mo, you should backup and clean it !");
this.warning("Your log file is larger than " + maxLogFileSize + "Mo, you should backup and clean it !");
FileHandler fileHandler = new FileHandler(logFile.getCanonicalPath(), true);
fileHandler.setLevel(Level.INFO);
fileHandler.setFormatter(new SimpleFormatter() {
@ -94,7 +79,6 @@ public class GeyserLogger implements org.geysermc.api.logger.Logger {
);
}
});
logger.addHandler(fileHandler);
} catch (IOException | SecurityException ex) {
Logger.getLogger(GeyserLogger.class.getName()).log(Level.SEVERE, null, ex);
}
@ -102,31 +86,36 @@ public class GeyserLogger implements org.geysermc.api.logger.Logger {
if (System.getenv().containsKey("DP_SENTRY_CLIENT_KEY")) {
Handler sentryHandler = new io.sentry.jul.SentryHandler();
sentryHandler.setLevel(Level.SEVERE);
logger.addHandler(sentryHandler);
Sentry.init(System.getenv().get("DP_SENTRY_CLIENT_KEY"));
}
}
public void info(String message) {
logger.info(printConsole(message, colored));
}
@Override
public void severe(String message) {
logger.severe(printConsole(message, colored));
System.out.println(printConsole(ChatColor.DARK_RED + message, colored));
}
@Override
public void error(String message) {
System.out.println(printConsole(ChatColor.RED + message, colored));
}
@Override
public void warning(String message) {
logger.warning(printConsole(message, colored));
System.out.println(printConsole(ChatColor.YELLOW + message, colored));
}
@Override
public void info(String message) {
System.out.println(printConsole(ChatColor.WHITE + message, colored));
}
@Override
public void debug(String message) {
if (debug)
info(message);
System.out.println(printConsole(ChatColor.GRAY + message, colored));
}
public void stop() {
for (Handler handler : logger.getHandlers())
handler.close();
}
public static String printConsole(String message, boolean colors) {

View File

@ -1,66 +0,0 @@
/*
* Copyright (c) 2019 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.connector.console;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.logging.Level;
import java.util.logging.Logger;
public class LoggingOutputStream extends ByteArrayOutputStream {
private Logger logger;
private Level level;
public LoggingOutputStream(Logger logger, Level level) {
this.logger = logger;
this.level = level;
}
@Override
public synchronized void write(int b) {
super.write(b);
try {
String contents = toString(StandardCharsets.UTF_8.name());
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))
logger.logp(level, "", "", contents);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(LoggingOutputStream.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public synchronized void flush() throws IOException {
String contents = toString(StandardCharsets.UTF_8.name());
super.reset();
if (!contents.isEmpty() && !contents.equals(System.getProperty("line.separator")))
logger.logp(level, "", "", contents);
}
}

View File

@ -51,16 +51,18 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
@Override
public BedrockPong onQuery(InetSocketAddress inetSocketAddress) {
System.out.println(inetSocketAddress + " has pinged you!");
GeyserConfiguration config = connector.getConfig();
BedrockPong pong = new BedrockPong();
pong.setEdition("MCPE");
pong.setMotd(config.getBedrock().getMotd1());
pong.setSubMotd(config.getBedrock().getMotd2());
pong.setPlayerCount(1);
pong.setPlayerCount(2);
pong.setMaximumPlayerCount(config.getMaxPlayers());
pong.setGameType("Default");
pong.setNintendoLimited(false);
pong.setProtocolVersion(GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion());
pong.setVersion("1.12.0");
return pong;
}
@ -69,7 +71,10 @@ public class ConnectorServerEventHandler implements BedrockServerEventHandler {
public void onSessionCreation(BedrockServerSession bedrockServerSession) {
bedrockServerSession.setLogging(true);
bedrockServerSession.setPacketHandler(new UpstreamPacketHandler(connector, new GeyserSession(connector, bedrockServerSession)));
bedrockServerSession.addDisconnectHandler((x) -> System.out.println("Bedrock user with ip: " + bedrockServerSession.getAddress().getAddress() + " has disconected for reason " + x));
bedrockServerSession.setPacketCodec(Bedrock_v361.V361_CODEC);
}
}

View File

@ -52,6 +52,7 @@ public class UpstreamPacketHandler implements BedrockPacketHandler {
System.err.println("Handled " + loginPacket.getClass().getSimpleName());
// TODO: Implement support for multiple protocols
if (loginPacket.getProtocolVersion() != GeyserConnector.BEDROCK_PACKET_CODEC.getProtocolVersion()) {
System.out.println("unsupported");
session.getUpstream().disconnect("Unsupported Bedrock version. Are you running an outdated version?");
return true;
}
@ -74,8 +75,12 @@ public class UpstreamPacketHandler implements BedrockPacketHandler {
return true;
}
PlayStatusPacket playStatus = new PlayStatusPacket();
playStatus.setStatus(PlayStatusPacket.Status.LOGIN_SUCCESS);
session.getUpstream().sendPacketImmediately(playStatus);
ResourcePacksInfoPacket resourcePacksInfo = new ResourcePacksInfoPacket();
@ -101,6 +106,7 @@ public class UpstreamPacketHandler implements BedrockPacketHandler {
session.getUpstream().disconnect("disconnectionScreen.resourcePack");
break;
}
return true;
}

View File

@ -31,6 +31,7 @@ import com.github.steveice10.packetlib.event.session.ConnectedEvent;
import com.github.steveice10.packetlib.event.session.DisconnectedEvent;
import com.github.steveice10.packetlib.event.session.PacketReceivedEvent;
import com.github.steveice10.packetlib.event.session.SessionAdapter;
import com.github.steveice10.packetlib.packet.Packet;
import com.github.steveice10.packetlib.tcp.TcpSessionFactory;
import com.nukkitx.network.util.DisconnectReason;
import com.nukkitx.protocol.PlayerSession;
@ -56,6 +57,8 @@ public class GeyserSession implements PlayerSession {
@Getter
private Client downstream;
private final GeyserSession THIS = this;
@Getter
private AuthenticationData authenticationData;
@ -78,13 +81,13 @@ public class GeyserSession implements PlayerSession {
@Override
public void disconnected(DisconnectedEvent event) {
connector.getLogger().info(authenticationData.getName() + " has disconnected from remote java server on address " + remoteServer.getAddress());
connector.getLogger().info(authenticationData.getName() + " has disconnected from remote java server on address " + remoteServer.getAddress() + " because of " + event.getReason());
upstream.disconnect(event.getReason());
}
@Override
public void packetReceived(PacketReceivedEvent event) {
Registry.JAVA.translate(event.getPacket().getClass(), event.getPacket());
Registry.JAVA.translate(event.getPacket().getClass(), event.getPacket(), THIS);
}
});

View File

@ -1,23 +1,26 @@
package org.geysermc.connector.network.translators;
import com.github.steveice10.packetlib.packet.Packet;
import org.geysermc.api.Geyser;
import org.geysermc.connector.network.session.GeyserSession;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
public class Registry<T> {
private final Map<Class<? extends T>, Consumer<? extends T>> MAP = new HashMap<>();
private final Map<Class<? extends T>, BiConsumer<? extends T, GeyserSession>> MAP = new HashMap<>();
public static final Registry<Packet> JAVA = new Registry<>();
public static <T extends Packet> void add(Class<T> clazz, Consumer<T> translator) {
public static <T extends Packet> void add(Class<T> clazz, BiConsumer<T, GeyserSession> translator) {
JAVA.MAP.put(clazz, translator);
}
public <P extends T> void translate(Class<P> clazz, P p) {
public <P extends T> void translate(Class<P> clazz, P p, GeyserSession s) {
try {
((Consumer<P>) JAVA.MAP.get(clazz)).accept(p);
((BiConsumer<P, GeyserSession>) JAVA.MAP.get(clazz)).accept(p, s);
} catch (NullPointerException e) {
System.err.println("could not translate packet" + p.getClass().getSimpleName());
}

View File

@ -4,23 +4,34 @@ import com.flowpowered.math.vector.Vector2f;
import com.flowpowered.math.vector.Vector3f;
import com.flowpowered.math.vector.Vector3i;
import com.github.steveice10.mc.protocol.packet.ingame.server.ServerJoinGamePacket;
import com.github.steveice10.mc.protocol.packet.ingame.server.entity.player.ServerPlayerAbilitiesPacket;
import com.nukkitx.protocol.bedrock.data.GamePublishSetting;
import com.nukkitx.protocol.bedrock.data.GameRule;
import com.nukkitx.protocol.bedrock.packet.StartGamePacket;
import com.nukkitx.protocol.bedrock.packet.*;
import com.nukkitx.protocol.bedrock.v340.serializer.FullChunkDataSerializer_v340;
import com.nukkitx.protocol.bedrock.v340.serializer.ResourcePackChunkDataSerializer_v340;
import org.geysermc.connector.utils.Toolbox;
public class TranslatorsInit {
public static void start() {
addLoginPackets();
}
private static void addLoginPackets() {
Registry.add(ServerJoinGamePacket.class, (x) -> {
Registry.add(ServerJoinGamePacket.class, (x, y) -> {
AdventureSettingsPacket bedrockPacket = new AdventureSettingsPacket();
bedrockPacket.setUniqueEntityId(x.getEntityId());
y.getUpstream().sendPacketImmediately(bedrockPacket);
System.out.println(y.getUpstream().isClosed());
StartGamePacket startGamePacket = new StartGamePacket();
startGamePacket.setUniqueEntityId(x.getEntityId());
startGamePacket.setRuntimeEntityId(x.getEntityId());
startGamePacket.setPlayerGamemode(x.getGameMode().ordinal());
startGamePacket.setPlayerPosition(new Vector3f(-249, 67, -275));
startGamePacket.setPlayerPosition(new Vector3f(0, 0, 0));
startGamePacket.setRotation(new Vector2f(1, 1));
startGamePacket.setSeed(1111);
@ -28,7 +39,7 @@ public class TranslatorsInit {
startGamePacket.setGeneratorId(0);
startGamePacket.setLevelGamemode(x.getGameMode().ordinal());
startGamePacket.setDifficulty(1);
startGamePacket.setDefaultSpawn(new Vector3i(-249, 67, -275));
startGamePacket.setDefaultSpawn(new Vector3i(0, 0, 0));
startGamePacket.setAcheivementsDisabled(true);
startGamePacket.setTime(1300);
startGamePacket.setEduLevel(false);
@ -61,6 +72,44 @@ public class TranslatorsInit {
startGamePacket.setEnchantmentSeed(1);
startGamePacket.setMultiplayerCorrelationId("");
startGamePacket.setCachedPalette(Toolbox.CACHED_PALLETE);
y.getUpstream().sendPacketImmediately(startGamePacket);
System.out.println(y.getUpstream().isClosed());
Vector3f pos = new Vector3f(0, 0, 0);
int chunkX = pos.getFloorX() >> 4;
int chunkZ = pos.getFloorX() >> 4;
for (int x1 = -3; x1 < 3; x1++) {
for (int z = -3; z < 3; z++) {
LevelChunkPacket data = new LevelChunkPacket();
data.setChunkX(chunkX + x1);
data.setChunkZ(chunkZ + z);
data.setData(new byte[0]);
y.getUpstream().sendPacketImmediately(data);
System.out.println(y.getUpstream().isClosed());
}
}
PlayStatusPacket packet = new PlayStatusPacket();
packet.setStatus(PlayStatusPacket.Status.PLAYER_SPAWN);
y.getUpstream().sendPacket(packet);
System.out.println(y.getUpstream().isClosed());
});
}
}

View File

@ -27,6 +27,7 @@ package org.geysermc.connector.plugin;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.nukkitx.protocol.bedrock.handler.BedrockPacketHandler;
import org.geysermc.api.Connector;
import org.geysermc.api.plugin.Plugin;
import java.io.File;