Skip to content

Commit

Permalink
Feat support 1.21 (#25)
Browse files Browse the repository at this point in the history
* feat: support 1.21

* chore: add 1.21 to buildtools
  • Loading branch information
NgLoader authored Jun 18, 2024
1 parent b63171d commit 0cc7a66
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 4 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ checkVersion "1.19.4" "17"
checkVersion "1.20.1" "17"
checkVersion "1.20.2" "17"
checkVersion "1.20.4" "17"
checkVersion "1.20.5" "21"
checkVersion "1.20.5" "21"
checkVersion "1.21" "21"
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<img align="right" src="https://user-images.githubusercontent.com/13753840/213946932-635d40a3-a278-427c-9e9c-245a89ed12a9.png" height="200" width="200">

# Zero Inventory Problems - ZIP
[![Release Status](https://github.com/Imprex-Development/zero-inventory-problems/workflows/Releases/badge.svg)](https://github.com/Imprex-Development/zero-inventory-problems/releases/latest) [![Build Status](https://github.com/Imprex-Development/zero-inventory-problems/workflows/Build/badge.svg)](https://github.com/Imprex-Development/zero-inventory-problems/actions?query=workflow%3ABuild)
<img align="right" src="https://user-images.githubusercontent.com/13753840/213946932-635d40a3-a278-427c-9e9c-245a89ed12a9.png" height="200" width="200">

Zero Inventory Problems is a plugin for Spigot based Minecraft Servers that adds backpacks to store there overfully inventorys.

Expand All @@ -23,7 +22,7 @@ Still having trouble getting Zero Inventory Problems to run check out our [commo

## License:

Copyright (C) 2023 by Imprex-Development. All rights reserved.
Copyright (C) 2023-2024 by Imprex-Development. All rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
Expand Down
1 change: 1 addition & 0 deletions zip-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@
<module>zip-nms-v1_20_R2</module>
<module>zip-nms-v1_20_R3</module>
<module>zip-nms-v1_20_R4</module>
<module>zip-nms-v1_21_R1</module>
</modules>
</project>
72 changes: 72 additions & 0 deletions zip-nms/zip-nms-v1_21_R1/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_R1</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-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-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.21-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-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.21-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,116 @@
package net.imprex.zip.nms.v1_21_R1;

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

import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_21_R1.CraftRegistry;
import org.bukkit.craftbukkit.v1_21_R1.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;

public class ZipNmsManager implements NmsManager {

private static final Class<?> CRAFTMETASKULL_CLASS = ReflectionUtil.getCraftBukkitClass("inventory.CraftMetaSkull");
private static final Method CRAFTMETASKULL_SET_PROFILE = ReflectionUtil.getMethod(CRAFTMETASKULL_CLASS,
"setProfile", GameProfile.class);

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");
}

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));
CRAFTMETASKULL_SET_PROFILE.invoke(meta, gameProfile);
} catch (Exception e) {
throw new ClassCastException("Error by setting skull profile");
}
}

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

0 comments on commit 0cc7a66

Please sign in to comment.