Skip to content

Commit b13b696

Browse files
committed
Address code reviews
1 parent 3dcea3e commit b13b696

File tree

10 files changed

+43
-65
lines changed

10 files changed

+43
-65
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ This is the current roadmap for the CommandAPI (as of 30th April 2024):
414414
<td valign="top">???</td>
415415
<td valign="top">
416416
<ul>
417-
<li>https://github.com/JorelAli/CommandAPI/pull/594 Adds a config option to allow the CommandAPI to be more lenient when updating to a new minor version (e. g. from 1.21 to 1.21.1)</li>
417+
<li>https://github.com/JorelAli/CommandAPI/pull/594 Adds a config option to allow the CommandAPI to be more lenient when updating to a new minor version (e.g. from 1.21 to 1.21.1)</li>
418418
</ul>
419419
</td>
420420
</tr>

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ public static void onLoad(CommandAPIConfig<?> config) {
112112
CommandAPIPlatform<?, ?, ?> platform = CommandAPIVersionHandler.getPlatform();
113113
new CommandAPIHandler<>(platform);
114114

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+
}
119+
115120
// Log platform load
116121
final String platformClassHierarchy;
117122
{

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public abstract class CommandAPIConfig<Impl
3939
boolean verboseOutput = false;
4040
boolean silentLogs = false;
4141
boolean useLatestNMSVersion = false;
42-
boolean lenientForMinorVersions = false;
42+
boolean beLenientForMinorVersions = false;
4343
String missingExecutorImplementationMessage = "This command has no implementations for %s";
4444

4545
File dispatcherFile = null;
@@ -93,7 +93,7 @@ public Impl useLatestNMSVersion(boolean value) {
9393

9494
/**
9595
* Sets whether the CommandAPI should load a (potentially unsupported) NMS version
96-
* when updating to a minor release of Minecraft, for example this setting would
96+
* when updating to a minor release of Minecraft. For example, this setting would
9797
* allow running a minor version of the game before the CommandAPI makes a proper
9898
* release for it. Unlike {@link #useLatestNMSVersion(boolean)}, this setting does
9999
* not blindly load the latest NMS version and as a result break cross version compatibility
@@ -102,8 +102,8 @@ public Impl useLatestNMSVersion(boolean value) {
102102
* @param value whether the CommandAPI should assume that minor Minecraft releases do not cause incompatibilities
103103
* @return this CommandAPIConfig
104104
*/
105-
public Impl lenientForMinorVersions(boolean value) {
106-
this.lenientForMinorVersions = value;
105+
public Impl beLenientForMinorVersions(boolean value) {
106+
this.beLenientForMinorVersions = value;
107107
return instance();
108108
}
109109

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public class InternalConfig {
4040
// Whether we should use the latest NMS version (which may not be compatible)
4141
private final boolean useLatestNMSVersion;
4242

43-
private final boolean beMoreLenientForMinorVerions;
43+
private final boolean beLenientForMinorVersions;
4444

4545
// The message to display when an executor implementation is missing
4646
private final String messageMissingExecutorImplementation;
@@ -67,7 +67,7 @@ public InternalConfig(CommandAPIConfig<?> config) {
6767
this.verboseOutput = config.verboseOutput;
6868
this.silentLogs = config.silentLogs;
6969
this.useLatestNMSVersion = config.useLatestNMSVersion;
70-
this.beMoreLenientForMinorVerions = config.lenientForMinorVersions;
70+
this.beLenientForMinorVersions = config.beLenientForMinorVersions;
7171
this.messageMissingExecutorImplementation = config.missingExecutorImplementationMessage;
7272
this.dispatcherFile = config.dispatcherFile;
7373
this.skipSenderProxy = config.skipSenderProxy;
@@ -100,8 +100,8 @@ public boolean shouldUseLatestNMSVersion() {
100100
/**
101101
* @return Whether the CommandAPI should assume that minor versions of officially unsupported versions do not cause incompatibilities
102102
*/
103-
public boolean shouldBeMoreLenientForMinorVersions() {
104-
return this.beMoreLenientForMinorVerions;
103+
public boolean shouldBeLenientForMinorVersions() {
104+
return this.beLenientForMinorVersions;
105105
}
106106

107107
/**

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/java/dev/jorel/commandapi/CommandAPIMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onLoad() {
5252
.dispatcherFile(fileConfig.getBoolean("create-dispatcher-json") ? new File(getDataFolder(), "command_registration.json") : null)
5353
.shouldHookPaperReload(fileConfig.getBoolean("hook-paper-reload"))
5454
.skipReloadDatapacks(fileConfig.getBoolean("skip-initial-datapack-reload"))
55-
.lenientForMinorVersions(fileConfig.getBoolean("lenient-for-minor-versions"));
55+
.beLenientForMinorVersions(fileConfig.getBoolean("lenient-for-minor-versions"));
5656

5757
for (String pluginName : fileConfig.getStringList("skip-sender-proxy")) {
5858
if (Bukkit.getPluginManager().getPlugin(pluginName) != null) {

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/src/main/resources/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use-latest-nms-version: false
4444
# Be lenient with version checks when loading for new minor Minecraft versions (default: false)
4545
# If "true", the CommandAPI loads NMS implementations for (potentially unsupported) Minecraft versions.
4646
# For example, this setting may allow updating from 1.21.1 to 1.21.2 as only the minor version is changing
47-
# but will not allow an update from 1.21.2 to 1.22
48-
# Keep in mind that implementations my vary and actually updating the CommandAPI might be necessary.
47+
# but will not allow an update from 1.21.2 to 1.22.
48+
# Keep in mind that implementations may vary and actually updating the CommandAPI might be necessary.
4949
lenient-for-minor-versions: false
5050

5151
# Hook into Paper's ServerResourcesReloadedEvent (default: true)

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/java/dev/jorel/commandapi/CommandAPIMain.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void onLoad() {
5252
.dispatcherFile(fileConfig.getBoolean("create-dispatcher-json") ? new File(getDataFolder(), "command_registration.json") : null)
5353
.shouldHookPaperReload(fileConfig.getBoolean("hook-paper-reload"))
5454
.skipReloadDatapacks(fileConfig.getBoolean("skip-initial-datapack-reload"))
55-
.lenientForMinorVersions(fileConfig.getBoolean("lenient-for-minor-versions"));
55+
.beLenientForMinorVersions(fileConfig.getBoolean("lenient-for-minor-versions"));
5656

5757
for (String pluginName : fileConfig.getStringList("skip-sender-proxy")) {
5858
if (Bukkit.getPluginManager().getPlugin(pluginName) != null) {

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin/src/main/resources/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ use-latest-nms-version: false
4444
# Be lenient with version checks when loading for new minor Minecraft versions (default: false)
4545
# If "true", the CommandAPI loads NMS implementations for (potentially unsupported) Minecraft versions.
4646
# For example, this setting may allow updating from 1.21.1 to 1.21.2 as only the minor version is changing
47-
# but will not allow an update from 1.21.2 to 1.22
48-
# Keep in mind that implementations my vary and actually updating the CommandAPI might be necessary.
47+
# but will not allow an update from 1.21.2 to 1.22.
48+
# Keep in mind that implementations may vary and actually updating the CommandAPI might be necessary.
4949
lenient-for-minor-versions: false
5050

5151
# Hook into Paper's ServerResourcesReloadedEvent (default: true)

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

Lines changed: 11 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,12 @@ public interface CommandAPIVersionHandler {
4949
* @return an instance of NMS which can run on the specified Minecraft version
5050
*/
5151
static CommandAPIPlatform<?, ?, ?> getPlatform() {
52-
CommandAPIPlatform<?, ?, ?> latestNMS = new NMS_1_21_R1();
52+
int latestMajorVersion = 21; // Change this for Minecraft's major update
5353
if (CommandAPI.getConfiguration().shouldUseLatestNMSVersion()) {
54-
return latestNMS;
54+
return new NMS_1_21_R1();
5555
} else {
5656
String version = Bukkit.getBukkitVersion().split("-")[0];
57-
Version minecraftVersion = new Version(version);
58-
CommandAPIPlatform<?, ?, ?> platform = switch (version) {
57+
return switch (version) {
5958
case "1.16.5" -> new NMS_1_16_R3();
6059
case "1.17" -> new NMS_1_17();
6160
case "1.17.1" -> new NMS_1_17_R1();
@@ -70,54 +69,16 @@ public interface CommandAPIVersionHandler {
7069
case "1.20.3", "1.20.4" -> new NMS_1_20_R3();
7170
case "1.20.5", "1.20.6" -> new NMS_1_20_R4();
7271
case "1.21", "1.21.1" -> new NMS_1_21_R1();
73-
default -> null;
74-
};
75-
if (platform != null) {
76-
return platform;
77-
} else {
78-
if (CommandAPI.getConfiguration().shouldBeMoreLenientForMinorVersions()) {
79-
return minecraftVersion.matchesPatch((NMS<?>) latestNMS);
80-
}
81-
throw new UnsupportedVersionException(version);
82-
}
83-
}
84-
}
85-
86-
class Version {
87-
88-
private final String version;
89-
90-
// The minor version is something like 1.x
91-
private final int minor;
92-
93-
private Version(String version) {
94-
this.version = version;
95-
String[] versionParts = version.split("\\.");
96-
minor = Integer.parseInt(versionParts[1]);
97-
}
98-
99-
private CommandAPIPlatform<?, ?, ?> matchesPatch(NMS<?> latestNMS) {
100-
if (minor <= 16) {
101-
// We absolutely do not support versions older than 1.16.5
102-
// As we match 1.16.5 in the getPlatform() method, this if branch means that
103-
// the server is on 1.16.4 or older
104-
throw new UnsupportedVersionException(version);
105-
} else {
106-
// Any other Minecraft that is older or exactly the last version we match in getPlatform()
107-
// does not appear here. Furthermore, we do not want to be lenient with 1.x versions so we
108-
// can simply return the latest NMS version here if its supported Minecraft version's minor version
109-
// is the same as the current Minecraft version
110-
String[] supportedNMSVersions = latestNMS.compatibleVersions();
111-
int minorVersion = Integer.parseInt(supportedNMSVersions[0].split("\\.")[1]); // Index 1 returns the minor version by SemVer's rules
112-
if (this.minor == minorVersion) {
113-
return (CommandAPIPlatform<?, ?, ?>) latestNMS;
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+
}
11479
}
115-
}
116-
// If we end up here, that means the server's current minor version in SemVer terms is newer than the
117-
// latest minor version of Minecraft in SemVer terms that this method allows. We do not support that.
118-
throw new UnsupportedVersionException(version);
80+
};
11981
}
120-
12182
}
12283

12384
}

docssrc/src/config.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ Take the warning from the [`use-latest-nms-version`](#use-latest-nms-version) an
132132

133133
</div>
134134

135+
**Default value**
136+
137+
```yml
138+
lenient-for-minor-versions: false
139+
```
140+
141+
**Example value**
142+
143+
```yml
144+
lenient-for-minor-versions: true
145+
```
146+
135147
-----
136148

137149
### `hook-paper-reload`

0 commit comments

Comments
 (0)