Skip to content

Commit

Permalink
Update ItemStac Serialization, add new 1.20.5+ default schematic
Browse files Browse the repository at this point in the history
  • Loading branch information
ceze88 committed Aug 22, 2024
1 parent 68a47ce commit 09e5291
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 42 deletions.
32 changes: 22 additions & 10 deletions src/main/java/com/craftaro/skyblock/config/FileManager.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.craftaro.skyblock.config;

import com.craftaro.core.compatibility.ServerVersion;
import com.craftaro.skyblock.SkyBlock;
import com.craftaro.skyblock.island.IslandWorld;
import com.google.common.io.ByteStreams;
Expand Down Expand Up @@ -93,17 +94,28 @@ public void loadConfigs() {

if (fileName.equals("structures/default.structure")) {
configFile.delete();
try {
configFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
try (InputStream is = this.plugin.getResource(fileName); OutputStream os = Files.newOutputStream(configFile.toPath())) {
if (is != null) {
ByteStreams.copy(is, os);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_20_5)) {
//Copy default_1_20_5.structure instead of default.structure
try (InputStream is = this.plugin.getResource("structures/default_1_20_5.structure"); OutputStream os = Files.newOutputStream(configFile.toPath())) {
if (is != null) {
ByteStreams.copy(is, os);
}
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
try {
configFile.createNewFile();
} catch (IOException ex) {
ex.printStackTrace();
}
try (InputStream is = this.plugin.getResource(fileName); OutputStream os = Files.newOutputStream(configFile.toPath())) {
if (is != null) {
ByteStreams.copy(is, os);
}
} catch (IOException ex) {
ex.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
}
continue;
}
Expand Down
78 changes: 46 additions & 32 deletions src/main/java/com/craftaro/skyblock/utils/item/ItemStackUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.util.Arrays;

public class ItemStackUtil {
private static final boolean isAbove1_16_R1 = MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_16)
Expand All @@ -28,47 +30,59 @@ public static ItemStack deserializeItemStack(String data) {

ItemStack itemStack = null;

try {
Class<?> NBTTagCompoundClass = ClassMapping.NBT_TAG_COMPOUND.getClazz();
Class<?> NMSItemStackClass = ClassMapping.ITEM_STACK.getClazz();
Object NBTTagCompound = isAbove1_16_R1 ? ClassMapping.NBT_COMPRESSED_STREAM_TOOLS.getClazz()
.getMethod("a", DataInput.class).invoke(null, dataInputStream)
: ClassMapping.NBT_COMPRESSED_STREAM_TOOLS.getClazz()
.getMethod("a", DataInputStream.class).invoke(null, dataInputStream);
Object craftItemStack;

assert NMSItemStackClass != null;
if (MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_13)) {
craftItemStack = NMSItemStackClass.getMethod("a", NBTTagCompoundClass).invoke(null, NBTTagCompound);
} else if (MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_11)) {
craftItemStack = NMSItemStackClass.getConstructor(NBTTagCompoundClass).newInstance(NBTTagCompound);
} else {
craftItemStack = NMSItemStackClass.getMethod("createStack", NBTTagCompoundClass).invoke(null,
NBTTagCompound);
}
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_20_5)) {
//We need net.minecraft.nbt.NbtIo class in this version
byte[] bytes = new BigInteger(data, 32).toByteArray();
itemStack = ItemStack.deserializeBytes(bytes);
} else {
try {
Class<?> NBTTagCompoundClass = ClassMapping.NBT_TAG_COMPOUND.getClazz();
Class<?> NMSItemStackClass = ClassMapping.ITEM_STACK.getClazz();
Object NBTTagCompound = isAbove1_16_R1 ? ClassMapping.NBT_COMPRESSED_STREAM_TOOLS.getClazz()
.getMethod("a", DataInput.class).invoke(null, dataInputStream)
: ClassMapping.NBT_COMPRESSED_STREAM_TOOLS.getClazz()
.getMethod("a", DataInputStream.class).invoke(null, dataInputStream);
Object craftItemStack;

assert NMSItemStackClass != null;
if (MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_13)) {
craftItemStack = NMSItemStackClass.getMethod("a", NBTTagCompoundClass).invoke(null, NBTTagCompound);
} else if (MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_11)) {
craftItemStack = NMSItemStackClass.getConstructor(NBTTagCompoundClass).newInstance(NBTTagCompound);
} else {
craftItemStack = NMSItemStackClass.getMethod("createStack", NBTTagCompoundClass).invoke(null,
NBTTagCompound);
}

itemStack = (ItemStack) NMSUtils.getCraftClass("inventory.CraftItemStack")
.getMethod("asBukkitCopy", NMSItemStackClass).invoke(null, craftItemStack);

// TODO: This method of serialization has some issues. Not all the names are the same between versions
// Make an exception for reeds/melon, they NEED to load in the island chest
// This code is here SPECIFICALLY to get the default.structure to load properly in all versions
// Other structures people make NEED to be saved from the version that they will be using so everything loads properly
if (itemStack.getType() == Material.AIR) {
if (NBTTagCompound.toString().equals("{id:\"minecraft:sugar_cane\",Count:1b}")) {
itemStack = XMaterial.SUGAR_CANE.parseItem();
} else if (NBTTagCompound.toString().equals("{id:\"minecraft:melon_slice\",Count:1b}")) {
itemStack = XMaterial.MELON_SLICE.parseItem();
itemStack = (ItemStack) NMSUtils.getCraftClass("inventory.CraftItemStack")
.getMethod("asBukkitCopy", NMSItemStackClass).invoke(null, craftItemStack);

// TODO: This method of serialization has some issues. Not all the names are the same between versions
// Make an exception for reeds/melon, they NEED to load in the island chest
// This code is here SPECIFICALLY to get the default.structure to load properly in all versions
// Other structures people make NEED to be saved from the version that they will be using so everything loads properly
if (itemStack.getType() == Material.AIR) {
if (NBTTagCompound.toString().equals("{id:\"minecraft:sugar_cane\",Count:1b}")) {
itemStack = XMaterial.SUGAR_CANE.parseItem();
} else if (NBTTagCompound.toString().equals("{id:\"minecraft:melon_slice\",Count:1b}")) {
itemStack = XMaterial.MELON_SLICE.parseItem();
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
} catch (Exception ex) {
ex.printStackTrace();
}

return itemStack;
}

public static String serializeItemStack(ItemStack item) {
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_20_5)) {
//We need net.minecraft.nbt.NbtIo class in this version
byte[] bytes = item.serializeAsBytes();
return new BigInteger(bytes).toString(32);
}

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutput = new DataOutputStream(outputStream);

Expand Down
1 change: 1 addition & 0 deletions src/main/resources/structures/default_1_20_5.structure

Large diffs are not rendered by default.

0 comments on commit 09e5291

Please sign in to comment.