mirror of
https://github.com/tr7zw/Item-NBT-API.git
synced 2026-11-13 12:31:58 -05:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ae98859524 | ||
|
|
70f0bb62f9 | ||
|
|
e2e8aad70a | ||
|
|
4874366e30 | ||
|
|
d2de90ee25 | ||
|
|
b339c9e9c2 | ||
|
|
3425a6672d | ||
|
|
67616266da | ||
|
|
b08e4aa12f | ||
|
|
27411e90da | ||
|
|
73776cfec4 | ||
|
|
bd674e31c0 | ||
|
|
3405ffe0a3 | ||
|
|
f1317bcb04 | ||
|
|
4a32343b95 | ||
|
|
3de6ed83eb | ||
|
|
e5bdce73ea | ||
|
|
410e88a35b | ||
|
|
e3142b4869 | ||
|
|
243a5c2bcd | ||
|
|
045c710366 | ||
|
|
4cd118671f | ||
|
|
d86c91f2e6 | ||
|
|
f8736c8188 | ||
|
|
b3aec54205 |
No files matched your search
@@ -8,7 +8,7 @@ updates:
|
||||
ignore:
|
||||
- dependency-name: com.github.ekryd.sortpom:sortpom-maven-plugin
|
||||
versions:
|
||||
- 2.13.2
|
||||
- 2.13.3-SNAPSHOT
|
||||
- 2.14.0
|
||||
- 2.14.1
|
||||
- 2.15.0
|
||||
@@ -25,7 +25,7 @@ jobs:
|
||||
- name: Copy artifacts
|
||||
shell: bash
|
||||
run: |
|
||||
mkdir -p artifacts/v2-api && cp -r item-nbt-api/target/apidocs/* artifacts/v2-api
|
||||
mkdir -p artifacts/v2-api && cp -r item-nbt-api/target/reports/apidocs/* artifacts/v2-api
|
||||
mkdir -p artifacts/v2-plugin && cp -r item-nbt-plugin/target/apidocs/* artifacts/v2-plugin
|
||||
- name: Deploy 🚀
|
||||
uses: JamesIves/github-pages-deploy-action@v4.4.1
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
package de.tr7zw.changeme.nbtapi;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.logging.Level;
|
||||
@@ -12,6 +15,7 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.iface.NBTFileHandle;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteItemNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadableItemNBT;
|
||||
@@ -200,9 +204,9 @@ public class NBT {
|
||||
}
|
||||
|
||||
/**
|
||||
* It takes a block entity and a function that takes a ReadableNBT and returns
|
||||
* a generic type T, and returns the result of the function, applied to the
|
||||
* block entities persistent data container
|
||||
* It takes a block entity and a function that takes a ReadableNBT and returns a
|
||||
* generic type T, and returns the result of the function, applied to the block
|
||||
* entities persistent data container
|
||||
*
|
||||
* @param blockState The block state of the block you want to get the data from.
|
||||
* @param getter A function that takes a ReadableNBT and returns a value of
|
||||
@@ -269,17 +273,17 @@ public class NBT {
|
||||
nbtEnt.setClosed();
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* It takes an ItemStack and a Consumer<ReadWriteNBT>, and then applies
|
||||
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
|
||||
* This method is quiet expensive, so don't overuse it.
|
||||
* This method is quite expensive, so don't overuse it.
|
||||
*
|
||||
* @param item The item you want to modify the components of
|
||||
* @param consumer The consumer that will be used to modify the components.
|
||||
*/
|
||||
public static void modifyComponents(ItemStack item, Consumer<ReadWriteNBT> consumer) {
|
||||
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
if (!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
throw new NbtApiException("This method only works for 1.20.5+!");
|
||||
}
|
||||
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
|
||||
@@ -287,18 +291,18 @@ public class NBT {
|
||||
ItemStack tmp = NBT.itemStackFromNBT(nbti);
|
||||
item.setItemMeta(tmp.getItemMeta());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* It takes an ItemStack and a Consumer<ReadWriteNBT>, and then applies
|
||||
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
|
||||
* This method is quiet expensive, so don't overuse it.
|
||||
* This method is quite expensive, so don't overuse it.
|
||||
*
|
||||
* @param item The item you want to modify the components of
|
||||
* @param function The consumer that will be used to modify the components.
|
||||
* @return The return type is the same as the return type of the function.
|
||||
*/
|
||||
public static <T> T modifyComponents(ItemStack item, Function<ReadWriteNBT, T> function) {
|
||||
if(!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
if (!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
throw new NbtApiException("This method only works for 1.20.5+!");
|
||||
}
|
||||
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
|
||||
@@ -308,6 +312,39 @@ public class NBT {
|
||||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* It takes an ItemStack and a Consumer<ReadWriteNBT>, and then applies
|
||||
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
|
||||
* This method is quite expensive, try to cache the results/use it smartly.
|
||||
*
|
||||
* @param item The item you want to read the components of
|
||||
* @param consumer The consumer that will be used to read the components.
|
||||
*/
|
||||
public static void getComponents(ItemStack item, Consumer<ReadableNBT> consumer) {
|
||||
if (!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
throw new NbtApiException("This method only works for 1.20.5+!");
|
||||
}
|
||||
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
|
||||
consumer.accept(nbti.getOrCreateCompound("components"));
|
||||
}
|
||||
|
||||
/**
|
||||
* It takes an ItemStack and a Consumer<ReadWriteNBT>, and then applies
|
||||
* the Consumer to the ItemStacks Components as NBT. This is for 1.20.5+ only.
|
||||
* This method is quite expensive, try to cache the results/use it smartly.
|
||||
*
|
||||
* @param item The item you want to read the components of
|
||||
* @param function The consumer that will be used to read the components.
|
||||
* @return The return type is the same as the return type of the function.
|
||||
*/
|
||||
public static <T> T getComponents(ItemStack item, Function<ReadableNBT, T> function) {
|
||||
if (!MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
throw new NbtApiException("This method only works for 1.20.5+!");
|
||||
}
|
||||
ReadWriteNBT nbti = NBT.itemStackToNBT(item);
|
||||
return function.apply(nbti.getOrCreateCompound("components"));
|
||||
}
|
||||
|
||||
/**
|
||||
* It takes an entity and a function that takes a ReadWriteNBT and applies the
|
||||
* function to the entity
|
||||
@@ -492,12 +529,70 @@ public class NBT {
|
||||
* It takes a nbt json string, and returns a ReadWriteNBT object
|
||||
*
|
||||
* @param nbtString The NBT string to parse.
|
||||
* @return A new NBTContainer object.
|
||||
* @return A new ReadWriteNBT object.
|
||||
*/
|
||||
public static ReadWriteNBT parseNBT(String nbtString) {
|
||||
return new NBTContainer(nbtString);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads in an NBT stream and returns a ReadWriteNBT object
|
||||
*
|
||||
* @param stream The NBT stream to read.
|
||||
* @return A new ReadWriteNBT object.
|
||||
*/
|
||||
public static ReadWriteNBT readNBT(InputStream stream) {
|
||||
return new NBTContainer(stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper method for other developers using NMS. Allows to wrap any
|
||||
* net.minecraft.nbt.CompoundTag to a NBTAPI ReadWriteNBT object.
|
||||
*
|
||||
* @param nmsNbtTag Needs to be a valid net.minecraft.nbt.CompoundTag
|
||||
* @return A new ReadWriteNBT object.
|
||||
*/
|
||||
public static ReadWriteNBT wrapNMSTag(Object nmsNbtTag) {
|
||||
return new NBTContainer(nmsNbtTag);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a NBTFileHandle that uses @param file to store its data. If this file
|
||||
* exists, the data will be loaded, otherwise a new file gets created.
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
*/
|
||||
public static NBTFileHandle getFileHandle(File file) throws IOException {
|
||||
return new NBTFile(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads NBT data from the provided file.
|
||||
* <p>
|
||||
* Returns an empty tag if the file does not exist.
|
||||
*
|
||||
* @param file file to read
|
||||
* @return ReadWriteNBT of the files data
|
||||
* @throws IOException exception
|
||||
*/
|
||||
public static ReadWriteNBT readFile(File file) throws IOException {
|
||||
return NBTFile.readFrom(file);
|
||||
}
|
||||
|
||||
/**
|
||||
* Saves NBT data to the provided file.
|
||||
* <p>
|
||||
* Will fully override the file if it already exists.
|
||||
*
|
||||
* @param file file
|
||||
* @param nbt NBT data
|
||||
* @throws IOException exception
|
||||
*/
|
||||
public static void writeFile(File file, ReadWriteNBT nbt) throws IOException {
|
||||
NBTFile.saveTo(file, (NBTCompound) nbt);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a read only proxy class for NBT, given an annotated interface.
|
||||
*
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.tr7zw.changeme.nbtapi;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadableNBT;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
|
||||
@@ -56,7 +57,8 @@ public class NBTCompoundList extends NBTList<ReadWriteNBT> implements ReadWriteN
|
||||
}
|
||||
}
|
||||
|
||||
public NBTCompound addCompound(ReadWriteNBT comp) {
|
||||
@Override
|
||||
public ReadWriteNBT addCompound(ReadableNBT comp) {
|
||||
if (comp instanceof NBTCompound) {
|
||||
return addCompound((NBTCompound) comp);
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ public class NBTContainer extends NBTCompound {
|
||||
/**
|
||||
* Creates an empty, standalone NBTCompound
|
||||
*/
|
||||
@Deprecated
|
||||
public NBTContainer() {
|
||||
super(null, null);
|
||||
nbt = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance();
|
||||
@@ -31,7 +32,9 @@ public class NBTContainer extends NBTCompound {
|
||||
* Takes in any NMS Compound to wrap it
|
||||
*
|
||||
* @param nbt
|
||||
* @deprecated Use NBT.wrapNMSTag
|
||||
*/
|
||||
@Deprecated
|
||||
public NBTContainer(Object nbt) {
|
||||
super(null, null);
|
||||
if (nbt == null) {
|
||||
@@ -47,7 +50,9 @@ public class NBTContainer extends NBTCompound {
|
||||
* Reads in a NBT InputStream
|
||||
*
|
||||
* @param inputsteam
|
||||
* @deprecated Use NBT.readNBT
|
||||
*/
|
||||
@Deprecated
|
||||
public NBTContainer(InputStream inputsteam) {
|
||||
super(null, null);
|
||||
this.nbt = NBTReflectionUtil.readNBT(inputsteam);
|
||||
@@ -58,7 +63,9 @@ public class NBTContainer extends NBTCompound {
|
||||
* {@link NbtApiException} in case something goes wrong.
|
||||
*
|
||||
* @param nbtString
|
||||
* @deprecated Use NBT.parseNBT
|
||||
*/
|
||||
@Deprecated
|
||||
public NBTContainer(String nbtString) {
|
||||
super(null, null);
|
||||
if (nbtString == null) {
|
||||
|
||||
@@ -4,6 +4,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.iface.NBTFileHandle;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ObjectCreator;
|
||||
|
||||
/**
|
||||
@@ -12,7 +13,7 @@ import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ObjectCreator;
|
||||
* @author tr7zw
|
||||
*
|
||||
*/
|
||||
public class NBTFile extends NBTCompound {
|
||||
public class NBTFile extends NBTCompound implements NBTFileHandle {
|
||||
|
||||
private final File file;
|
||||
private Object nbt;
|
||||
@@ -23,7 +24,9 @@ public class NBTFile extends NBTCompound {
|
||||
*
|
||||
* @param file
|
||||
* @throws IOException
|
||||
* @deprecated Use NBT.getFileHandle(file)
|
||||
*/
|
||||
@Deprecated
|
||||
public NBTFile(File file) throws IOException {
|
||||
super(null, null);
|
||||
if (file == null) {
|
||||
@@ -43,6 +46,7 @@ public class NBTFile extends NBTCompound {
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
@Override
|
||||
public void save() throws IOException {
|
||||
try {
|
||||
getWriteLock().lock();
|
||||
@@ -55,6 +59,7 @@ public class NBTFile extends NBTCompound {
|
||||
/**
|
||||
* @return The File used to store the data
|
||||
*/
|
||||
@Override
|
||||
public File getFile() {
|
||||
return file;
|
||||
}
|
||||
@@ -77,7 +82,9 @@ public class NBTFile extends NBTCompound {
|
||||
* @param file file to read
|
||||
* @return NBTCompound holding file's nbt data
|
||||
* @throws IOException exception
|
||||
* @deprecated Use NBT.readFile(file)
|
||||
*/
|
||||
@Deprecated
|
||||
public static NBTCompound readFrom(File file) throws IOException {
|
||||
if (!file.exists())
|
||||
return new NBTContainer();
|
||||
@@ -92,7 +99,9 @@ public class NBTFile extends NBTCompound {
|
||||
* @param file file
|
||||
* @param nbt NBT data
|
||||
* @throws IOException exception
|
||||
* @deprecated Use NBT.writeFile(file, nbt)
|
||||
*/
|
||||
@Deprecated
|
||||
public static void saveTo(File file, NBTCompound nbt) throws IOException {
|
||||
if (!file.exists()) {
|
||||
file.getParentFile().mkdirs();
|
||||
|
||||
@@ -154,6 +154,9 @@ public class NBTReflectionUtil {
|
||||
public static void writeApiNBT(NBTCompound comp, OutputStream stream) {
|
||||
try {
|
||||
Object workingtag = comp.getResolvedObject();
|
||||
if(workingtag == null) {
|
||||
workingtag = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance();
|
||||
}
|
||||
ReflectionMethod.NBTFILE_WRITE.run(null, workingtag, stream);
|
||||
} catch (Exception e) {
|
||||
throw new NbtApiException("Exception while writing NBT!", e);
|
||||
@@ -213,7 +216,7 @@ public class NBTReflectionUtil {
|
||||
try {
|
||||
Object nmsComp = getToCompount(nbtcompound.getCompound(), nbtcompound);
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
if (nbtcompound.hasTag("tag")) {
|
||||
if (nbtcompound.hasTag("tag") || nbtcompound.hasTag("Count")) {
|
||||
nmsComp = DataFixerUtil.fixUpRawItemData(nmsComp, DataFixerUtil.VERSION1_20_4,
|
||||
DataFixerUtil.getCurrentVersion());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package de.tr7zw.changeme.nbtapi.iface;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public interface NBTFileHandle extends ReadWriteNBT {
|
||||
|
||||
/**
|
||||
* Saves the data to the file
|
||||
*
|
||||
* @throws IOException
|
||||
*/
|
||||
void save() throws IOException;
|
||||
|
||||
/**
|
||||
* @return The File used to store the data
|
||||
*/
|
||||
File getFile();
|
||||
|
||||
}
|
||||
+9
@@ -10,6 +10,15 @@ public interface ReadWriteNBTCompoundList extends ReadableNBTList<ReadWriteNBT>
|
||||
* @return A new instance of the class.
|
||||
*/
|
||||
ReadWriteNBT addCompound();
|
||||
|
||||
/**
|
||||
* Adds a copy of the Compound to the end of the List and returns it. When null
|
||||
* is given, a new Compound will be created
|
||||
*
|
||||
* @param comp
|
||||
* @return
|
||||
*/
|
||||
ReadWriteNBT addCompound(ReadableNBT comp);
|
||||
|
||||
/**
|
||||
* Removes the element at the specified position in this list
|
||||
|
||||
@@ -30,6 +30,8 @@ public class DataFixerUtil {
|
||||
public static final int VERSION1_20_4 = 3700;
|
||||
public static final int VERSION1_20_5 = 3837;
|
||||
public static final int VERSION1_21 = 3953;
|
||||
public static final int VERSION1_21_2 = 4080;
|
||||
public static final int VERSION1_21_3 = 4189;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object fixUpRawItemData(Object nbt, int fromVersion, int toVersion)
|
||||
@@ -62,7 +64,11 @@ public class DataFixerUtil {
|
||||
* @return
|
||||
*/
|
||||
public static int getCurrentVersion() {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_21_R1)) {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_21_R3)) {
|
||||
return VERSION1_21_3;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_21_R2)) {
|
||||
return VERSION1_21_2;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_21_R1)) {
|
||||
return VERSION1_21;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return VERSION1_20_5;
|
||||
|
||||
@@ -24,7 +24,7 @@ public enum MinecraftVersion {
|
||||
MC1_13_R1(1131), MC1_13_R2(1132), MC1_14_R1(1141), MC1_15_R1(1151), MC1_16_R1(1161), MC1_16_R2(1162),
|
||||
MC1_16_R3(1163), MC1_17_R1(1171), MC1_18_R1(1181, true), MC1_18_R2(1182, true), MC1_19_R1(1191, true),
|
||||
MC1_19_R2(1192, true), MC1_19_R3(1193, true), MC1_20_R1(1201, true), MC1_20_R2(1202, true), MC1_20_R3(1203, true),
|
||||
MC1_20_R4(1204, true), MC1_21_R1(1211, true);
|
||||
MC1_20_R4(1204, true), MC1_21_R1(1211, true), MC1_21_R2(1212, true), MC1_21_R3(1213, true);
|
||||
|
||||
private static MinecraftVersion version;
|
||||
private static Boolean hasGsonSupport;
|
||||
@@ -39,7 +39,7 @@ public enum MinecraftVersion {
|
||||
private static Logger logger = Logger.getLogger("NBTAPI");
|
||||
|
||||
// NBT-API Version
|
||||
protected static final String VERSION = "2.13.2";
|
||||
protected static final String VERSION = "2.14.1";
|
||||
|
||||
private final int versionId;
|
||||
private final boolean mojangMapping;
|
||||
@@ -57,6 +57,9 @@ public enum MinecraftVersion {
|
||||
this.put("1.20.6", MC1_20_R4);
|
||||
this.put("1.21", MC1_21_R1);
|
||||
this.put("1.21.1", MC1_21_R1);
|
||||
this.put("1.21.2", MC1_21_R2);
|
||||
this.put("1.21.3", MC1_21_R2);
|
||||
this.put("1.21.4", MC1_21_R3);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package de.tr7zw.changeme.nbtapi.utils;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
import com.mojang.serialization.Codec;
|
||||
import com.mojang.serialization.DataResult;
|
||||
import com.mojang.serialization.JsonOps;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.MojangToMapping;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
|
||||
|
||||
public class NBTJsonUtil {
|
||||
|
||||
/**
|
||||
* 1.20.3+ only. Used to convert items into Json, used in Chat Hover Components.
|
||||
*
|
||||
* @param itemStack
|
||||
* @return
|
||||
* @throws NbtApiException
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public static JsonElement itemStackToJson(ItemStack itemStack) {
|
||||
try {
|
||||
Codec<Object> itemStackCodec = (Codec<Object>) ClassWrapper.NMS_ITEMSTACK.getClazz()
|
||||
.getField(MojangToMapping.getMapping().get("net.minecraft.world.item.ItemStack#CODEC")).get(null);
|
||||
Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, itemStack);
|
||||
DataResult<JsonElement> result = itemStackCodec.encode(stack, JsonOps.INSTANCE,
|
||||
JsonOps.INSTANCE.emptyMap());
|
||||
Optional<JsonElement> opt = (Optional<JsonElement>) result.getClass().getMethod("result").invoke(result);
|
||||
return opt.orElse(null);
|
||||
} catch (Exception ex) {
|
||||
throw new NbtApiException("Error trying to get Json of an ItemStack.", ex);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+30
@@ -145,6 +145,7 @@ public class MojangToMapping {
|
||||
put("net.minecraft.world.entity.Entity#getEncodeId()", "bw");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#saveWithId()", "p");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#getBlockState()", "r");
|
||||
put("net.minecraft.world.item.ItemStack#CODEC", "a");
|
||||
}
|
||||
|
||||
};
|
||||
@@ -169,6 +170,7 @@ public class MojangToMapping {
|
||||
put("net.minecraft.util.datafix.DataFixers#getDataFixer()", "a");
|
||||
put("net.minecraft.util.datafix.fixes.References#ITEM_STACK", "t");
|
||||
put("net.minecraft.nbt.NbtOps#INSTANCE", "a");
|
||||
put("net.minecraft.world.item.ItemStack#CODEC", "b");
|
||||
}
|
||||
|
||||
};
|
||||
@@ -183,10 +185,38 @@ public class MojangToMapping {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static Map<String, String> MC1_21R2 = new HashMap<String, String>() {
|
||||
|
||||
{
|
||||
putAll(MC1_21R1);
|
||||
|
||||
put("net.minecraft.server.MinecraftServer#registryAccess()", "ba");
|
||||
put("net.minecraft.world.entity.Entity#getEncodeId()", "bK");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#getBlockState()", "m");
|
||||
put("net.minecraft.world.item.ItemStack#CODEC", "a");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static Map<String, String> MC1_21R3 = new HashMap<String, String>() {
|
||||
|
||||
{
|
||||
putAll(MC1_21R2);
|
||||
|
||||
put("net.minecraft.world.item.component.CustomData#copyTag()", "d");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static Map<String, String> getMapping() {
|
||||
switch (MinecraftVersion.getVersion()) {
|
||||
case MC1_21_R3:
|
||||
return MC1_21R3;
|
||||
case MC1_21_R2:
|
||||
return MC1_21R2;
|
||||
case MC1_21_R1:
|
||||
return MC1_21R1;
|
||||
case MC1_20_R4:
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</parent>
|
||||
<artifactId>item-nbt-api-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -12,7 +12,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -41,6 +41,7 @@ import de.tr7zw.nbtapi.plugin.tests.items.DirectApplyMetaTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.DirectApplyTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.EmptyItemTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemConversionTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemJsonTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemMergingTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemStackConversionTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.LegacyItemTest;
|
||||
@@ -111,6 +112,7 @@ public class NBTAPI extends JavaPlugin {
|
||||
}
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
apiTests.add(new LegacyItemTest());
|
||||
apiTests.add(new ItemJsonTest());
|
||||
}
|
||||
apiTests.add(new ComponentsTest());
|
||||
apiTests.add(new EmptyItemTest());
|
||||
|
||||
@@ -4,9 +4,9 @@ import java.util.UUID;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTGameProfile;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
|
||||
public class GameprofileTest implements Test {
|
||||
|
||||
@@ -14,9 +14,9 @@ public class GameprofileTest implements Test {
|
||||
public void test() throws Exception {
|
||||
UUID uuid = UUID.randomUUID();
|
||||
GameProfile profile = new GameProfile(uuid, "random");
|
||||
NBTCompound nbt = NBTGameProfile.toNBT(profile);
|
||||
ReadWriteNBT nbt = NBT.gameProfileToNBT(profile);
|
||||
profile = null;
|
||||
profile = NBTGameProfile.fromNBT(nbt);
|
||||
profile = NBT.gameProfileFromNBT(nbt);
|
||||
if (profile == null || !profile.getId().equals(uuid)) {
|
||||
throw new NbtApiException("Error when converting a GameProfile from/to NBT!");
|
||||
}
|
||||
|
||||
@@ -3,10 +3,10 @@ package de.tr7zw.nbtapi.plugin.tests;
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBTFile;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.NBTFileHandle;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.NBTAPI;
|
||||
|
||||
public class NBTFileTest implements Test {
|
||||
@@ -16,18 +16,18 @@ public class NBTFileTest implements Test {
|
||||
NBTAPI.getInstance().getDataFolder().mkdirs();
|
||||
File testFile = new File(NBTAPI.getInstance().getDataFolder(), "test.nbt");
|
||||
Files.deleteIfExists(testFile.toPath());
|
||||
NBTFile file = new NBTFile(testFile);
|
||||
file.addCompound("testcomp").setString("test1", "ok");
|
||||
NBTCompound comp = file.getCompound("testcomp");
|
||||
NBTFileHandle file = NBT.getFileHandle(testFile);
|
||||
file.getOrCreateCompound("testcomp").setString("test1", "ok");
|
||||
ReadWriteNBT comp = file.getOrCreateCompound("testcomp");
|
||||
if (comp == null) {
|
||||
throw new NbtApiException("Error getting compound!");
|
||||
}
|
||||
comp.setString("test2", "ok");
|
||||
file.setLong("time", System.currentTimeMillis());
|
||||
file.setString("test", "test");
|
||||
NBTCompound chunks = file.addCompound("chunks");
|
||||
NBTCompound chunk = chunks.addCompound("somechunk");
|
||||
NBTCompound block = chunk.addCompound("someblock");
|
||||
ReadWriteNBT chunks = file.getOrCreateCompound("chunks");
|
||||
ReadWriteNBT chunk = chunks.getOrCreateCompound("somechunk");
|
||||
ReadWriteNBT block = chunk.getOrCreateCompound("someblock");
|
||||
block.setString("type", "wool");
|
||||
file.save();
|
||||
|
||||
@@ -35,25 +35,25 @@ public class NBTFileTest implements Test {
|
||||
throw new NbtApiException("SubCompounds did not work!");
|
||||
}
|
||||
|
||||
NBTFile fileLoaded = new NBTFile(testFile);
|
||||
NBTFileHandle fileLoaded = NBT.getFileHandle(testFile);
|
||||
if (!fileLoaded.getString("test").equals("test")) {
|
||||
throw new NbtApiException("Wasn't able to load NBT File with the correct content!");
|
||||
}
|
||||
Files.deleteIfExists(fileLoaded.getFile().toPath());
|
||||
// String
|
||||
String str = fileLoaded.toString();
|
||||
NBTContainer rebuild = new NBTContainer(str);
|
||||
ReadWriteNBT rebuild = NBT.parseNBT(str);
|
||||
if (!str.equals(rebuild.toString())) {
|
||||
throw new NbtApiException("Wasn't able to parse NBT from a String!");
|
||||
}
|
||||
|
||||
NBTCompound dummy = new NBTContainer();
|
||||
ReadWriteNBT dummy = NBT.createNBTObject();
|
||||
dummy.setString("test1", "key1");
|
||||
NBTFile.saveTo(testFile, dummy);
|
||||
dummy = new NBTContainer();
|
||||
NBT.writeFile(testFile, dummy);
|
||||
dummy = NBT.createNBTObject();
|
||||
dummy.setString("test2", "key2");
|
||||
NBTFile.saveTo(testFile, dummy);
|
||||
dummy = NBTFile.readFrom(testFile);
|
||||
NBT.writeFile(testFile, dummy);
|
||||
dummy = NBT.readFile(testFile);
|
||||
if (dummy.hasTag("test1") || !dummy.getString("test2").equals("key2")) {
|
||||
throw new NbtApiException("Wasn't able to save NBT File with the correct content!");
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTType;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class EnumTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
NBTCompound comp = new NBTContainer();
|
||||
ReadWriteNBT comp = NBT.createNBTObject();
|
||||
comp.setEnum("test", NBTType.NBTTagEnd);
|
||||
NBTType type = comp.getEnum("test", NBTType.class);
|
||||
NBTType typeNonNull = comp.getOrNull("test", NBTType.class);
|
||||
|
||||
+18
-18
@@ -5,44 +5,44 @@ import java.util.Arrays;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class EqualsTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
NBTContainer cont = new NBTContainer();
|
||||
ReadWriteNBT cont = NBT.createNBTObject();
|
||||
cont.setString("hello", "world");
|
||||
cont.setInteger("theAnswer", 42);
|
||||
cont.addCompound("sub").setString("me", "too");
|
||||
cont.getOrCreateCompound("sub").setString("me", "too");
|
||||
cont.getStringList("somelist").addAll(Arrays.asList("a", "b", "c"));
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
NBTItem nbti = new NBTItem(item);
|
||||
NBTCompound customData = nbti.addCompound("customData");
|
||||
// reverse order
|
||||
customData.addCompound("sub").setString("me", "too");
|
||||
customData.setInteger("theAnswer", 42);
|
||||
customData.setString("hello", "world");
|
||||
customData.getStringList("somelist").addAll(Arrays.asList("a", "b", "c"));
|
||||
if (!customData.equals(cont)) {
|
||||
throw new NbtApiException("Compounds did not match! " + customData + " " + cont);
|
||||
}
|
||||
NBT.modify(item, nbti -> {
|
||||
ReadWriteNBT customData = nbti.getOrCreateCompound("customData");
|
||||
// reverse order
|
||||
customData.getOrCreateCompound("sub").setString("me", "too");
|
||||
customData.setInteger("theAnswer", 42);
|
||||
customData.setString("hello", "world");
|
||||
customData.getStringList("somelist").addAll(Arrays.asList("a", "b", "c"));
|
||||
if (!customData.equals(cont)) {
|
||||
throw new NbtApiException("Compounds did not match! " + customData + " " + cont);
|
||||
}
|
||||
});
|
||||
|
||||
// empty test
|
||||
|
||||
if (!new NBTContainer().equals(new NBTContainer())) {
|
||||
if (!NBT.createNBTObject().equals(NBT.createNBTObject())) {
|
||||
throw new NbtApiException("Two empty tags did not match!");
|
||||
}
|
||||
|
||||
// not equal test
|
||||
NBTContainer part1 = new NBTContainer();
|
||||
ReadWriteNBT part1 = NBT.createNBTObject();
|
||||
part1.setString("a", "a");
|
||||
part1.setString("b", "b");
|
||||
NBTContainer part2 = new NBTContainer();
|
||||
ReadWriteNBT part2 = NBT.createNBTObject();
|
||||
part2.setString("a", "a");
|
||||
part2.setString("b", "a");
|
||||
if (part1.equals(part2)) {
|
||||
|
||||
+14
-16
@@ -3,24 +3,22 @@ package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompoundList;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBTList;
|
||||
import de.tr7zw.changeme.nbtapi.NBTListCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTType;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTList;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class ListTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
NBTContainer comp = new NBTContainer();
|
||||
ReadWriteNBT comp = NBT.createNBTObject();
|
||||
|
||||
// Strings
|
||||
NBTList<String> list = comp.getStringList("testlist");
|
||||
ReadWriteNBTList<String> list = comp.getStringList("testlist");
|
||||
list.add("test1");
|
||||
list.add("test2");
|
||||
list.add("test3");
|
||||
@@ -32,14 +30,14 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// Compound
|
||||
NBTCompoundList taglist = comp.getCompoundList("complist");
|
||||
NBTListCompound lcomp = taglist.addCompound();
|
||||
ReadWriteNBTCompoundList taglist = comp.getCompoundList("complist");
|
||||
ReadWriteNBT lcomp = taglist.addCompound();
|
||||
lcomp.setDouble("double1", 0.3333);
|
||||
lcomp.setInteger("int1", 42);
|
||||
lcomp.setString("test1", "test1");
|
||||
lcomp.setString("test2", "test2");
|
||||
lcomp.removeKey("test1");
|
||||
NBTCompound subsubcomp = lcomp.addCompound("listsubkey");
|
||||
ReadWriteNBT subsubcomp = lcomp.getOrCreateCompound("listsubkey");
|
||||
subsubcomp.setString("deep", "String");
|
||||
subsubcomp.getCompoundList("deeplist").addCompound().setString("test", "test");
|
||||
subsubcomp.getCompoundList("clonelist").addCompound(comp);
|
||||
@@ -77,7 +75,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// Integer
|
||||
NBTList<Integer> intlist = comp.getIntegerList("inttest");
|
||||
ReadWriteNBTList<Integer> intlist = comp.getIntegerList("inttest");
|
||||
intlist.add(42);
|
||||
intlist.add(69);
|
||||
if (intlist.size() == 2 && intlist.get(0) == 42 && intlist.get(1) == 69) {
|
||||
@@ -91,7 +89,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// Double
|
||||
NBTList<Double> doublelist = comp.getDoubleList("doubletest");
|
||||
ReadWriteNBTList<Double> doublelist = comp.getDoubleList("doubletest");
|
||||
doublelist.add(42.23d);
|
||||
doublelist.add(69.69d);
|
||||
if (doublelist.size() == 2 && doublelist.get(0) == 42.23d && doublelist.get(1) == 69.69d) {
|
||||
@@ -106,7 +104,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// Float
|
||||
NBTList<Float> floatlist = comp.getFloatList("floattest");
|
||||
ReadWriteNBTList<Float> floatlist = comp.getFloatList("floattest");
|
||||
floatlist.add(42.23f);
|
||||
floatlist.add(69.69f);
|
||||
if (floatlist.size() == 2 && floatlist.get(0) == 42.23f && floatlist.get(1) == 69.69f) {
|
||||
@@ -120,7 +118,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// Long
|
||||
NBTList<Long> longlist = comp.getLongList("longtest");
|
||||
ReadWriteNBTList<Long> longlist = comp.getLongList("longtest");
|
||||
longlist.add(1241234124124l);
|
||||
longlist.add(1231454321312l);
|
||||
if (longlist.size() == 2 && longlist.get(0) == 1241234124124l && longlist.get(1) == 1231454321312l) {
|
||||
@@ -134,7 +132,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// int[]
|
||||
NBTList<int[]> intArrayList = comp.getIntArrayList("intatest");
|
||||
ReadWriteNBTList<int[]> intArrayList = comp.getIntArrayList("intatest");
|
||||
intArrayList.add(new int[] { 1, 2, 3 });
|
||||
intArrayList.add(new int[] { 4, 2, 0 });
|
||||
if (intArrayList.size() == 2 && Arrays.equals(new int[] { 1, 2, 3 }, intArrayList.get(0))
|
||||
@@ -149,7 +147,7 @@ public class ListTest implements Test {
|
||||
}
|
||||
|
||||
// UUID
|
||||
NBTList<UUID> uuidList = comp.getUUIDList("uuidtest");
|
||||
ReadWriteNBTList<UUID> uuidList = comp.getUUIDList("uuidtest");
|
||||
uuidList.add(UUID.fromString("fce0323d-7f50-4317-9720-5f6b14cf78ea"));
|
||||
uuidList.add(UUID.fromString("853c80ef-3c37-49fd-aa49-938b674adae6"));
|
||||
if (uuidList.size() == 2 && UUID.fromString("fce0323d-7f50-4317-9720-5f6b14cf78ea").equals(uuidList.get(0))
|
||||
|
||||
@@ -3,6 +3,10 @@ package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
@@ -21,6 +25,11 @@ public class StreamTest implements Test {
|
||||
if (!container.toString().equals(base.getOrCreateCompound("sub").toString())) {
|
||||
throw new NbtApiException("Component content did not match! " + base.getCompound("sub") + " " + container);
|
||||
}
|
||||
ItemStack item = new ItemStack(Material.STICK);
|
||||
|
||||
NBT.modify(item, nbt -> {
|
||||
nbt.writeCompound(outStream);
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -1,8 +1,8 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class SubCompoundsTest implements Test {
|
||||
@@ -19,9 +19,9 @@ public class SubCompoundsTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
NBTContainer cont = new NBTContainer();
|
||||
ReadWriteNBT cont = NBT.createNBTObject();
|
||||
|
||||
NBTCompound comp = cont.addCompound(COMP_TEST_KEY);
|
||||
ReadWriteNBT comp = cont.getOrCreateCompound(COMP_TEST_KEY);
|
||||
comp.setString(STRING_TEST_KEY, STRING_TEST_VALUE + "2");
|
||||
comp.setInteger(INT_TEST_KEY, INT_TEST_VALUE * 2);
|
||||
comp.setDouble(DOUBLE_TEST_KEY, DOUBLE_TEST_VALUE * 2);
|
||||
|
||||
@@ -1,19 +1,19 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTType;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class TypeTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
NBTCompound comp = new NBTContainer();
|
||||
ReadWriteNBT comp = NBT.createNBTObject();
|
||||
comp.setString("s", "test");
|
||||
comp.setInteger("i", 42);
|
||||
comp.addCompound("c");
|
||||
comp.getOrCreateCompound("c");
|
||||
if (comp.getType("s") != NBTType.NBTTagString || comp.getType("i") != NBTType.NBTTagInt
|
||||
|| comp.getType("c") != NBTType.NBTTagCompound)
|
||||
throw new NbtApiException("One parsed type did not match what it should have been!");
|
||||
|
||||
+11
-10
@@ -6,8 +6,7 @@ import org.bukkit.entity.Animals;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Monster;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTEntity;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
@@ -23,14 +22,16 @@ public class EntityCustomNbtPersistentTest implements Test {
|
||||
try {
|
||||
if (!world.getEntitiesByClasses(Animals.class, Monster.class).isEmpty()) {
|
||||
Entity ent = world.getEntitiesByClasses(Animals.class, Monster.class).iterator().next();
|
||||
NBTEntity nbtEnt = new NBTEntity(ent);
|
||||
NBTCompound comp = nbtEnt.getPersistentDataContainer();
|
||||
comp.setString("Hello", "World");
|
||||
NBTEntity nbtent = new NBTEntity(ent);
|
||||
if (!nbtent.toString().contains("Hello:\"World\"")) {
|
||||
throw new NbtApiException("Custom Data did not save to the Entity!");
|
||||
}
|
||||
comp.removeKey("Hello");
|
||||
NBT.modifyPersistentData(ent, comp -> {
|
||||
comp.setString("Hello", "World");
|
||||
});
|
||||
|
||||
NBT.modifyPersistentData(ent, comp -> {
|
||||
if (!comp.toString().contains("Hello:\"World\"")) {
|
||||
throw new NbtApiException("Custom Data did not save to the Entity!");
|
||||
}
|
||||
comp.removeKey("Hello");
|
||||
});
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.items;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.NBTJsonUtil;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class ItemJsonTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName("test");
|
||||
meta.setUnbreakable(true);
|
||||
item.setItemMeta(meta);
|
||||
JsonElement elem = NBTJsonUtil.itemStackToJson(item);
|
||||
if (elem == null) {
|
||||
throw new NbtApiException("Getting the Json didn't work correctly! " + item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+4
-4
@@ -3,9 +3,9 @@ package de.tr7zw.nbtapi.plugin.tests.items;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class ItemStackConversionTest implements Test {
|
||||
@@ -14,8 +14,8 @@ public class ItemStackConversionTest implements Test {
|
||||
public void test() throws Exception {
|
||||
ItemStack[] src = new ItemStack[] { new ItemStack(Material.STONE), new ItemStack(Material.STICK),
|
||||
new ItemStack(Material.AIR), new ItemStack(Material.STONE) };
|
||||
NBTCompound comp = NBTItem.convertItemArraytoNBT(src);
|
||||
ItemStack[] recreated = NBTItem.convertNBTtoItemArray(comp);
|
||||
ReadWriteNBT comp = NBT.itemStackArrayToNBT(src);
|
||||
ItemStack[] recreated = NBT.itemStackArrayFromNBT(comp);
|
||||
if (recreated == null || src.length != recreated.length) {
|
||||
throw new NbtApiException("Size did not match!");
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTTileEntity;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
@@ -24,6 +25,14 @@ public class TileTest implements Test {
|
||||
world.getSpawnLocation().getBlockZ());
|
||||
if (world.isChunkLoaded(block.getX() >> 4, block.getZ() >> 4) && block.getType() == Material.AIR) {
|
||||
block.setType(Material.CHEST);
|
||||
if (MinecraftVersion.isNewerThan(MinecraftVersion.MC1_21_R1)) {
|
||||
// 1.21 changed the lock logic. So just try to get/set data, dont check for now
|
||||
NBT.modify(block.getState(), nbt -> {
|
||||
nbt.setString("foo", "bar");
|
||||
});
|
||||
block.setType(Material.AIR);
|
||||
return;
|
||||
}
|
||||
NBTTileEntity tile = new NBTTileEntity(block.getState());
|
||||
if (!MinecraftVersion.isNewerThan(MinecraftVersion.MC1_17_R1)) {
|
||||
if (tile.getInteger("y") != 254) {
|
||||
|
||||
+11
-10
@@ -5,8 +5,7 @@ import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Block;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTTileEntity;
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
@@ -27,14 +26,16 @@ public class TilesCustomNBTPersistentTest implements Test {
|
||||
world.getSpawnLocation().getBlockZ());
|
||||
if (world.isChunkLoaded(block.getX() >> 4, block.getZ() >> 4) && block.getType() == Material.AIR) {
|
||||
block.setType(Material.CHEST);
|
||||
NBTTileEntity comp = new NBTTileEntity(block.getState());
|
||||
NBTCompound persistentData = comp.getPersistentDataContainer();
|
||||
persistentData.setString("Foo", "Bar");
|
||||
if (!new NBTTileEntity(block.getState()).getPersistentDataContainer().getString("Foo")
|
||||
.equals("Bar")) {
|
||||
block.setType(Material.AIR);
|
||||
throw new NbtApiException("Custom Data did not save to the Tile!");
|
||||
}
|
||||
NBT.modifyPersistentData(block.getState(), persistentData -> {
|
||||
persistentData.setString("Foo", "Bar");
|
||||
});
|
||||
NBT.getPersistentData(block.getState(), persistentData -> {
|
||||
if (!persistentData.getString("Foo").equals("Bar")) {
|
||||
block.setType(Material.AIR);
|
||||
throw new NbtApiException("Custom Data did not save to the Tile!");
|
||||
}
|
||||
return null;
|
||||
});
|
||||
block.setType(Material.AIR);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</parent>
|
||||
<artifactId>mappings-parser</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -12,7 +12,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -24,7 +24,7 @@ public class MappingsParser {
|
||||
public static StringBuilder builder = new StringBuilder();
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
File input = new File("minecraft_server.1.21.txt");
|
||||
File input = new File("minecraft_server.1.21.4.txt");
|
||||
List<String> lines = Files.readAllLines(input.toPath());
|
||||
|
||||
Map<String, ClassWrapper> classes = new HashMap<>();
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>nbt-data-api</artifactId>
|
||||
@@ -14,7 +14,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</parent>
|
||||
<artifactId>nbt-injector</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -12,7 +12,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
<packaging>pom</packaging>
|
||||
<inceptionYear>2015</inceptionYear>
|
||||
<organization>
|
||||
@@ -110,7 +110,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.8.0</version>
|
||||
<version>3.11.1</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
</configuration>
|
||||
|
||||
@@ -112,7 +112,7 @@ Just use the Plugin as Maven dependency and remember to add "NBTAPI" as dependen
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api-plugin</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
[/CODE]
|
||||
[/SPOILER]
|
||||
@@ -122,7 +122,7 @@ Using this method the API gets copied into your plugin. Server owners don't have
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.13.2</version>
|
||||
<version>2.14.1</version>
|
||||
</dependency>
|
||||
...
|
||||
<plugins>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
- [CommandAPI](https://github.com/JorelAli/CommandAPI)
|
||||
- [SkBee](https://github.com/ShaneBeee/SkBee/)
|
||||
- [Monumenta](https://github.com/TeamMonumenta/monumenta-plugins-public/)
|
||||
- [SummonFriendlyMobs](https://github.com/ArmyFury/SummonFriendlyMobs)
|
||||
|
||||
___
|
||||
|
||||
|
||||
+55
-15
@@ -2,7 +2,7 @@
|
||||
|
||||
### Basics overview
|
||||
|
||||
Most of the things are exposed inside the [NBT](https://tr7zw.github.io/Item-NBT-API/v2-api/de/tr7zw/changeme/nbtapi/NBT.html) class. Make sure to explore the Javadoc, or ask your IDE for suggestions!
|
||||
Most of the things are exposed inside the [NBT](https://tr7zw.github.io/Item-NBT-API/v2-api/de/tr7zw/changeme/nbtapi/NBT.html) class. Your specific use-case might not yet be covered by the wiki, so make sure to explore the [Javadoc](https://tr7zw.github.io/Item-NBT-API/v2-api/), or ask your IDE for suggestions!
|
||||
|
||||
#### Basic getting and setting of data
|
||||
|
||||
@@ -115,9 +115,13 @@ nbt.resolveOrDefault("list_key[-1]", "fallback_value"); // Get the last value in
|
||||
|
||||
|
||||
// NBT compound lists
|
||||
// Get the list (will create it if needed)
|
||||
ReadWriteNBTCompoundList nbtList = nbt.getOrCreateCompound("foo").getCompoundList("other_key");
|
||||
ReadWriteNBT nbtListEntry = addCompound();
|
||||
// Add new compound to the list
|
||||
ReadWriteNBT nbtListEntry = nbtList.addCompound();
|
||||
nbtListEntry.setBoolean("bar", true);
|
||||
// Add existing nbt compound to the list
|
||||
ReadWriteNBT otherNbtListEntry = nbtList.addCompound(NBT.parseNBT("{foo_bar:1b}"));
|
||||
|
||||
// You can fetch compounds in lists
|
||||
nbt.resolveCompound("foo.other_key[0]"); // Get the first compound in list, or null
|
||||
@@ -162,6 +166,27 @@ int someValue = NBT.modify(itemStack, nbt -> {
|
||||
|
||||
> [!WARNING]
|
||||
> Never mix the usage of ItemMeta and NBT.
|
||||
> Setting ItemMeta will override any changes made to item's nbt.
|
||||
|
||||
```java
|
||||
// WRONG, do not do this!
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
meta.setDisplayName("Modified!");
|
||||
NBT.modify(itemStack, nbt -> nbt.setBoolean("modified", true));
|
||||
itemStack.setItemMeta(meta); // Will undo all changes to nbt!
|
||||
|
||||
|
||||
// Correct, both ItemMeta and NBT will be applied
|
||||
NBT.modify(itemStack, nbt -> nbt.setBoolean("modified", true));
|
||||
// Taking ItemMeta snapshot after changes to nbt
|
||||
ItemMeta meta = itemStack.getItemMeta();
|
||||
meta.setDisplayName("Modified!");
|
||||
itemStack.setItemMeta(meta);
|
||||
// If making any further changes with NBT & ItemMeta,
|
||||
// re-take the item's ItemMeta after changing the nbt!
|
||||
```
|
||||
|
||||
Alternatively, you may safely modify ItemMeta inside the NBT scope:
|
||||
|
||||
```java
|
||||
// Updating ItemMeta using NBT
|
||||
@@ -184,6 +209,15 @@ NBT.modify(itemStack, nbt -> {
|
||||
|
||||
```java
|
||||
// NOTE: This code is only for 1.20.5+!
|
||||
|
||||
// Only reading vanilla data
|
||||
NBT.getComponents(item, nbt -> {
|
||||
if (nbt.hasTag("minecraft:custom_name")) {
|
||||
String customName = nbt.getString("minecraft:custom_name");
|
||||
}
|
||||
});
|
||||
|
||||
// Modifying vanilla data
|
||||
NBT.modifyComponents(item, nbt -> {
|
||||
nbt.setString("minecraft:custom_name", "{\"extra\":[\"foobar\"],\"text\":\"\"}");
|
||||
});
|
||||
@@ -271,20 +305,26 @@ Moreover, since the data is stored inside a Chunk, this will increase the chunk'
|
||||
|
||||
### NBT files
|
||||
|
||||
```java
|
||||
// Creating nbt file
|
||||
// NBTFile will automatically create the file if it does not exist
|
||||
NBTFile file = new NBTFile(new File("directory"), "test.nbt"); // Will be created at ./directory/test.nbt
|
||||
// Setting data
|
||||
file.setString("foo", "bar");
|
||||
// Saving file
|
||||
file.save();
|
||||
NBTFileHandle allows working with nbt files.
|
||||
|
||||
// Alternatively, you may use helper methods in NBTFile class
|
||||
// To read the file without creating it if does not exist:
|
||||
ReadWriteNBT nbt = NBTFile.readFrom(file);
|
||||
// To save nbt to the file:
|
||||
NBTFile.saveTo(file, nbt);
|
||||
```java
|
||||
// Will automatically create the file if it does not exist
|
||||
NBTFileHandle nbtFile = NBT.getFileHandle(new File("directory", "test.nbt"));
|
||||
// Setting data
|
||||
nbtFile.setString("foo", "bar");
|
||||
// Saving the file after applying changes
|
||||
nbtFile.save();
|
||||
```
|
||||
|
||||
Alternatively, you may read the nbt file without maintaining the link to it.
|
||||
Unlike NBTFileHandle, the file won't be automatically created if it does not exist.
|
||||
|
||||
```java
|
||||
File file = new File("directory", "test.nbt");
|
||||
// Reading data from file (will return an empty compound if the file does not exist)
|
||||
ReadWriteNBT nbt = NBT.readFile(file);
|
||||
// Saving nbt to file
|
||||
NBT.writeFile(file, nbt);
|
||||
```
|
||||
|
||||
### Converting Minecraft Objects to NBT and Strings
|
||||
|
||||
Reference in new issue
Block a user