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
8 changes: 4 additions & 4 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set BUILD_VERSION
run: echo "BUILD_VERSION=$(git describe --tags $(git rev-list --tags --max-count=1))-b$GITHUB_RUN_NUMBER" >> $GITHUB_ENV
- name: Maven cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-
- name: Set up JDK 21
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 21
Expand All @@ -36,7 +36,7 @@ jobs:
mvn clean package -pl zip-plugin --batch-mode --also-make -Drevision=$BUILD_VERSION
mv zip-plugin/target/zip-*.jar ./
- name: Upload artifacts
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: zero-inventory-problems-plugin
path: ./zip-*.jar
3 changes: 2 additions & 1 deletion .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ checkVersion "1.20.4" "17"
checkVersion "1.20.6" "21"
checkVersion "1.21" "21"
checkVersion "1.21.3" "21"
checkVersion "1.21.4" "21"
checkVersion "1.21.4" "21"
checkVersion "1.21.5" "21"
6 changes: 3 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ jobs:

steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set RELEASE_VERSION
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Maven cache
uses: actions/cache@v3
uses: actions/cache@v4
with:
path: ~/.m2/repository
key: ${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-
- name: Set up JDK 21
uses: actions/setup-java@v3
uses: actions/setup-java@v4
with:
distribution: 'temurin'
java-version: 21
Expand Down
1 change: 1 addition & 0 deletions zip-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@
<module>zip-nms-v1_21_R1</module>
<module>zip-nms-v1_21_R2</module>
<module>zip-nms-v1_21_R3</module>
<module>zip-nms-v1_21_R4</module>
</modules>
</project>
70 changes: 70 additions & 0 deletions zip-nms/zip-nms-v1_21_R4/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_R4</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.5-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.5-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.21.5-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.5-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.21.5-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,155 @@
package net.imprex.zip.nms.v1_21_R4;

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_R4.CraftRegistry;
import org.bukkit.craftbukkit.v1_21_R4.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")) {
Optional<ListTag> list = nbt.getList("i");
if (list.isEmpty()) {
return items;
}

for (Tag base : list.get()) {
if (base instanceof CompoundTag itemTag) {
String itemType = itemTag.getString("id").orElse("");
if (itemType.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 @@ -107,5 +107,11 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>zip-nms-v1_21_R4</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>