Skip to content

Commit 0c01a11

Browse files
author
LewUwU
authored
Fixed protocol injection with latest netty on minecraft 1.11 and below (#1067)
1 parent 7ce3f47 commit 0c01a11

File tree

3 files changed

+97
-3
lines changed

3 files changed

+97
-3
lines changed

src/main/java/com/comphenix/protocol/ProtocolLib.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import com.comphenix.protocol.updater.Updater.UpdateType;
3939
import com.comphenix.protocol.utility.ChatExtensions;
4040
import com.comphenix.protocol.utility.ByteBuddyFactory;
41+
import com.comphenix.protocol.utility.NettyVersion;
4142
import com.comphenix.protocol.utility.MinecraftVersion;
4243
import com.google.common.base.Splitter;
4344
import com.google.common.collect.Iterables;
@@ -170,7 +171,11 @@ public void onLoad() {
170171
// Print the state of the debug mode
171172
if (config.isDebug()) {
172173
logger.warning("Debug mode is enabled!");
174+
logger.info("Detected netty version: " + NettyVersion.getVersion());
175+
} else {
176+
NettyVersion.getVersion(); // this will cache the version
173177
}
178+
174179
// And the state of the error reporter
175180
if (config.isDetailedErrorReporting()) {
176181
detailedReporter.setDetailedReporting(true);

src/main/java/com/comphenix/protocol/injector/netty/ProtocolInjector.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.comphenix.protocol.injector.server.TemporaryPlayerFactory;
4747
import com.comphenix.protocol.reflect.FuzzyReflection;
4848
import com.comphenix.protocol.reflect.VolatileField;
49+
import com.comphenix.protocol.utility.NettyVersion;
4950
import com.comphenix.protocol.utility.MinecraftReflection;
5051
import com.comphenix.protocol.utility.MinecraftVersion;
5152
import com.google.common.collect.Lists;
@@ -146,10 +147,15 @@ public synchronized void inject() {
146147
protected void initChannel(final Channel channel) throws Exception {
147148
try {
148149
synchronized (networkManagers) {
149-
// For some reason it needs to be delayed on 1.12, but the delay breaks 1.11 and below
150+
// For some reason it needs to be delayed when using netty 4.1.24 (minecraft 1.12) or newer,
151+
// but the delay breaks older minecraft versions
150152
// TODO I see this more as a temporary hotfix than a permanent solution
151-
if (MinecraftVersion.getCurrentVersion().getMinor() >= 12) {
152-
channel.eventLoop().submit(() ->
153+
154+
// Check if the netty version is greater than 4.1.24, that's the version bundled with spigot 1.12
155+
NettyVersion ver = NettyVersion.getVersion();
156+
if ((ver.isValid() && ver.isGreaterThan(4,1,24)) ||
157+
MinecraftVersion.getCurrentVersion().getMinor() >= 12) { // fallback if netty version couldn't be detected
158+
channel.eventLoop().submit(() ->
153159
injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject());
154160
} else {
155161
injectionFactory.fromChannel(channel, ProtocolInjector.this, playerFactory).inject();
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package com.comphenix.protocol.utility;
2+
3+
import com.comphenix.protocol.ProtocolLibrary;
4+
import io.netty.util.Version;
5+
6+
import java.util.Map;
7+
8+
public class NettyVersion {
9+
private final static String NETTY_ARTIFACT_ID = "netty-common";
10+
private static NettyVersion version;
11+
12+
public static NettyVersion getVersion() {
13+
if(version == null) {
14+
version = detectVersion();
15+
}
16+
return version;
17+
}
18+
19+
private static NettyVersion detectVersion() {
20+
Map<String, Version> nettyArtifacts = Version.identify();
21+
Version version = nettyArtifacts.get(NETTY_ARTIFACT_ID);
22+
if(version != null) {
23+
return new NettyVersion(version.artifactVersion());
24+
}
25+
return new NettyVersion(null);
26+
}
27+
28+
private boolean valid = false;
29+
private int major, minor, revision;
30+
31+
public NettyVersion(String s) {
32+
if(s == null) {
33+
return;
34+
}
35+
String[] split = s.split( "\\.");
36+
try {
37+
this.major = Integer.parseInt(split[0]);
38+
this.minor = Integer.parseInt(split[1]);
39+
this.revision = Integer.parseInt(split[2]);
40+
this.valid = true;
41+
} catch (Throwable t) {
42+
ProtocolLibrary.getPlugin().getLogger().warning("Could not detect netty version: '" + s + "'");
43+
}
44+
}
45+
46+
@Override
47+
public String toString() {
48+
if(!valid) {
49+
return "(invalid)";
50+
}
51+
return major + "." + minor + "." + revision;
52+
}
53+
54+
@Override
55+
public boolean equals(Object obj) {
56+
if(!(obj instanceof NettyVersion)) {
57+
return false;
58+
}
59+
NettyVersion v = (NettyVersion) obj;
60+
return v.major == major && v.minor == minor && v.revision == revision;
61+
}
62+
63+
public int getMajor() {
64+
return major;
65+
}
66+
67+
public int getMinor() {
68+
return minor;
69+
}
70+
71+
public int getRevision() {
72+
return revision;
73+
}
74+
75+
public boolean isValid() {
76+
return this.valid;
77+
}
78+
79+
public boolean isGreaterThan(int major, int minor, int rev) {
80+
return this.major > major || this.minor > minor || this.revision > rev;
81+
}
82+
83+
}

0 commit comments

Comments
 (0)