Added debug levels. Code style

This commit is contained in:
Nanit 2020-11-26 20:35:12 +02:00
parent bc05228da3
commit 7cb3846848
10 changed files with 69 additions and 48 deletions

View File

@ -9,11 +9,11 @@ public final class LimboConfig {
private static String host;
private static int port;
private static boolean onlineMode;
private static int maxPlayers;
private static IpForwardingType ipForwardingType;
private static long readTimeout;
private static PingData pingData;
private static int debugLevel = 3;
public static void load(Path file) throws IOException {
if (!Files.exists(file)){
@ -25,14 +25,16 @@ public final class LimboConfig {
host = properties.getProperty("host");
port = Integer.parseInt(properties.getProperty("port"));
onlineMode = Boolean.parseBoolean(properties.getProperty("online-mode"));
maxPlayers = Integer.parseInt(properties.getProperty("max-players"));
ipForwardingType = IpForwardingType.valueOf(properties.getProperty("ip-forwarding").toUpperCase());
readTimeout = Long.parseLong(properties.getProperty("read-timeout"));
pingData = new PingData();
pingData = new PingData();
pingData.setVersion(properties.getProperty("ping-version"));
pingData.setDescription(properties.getProperty("ping-description"));
debugLevel = Integer.parseInt(properties.getProperty("debug-level"));
}
public static String getHost() {
@ -43,10 +45,6 @@ public final class LimboConfig {
return port;
}
public static boolean isOnlineMode() {
return onlineMode;
}
public static int getMaxPlayers() {
return maxPlayers;
}
@ -63,6 +61,10 @@ public final class LimboConfig {
return pingData;
}
public static int getDebugLevel() {
return debugLevel;
}
public enum IpForwardingType {
NONE,
LEGACY,

View File

@ -4,7 +4,6 @@ import io.netty.channel.Channel;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import ru.nanit.limbo.LimboConfig;
import ru.nanit.limbo.protocol.packets.login.*;
import ru.nanit.limbo.protocol.packets.play.*;
@ -19,7 +18,7 @@ import ru.nanit.limbo.protocol.registry.State;
import ru.nanit.limbo.server.LimboServer;
import ru.nanit.limbo.util.Logger;
import ru.nanit.limbo.util.UuidUtil;
import ru.nanit.limbo.world.DefaultDimension;
import ru.nanit.limbo.world.DimensionRegistry;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
@ -38,10 +37,6 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
return uuid;
}
public String getUsername() {
return username;
}
public ClientConnection(Channel channel, LimboServer server){
this.server = server;
this.channel = channel;
@ -105,10 +100,6 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
startJoinProcess();
}
if (packet instanceof PacketKeepAlive){
System.out.println("Get KeepAlive " + ((PacketKeepAlive)packet).getId());
}
}
private void startJoinProcess(){
@ -127,8 +118,8 @@ public class ClientConnection extends ChannelInboundHandlerAdapter {
joinGame.setWorldName("minecraft:world");
joinGame.setWorldNames("minecraft:world");
joinGame.setHashedSeed(0);
joinGame.setDimensionCodec(DefaultDimension.getCodec());
joinGame.setDimension(DefaultDimension.getDimension());
joinGame.setDimensionCodec(DimensionRegistry.getCodec());
joinGame.setDimension(DimensionRegistry.getDimension());
PacketPlayerPositionAndLook positionAndLook = new PacketPlayerPositionAndLook();

View File

@ -22,7 +22,7 @@ public class VarIntByteDecoder implements ByteProcessor {
return true;
}
public int getReadVarint() {
public int getReadVarInt() {
return readVarInt;
}

View File

@ -16,31 +16,27 @@ public class VarIntFrameDecoder extends ByteToMessageDecoder {
return;
}
final VarIntByteDecoder reader = new VarIntByteDecoder();
VarIntByteDecoder reader = new VarIntByteDecoder();
int varIntEnd = in.forEachByte(reader);
int varintEnd = in.forEachByte(reader);
if (varintEnd == -1) {
return;
}
if (varIntEnd == -1) return;
if (reader.getResult() == VarIntByteDecoder.DecodeResult.SUCCESS) {
int readVarint = reader.getReadVarint();
int readVarInt = reader.getReadVarInt();
int bytesRead = reader.getBytesRead();
if (readVarint < 0) {
Logger.error("BAD_LENGTH_CACHED");
} else if (readVarint == 0) {
// skip over the empty packet and ignore it
in.readerIndex(varintEnd + 1);
if (readVarInt < 0) {
Logger.error("[VarIntFrameDecoder] Bad data length");
} else if (readVarInt == 0) {
in.readerIndex(varIntEnd + 1);
} else {
int minimumRead = bytesRead + readVarint;
int minimumRead = bytesRead + readVarInt;
if (in.isReadable(minimumRead)) {
out.add(in.retainedSlice(varintEnd + 1, readVarint));
out.add(in.retainedSlice(varIntEnd + 1, readVarInt));
in.skipBytes(minimumRead);
}
}
} else if (reader.getResult() == VarIntByteDecoder.DecodeResult.TOO_BIG) {
Logger.error("Too big");
Logger.error("[VarIntFrameDecoder] Too big data");
}
}
}

View File

@ -10,14 +10,14 @@ import ru.nanit.limbo.protocol.ByteMessage;
public class VarIntLengthEncoder extends MessageToByteEncoder<ByteBuf> {
@Override
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) throws Exception {
protected void encode(ChannelHandlerContext ctx, ByteBuf buf, ByteBuf out) {
ByteMessage msg = new ByteMessage(out);
msg.writeVarInt(buf.readableBytes());
msg.writeBytes(buf);
}
@Override
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) throws Exception {
protected ByteBuf allocateBuffer(ChannelHandlerContext ctx, ByteBuf msg, boolean preferDirect) {
int anticipatedRequiredCapacity = 5 + msg.readableBytes();
return ctx.alloc().heapBuffer(anticipatedRequiredCapacity);
}

View File

@ -7,7 +7,7 @@ import ru.nanit.limbo.LimboConfig;
import ru.nanit.limbo.connection.ClientChannelInitializer;
import ru.nanit.limbo.connection.ClientConnection;
import ru.nanit.limbo.util.Logger;
import ru.nanit.limbo.world.DefaultDimension;
import ru.nanit.limbo.world.DimensionRegistry;
import java.nio.file.Paths;
import java.util.Map;
@ -38,7 +38,7 @@ public final class LimboServer {
Logger.info("Starting server...");
LimboConfig.load(Paths.get("./settings.properties"));
DefaultDimension.init();
DimensionRegistry.init();
executor.scheduleAtFixedRate(this::broadcastKeepAlive, 0L, 5L, TimeUnit.SECONDS);

View File

@ -1,5 +1,7 @@
package ru.nanit.limbo.util;
import ru.nanit.limbo.LimboConfig;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
@ -7,6 +9,8 @@ public final class Logger {
private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("hh:mm:ss");
private Logger(){}
public static void info(Object msg, Object... args){
print(Level.INFO, msg, null, args);
}
@ -32,8 +36,10 @@ public final class Logger {
}
public static void print(Level level, Object msg, Throwable t, Object... args){
System.out.println(String.format("%s: %s", getPrefix(level), String.format(msg.toString(), args)));
if (t != null) t.printStackTrace();
if (LimboConfig.getDebugLevel() >= level.getIndex()){
System.out.println(String.format("%s: %s", getPrefix(level), String.format(msg.toString(), args)));
if (t != null) t.printStackTrace();
}
}
private static String getPrefix(Level level){
@ -46,18 +52,24 @@ public final class Logger {
public enum Level {
INFO ("INFO"),
WARNING("WARNING"),
ERROR("ERROR");
INFO ("INFO", 1),
WARNING("WARNING", 2),
ERROR("ERROR", 3);
private final String display;
private final int index;
Level(String display){
Level(String display, int index){
this.display = display;
this.index = index;
}
public String getDisplay() {
return display;
}
public int getIndex() {
return index;
}
}
}

View File

@ -3,7 +3,9 @@ package ru.nanit.limbo.util;
import java.nio.charset.StandardCharsets;
import java.util.UUID;
public class UuidUtil {
public final class UuidUtil {
private UuidUtil(){}
public static UUID getOfflineModeUuid(String username){
return UUID.nameUUIDFromBytes(("OfflinePlayer:" + username)

View File

@ -3,7 +3,7 @@ package ru.nanit.limbo.world;
import net.kyori.adventure.nbt.CompoundBinaryTag;
import net.kyori.adventure.nbt.ListBinaryTag;
public final class DefaultDimension {
public final class DimensionRegistry {
private static CompoundBinaryTag CODEC;
private static CompoundBinaryTag DIMENSION;

View File

@ -2,10 +2,19 @@
# NanoLimbo configuration
#
# Server's host address
host=localhost
# Server's port
port=65535
# Max amount of players can join to server
max-players=100
# Version string when client version is not compatible with server one
ping-version=NanoLimbo
# Server's description component
ping-description={"text": "NanoLimbo"}
# Player info forwarding support. Available types: NONE, LEGACY, MODERN
@ -15,4 +24,13 @@ ip-forwarding=LEGACY
ip-forwarding-secret=<YOUR_SECRET_HERE>
# Read timeout for connections in milliseconds
read-timeout=30000
read-timeout=30000
# Define debug level. On release, i recommend to use 1 level, since
# there are many useless for release warnings about undefined packets and other.
# Levels:
# 0 - Display nothing
# 1 - Display only useful info
# 2 - Display info and warnings
# 3 - Display info, warnings, errors
debug-level=3