Implemented NBT Saving

1.13: CustomItemTagContainer
1.14: PersistentDataContainer
This commit is contained in:
Sn0wStorm
2019-10-20 21:06:35 +02:00
parent f431c13100
commit 4909e59c90
11 changed files with 222 additions and 29 deletions
+48 -5
View File
@@ -1,14 +1,11 @@
package com.dre.brewery.utility;
import com.dre.brewery.P;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.TreeSpecies;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.Levelled;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.material.Cauldron;
import org.bukkit.material.MaterialData;
import org.bukkit.material.Tree;
@@ -30,6 +27,8 @@ public class LegacyUtil {
private static Method GET_BLOCK_TYPE_ID_AT;
private static Method SET_DATA;
public static boolean NewNbtVer;
static {
// <= 1.12.2 methods
// These will be rarely used
@@ -248,4 +247,48 @@ public class LegacyUtil {
}
}
/**
* MC 1.13 uses a different NBT API than the newer versions..
* We decide here which to use, the new or the old
*
* @return true if we can use nbt at all
*/
public static boolean initNbt() {
try {
Class.forName("org.bukkit.persistence.PersistentDataContainer");
NewNbtVer = true;
P.p.debugLog("Using the NEW nbt api");
return true;
} catch (ClassNotFoundException e) {
try {
Class.forName("org.bukkit.inventory.meta.tags.CustomItemTagContainer");
NewNbtVer = false;
P.p.debugLog("Using the OLD nbt api");
return true;
} catch (ClassNotFoundException ex) {
NewNbtVer = false;
P.p.debugLog("No nbt api found, using Lore Save System");
return false;
}
}
}
@SuppressWarnings("deprecation")
public static void writeBytesItem(byte[] bytes, ItemMeta meta, NamespacedKey key) {
if (NewNbtVer) {
meta.getPersistentDataContainer().set(key, org.bukkit.persistence.PersistentDataType.BYTE_ARRAY, bytes);
} else {
meta.getCustomTagContainer().setCustomTag(key, org.bukkit.inventory.meta.tags.ItemTagType.BYTE_ARRAY, bytes);
}
}
@SuppressWarnings("deprecation")
public static byte[] readBytesItem(ItemMeta meta, NamespacedKey key) {
if (NewNbtVer) {
return meta.getPersistentDataContainer().get(key, org.bukkit.persistence.PersistentDataType.BYTE_ARRAY);
} else {
return meta.getCustomTagContainer().getCustomTag(key, org.bukkit.inventory.meta.tags.ItemTagType.BYTE_ARRAY);
}
}
}