Skip to content

Commit faddde2

Browse files
committed
Use fallback NMS version if not supported
1 parent 91f2295 commit faddde2

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

bukkit/nms/src/main/kotlin/io/github/rothes/protocolstringreplacer/nms/NmsManager.kt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ import io.github.rothes.protocolstringreplacer.nms.packetreader.IBlockEntityType
44
import io.github.rothes.protocolstringreplacer.nms.packetreader.IDisguisedPacketHandler
55
import io.github.rothes.protocolstringreplacer.nms.packetreader.IMenuTypeGetter
66
import io.github.rothes.protocolstringreplacer.nms.packetreader.IPacketReader
7+
import java.util.function.Consumer
78

89
object NmsManager {
910

1011
private const val PACKAGE_PREFIX = "io.github.rothes.protocolstringreplacer.nms"
1112

1213
private lateinit var minecraftVersion: String
14+
lateinit var warningFunction: Consumer<String>
1315

1416
fun setVersion(major: Int, minor: Int) {
1517
var m = minor
@@ -22,6 +24,20 @@ object NmsManager {
2224
m++
2325
}
2426
}
27+
// On older versions we are not using NMS.
28+
if (major >= 19) {
29+
warningFunction.accept("Minecraft version 1.$major.$minor is not supported by the plugin version you are running on.")
30+
m = minor
31+
while (--m >= 0) {
32+
setVersionProp(major, m)
33+
try {
34+
create<IPacketReader>()
35+
warningFunction.accept("Fallback to 1.$major.$m support and luckily it could be compatible.")
36+
return
37+
} catch (ignored: ClassNotFoundException) {
38+
}
39+
}
40+
}
2541
setVersionProp(major, minor)
2642
}
2743

bukkit/src/main/java/io/github/rothes/protocolstringreplacer/ProtocolStringReplacer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
import io.github.rothes.protocolstringreplacer.upgrade.AbstractUpgradeHandler;
1919
import io.github.rothes.protocolstringreplacer.upgrade.UpgradeEnum;
2020
import io.github.rothes.protocolstringreplacer.util.FileUtils;
21+
import kotlin.Unit;
22+
import kotlin.jvm.functions.Function1;
2123
import org.apache.commons.lang.Validate;
2224
import org.bukkit.Bukkit;
2325
import org.bukkit.configuration.ConfigurationSection;
@@ -102,6 +104,7 @@ public ProtocolStringReplacer() {
102104
String[] split = Bukkit.getServer().getBukkitVersion().split("-")[0].split("\\.");
103105
serverMajorVersion = Byte.parseByte(split[1]);
104106
serverMinorVersion = split.length > 2 ? Byte.parseByte(split[2]) : 0;
107+
NmsManager.INSTANCE.setWarningFunction(ProtocolStringReplacer::warn);
105108
NmsManager.INSTANCE.setVersion(serverMajorVersion, serverMinorVersion);
106109

107110
// Start Console Replacer first to remove the Ansi in log files.

0 commit comments

Comments
 (0)