Skip to content

Commit 90e6af8

Browse files
committed
Use MojangMappedVersionHandler to detect when alternate variable names should be used by SafeVarHandle
1 parent d494b7a commit 90e6af8

File tree

10 files changed

+84
-19
lines changed

10 files changed

+84
-19
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
/**
44
* This file handles loading the correct platform implementation. The CommandAPIVersionHandler
55
* file within the commandapi-core module is NOT used at run time. Instead, the platform modules
6-
* replace this class with their own version that handles loads the correct class for their version
6+
* replace this class with their own version that handles loads the correct class for their version.
77
*/
88
public interface CommandAPIVersionHandler {
99

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.jorel.commandapi;
2+
3+
/**
4+
* This file handles loading the correct mappings information. The MojangMappedVersionHandler
5+
* file within the commandapi-core module may not be used at run time. Instead, if a platform module
6+
* wants to be mojang-mapped, they can replace this class with one where {@link #isMojangMapped()} returns {@code true}.
7+
*/
8+
public interface MojangMappedVersionHandler {
9+
static boolean isMojangMapped() {
10+
return false;
11+
}
12+
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
public class SafeVarHandle<Type, FieldType> {
1515

16-
public static boolean USING_MOJANG_MAPPINGS = false; // This should only be set to true in testing.
16+
public static boolean USING_MOJANG_MAPPINGS = MojangMappedVersionHandler.isMojangMapped();
1717

1818
private VarHandle handle;
1919

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkit.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,6 @@ public static InternalBukkitConfig getConfiguration() {
139139
@Override
140140
public void onLoad(CommandAPIConfig<?> config) {
141141
if(config instanceof CommandAPIBukkitConfig bukkitConfig) {
142-
// A little unconventional, but we really don't need to implement mojang mapping flags
143-
// all over the place, we want it to have as minimal interaction as possible so it can
144-
// be used by the test framework as a global static flag. Also, we want to set this
145-
// as early as possible in the CommandAPI's loading sequence!
146-
if (bukkitConfig.shouldUseMojangMappings) {
147-
SafeVarHandle.USING_MOJANG_MAPPINGS = true;
148-
}
149-
150142
CommandAPIBukkit.setInternalConfig(new InternalBukkitConfig(bukkitConfig));
151143
} else {
152144
CommandAPI.logError("CommandAPIBukkit was loaded with non-Bukkit config!");

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-core/src/main/java/dev/jorel/commandapi/CommandAPIBukkitConfig.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import io.papermc.paper.event.server.ServerResourcesReloadedEvent;
44
import org.bukkit.plugin.java.JavaPlugin;
55

6-
import java.util.regex.Pattern;
7-
86
/**
97
* A class that contains information needed to configure the CommandAPI on Bukkit-based servers.
108
*/
@@ -14,7 +12,6 @@ public class CommandAPIBukkitConfig extends CommandAPIConfig<CommandAPIBukkitCon
1412

1513
// Default configuration
1614
boolean shouldHookPaperReload = true;
17-
boolean shouldUseMojangMappings = false;
1815

1916
/**
2017
* Creates a new CommandAPIBukkitConfig object. Variables in this
@@ -47,9 +44,16 @@ public CommandAPIBukkitConfig shouldHookPaperReload(boolean hooked) {
4744
*
4845
* @param useMojangMappings whether the CommandAPI should use Mojang mappings for internal calls
4946
* @return this CommandAPIBukkitConfig
47+
* @deprecated Use the `commandapi-bukkit-shade-mojang-mapped` depdendency instead of `commandapi-bukkit-shade` if you want to use mojang mappings.
5048
*/
49+
@Deprecated(since = "9.5.0", forRemoval = true)
5150
public CommandAPIBukkitConfig useMojangMappings(boolean useMojangMappings) {
52-
this.shouldUseMojangMappings = useMojangMappings;
51+
// A little unconventional, but we really don't need to implement mojang mapping flags
52+
// all over the place, we want it to have as minimal interaction as possible so it can
53+
// be used by the test framework as a global static flag. Also, we want to set this
54+
// as early as possible in the CommandAPI's loading sequence, including before loading
55+
// an NMS class, which setup reflection based on `USING_MOJANG_MAPPINGS`.
56+
SafeVarHandle.USING_MOJANG_MAPPINGS = useMojangMappings;
5357
return this;
5458
}
5559

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
Copyright 2018, 2021 Jorel Ali (Skepter) - MIT License
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
-->
22+
23+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
24+
<parent>
25+
<artifactId>commandapi-bukkit</artifactId>
26+
<groupId>dev.jorel</groupId>
27+
<version>9.5.0-SNAPSHOT</version>
28+
</parent>
29+
<modelVersion>4.0.0</modelVersion>
30+
31+
<artifactId>commandapi-bukkit-mojang-mapped</artifactId>
32+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package dev.jorel.commandapi;
2+
3+
/**
4+
* This file handles loading the correct mappings information. The MojangMappedVersionHandler
5+
* file within the commandapi-core module may not be used at run time. Instead, if a platform module
6+
* wants to be mojang-mapped, they can replace it with this class where {@link #isMojangMapped()} returns {@code true}.
7+
*/
8+
public interface MojangMappedVersionHandler {
9+
static boolean isMojangMapped() {
10+
return true;
11+
}
12+
}

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-plugin-mojang-mapped/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@
6868
<artifactId>commandapi-bukkit-vh</artifactId>
6969
<version>${project.version}</version>
7070
</dependency>
71+
<dependency>
72+
<groupId>dev.jorel</groupId>
73+
<artifactId>commandapi-bukkit-mojang-mapped</artifactId>
74+
<version>${project.version}</version>
75+
</dependency>
7176
<dependency>
7277
<groupId>io.papermc.paper</groupId>
7378
<artifactId>paper-api</artifactId>
@@ -100,13 +105,14 @@
100105
<goal>shade</goal>
101106
</goals>
102107
<configuration>
103-
<!-- Remove commandapi-core's VersionHandler in favor of ours which
104-
loads CommandAPIBukkit for the current version -->
108+
<!-- Remove commandapi-core's VersionHandlers in favor of ours which
109+
loads CommandAPIBukkit for the current version and sets up mojang mappings -->
105110
<filters>
106111
<filter>
107112
<artifact>dev.jorel:commandapi-core</artifact>
108113
<excludes>
109114
<exclude>dev/jorel/commandapi/CommandAPIVersionHandler**</exclude>
115+
<exclude>dev/jorel/commandapi/MojangMappedVersionHandler**</exclude>
110116
</excludes>
111117
</filter>
112118
</filters>

commandapi-platforms/commandapi-bukkit/commandapi-bukkit-shade-mojang-mapped/pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@
5050
<artifactId>commandapi-bukkit-vh</artifactId>
5151
<version>${project.version}</version>
5252
</dependency>
53+
<dependency>
54+
<groupId>dev.jorel</groupId>
55+
<artifactId>commandapi-bukkit-mojang-mapped</artifactId>
56+
<version>${project.version}</version>
57+
</dependency>
5358
</dependencies>
5459

5560
<build>
@@ -67,13 +72,14 @@
6772
<configuration>
6873
<promoteTransitiveDependencies>true</promoteTransitiveDependencies>
6974
<createSourcesJar>true</createSourcesJar>
70-
<!-- Remove commandapi-core's VersionHandler in favor of ours which
71-
loads CommandAPIBukkit for the current version -->
75+
<!-- Remove commandapi-core's VersionHandlers in favor of ours which
76+
loads CommandAPIBukkit for the current version, and sets up mojang mappings -->
7277
<filters>
7378
<filter>
7479
<artifact>dev.jorel:commandapi-core</artifact>
7580
<excludes>
7681
<exclude>dev/jorel/commandapi/CommandAPIVersionHandler**</exclude>
82+
<exclude>dev/jorel/commandapi/MojangMappedVersionHandler**</exclude>
7783
</excludes>
7884
</filter>
7985
</filters>

commandapi-platforms/commandapi-bukkit/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
<!-- NMS versions -->
2121
<module>commandapi-bukkit-nms</module>
2222

23-
<!-- NMS selector -->
23+
<!-- Version selectors -->
2424
<module>commandapi-bukkit-vh</module>
25+
<module>commandapi-bukkit-mojang-mapped</module>
2526

2627
<!-- Outputs -->
2728
<module>commandapi-bukkit-plugin</module>

0 commit comments

Comments
 (0)