Skip to content

Commit e76a588

Browse files
committed
feat: 1.21.11 mojang mapping support
1 parent 7e7c26f commit e76a588

File tree

6 files changed

+147
-13
lines changed

6 files changed

+147
-13
lines changed

pom.xml

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
<plugin.compile.version>3.10.1</plugin.compile.version>
2828
<plugin.shade.version>3.4.0</plugin.shade.version>
29+
<plugin.jar.version>3.4.2</plugin.jar.version>
2930
<plugin.flatten.version>1.2.7</plugin.flatten.version>
3031
<plugin.specialsource.version>2.0.2</plugin.specialsource.version>
3132

@@ -51,14 +52,6 @@
5152
</resource>
5253
</resources>
5354
<plugins>
54-
<plugin>
55-
<groupId>org.apache.maven.plugins</groupId>
56-
<artifactId>maven-compiler-plugin</artifactId>
57-
<version>${plugin.compile.version}</version>
58-
<configuration>
59-
<release>17</release>
60-
</configuration>
61-
</plugin>
6255
<plugin>
6356
<groupId>org.apache.maven.plugins</groupId>
6457
<artifactId>maven-shade-plugin</artifactId>
@@ -90,6 +83,26 @@
9083
</filters>
9184
</configuration>
9285
</plugin>
86+
<plugin>
87+
<groupId>org.apache.maven.plugins</groupId>
88+
<artifactId>maven-jar-plugin</artifactId>
89+
<version>${plugin.jar.version}</version>
90+
<configuration>
91+
<archive>
92+
<manifestEntries>
93+
<paperweight-mappings-namespace>mojang</paperweight-mappings-namespace>
94+
</manifestEntries>
95+
</archive>
96+
</configuration>
97+
</plugin>
98+
<plugin>
99+
<groupId>org.apache.maven.plugins</groupId>
100+
<artifactId>maven-compiler-plugin</artifactId>
101+
<version>${plugin.compile.version}</version>
102+
<configuration>
103+
<release>17</release>
104+
</configuration>
105+
</plugin>
93106
<plugin>
94107
<groupId>org.codehaus.mojo</groupId>
95108
<artifactId>flatten-maven-plugin</artifactId>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @author Imprex-Development
3+
* @see <a href="https://github.com/Imprex-Development/orebfuscator/blob/master/orebfuscator-nms/orebfuscator-nms-api/src/main/java/net/imprex/orebfuscator/util/ServerVersion.java">ServerVersion.java</a>
4+
*/
5+
package net.imprex.zip.common;
6+
7+
public class ServerVersion {
8+
9+
private static final boolean IS_MOJANG_MAPPED = classExists("net.minecraft.core.BlockPos")
10+
&& fieldExists("net.minecraft.world.level.block.Blocks", "AIR");
11+
private static final boolean IS_FOLIA = classExists("io.papermc.paper.threadedregions.RegionizedServer");
12+
private static final boolean IS_PAPER = !IS_FOLIA && classExists("com.destroystokyo.paper.PaperConfig");
13+
private static final boolean IS_BUKKIT = !IS_FOLIA && !IS_PAPER;
14+
15+
private static boolean classExists(String className) {
16+
try {
17+
Class.forName(className);
18+
return true;
19+
} catch (ClassNotFoundException e) {
20+
return false;
21+
}
22+
}
23+
24+
private static boolean fieldExists(String className, String fieldName) {
25+
try {
26+
Class<?> target = Class.forName(className);
27+
return target.getDeclaredField(fieldName) != null;
28+
} catch (Exception e) {
29+
return false;
30+
}
31+
}
32+
33+
public static boolean isMojangMapped() {
34+
return IS_MOJANG_MAPPED;
35+
}
36+
37+
public static boolean isFolia() {
38+
return IS_FOLIA;
39+
}
40+
41+
public static boolean isPaper() {
42+
return IS_PAPER;
43+
}
44+
45+
public static boolean isBukkit() {
46+
return IS_BUKKIT;
47+
}
48+
}

zip-nms/zip-nms-v1_21_R7/pom.xml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
2-
<modelVersion>4.0.0</modelVersion>
2+
<modelVersion>4.0.0</modelVersion>
33

44
<parent>
55
<groupId>net.imprex</groupId>
@@ -27,6 +27,20 @@
2727

2828
<build>
2929
<plugins>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-shade-plugin</artifactId>
33+
<configuration>
34+
<shadedArtifactAttached>true</shadedArtifactAttached>
35+
<shadedClassifierName>mojang-mapped</shadedClassifierName>
36+
<relocations>
37+
<relocation>
38+
<pattern>net.imprex.zip.nms.v1_21_R7</pattern>
39+
<shadedPattern>net.imprex.zip.nms.v1_21_R7_mojang</shadedPattern>
40+
</relocation>
41+
</relocations>
42+
</configuration>
43+
</plugin>
3044
<plugin>
3145
<groupId>net.md-5</groupId>
3246
<artifactId>specialsource-maven-plugin</artifactId>

zip-nms/zip-nms-v1_21_R7/src/main/java/net/imprex/zip/nms/v1_21_R7/ZipNmsManager.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import java.util.UUID;
1111
import java.util.function.BiConsumer;
1212

13+
import org.bukkit.Bukkit;
1314
import org.bukkit.Material;
14-
import org.bukkit.craftbukkit.v1_21_R7.inventory.CraftItemStack;
1515
import org.bukkit.inventory.ItemStack;
1616
import org.bukkit.inventory.meta.SkullMeta;
1717

