Skip to content

Commit

Permalink
feat: support minecraft version 1.21.4
Browse files Browse the repository at this point in the history
  • Loading branch information
NgLoader committed Dec 14, 2024
1 parent d30f34f commit ba1f4f0
Show file tree
Hide file tree
Showing 5 changed files with 231 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ checkVersion "1.20.2" "17"
checkVersion "1.20.4" "17"
checkVersion "1.20.6" "21"
checkVersion "1.21" "21"
checkVersion "1.21.3" "21"
checkVersion "1.21.3" "21"
checkVersion "1.21.4" "21"
1 change: 1 addition & 0 deletions zip-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
<module>zip-nms-v1_20_R4</module>
<module>zip-nms-v1_21_R1</module>
<module>zip-nms-v1_21_R2</module>
<module>zip-nms-v1_21_R3</module>
</modules>
</project>
72 changes: 72 additions & 0 deletions zip-nms/zip-nms-v1_21_R3/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<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_R3</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.4-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.4-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.21.4-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.4-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.21.4-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,150 @@
package net.imprex.zip.nms.v1_21_R3;

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

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

import com.mojang.authlib.GameProfile;
import com.mojang.authlib.properties.Property;

import net.imprex.zip.common.ReflectionUtil;
import net.imprex.zip.nms.api.NmsManager;
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.Tag;
import net.minecraft.world.item.component.ResolvableProfile;

public class ZipNmsManager implements NmsManager {

private static final BiConsumer<SkullMeta, GameProfile> SET_PROFILE;

private static final RegistryAccess DEFAULT_REGISTRY = CraftRegistry.getMinecraftRegistry();

private static final CompoundTag NBT_EMPTY_ITEMSTACK = new CompoundTag();

static {
NBT_EMPTY_ITEMSTACK.putString("id", "minecraft:air");

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, new ResolvableProfile(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;
}

public byte[] nbtToBinary(CompoundTag compound) {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
NbtIo.writeCompressed(compound, outputStream);
return outputStream.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

public CompoundTag binaryToNBT(byte[] binary) {
try (ByteArrayInputStream inputStream = new ByteArrayInputStream(binary)) {
return NbtIo.readCompressed(inputStream, NbtAccounter.unlimitedHeap());
} catch (Exception e) {
e.printStackTrace();
}
return new CompoundTag();
}

@Override
public byte[] itemstackToBinary(ItemStack[] items) {
CompoundTag inventory = new CompoundTag();
ListTag list = new ListTag();
for (ItemStack itemStack : items) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
list.add(NBT_EMPTY_ITEMSTACK);
} else {
net.minecraft.world.item.ItemStack craftItem = CraftItemStack.asNMSCopy(itemStack);
Tag tag = craftItem.save(DEFAULT_REGISTRY);
list.add(tag);
}
}
inventory.put("i", list);
return nbtToBinary(inventory);
}

@Override
public List<ItemStack> binaryToItemStack(byte[] binary) {
CompoundTag nbt = binaryToNBT(binary);
List<ItemStack> items = new ArrayList<>();
if (nbt.contains("i", 9)) {
ListTag list = nbt.getList("i", 10);
for (Tag base : list) {
if (base instanceof CompoundTag itemTag) {
if (itemTag.getString("id").equals("minecraft:air")) {
items.add(new ItemStack(Material.AIR));
} else {
Optional<net.minecraft.world.item.ItemStack> optional = net.minecraft.world.item.ItemStack.parse(DEFAULT_REGISTRY, itemTag);
if (optional.isPresent()) {
items.add(CraftItemStack.asBukkitCopy(optional.get()));
}
}
}
}
}
return items;
}

@Override
public void setSkullProfile(SkullMeta meta, String texture) {
try {
GameProfile gameProfile = new GameProfile(UUID.randomUUID(), "");
gameProfile.getProperties().put("textures", new Property("textures", texture));

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 @@ -101,5 +101,11 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>zip-nms-v1_21_R3</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

0 comments on commit ba1f4f0

Please sign in to comment.