Skip to content

Commit ca5600a

Browse files
committed
Add a LoadContext to only display a warning when applicable
1 parent e6c80ac commit ca5600a

File tree

7 files changed

+38
-26
lines changed

7 files changed

+38
-26
lines changed

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPI.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ public static void onLoad(CommandAPIConfig<?> config) {
109109
CommandAPI.config = new InternalConfig(config);
110110

111111
// Initialize handlers
112-
CommandAPIPlatform<?, ?, ?> platform = CommandAPIVersionHandler.getPlatform();
112+
LoadContext loadContext = CommandAPIVersionHandler.getPlatform();
113+
CommandAPIPlatform<?, ?, ?> platform = loadContext.platform();
113114
new CommandAPIHandler<>(platform);
114-
115-
if (CommandAPI.getConfiguration().shouldUseLatestNMSVersion() || CommandAPI.getConfiguration().shouldBeLenientForMinorVersions()) {
116-
CommandAPI.logWarning("Loading the CommandAPI with a potentially incompatible NMS implementation.");
117-
CommandAPI.logWarning("While you may find success with this, further updates might be necessary to fully support the version you are using.");
118-
}
115+
loadContext.context().run();
119116

120117
// Log platform load
121118
final String platformClassHierarchy;

commandapi-core/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public interface CommandAPIVersionHandler {
1212
*
1313
* @return an instance of CommandAPIPlatform which can run on the currently active server
1414
*/
15-
static CommandAPIPlatform<?, ?, ?> getPlatform() {
15+
static LoadContext getPlatform() {
1616
throw new IllegalStateException("You have the wrong copy of the CommandAPI! If you're shading, did you use commandapi-core instead of commandapi-{platform}-shade?");
1717
}
1818
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package dev.jorel.commandapi;
2+
3+
public record LoadContext(CommandAPIPlatform<?, ?, ?> platform, Runnable context) {
4+
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-kotlin-test/src/test/kotlin/dev/jorel/commandapi/CommandAPIVersionHandler.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ interface CommandAPIVersionHandler {
1313
companion object {
1414

1515
@JvmStatic
16-
fun getPlatform() : CommandAPIPlatform<*, *, *> {
17-
return MockNMS(NMS_1_19_1_R1());
16+
fun getPlatform() : LoadContext {
17+
return LoadContext(MockNMS(NMS_1_19_1_R1())) {}
1818
}
1919

2020
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-test/commandapi-bukkit-test-tests/src/test/java/dev/jorel/commandapi/CommandAPIVersionHandler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ private static boolean isMojangMapped() {
4343
}
4444
}
4545

46-
static CommandAPIPlatform<?, ?, ?> getPlatform() {
46+
static LoadContext getPlatform() {
4747
if(profileId == null) {
4848
System.out.println("Using default version 1.19.4");
49-
return new MockNMS(new NMS_1_19_4_R3());
49+
return new LoadContext(new MockNMS(new NMS_1_19_4_R3()), () -> {});
5050
} else {
51-
return new MockNMS(switch(profileId) {
51+
return new LoadContext(new MockNMS(switch(profileId) {
5252
case "Minecraft_1_20_5" -> new NMS_1_20_R4();
5353
case "Minecraft_1_20_3" -> new NMS_1_20_R3();
5454
case "Minecraft_1_20_2" -> new NMS_1_20_R2();
@@ -59,7 +59,7 @@ private static boolean isMojangMapped() {
5959
case "Minecraft_1_17" -> new NMS_1_17();
6060
case "Minecraft_1_16_5" -> new NMS_1_16_R3();
6161
default -> throw new IllegalArgumentException("Unexpected value: " + System.getProperty("profileId"));
62-
});
62+
}), () -> {});
6363
}
6464
}
6565

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-vh/src/main/java/dev/jorel/commandapi/CommandAPIVersionHandler.java

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,16 @@ public interface CommandAPIVersionHandler {
4848
*
4949
* @return an instance of NMS which can run on the specified Minecraft version
5050
*/
51-
static CommandAPIPlatform<?, ?, ?> getPlatform() {
51+
static LoadContext getPlatform() {
5252
int latestMajorVersion = 21; // Change this for Minecraft's major update
5353
if (CommandAPI.getConfiguration().shouldUseLatestNMSVersion()) {
54-
return new NMS_1_21_R1();
54+
return new LoadContext(new NMS_1_21_R1(), () -> {
55+
CommandAPI.logWarning("Loading the CommandAPI with a potentially incompatible NMS implementation.");
56+
CommandAPI.logWarning("While you may find success with this, further updates might be necessary to fully support the version you are using.");
57+
});
5558
} else {
5659
String version = Bukkit.getBukkitVersion().split("-")[0];
57-
return switch (version) {
60+
CommandAPIPlatform<?, ?, ?> platform = switch (version) {
5861
case "1.16.5" -> new NMS_1_16_R3();
5962
case "1.17" -> new NMS_1_17();
6063
case "1.17.1" -> new NMS_1_17_R1();
@@ -69,15 +72,23 @@ public interface CommandAPIVersionHandler {
6972
case "1.20.3", "1.20.4" -> new NMS_1_20_R3();
7073
case "1.20.5", "1.20.6" -> new NMS_1_20_R4();
7174
case "1.21", "1.21.1" -> new NMS_1_21_R1();
72-
default -> {
73-
int currentMajorVersion = Integer.parseInt(version.split("\\.")[1]);
74-
if (CommandAPI.getConfiguration().shouldBeLenientForMinorVersions() && latestMajorVersion == currentMajorVersion) {
75-
yield new NMS_1_21_R1();
76-
} else {
77-
throw new UnsupportedVersionException(version);
78-
}
79-
}
75+
default -> null;
8076
};
77+
if (platform != null) {
78+
return new LoadContext(platform, () -> {});
79+
}
80+
if (CommandAPI.getConfiguration().shouldBeLenientForMinorVersions()) {
81+
int currentMajorVersion = Integer.parseInt(version.split("\\.")[1]);
82+
if (latestMajorVersion == currentMajorVersion) {
83+
return new LoadContext(new NMS_1_21_R1(), () -> {
84+
CommandAPI.logWarning("Loading the CommandAPI with a potentially incompatible NMS implementation.");
85+
CommandAPI.logWarning("While you may find success with this, further updates might be necessary to fully support the version you are using.");
86+
});
87+
} else {
88+
throw new UnsupportedVersionException(version);
89+
}
90+
}
91+
throw new UnsupportedVersionException(version);
8192
}
8293
}
8394

Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package dev.jorel.commandapi;
22

33
public interface CommandAPIVersionHandler {
4-
static CommandAPIPlatform<?, ?, ?> getPlatform() {
5-
return new CommandAPIVelocity();
4+
static LoadContext getPlatform() {
5+
return new LoadContext(new CommandAPIVelocity(), () -> {});
66
}
77
}

0 commit comments

Comments
 (0)