Update ItemStac Serialization, add new 1.20.5+ default schematic

This commit is contained in:
ceze88 2024-08-22 15:38:17 +02:00
parent 68a47ce0c5
commit 09e5291478
3 changed files with 69 additions and 42 deletions

View File

@ -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;
@ -93,17 +94,28 @@ public class FileManager {
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;
}

View File

@ -16,7 +16,9 @@ import java.io.DataInputStream;
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)
@ -28,47 +30,59 @@ public class ItemStackUtil {
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;
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();
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();
}
}
} 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);

File diff suppressed because one or more lines are too long