Skip to content

Commit a04cb95

Browse files
authored
Try to fix #849, #854, add configurable strict mode in protocol, introduce slf4j logger, cleanup (#855)
* Try to fix #849, #854, add configurable strict mode in protocol, introduce slf4j logger, cleanup * Try to fix #857 * Remove legacy jul logger * Log formatting... * Log formatting...
1 parent 2f7f716 commit a04cb95

35 files changed

Lines changed: 147 additions & 126 deletions

leaves-server/src/main/java/org/leavesmc/leaves/LeavesConfig.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public static void init(final @NotNull File file) {
7878
throw new IOException("Can't create file");
7979
}
8080
} catch (final Exception ex) {
81-
LeavesLogger.LOGGER.severe("Failure to create leaves config", ex);
81+
LeavesLogger.LOGGER.error("Failure to create leaves config", ex);
8282
}
8383
} else {
8484
try {
8585
config.load(file);
8686
} catch (final Exception ex) {
87-
LeavesLogger.LOGGER.severe("Failure to load leaves config", ex);
87+
LeavesLogger.LOGGER.error("Failure to load leaves config", ex);
8888
throw new RuntimeException(ex);
8989
}
9090
}
@@ -104,7 +104,7 @@ public static void reload() {
104104
try {
105105
config.load(LeavesConfig.configFile);
106106
} catch (final Exception ex) {
107-
LeavesLogger.LOGGER.severe("Failure to reload leaves config", ex);
107+
LeavesLogger.LOGGER.error("Failure to reload leaves config", ex);
108108
throw new RuntimeException(ex);
109109
}
110110

@@ -115,7 +115,7 @@ public static void save() {
115115
try {
116116
config.save(LeavesConfig.configFile);
117117
} catch (final Exception ex) {
118-
LeavesLogger.LOGGER.severe("Unable to save leaves config", ex);
118+
LeavesLogger.LOGGER.error("Unable to save leaves config", ex);
119119
}
120120
}
121121

@@ -863,6 +863,9 @@ public static class PerformanceRemoveConfig {
863863
@GlobalConfigCategory("protocol")
864864
public static class ProtocolConfig {
865865

866+
@GlobalConfig("strict-mode")
867+
public boolean strictMode = false;
868+
866869
public BladerenConfig bladeren = new BladerenConfig();
867870

868871
@GlobalConfigCategory("bladeren")
@@ -1113,7 +1116,7 @@ public void runAfterLoader(Boolean value, boolean reload) {
11131116
if (!reload) {
11141117
LeavesUpdateHelper.init();
11151118
if (value) {
1116-
LeavesLogger.LOGGER.warning("Auto-Update is not completely safe. Enabling it may cause data security problems!");
1119+
LeavesLogger.LOGGER.warn("Auto-Update is not completely safe. Enabling it may cause data security problems!");
11171120
}
11181121
}
11191122
}
@@ -1149,7 +1152,7 @@ public static class ExtraYggdrasilValidator extends BooleanConfigValidator {
11491152
@Override
11501153
public void verify(Boolean old, Boolean value) throws IllegalArgumentException {
11511154
if (value) {
1152-
LeavesLogger.LOGGER.warning("extra-yggdrasil-service is an unofficial support. Enabling it may cause data security problems!");
1155+
LeavesLogger.LOGGER.warn("extra-yggdrasil-service is an unofficial support. Enabling it may cause data security problems!");
11531156
GlobalConfiguration.get().unsupportedSettings.performUsernameValidation = true; // always check username
11541157
}
11551158
}
@@ -1315,7 +1318,7 @@ private static class CollisionBehaviorValidator extends EnumConfigValidator<Coll
13151318
@Override
13161319
public CollisionBehavior stringConvert(@NotNull String value) throws IllegalArgumentException {
13171320
if (value.equalsIgnoreCase("BLOCK_SHAPE_VANILLA")) {
1318-
LeavesLogger.LOGGER.warning("Paper has updated the collision behavior to BLOCK_SHAPE_VANILLA mode, converting this to PAPER...");
1321+
LeavesLogger.LOGGER.warn("Paper has updated the collision behavior to BLOCK_SHAPE_VANILLA mode, converting this to PAPER...");
13191322
value = "PAPER";
13201323
}
13211324
return super.stringConvert(value);
Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,9 @@
11
package org.leavesmc.leaves;
22

3-
import org.bukkit.Bukkit;
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
45

5-
import java.util.logging.Level;
6-
import java.util.logging.Logger;
6+
public class LeavesLogger {
77

8-
public class LeavesLogger extends Logger {
9-
public static final LeavesLogger LOGGER = new LeavesLogger();
10-
11-
private LeavesLogger() {
12-
super("Leaves", null);
13-
setParent(Bukkit.getLogger());
14-
setLevel(Level.ALL);
15-
}
16-
17-
public void severe(String msg, Exception exception) {
18-
this.log(Level.SEVERE, msg, exception);
19-
}
20-
21-
public void warning(String msg, Exception exception) {
22-
this.log(Level.WARNING, msg, exception);
23-
}
24-
}
8+
public static final Logger LOGGER = LoggerFactory.getLogger("Leaves");
9+
}

leaves-server/src/main/java/org/leavesmc/leaves/bot/BotDataStorage.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public class BotDataStorage {
2929
private final File botDir;
3030
private final File botListFile;
3131

32-
private CompoundTag savedBotList;
32+
private final CompoundTag savedBotList;
3333

3434
public BotDataStorage(LevelStorageSource.@NotNull LevelStorageAccess session, String dataDir, String listFileName) {
3535
this.botDir = session.getLevelPath(new LevelResource(dataDir)).toFile();

leaves-server/src/main/java/org/leavesmc/leaves/bot/ServerBot.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import java.util.ArrayList;
7676
import java.util.Collection;
7777
import java.util.EnumSet;
78-
import java.util.HashMap;
7978
import java.util.List;
8079
import java.util.Map;
8180
import java.util.Objects;
@@ -449,7 +448,7 @@ public void readAdditionalSaveData(@NotNull ValueInput nbt) {
449448
for (CompoundTag configTag : configNbt) {
450449
String key = configTag.getString("configName").orElseThrow();
451450
if (!this.configs.containsKey(key)) {
452-
LeavesLogger.LOGGER.warning("Trying to load a unknown config \"" + key + "\", discard.");
451+
LeavesLogger.LOGGER.warn("Trying to load a unknown config \"{}\", discard.", key);
453452
continue;
454453
}
455454
this.configs.get(key).load(configTag);
@@ -475,7 +474,7 @@ public void sendFakeData(ServerPlayerConnection playerConnection, boolean login)
475474
ChunkMap.TrackedEntity entityTracker = this.level().getChunkSource().chunkMap.entityMap.get(this.getId());
476475

477476
if (entityTracker == null) {
478-
LeavesLogger.LOGGER.warning("Fakeplayer cant get entity tracker for " + this.getId());
477+
LeavesLogger.LOGGER.warn("Fakeplayer cant get entity tracker for {}", this.getId());
479478
return;
480479
}
481480

leaves-server/src/main/java/org/leavesmc/leaves/bot/agent/actions/AbstractBotAction.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void tryTick(ServerBot bot) {
124124
e.providePlayer(bot);
125125
e.consume();
126126
} catch (Exception e) {
127-
LeavesLogger.LOGGER.severe("An error occurred while executing bot " + bot.displayName + ", action " + this.name, e);
127+
LeavesLogger.LOGGER.error("An error occurred while executing bot {}, action {}", bot.displayName, this.name, e);
128128
}
129129

130130
if (result) {

leaves-server/src/main/java/org/leavesmc/leaves/bytebuf/internal/InternalBytebufHandler.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import net.minecraft.server.MinecraftServer;
1515
import net.minecraft.server.level.ServerPlayer;
1616
import org.leavesmc.leaves.LeavesConfig;
17+
import org.leavesmc.leaves.LeavesLogger;
1718
import org.leavesmc.leaves.bytebuf.Bytebuf;
1819
import org.leavesmc.leaves.bytebuf.PacketAudience;
1920
import org.leavesmc.leaves.bytebuf.PacketFlow;
@@ -34,13 +35,26 @@ public class InternalBytebufHandler {
3435
private static final UniversalCodec CODEC = new UniversalCodec();
3536

3637
public static void init() {
38+
if (!LeavesConfig.mics.leavesPacketEvent) {
39+
return;
40+
}
3741
ChannelInitializeListenerHolder.addListener(Key.key("leaves:bytebuf"), channel ->
3842
channel.pipeline().addBefore("packet_handler", PacketHandler.handlerName, new PacketHandler(channel)));
3943
}
4044

4145
public static void updatePlayer(ServerPlayer player) {
42-
PacketHandler handler = (PacketHandler) player.connection.connection.channel.pipeline().get(PacketHandler.handlerName);
43-
handler.audienceHolder.setPlayer(player.getBukkitEntity());
46+
if (!LeavesConfig.mics.leavesPacketEvent) {
47+
return;
48+
}
49+
try {
50+
PacketHandler handler = (PacketHandler) player.connection.connection.channel.pipeline().get(PacketHandler.handlerName);
51+
if (handler == null) {
52+
return;
53+
}
54+
handler.audienceHolder.setPlayer(player.getBukkitEntity());
55+
} catch (Exception e) {
56+
LeavesLogger.LOGGER.warn("Failed to inject player for bytebuf API", e);
57+
}
4458
}
4559

4660
public static SimpleBytebufAllocator allocator() {
@@ -74,12 +88,16 @@ public static net.minecraft.network.protocol.Packet<?> callPacketEvent(PacketAud
7488

7589
public static void sendPacket(PacketAudience audience, PacketType type, Bytebuf bytebuf) {
7690
Channel channel = (Channel) audience.getChannel();
77-
Connection connection = (Connection) channel.pipeline().get("packet_handler");
91+
Connection connection = (Connection) channel.pipeline().get(PacketHandler.vanillaHandlerName);
92+
if (connection == null) {
93+
return;
94+
}
7895
connection.send(CODEC.decode(type, ((WrappedBytebuf) bytebuf).getRegistryBuf(), audience.getPlayer() == null));
7996
}
8097

8198
private static class PacketHandler extends ChannelDuplexHandler {
8299

100+
private final static String vanillaHandlerName = "packet_handler";
83101
private final static String handlerName = "leaves-bytebuf-handler";
84102

85103
private final AudienceHolder audienceHolder;

leaves-server/src/main/java/org/leavesmc/leaves/config/GlobalConfigManager.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ private static void initCategory(@NotNull Field categoryField, @NotNull GlobalCo
8383
}
8484
traverseToNodeOrCreate(categoryPath.substring(CONFIG_START.length()));
8585
} catch (Exception e) {
86-
LeavesLogger.LOGGER.severe("Failure to load leaves config" + upstreamPath, e);
86+
LeavesLogger.LOGGER.error("Failure to load leaves config {}", upstreamPath, e);
8787
}
8888
}
8989

@@ -112,13 +112,13 @@ private static void initConfig(@NotNull Field field, GlobalConfig globalConfig,
112112
field.set(upstreamField, savedValue);
113113
} catch (IllegalArgumentException | ClassCastException e) {
114114
LeavesConfig.config.set(path, defValue);
115-
LeavesLogger.LOGGER.warning(e.getMessage() + ", reset to " + defValue);
115+
LeavesLogger.LOGGER.warn("{}, reset to {}", e.getMessage(), defValue);
116116
}
117117

118118
verifiedConfigs.put(path.substring(CONFIG_START.length()), verifiedConfig);
119119
traverseToNodeOrCreate(path.substring(CONFIG_START.length()));
120120
} catch (Exception e) {
121-
LeavesLogger.LOGGER.severe("Failure to load leaves config", e);
121+
LeavesLogger.LOGGER.error("Failure to load leaves config", e);
122122
throw new RuntimeException();
123123
}
124124
}

leaves-server/src/main/java/org/leavesmc/leaves/config/VerifiedConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public Object get() {
3939
try {
4040
return field.get(upstreamField);
4141
} catch (IllegalAccessException e) {
42-
LeavesLogger.LOGGER.severe("Failure to get " + path + " value", e);
42+
LeavesLogger.LOGGER.error("Failure to get {} value", path, e);
4343
return "<VALUE ERROR>";
4444
}
4545
}
@@ -56,7 +56,7 @@ public String getString() {
5656
}
5757
}
5858
} catch (IllegalArgumentException e) {
59-
LeavesLogger.LOGGER.severe("Failure to get " + path + " value", e);
59+
LeavesLogger.LOGGER.error("Failure to get {} value", path, e);
6060
}
6161
return value.toString();
6262
}
@@ -70,7 +70,7 @@ public static VerifiedConfig build(@NotNull GlobalConfig config, @NotNull Field
7070
try {
7171
validator = createValidator(config.validator(), field);
7272
} catch (Exception e) {
73-
LeavesLogger.LOGGER.severe("Failure to load leaves config" + path, e);
73+
LeavesLogger.LOGGER.error("Failure to load leaves config {}", path, e);
7474
throw new RuntimeException();
7575
}
7676

leaves-server/src/main/java/org/leavesmc/leaves/config/VerifiedTransferConfig.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public void run() {
2323
field.set(upstreamField, savedValue);
2424
LeavesConfig.config.set(path, null);
2525
} catch (IllegalAccessException | IllegalArgumentException e) {
26-
LeavesLogger.LOGGER.warning("Failure to load leaves config" + path, e);
26+
LeavesLogger.LOGGER.warn("Failure to load leaves config{}", path, e);
2727
} catch (ConfigTransformer.StopTransformException ignored) {
2828
}
2929
} else {
30-
LeavesLogger.LOGGER.warning("Failed to convert saved value for " + path + ", reset to default");
30+
LeavesLogger.LOGGER.warn("Failed to convert saved value for {}, reset to default", path);
3131
}
3232
}
3333
}
@@ -40,7 +40,7 @@ public void run() {
4040
try {
4141
transformer = createTransformer(config.transformer(), field);
4242
} catch (Exception e) {
43-
LeavesLogger.LOGGER.warning("Failure to load leaves config transformer for " + path, e);
43+
LeavesLogger.LOGGER.warn("Failure to load leaves config transformer for {}", path, e);
4444
}
4545

4646
return new VerifiedTransferConfig(transformer, field, upstreamField, path);

leaves-server/src/main/java/org/leavesmc/leaves/network/AsyncKeepaliveManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
import net.minecraft.util.Util;
66
import org.leavesmc.leaves.LeavesConfig;
77
import org.leavesmc.leaves.LeavesLogger;
8+
import org.slf4j.Logger;
89

910
import java.util.Map;
1011
import java.util.concurrent.ConcurrentHashMap;
1112
import java.util.concurrent.Executors;
1213
import java.util.concurrent.ScheduledExecutorService;
1314
import java.util.concurrent.TimeUnit;
14-
import java.util.logging.Level;
1515

1616
public final class AsyncKeepaliveManager {
1717

18-
private static final LeavesLogger LOGGER = LeavesLogger.LOGGER;
18+
private static final Logger LOGGER = LeavesLogger.LOGGER;
1919
private static final Map<Connection, ServerCommonPacketListenerImpl> ACTIVE_LISTENERS = new ConcurrentHashMap<>();
2020
private static final ScheduledExecutorService EXECUTOR = Executors.newSingleThreadScheduledExecutor(runnable -> {
2121
Thread thread = new Thread(runnable, "Leaves Async Keepalive");
@@ -36,7 +36,6 @@ public static void register(ServerCommonPacketListenerImpl listener) {
3636
if (!LeavesConfig.mics.asyncKeepalive.enable) {
3737
return;
3838
}
39-
4039
ACTIVE_LISTENERS.put(listener.connection, listener);
4140
}
4241

@@ -56,7 +55,7 @@ private static void tickAll() {
5655
}
5756
} catch (Throwable throwable) {
5857
ACTIVE_LISTENERS.remove(listener.connection, listener);
59-
LOGGER.log(Level.SEVERE, "Failed to run async keepalive for connection " + listener.connection.getRemoteAddress(), throwable);
58+
LOGGER.error("Failed to run async keepalive for connection {}", listener.connection.getRemoteAddress(), throwable);
6059
}
6160
}
6261
}

0 commit comments

Comments
 (0)