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; package com.craftaro.skyblock.config;
import com.craftaro.core.compatibility.ServerVersion;
import com.craftaro.skyblock.SkyBlock; import com.craftaro.skyblock.SkyBlock;
import com.craftaro.skyblock.island.IslandWorld; import com.craftaro.skyblock.island.IslandWorld;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
@ -93,6 +94,16 @@ public class FileManager {
if (fileName.equals("structures/default.structure")) { if (fileName.equals("structures/default.structure")) {
configFile.delete(); configFile.delete();
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 { try {
configFile.createNewFile(); configFile.createNewFile();
} catch (IOException ex) { } catch (IOException ex) {
@ -105,6 +116,7 @@ public class FileManager {
} catch (IOException ex) { } catch (IOException ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
}
continue; continue;
} }

View File

@ -16,7 +16,9 @@ import java.io.DataInputStream;
import java.io.DataOutput; import java.io.DataOutput;
import java.io.DataOutputStream; import java.io.DataOutputStream;
import java.lang.reflect.Constructor; import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.math.BigInteger; import java.math.BigInteger;
import java.util.Arrays;
public class ItemStackUtil { public class ItemStackUtil {
private static final boolean isAbove1_16_R1 = MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_16) private static final boolean isAbove1_16_R1 = MajorServerVersion.isServerVersionAtLeast(MajorServerVersion.V1_16)
@ -28,6 +30,11 @@ public class ItemStackUtil {
ItemStack itemStack = null; ItemStack itemStack = null;
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 { try {
Class<?> NBTTagCompoundClass = ClassMapping.NBT_TAG_COMPOUND.getClazz(); Class<?> NBTTagCompoundClass = ClassMapping.NBT_TAG_COMPOUND.getClazz();
Class<?> NMSItemStackClass = ClassMapping.ITEM_STACK.getClazz(); Class<?> NMSItemStackClass = ClassMapping.ITEM_STACK.getClazz();
@ -64,11 +71,18 @@ public class ItemStackUtil {
} catch (Exception ex) { } catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();
} }
}
return itemStack; return itemStack;
} }
public static String serializeItemStack(ItemStack item) { 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(); ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
DataOutputStream dataOutput = new DataOutputStream(outputStream); DataOutputStream dataOutput = new DataOutputStream(outputStream);

File diff suppressed because one or more lines are too long