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
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:
key: ${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
java-version: 21
- name: Run BuildTools
run: |
bash ./.github/workflows/buildtools.sh
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/buildtools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,5 @@ checkVersion "1.19.3" "17"
checkVersion "1.19.4" "17"
checkVersion "1.20.1" "17"
checkVersion "1.20.2" "17"
checkVersion "1.20.4" "17"
checkVersion "1.20.4" "17"
checkVersion "1.20.5" "21"
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ jobs:
key: ${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-${{ hashFiles('./.github/workflows/buildtools.sh') }}
restore-keys: |
${{ runner.os }}-maven-${{ env.CACHE_VERSION }}-
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v3
with:
distribution: 'temurin'
java-version: 17
java-version: 21
- name: Run BuildTools
run: |
bash ./.github/workflows/buildtools.sh
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<plugin.compile.version>3.10.1</plugin.compile.version>
<plugin.shade.version>3.4.0</plugin.shade.version>
<plugin.flatten.version>1.2.7</plugin.flatten.version>
<plugin.specialsource.version>1.2.4</plugin.specialsource.version>
<plugin.specialsource.version>2.0.2</plugin.specialsource.version>

<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
Expand Down
1 change: 1 addition & 0 deletions zip-nms/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@
<module>zip-nms-v1_20_R1</module>
<module>zip-nms-v1_20_R2</module>
<module>zip-nms-v1_20_R3</module>
<module>zip-nms-v1_20_R4</module>
</modules>
</project>
72 changes: 72 additions & 0 deletions zip-nms/zip-nms-v1_20_R4/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_20_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.20.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.20.5-R0.1-SNAPSHOT:txt:maps-mojang</srgIn>
<reverse>true</reverse>
<remappedDependencies>
org.spigotmc:spigot:1.20.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.20.5-R0.1-SNAPSHOT:csrg:maps-spigot</srgIn>
<remappedDependencies>
org.spigotmc:spigot:1.20.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,116 @@
package net.imprex.zip.nms.v1_20_R4;

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_20_R4.CraftRegistry;
import org.bukkit.craftbukkit.v1_20_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;

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 @@ -83,5 +83,11 @@
<version>${revision}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>net.imprex</groupId>
<artifactId>zip-nms-v1_20_R4</artifactId>
<version>${revision}</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
9 changes: 7 additions & 2 deletions zip-plugin/src/main/java/net/imprex/zip/Backpack.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,17 @@ public Backpack(BackpackPlugin plugin, BackpackType type) {
this.backpackHandler.registerBackpack(this);
}

public Backpack(BackpackPlugin plugin, Ingrim4Buffer buffer) {
public Backpack(BackpackPlugin plugin, UniqueId id, Ingrim4Buffer buffer) {
this.backpackHandler = plugin.getBackpackHandler();
this.storageKey = plugin.getBackpackStorageKey();
this.messageConfig = plugin.getBackpackConfig().message();

this.id = UniqueId.fromByteArray(buffer.readByteArray());
/*
* Load backpack id from buffer but don't use it!
* Just for later migration to SQL
*/
buffer.readByteArray();
this.id = id;

this.typeRaw = buffer.readString();
this.type = plugin.getBackpackRegistry().getTypeByName(this.typeRaw);
Expand Down
43 changes: 22 additions & 21 deletions zip-plugin/src/main/java/net/imprex/zip/BackpackHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

import org.bukkit.NamespacedKey;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -37,9 +37,9 @@ public class BackpackHandler implements ZIPHandler {
private final NamespacedKey backpackIdentifierKey;
private final Path folderPath;

private Map<UniqueId, Backpack> backpackById = new ConcurrentHashMap<>();
private Map<Inventory, Backpack> backpackByInventory = new ConcurrentHashMap<>();
private List<UniqueId> loadingIssue = new CopyOnWriteArrayList<>();
private Map<UniqueId, Backpack> backpackById = new HashMap<>();
private Map<Inventory, Backpack> backpackByInventory = new HashMap<>();
private List<UniqueId> loadingIssue = new ArrayList<>();

public BackpackHandler(BackpackPlugin plugin) {
this.plugin = plugin;
Expand All @@ -64,7 +64,8 @@ public void disable() {
this.backpackByInventory.clear();
}

public Backpack loadBackpack(Path file) {
private Backpack loadBackpack(UniqueId id) {
Path file = this.folderPath.resolve(id.toString());
if (!Files.isRegularFile(file)) {
return null;
}
Expand All @@ -73,26 +74,14 @@ public Backpack loadBackpack(Path file) {
byte[] data = ByteStreams.toByteArray(inputStream);
Ingrim4Buffer buffer = new Ingrim4Buffer(Unpooled.wrappedBuffer(data));

Backpack backpack = new Backpack(this.plugin, buffer);
Backpack backpack = new Backpack(this.plugin, id, buffer);
return backpack;
} catch (Exception e) {
ZIPLogger.error("Unable to load backpack for id '" + file.getFileName().toString() + "'", e);
}
return null;
}

public Backpack loadBackpack(UniqueId id) {
if (this.loadingIssue.contains(id)) {
return null;
}

Backpack backpack = this.loadBackpack(this.folderPath.resolve(id.toString()));
if (backpack == null) {
this.loadingIssue.add(id);
}
return backpack;
}

@Override
public void save(ZIPBackpack backpack) {
if (Files.notExists(this.folderPath)) {
Expand All @@ -116,11 +105,23 @@ public void save(ZIPBackpack backpack) {
}
}

public Backpack getBackpack(UniqueId id) {
if (this.loadingIssue.contains(id)) {
return null;
}

Backpack backpack = this.loadBackpack(id);
if (backpack == null) {
this.loadingIssue.add(id);
}
return backpack;
}

@Override
public Backpack getBackpack(ZIPUniqueId id) {
Backpack backpack = this.backpackById.get(id);
Backpack backpack = this.backpackById.get((UniqueId) id);
if (backpack == null) {
backpack = this.loadBackpack((UniqueId) id);
backpack = this.getBackpack((UniqueId) id);
}
return backpack;
}
Expand Down