Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ checkVersion "1.21" "21"
checkVersion "1.21.3" "21"
checkVersion "1.21.4" "21"
checkVersion "1.21.5" "21"
checkVersion "1.21.6" "21"
checkVersion "1.21.6" "21"
checkVersion "1.21.9" "21"
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private static final class NmsMapping {
private static final List<NmsMapping> MAPPINGS = new ArrayList<>();

static {
MAPPINGS.add(new NmsMapping("1.21.9", "v1_21_R6"));
MAPPINGS.add(new NmsMapping("1.21.6", "v1_21_R5"));
MAPPINGS.add(new NmsMapping("1.21.5", "v1_21_R4"));
MAPPINGS.add(new NmsMapping("1.21.4", "v1_21_R3"));
Expand Down
1 change: 1 addition & 0 deletions zip-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@
<module>zip-nms-v1_21_R3</module>
<module>zip-nms-v1_21_R4</module>
<module>zip-nms-v1_21_R5</module>
<module>zip-nms-v1_21_R6</module>
</modules>
</project>
70 changes: 70 additions & 0 deletions zip-nms/zip-nms-v1_21_R6/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<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">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.imprex</groupId>
<artifactId>zip-nms</artifactId>
<version>${revision}</version>
</parent>

<artifactId>zip-nms-v1_21_R6</artifactId>

<dependencies>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>zip-nms-api</artifactId>
<version>${revision}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.21.9-R0.1-SNAPSHOT</version>
<classifier>remapped-mojang</classifier>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>net.md-5</groupId>
<artifactId>specialsource-maven-plugin</artifactId>
<version>${plugin.specialsource.version}</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>remap</goal>
</goals>
<id>remap-obf</id>
<configuration>
<srgIn>
org.spigotmc:minecraft-server:1.21.9-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.21.9-R0.1-SNAPSHOT:jar:remapped-mojang</remappedDependencies>
<remappedArtifactAttached>true</remappedArtifactAttached>
<remappedClassifierName>remapped-obf</remappedClassifierName>
</configuration>
</execution>
<execution>
<phase>package</phase>
<goals>
<goal>remap</goal>
</goals>
<id>remap-spigot</id>
<configuration>
<inputFile>
${project.build.directory}/${project.artifactId}-${project.version}-remapped-obf.jar</inputFile>
<srgIn>
org.spigotmc:minecraft-server:1.21.9-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.21.9-R0.1-SNAPSHOT:jar:remapped-obf</remappedDependencies>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
package net.imprex.zip.nms.v1_21_R6;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.function.BiConsumer;

import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_21_R6.inventory.CraftItemStack;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.SkullMeta;

import com.google.common.collect.Multimaps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;
import com.mojang.authlib.properties.PropertyMap;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.Dynamic;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.JsonOps;

import net.imprex.zip.common.BPConstants;
import net.imprex.zip.common.ReflectionUtil;
import net.imprex.zip.nms.api.ItemStackContainerResult;
import net.imprex.zip.nms.api.ItemStackWithSlot;
import net.imprex.zip.nms.api.NmsManager;
import net.minecraft.SharedConstants;
import net.minecraft.core.RegistryAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.NbtAccounter;
import net.minecraft.nbt.NbtIo;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.datafix.DataFixers;
import net.minecraft.util.datafix.fixes.References;
import net.minecraft.world.item.component.ResolvableProfile;

public class ZipNmsManager implements NmsManager {

private static final int DATA_VERSION = SharedConstants.getCurrentVersion().dataVersion().version();

@SuppressWarnings("deprecation")
private static final RegistryAccess DEFAULT_REGISTRY = MinecraftServer.getServer().registryAccess();

private static final DynamicOps<Tag> DYNAMIC_OPS_NBT = DEFAULT_REGISTRY.createSerializationContext(NbtOps.INSTANCE);
private static final DynamicOps<JsonElement> DYNAMIC_OPS_JSON = DEFAULT_REGISTRY.createSerializationContext(JsonOps.INSTANCE);

private static final BiConsumer<SkullMeta, GameProfile> SET_PROFILE;

static {
BiConsumer<SkullMeta, GameProfile> setProfile = (meta, profile) -> {
throw new NullPointerException("Unable to find 'setProfile' method!");
};

Class<?> craftMetaSkullClass = new ItemStack(Material.PLAYER_HEAD)
.getItemMeta()
.getClass();

Method setResolvableProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, ResolvableProfile.class);
if (setResolvableProfileMethod != null) {
setProfile = (meta, profile) -> {
try {
setResolvableProfileMethod.invoke(meta, ResolvableProfile.createResolved(profile));
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
};
} else {
Method setProfileMethod = ReflectionUtil.searchMethod(craftMetaSkullClass, void.class, GameProfile.class);
if (setProfileMethod != null) {
setProfile = (meta, profile) -> {
try {
setProfileMethod.invoke(meta, profile);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
};
}
}

SET_PROFILE = setProfile;
}

@Override
public JsonObject itemstackToJsonElement(ItemStack[] items) {
JsonArray jsonItems = new JsonArray();
for (int slot = 0; slot < items.length; slot++) {
ItemStack item = items[slot];
if (item == null || item.getType() == Material.AIR) {
continue;
}
net.minecraft.world.item.ItemStack minecraftItem = CraftItemStack.asNMSCopy(item);

DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
JsonObject resultJson = result.getOrThrow().getAsJsonObject();

resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, slot);
jsonItems.add(resultJson);
}

JsonObject outputJson = new JsonObject();
outputJson.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
outputJson.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
outputJson.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, items.length);
outputJson.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
return outputJson;
}

@Override
public ItemStackContainerResult jsonElementToItemStack(JsonObject json) {
// check if current version the same
if (json.get(BPConstants.KEY_INVENTORY_VERSION).getAsInt() != BPConstants.INVENTORY_VERSION) {
throw new IllegalStateException("Unable to convert binary to itemstack because zip version is missmatching");
}

int dataVersion = json.get(BPConstants.KEY_INVENTORY_DATA_VERSION).getAsInt();
int itemsSize = json.get(BPConstants.KEY_INVENTORY_ITEMS_SIZE).getAsInt();

List<ItemStackWithSlot> items = new ArrayList<>();

JsonArray jsonItems = json.get(BPConstants.KEY_INVENTORY_ITEMS).getAsJsonArray();
for (JsonElement item : jsonItems) {
Dynamic<JsonElement> dynamicItem = new Dynamic<>(JsonOps.INSTANCE, item);
Dynamic<JsonElement> dynamicItemFixed = DataFixers.getDataFixer()
.update(References.ITEM_STACK, dynamicItem, dataVersion, DATA_VERSION);

net.minecraft.world.item.ItemStack minecraftItem = net.minecraft.world.item.ItemStack.CODEC
.parse(DYNAMIC_OPS_JSON, dynamicItemFixed.getValue())
.getOrThrow();

ItemStack bukkitItem = CraftItemStack.asCraftMirror(minecraftItem);
int slot = item.getAsJsonObject().get(BPConstants.KEY_INVENTORY_SLOT).getAsInt();

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

return new ItemStackContainerResult(itemsSize, items);
}

@Override
public JsonObject migrateToJsonElement(byte[] binary) {
CompoundTag compound;
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(binary)) {
compound = NbtIo.readCompressed(inputStream, NbtAccounter.unlimitedHeap());
} catch (IOException e) {
throw new IllegalStateException("Unable to parse binary to nbt", e);
}

ListTag list = compound.getListOrEmpty("i");

int currentSlot = 0;

JsonArray jsonItems = new JsonArray();
for (Tag base : list) {
if (base instanceof CompoundTag itemTag) {
String itemType = itemTag.getString("id").orElse("");
if (itemType.equals("minecraft:air")) {
currentSlot++;
continue;
}

Dynamic<Tag> dynamicItem = new Dynamic<>(NbtOps.INSTANCE, itemTag);
net.minecraft.world.item.ItemStack minecraftItem = net.minecraft.world.item.ItemStack.CODEC
.parse(DYNAMIC_OPS_NBT, dynamicItem.getValue())
.getOrThrow();

DataResult<JsonElement> result = net.minecraft.world.item.ItemStack.CODEC.encodeStart(DYNAMIC_OPS_JSON, minecraftItem);
JsonObject resultJson = result.getOrThrow().getAsJsonObject();

resultJson.addProperty(BPConstants.KEY_INVENTORY_SLOT, currentSlot);
jsonItems.add(resultJson);

currentSlot++;
}
}

JsonObject json = new JsonObject();
json.addProperty(BPConstants.KEY_INVENTORY_VERSION, BPConstants.INVENTORY_VERSION);
json.addProperty(BPConstants.KEY_INVENTORY_DATA_VERSION, DATA_VERSION);
json.addProperty(BPConstants.KEY_INVENTORY_ITEMS_SIZE, list.size());
json.add(BPConstants.KEY_INVENTORY_ITEMS, jsonItems);
return json;
}

@Override
public void setSkullProfile(SkullMeta meta, String texture) {
try {
HashMap<String, Property> properties = new HashMap<>();
properties.put("textures", new Property("textures", texture));

PropertyMap propertyMap = new PropertyMap(Multimaps.forMap(properties));
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "", propertyMap);

SET_PROFILE.accept(meta, gameProfile);
} catch (Exception e) {
e.printStackTrace();
}
}

@Override
public boolean isAir(Material material) {
return material == null || material == Material.AIR;
}
}
6 changes: 6 additions & 0 deletions zip-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,11 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>zip-nms-v1_21_R6</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>