@@ -57,6 +57,9 @@ public class ZipNmsManager implements NmsManager {
5757

5858
private static final BiConsumer<SkullMeta, GameProfile> SET_PROFILE;
5959

60+
private static final Method CRAFT_ITEM_STACK_AS_NMS_COPY;
61+
private static final Method CRAFT_ITEM_STACK_AS_CRAFT_MIRROR;
62+
6063
static {
6164
BiConsumer<SkullMeta, GameProfile> setProfile = (meta, profile) -> {
6265
throw new NullPointerException("Unable to find 'setProfile' method!");
@@ -89,6 +92,49 @@ public class ZipNmsManager implements NmsManager {
8992
}
9093

9194
SET_PROFILE = setProfile;
95+
96+
String craftItemStackClass = Bukkit.getServer().getClass().getPackageName() + ".inventory.CraftItemStack";
97+
// CraftItemStack.asNMSCopy(item)
98+
try {
99+
Class<?> craftItemStack = Class.forName(craftItemStackClass);
100+
Method method = ReflectionUtil.searchMethod(
101+
craftItemStack,
102+
net.minecraft.world.item.ItemStack.class,
103+
ItemStack.class);
104+
method.setAccessible(true);
105+
CRAFT_ITEM_STACK_AS_NMS_COPY = method;
106+
} catch (ClassNotFoundException e) {
107+
throw new IllegalStateException(e);
108+
}
109+
110+
// CraftItemStack.asCraftMirror(item)
111+
try {
112+
Class<?> craftItemStack = Class.forName(craftItemStackClass);
113+
Method method = ReflectionUtil.searchMethod(
114+
craftItemStack,
115+
craftItemStack,
116+
net.minecraft.world.item.ItemStack.class);
117+
method.setAccessible(true);
118+
CRAFT_ITEM_STACK_AS_CRAFT_MIRROR = method;
119+
} catch (ClassNotFoundException e) {
120+
throw new IllegalStateException(e);
121+
}
122+
}
123+
124+
private static net.minecraft.world.item.ItemStack asNmsCopy(ItemStack item) {
125+
try {
126+
return (net.minecraft.world.item.ItemStack) CRAFT_ITEM_STACK_AS_NMS_COPY.invoke(null, item);
127+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
128+
throw new IllegalStateException(e);
129+
}
130+
}
131+
132+
private static ItemStack asCraftMirror(net.minecraft.world.item.ItemStack item) {
133+
try {
134+
return (ItemStack) CRAFT_ITEM_STACK_AS_CRAFT_MIRROR.invoke(null, item);
135+
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
136+
throw new IllegalStateException(e);
137+
}
92138
}
93139

94140
@Override
@@ -99,7 +145,7 @@ public JsonObject itemstackToJsonElement(ItemStack[] items) {
99145
if (item == null || item.getType() == Material.AIR) {
100146
continue;
101147
}
102-
net.minecraft.world.item.ItemStack minecraftItem = CraftItemStack.asNMSCopy(item);
148+
net.minecraft.world.item.ItemStack minecraftItem = asNmsCopy(item);
103149

104150
DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
105151
JsonObject resultJson = result.getOrThrow().getAsJsonObject();
@@ -138,7 +184,7 @@ public ItemStackContainerResult jsonElementToItemStack(JsonObject json) {
138184
.parse(DYNAMIC_OPS_JSON, dynamicItemFixed.getValue())
139185
.getOrThrow();
140186

141-
ItemStack bukkitItem = CraftItemStack.asCraftMirror(minecraftItem);
187+
ItemStack bukkitItem = asCraftMirror(minecraftItem);
142188
int slot = item.getAsJsonObject().get(BPConstants.KEY_INVENTORY_SLOT).getAsInt();
143189

144190
items.add(new ItemStackWithSlot(slot, bukkitItem));

zip-plugin/pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,5 +131,12 @@
131131
<version>${revision}</version>
132132
<scope>compile</scope>
133133
</dependency>
134+
<dependency>
135+
<groupId>net.imprex</groupId>
136+
<artifactId>zip-nms-v1_21_R7</artifactId>
137+
<version>${revision}</version>
138+
<classifier>mojang-mapped</classifier>
139+
<scope>compile</scope>
140+
</dependency>
134141
</dependencies>
135142
</project>

zip-plugin/src/main/java/net/imprex/zip/NmsInstance.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,26 @@
1010
import com.google.gson.JsonObject;
1111

1212
import net.imprex.zip.common.MinecraftVersion;
13+
import net.imprex.zip.common.ServerVersion;
1314
import net.imprex.zip.common.ZIPLogger;
1415
import net.imprex.zip.nms.api.ItemStackContainerResult;
1516
import net.imprex.zip.nms.api.NmsManager;
1617

1718
public class NmsInstance {
18-
19+
1920
private static NmsManager instance;
2021

2122
public static void initialize() {
23+
2224
if (NmsInstance.instance != null) {
2325
throw new IllegalStateException("NMS adapter is already initialized!");
2426
}
2527

2628
String nmsVersion = MinecraftVersion.nmsVersion();
29+
if (ServerVersion.isMojangMapped()) {
30+
nmsVersion += "_mojang";
31+
}
32+
2733
ZIPLogger.info("Searching NMS adapter for server version \"" + nmsVersion + "\"!");
2834

2935
try {

0 commit comments

Comments
 (0)