Compare commits
73
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c0a9197113 | ||
|
|
e14dd4f82a | ||
|
|
198dbfabae | ||
|
|
8bd49db49b | ||
|
|
caf1d7cd98 | ||
|
|
027f2a2edd | ||
|
|
b74b0b852e | ||
|
|
6ea6ab7e13 | ||
|
|
372225626c | ||
|
|
2483f9b947 | ||
|
|
fdc16aad69 | ||
|
|
c723eef68b | ||
|
|
03299012ac | ||
|
|
eabf0a9696 | ||
|
|
9367942b54 | ||
|
|
167487e6ab | ||
|
|
6395dd1964 | ||
|
|
d3ef690f2e | ||
|
|
012f32dca9 | ||
|
|
0b8d5b153c | ||
|
|
246e3fa38e | ||
|
|
19777de6d4 | ||
|
|
3afd9df2ae | ||
|
|
2cb16ed0c4 | ||
|
|
ac64acf271 | ||
|
|
85cc13a817 | ||
|
|
75c8575b5b | ||
|
|
67557d5f94 | ||
|
|
330fb7711c | ||
|
|
e7f48c72bd | ||
|
|
3eefb1d315 | ||
|
|
a37a2c6729 | ||
|
|
e42fad0761 | ||
|
|
eee2dfa181 | ||
|
|
4728449294 | ||
|
|
ccd78d1b00 | ||
|
|
83bea69710 | ||
|
|
87fd1ee8ee | ||
|
|
af0528328f | ||
|
|
86c802e2db | ||
|
|
7bf91d0510 | ||
|
|
aed5af12a2 | ||
|
|
dd6e9e8312 | ||
|
|
42871b6c06 | ||
|
|
4ad7a172a0 | ||
|
|
6f112c0130 | ||
|
|
5a948f017a | ||
|
|
f5a1b21761 | ||
|
|
4af722752e | ||
|
|
af3b7a49d0 | ||
|
|
189ff2f369 | ||
|
|
ddf8ab3c9b | ||
|
|
a8a81eee83 | ||
|
|
fb384784f0 | ||
|
|
256abbc500 | ||
|
|
dc333ae25a | ||
|
|
d18adb459d | ||
|
|
bc914b73bc | ||
|
|
60172ea2a7 | ||
|
|
7b30c1533e | ||
|
|
a1c90658f3 | ||
|
|
0b0fdeaa60 | ||
|
|
d989797453 | ||
|
|
c70aefe670 | ||
|
|
7d0fc09470 | ||
|
|
427113e6f0 | ||
|
|
20ae1cb178 | ||
|
|
83b011fafa | ||
|
|
3affeee151 | ||
|
|
06da2114ad | ||
|
|
a3efdc622b | ||
|
|
9b60978af6 | ||
|
|
598db19d8d |
No files matched your search
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 - 2021, tr7zw
|
||||
Copyright (c) 2015 - 2024, tr7zw
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
@@ -24,3 +24,12 @@ Import the API [using Maven](https://github.com/tr7zw/Item-NBT-API/wiki/Using-Ma
|
||||
### Github Stargazers
|
||||
|
||||
<img src="https://starchart.cc/tr7zw/Item-NBT-API.svg" alt="stargazers" width="800"/>
|
||||
|
||||
### Supported by YourKit
|
||||
|
||||

|
||||
YourKit supports open source projects with innovative and intelligent tools
|
||||
for monitoring and profiling Java and .NET applications.
|
||||
YourKit is the creator of <a href="https://www.yourkit.com/java/profiler/">YourKit Java Profiler</a>,
|
||||
<a href="https://www.yourkit.com/.net/profiler/">YourKit .NET Profiler</a>,
|
||||
and <a href="https://www.yourkit.com/youmonitor/">YourKit YouMonitor</a>.
|
||||
+11
-2
@@ -5,12 +5,21 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.mojang</groupId>
|
||||
<artifactId>datafixerupper</artifactId>
|
||||
<version>4.0.26</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<defaultGoal>clean install javadoc:javadoc</defaultGoal>
|
||||
<resources>
|
||||
@@ -23,7 +32,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<version>3.5.3</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||
<transformers>
|
||||
|
||||
@@ -19,6 +19,7 @@ import de.tr7zw.changeme.nbtapi.iface.ReadableNBT;
|
||||
import de.tr7zw.changeme.nbtapi.utils.CheckUtil;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.changeme.nbtapi.utils.PathUtil;
|
||||
import de.tr7zw.changeme.nbtapi.utils.PathUtil.PathSegment;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.Forge1710Mappings;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
|
||||
|
||||
@@ -102,7 +103,7 @@ public class NBTCompound implements ReadWriteNBT {
|
||||
}
|
||||
if (!NBTReflectionUtil.valideCompound(this))
|
||||
throw new NbtApiException("The Compound wasn't able to be linked back to the root!");
|
||||
Object workingtag = NBTReflectionUtil.gettoCompount(rootnbttag, this);
|
||||
Object workingtag = NBTReflectionUtil.getToCompount(rootnbttag, this);
|
||||
if (readOnly) {
|
||||
this.readOnlyCache = workingtag;
|
||||
}
|
||||
@@ -195,10 +196,6 @@ public class NBTCompound implements ReadWriteNBT {
|
||||
}
|
||||
}
|
||||
|
||||
protected String getContent(String key) {
|
||||
return NBTReflectionUtil.getContent(this, key);
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter
|
||||
*
|
||||
@@ -1063,38 +1060,205 @@ public class NBTCompound implements ReadWriteNBT {
|
||||
|
||||
@Override
|
||||
public <T> T resolveOrNull(String key, Class<?> type) {
|
||||
List<String> keys = PathUtil.splitPath(key);
|
||||
List<PathSegment> keys = PathUtil.splitPath(key);
|
||||
NBTCompound tag = this;
|
||||
for (int i = 0; i < keys.size() - 1; i++) {
|
||||
tag = tag.getCompound(keys.get(i));
|
||||
if (tag == null) {
|
||||
return null;
|
||||
PathSegment segment = keys.get(i);
|
||||
if (!segment.hasIndex()) {
|
||||
tag = tag.getCompound(segment.getPath());
|
||||
if (tag == null) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
if (tag.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& tag.getListType(segment.getPath()) == NBTType.NBTTagCompound) {
|
||||
NBTCompoundList list = tag.getCompoundList(segment.getPath());
|
||||
if (segment.getIndex() >= 0) {
|
||||
tag = list.get(segment.getIndex());
|
||||
} else {
|
||||
tag = list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tag.getOrNull(keys.get(keys.size() - 1), type);
|
||||
PathSegment segment = keys.get(keys.size() - 1);
|
||||
if (!segment.hasIndex()) {
|
||||
return tag.getOrNull(segment.getPath(), type);
|
||||
} else {
|
||||
return getIndexedValue(tag, segment, (Class<T>)type);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T resolveOrDefault(String key, T defaultValue) {
|
||||
List<String> keys = PathUtil.splitPath(key);
|
||||
List<PathSegment> keys = PathUtil.splitPath(key);
|
||||
NBTCompound tag = this;
|
||||
for (int i = 0; i < keys.size() - 1; i++) {
|
||||
tag = tag.getCompound(keys.get(i));
|
||||
if (tag == null) {
|
||||
return defaultValue;
|
||||
PathSegment segment = keys.get(i);
|
||||
if (!segment.hasIndex()) {
|
||||
tag = tag.getCompound(segment.getPath());
|
||||
if (tag == null) {
|
||||
return defaultValue;
|
||||
}
|
||||
} else {
|
||||
if (tag.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& tag.getListType(segment.getPath()) == NBTType.NBTTagCompound) {
|
||||
NBTCompoundList list = tag.getCompoundList(segment.getPath());
|
||||
if (segment.getIndex() >= 0) {
|
||||
tag = list.get(segment.getIndex());
|
||||
} else {
|
||||
tag = list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tag.getOrDefault(keys.get(keys.size() - 1), defaultValue);
|
||||
PathSegment segment = keys.get(keys.size() - 1);
|
||||
if (!segment.hasIndex()) {
|
||||
return tag.getOrDefault(segment.getPath(), defaultValue);
|
||||
} else {
|
||||
return getIndexedValue(tag, segment, (Class<T>)defaultValue.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: before I'm even done writing this method, this sucks. Needs refactoring at some point
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getIndexedValue(NBTCompound comp, PathSegment segment, Class<T> type) {
|
||||
if (type == String.class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagString) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getStringList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<String> list = comp.getStringList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == int.class || type == Integer.class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagInt) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getIntegerList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<Integer> list = comp.getIntegerList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
} else if(comp.getType(segment.getPath()) == NBTType.NBTTagIntArray) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
int[] array = comp.getIntArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Integer) array[segment.getIndex()];
|
||||
}
|
||||
} else {
|
||||
int[] array = comp.getIntArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Integer) array[array.length + segment.getIndex()];
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == long.class || type == Long.class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagLong) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getLongList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<Long> list = comp.getLongList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
} else if(comp.getType(segment.getPath()) == NBTType.NBTTagLongArray) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
long[] array = comp.getLongArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Long) array[segment.getIndex()];
|
||||
}
|
||||
} else {
|
||||
long[] array = comp.getLongArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Long) array[array.length + segment.getIndex()];
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == float.class || type == Float.class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagFloat) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getFloatList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<Float> list = comp.getFloatList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == double.class || type == Double.class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagDouble) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getDoubleList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<Double> list = comp.getDoubleList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == int[].class) {
|
||||
if (comp.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& comp.getListType(segment.getPath()) == NBTType.NBTTagIntArray) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
return (T) comp.getIntArrayList(segment.getPath()).get(segment.getIndex());
|
||||
} else {
|
||||
List<int[]> list = comp.getIntArrayList(segment.getPath());
|
||||
return (T) list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
if (type == byte.class || type == Byte.class) {
|
||||
if(comp.getType(segment.getPath()) == NBTType.NBTTagByteArray) {
|
||||
if (segment.getIndex() >= 0) {
|
||||
byte[] array = comp.getByteArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Byte) array[segment.getIndex()];
|
||||
}
|
||||
} else {
|
||||
byte[] array = comp.getByteArray(segment.getPath());
|
||||
if(array != null) {
|
||||
return (T)(Byte) array[array.length + segment.getIndex()];
|
||||
}
|
||||
}
|
||||
}
|
||||
throw new NbtApiException("No fitting list/array found for " + segment.getPath() + " of type " + type);
|
||||
}
|
||||
throw new NbtApiException("Unable to get indexed value for type " + type);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ReadWriteNBT resolveCompound(String key) {
|
||||
List<String> keys = PathUtil.splitPath(key);
|
||||
List<PathSegment> keys = PathUtil.splitPath(key);
|
||||
NBTCompound tag = this;
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
tag = tag.getCompound(keys.get(i));
|
||||
if (tag == null) {
|
||||
return null;
|
||||
PathSegment segment = keys.get(i);
|
||||
if (!segment.hasIndex()) {
|
||||
tag = tag.getCompound(segment.getPath());
|
||||
if (tag == null) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
if (tag.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& tag.getListType(segment.getPath()) == NBTType.NBTTagCompound) {
|
||||
NBTCompoundList list = tag.getCompoundList(segment.getPath());
|
||||
if (segment.getIndex() >= 0) {
|
||||
tag = list.get(segment.getIndex());
|
||||
} else {
|
||||
tag = list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tag;
|
||||
@@ -1102,12 +1266,25 @@ public class NBTCompound implements ReadWriteNBT {
|
||||
|
||||
@Override
|
||||
public ReadWriteNBT resolveOrCreateCompound(String key) {
|
||||
List<String> keys = PathUtil.splitPath(key);
|
||||
List<PathSegment> keys = PathUtil.splitPath(key);
|
||||
NBTCompound tag = this;
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
tag = tag.getOrCreateCompound(keys.get(i));
|
||||
if (tag == null) {
|
||||
return null;
|
||||
PathSegment segment = keys.get(i);
|
||||
if (!segment.hasIndex()) {
|
||||
tag = tag.getOrCreateCompound(segment.getPath());
|
||||
if (tag == null) {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
if (tag.getType(segment.getPath()) == NBTType.NBTTagList
|
||||
&& tag.getListType(segment.getPath()) == NBTType.NBTTagCompound) {
|
||||
NBTCompoundList list = tag.getCompoundList(segment.getPath());
|
||||
if (segment.getIndex() >= 0) {
|
||||
tag = list.get(segment.getIndex());
|
||||
} else {
|
||||
tag = list.get(list.size() + segment.getIndex());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return tag;
|
||||
|
||||
@@ -35,7 +35,7 @@ public class NBTContainer extends NBTCompound {
|
||||
public NBTContainer(Object nbt) {
|
||||
super(null, null);
|
||||
if (nbt == null) {
|
||||
throw new NullPointerException("The NBT-Object can't be null!");
|
||||
nbt = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance();
|
||||
}
|
||||
if (!ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().isAssignableFrom(nbt.getClass())) {
|
||||
throw new NbtApiException("The object '" + nbt.getClass() + "' is not a valid NBT-Object!");
|
||||
|
||||
@@ -2,6 +2,8 @@ package de.tr7zw.changeme.nbtapi;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.utils.GameprofileUtil;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ObjectCreator;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
|
||||
|
||||
@@ -14,6 +16,9 @@ public class NBTGameProfile {
|
||||
* @return A NBTContainer with all the GameProfile data
|
||||
*/
|
||||
public static NBTCompound toNBT(GameProfile profile) {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return (NBTCompound) GameprofileUtil.writeGameProfile(NBT.createNBTObject(), profile);
|
||||
}
|
||||
return new NBTContainer(ReflectionMethod.GAMEPROFILE_SERIALIZE.run(null,
|
||||
ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance(), profile));
|
||||
}
|
||||
@@ -25,8 +30,11 @@ public class NBTGameProfile {
|
||||
* @return The reconstructed GameProfile
|
||||
*/
|
||||
public static GameProfile fromNBT(NBTCompound compound) {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return GameprofileUtil.readGameProfile(compound);
|
||||
}
|
||||
return (GameProfile) ReflectionMethod.GAMEPROFILE_DESERIALIZE.run(null,
|
||||
NBTReflectionUtil.gettoCompount(compound.getCompound(), compound));
|
||||
NBTReflectionUtil.getToCompount(compound.getCompound(), compound));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import org.bukkit.inventory.meta.ItemMeta;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteItemNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
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;
|
||||
|
||||
@@ -130,11 +131,11 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
}
|
||||
if (ClassWrapper.CRAFT_ITEMSTACK.getClazz().isAssignableFrom(originalSrcStack.getClass())) {
|
||||
Object nmsStack = NBTReflectionUtil.getCraftItemHandle(originalSrcStack);
|
||||
ReflectionMethod.ITEMSTACK_SET_TAG.run(nmsStack, cachedCompound);
|
||||
NBTReflectionUtil.setItemStackCompound(nmsStack, cachedCompound);
|
||||
bukkitItem = originalSrcStack;
|
||||
} else {
|
||||
Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem);
|
||||
ReflectionMethod.ITEMSTACK_SET_TAG.run(stack, cachedCompound);
|
||||
NBTReflectionUtil.setItemStackCompound(stack, cachedCompound);
|
||||
bukkitItem = (ItemStack) ReflectionMethod.ITEMSTACK_BUKKITMIRROR.run(null, stack);
|
||||
originalSrcStack.setItemMeta(bukkitItem.getItemMeta());
|
||||
}
|
||||
@@ -168,10 +169,10 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
}
|
||||
if (ClassWrapper.CRAFT_ITEMSTACK.getClazz().isAssignableFrom(bukkitItem.getClass())) {
|
||||
Object nmsStack = NBTReflectionUtil.getCraftItemHandle(bukkitItem);
|
||||
ReflectionMethod.ITEMSTACK_SET_TAG.run(nmsStack, compound);
|
||||
NBTReflectionUtil.setItemStackCompound(nmsStack, compound);
|
||||
} else {
|
||||
Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem);
|
||||
ReflectionMethod.ITEMSTACK_SET_TAG.run(stack, compound);
|
||||
NBTReflectionUtil.setItemStackCompound(stack, compound);
|
||||
bukkitItem = (ItemStack) ReflectionMethod.ITEMSTACK_BUKKITMIRROR.run(null, stack);
|
||||
}
|
||||
}
|
||||
@@ -210,10 +211,18 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
*
|
||||
* @param item ItemStack that should get the new NBT data
|
||||
*/
|
||||
@Deprecated
|
||||
public void mergeCustomNBT(ItemStack item) {
|
||||
if (item == null || item.getType() == Material.AIR) {
|
||||
throw new NullPointerException("ItemStack can't be null/Air!");
|
||||
}
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
// 1.20.5+ doesn't have any vanilla tags
|
||||
NBT.modify(item, nbt -> {
|
||||
nbt.mergeCompound(this);
|
||||
});
|
||||
return;
|
||||
}
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
NBTReflectionUtil.getUnhandledNBTTags(meta)
|
||||
.putAll(NBTReflectionUtil.getUnhandledNBTTags(bukkitItem.getItemMeta()));
|
||||
@@ -225,7 +234,12 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
*
|
||||
* @return true when custom tags are present
|
||||
*/
|
||||
@Deprecated
|
||||
public boolean hasCustomNbtData() {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
// 1.20.5+ doesn't have any vanilla tags
|
||||
return hasNBTData();
|
||||
}
|
||||
finalizeChanges();
|
||||
ItemMeta meta = bukkitItem.getItemMeta();
|
||||
return !NBTReflectionUtil.getUnhandledNBTTags(meta).isEmpty();
|
||||
@@ -234,8 +248,14 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
/**
|
||||
* Remove all custom (non-vanilla) NBT tags from the NBTItem.
|
||||
*/
|
||||
@Deprecated
|
||||
public void clearCustomNBT() {
|
||||
finalizeChanges();
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
// 1.20.5+ doesn't have any vanilla tags
|
||||
setCompound(null);
|
||||
return;
|
||||
}
|
||||
ItemMeta meta = bukkitItem.getItemMeta();
|
||||
NBTReflectionUtil.getUnhandledNBTTags(meta).clear();
|
||||
bukkitItem.setItemMeta(meta);
|
||||
@@ -254,7 +274,8 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the item has NBT data.
|
||||
* Returns true if the item has NBT data. This needs to be checked before
|
||||
* calling methods like remove, otherwise the value might be wrong!
|
||||
*
|
||||
* @return Does the ItemStack have a NBTCompound.
|
||||
*/
|
||||
@@ -279,6 +300,9 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
|
||||
bukkitItem.setItemMeta(meta);
|
||||
updateCachedCompound();
|
||||
if (directApply) {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
throw new NbtApiException("Direct apply mode meta changes don't work anymore in 1.20.5+. Please switch to the modern NBT.modify sytnax!");
|
||||
}
|
||||
applyNBT(originalSrcStack);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,21 +6,23 @@ import java.io.OutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.BlockState;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil;
|
||||
import de.tr7zw.changeme.nbtapi.utils.GsonWrapper;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ClassWrapper;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.MojangToMapping;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ObjectCreator;
|
||||
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
|
||||
|
||||
@@ -36,6 +38,8 @@ public class NBTReflectionUtil {
|
||||
|
||||
private static Field field_unhandledTags = null;
|
||||
private static Field field_handle = null;
|
||||
private static Object type_custom_data = null;
|
||||
private static Object registry_access = null;
|
||||
|
||||
static {
|
||||
try {
|
||||
@@ -50,6 +54,21 @@ public class NBTReflectionUtil {
|
||||
} catch (NoSuchFieldException e) {
|
||||
|
||||
}
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
try {
|
||||
Field typeField = ClassWrapper.NMS_DATACOMPONENTS.getClazz().getDeclaredField(
|
||||
MojangToMapping.getMapping().get("net.minecraft.core.component.DataComponents#CUSTOM_DATA"));
|
||||
type_custom_data = typeField.get(null);
|
||||
} catch (NoSuchFieldException | IllegalArgumentException | IllegalAccessException e) {
|
||||
|
||||
}
|
||||
try {
|
||||
Object nmsServer = ReflectionMethod.NMSSERVER_GETSERVER.run(Bukkit.getServer());
|
||||
registry_access = ReflectionMethod.NMSSERVER_GETREGISTRYACCESS.run(nmsServer);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,7 +100,12 @@ public class NBTReflectionUtil {
|
||||
*/
|
||||
public static Object readNBT(InputStream stream) {
|
||||
try {
|
||||
return ReflectionMethod.NBTFILE_READ.run(null, stream);
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R3)) {
|
||||
return ReflectionMethod.NBTFILE_READV2.run(null, stream,
|
||||
ReflectionMethod.NBTACCOUNTER_CREATE_UNLIMITED.run(null));
|
||||
} else {
|
||||
return ReflectionMethod.NBTFILE_READ.run(null, stream);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
try {
|
||||
stream.close();
|
||||
@@ -145,13 +169,40 @@ public class NBTReflectionUtil {
|
||||
*/
|
||||
public static Object getItemRootNBTTagCompound(Object nmsitem) {
|
||||
try {
|
||||
Object answer = ReflectionMethod.NMSITEM_GETTAG.run(nmsitem);
|
||||
return answer;
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
Object customData = ReflectionMethod.NMSDATACOMPONENTHOLDER_GET.run(nmsitem, type_custom_data);
|
||||
if (customData == null) {
|
||||
return null;
|
||||
}
|
||||
return ReflectionMethod.NMSCUSTOMDATA_GETCOPY.run(customData);
|
||||
} else {
|
||||
Object answer = ReflectionMethod.NMSITEM_GETTAG.run(nmsitem);
|
||||
return answer;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new NbtApiException("Exception while getting an Itemstack's NBTCompound!", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Compound as the Items NBT or CustomData
|
||||
*
|
||||
* @param nmsItem
|
||||
* @param compound
|
||||
*/
|
||||
public static void setItemStackCompound(Object nmsItem, Object compound) {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
if (compound == null) {
|
||||
ReflectionMethod.NMSITEM_SET.run(nmsItem, new Object[] { type_custom_data, null });
|
||||
} else {
|
||||
ReflectionMethod.NMSITEM_SET.run(nmsItem, type_custom_data,
|
||||
ObjectCreator.NMS_CUSTOMDATA.getInstance(compound));
|
||||
}
|
||||
} else {
|
||||
ReflectionMethod.ITEMSTACK_SET_TAG.run(nmsItem, compound);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Converts {@link NBTCompound} to NMS ItemStacks
|
||||
*
|
||||
@@ -160,8 +211,13 @@ public class NBTReflectionUtil {
|
||||
*/
|
||||
public static Object convertNBTCompoundtoNMSItem(NBTCompound nbtcompound) {
|
||||
try {
|
||||
Object nmsComp = gettoCompount(nbtcompound.getCompound(), nbtcompound);
|
||||
if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_11_R1.getVersionId()) {
|
||||
Object nmsComp = getToCompount(nbtcompound.getCompound(), nbtcompound);
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
if(nbtcompound.hasTag("tag")) {
|
||||
nmsComp = DataFixerUtil.fixUpRawItemData(nmsComp, DataFixerUtil.VERSION1_20_4, DataFixerUtil.getCurrentVersion());
|
||||
}
|
||||
return ReflectionMethod.NMSITEM_LOAD.run(null, registry_access, nmsComp);
|
||||
} else if (MinecraftVersion.getVersion().getVersionId() >= MinecraftVersion.MC1_11_R1.getVersionId()) {
|
||||
return ObjectCreator.NMS_COMPOUNDFROMITEM.getInstance(nmsComp);
|
||||
} else {
|
||||
return ReflectionMethod.NMSITEM_CREATESTACK.run(null, nmsComp);
|
||||
@@ -179,8 +235,13 @@ public class NBTReflectionUtil {
|
||||
*/
|
||||
public static NBTContainer convertNMSItemtoNBTCompound(Object nmsitem) {
|
||||
try {
|
||||
Object answer = ReflectionMethod.NMSITEM_SAVE.run(nmsitem, ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance());
|
||||
return new NBTContainer(answer);
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return new NBTContainer(ReflectionMethod.NMSITEM_SAVE_MODERN.run(nmsitem, registry_access));
|
||||
} else {
|
||||
Object answer = ReflectionMethod.NMSITEM_SAVE.run(nmsitem,
|
||||
ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance());
|
||||
return new NBTContainer(answer);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new NbtApiException("Exception while converting NMS ItemStack to NBTCompound!", e);
|
||||
}
|
||||
@@ -193,6 +254,7 @@ public class NBTReflectionUtil {
|
||||
* @return Map containing unhandled (custom) NBT tags
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@Deprecated
|
||||
public static Map<String, Object> getUnhandledNBTTags(ItemMeta meta) {
|
||||
try {
|
||||
return (Map<String, Object>) field_unhandledTags.get(meta);
|
||||
@@ -255,7 +317,9 @@ public class NBTReflectionUtil {
|
||||
}
|
||||
|
||||
Object answer = null;
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
answer = ReflectionMethod.TILEENTITY_GET_NBT_1205.run(o, registry_access);
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) {
|
||||
answer = ReflectionMethod.TILEENTITY_GET_NBT_1181.run(o);
|
||||
} else {
|
||||
answer = ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance();
|
||||
@@ -288,7 +352,9 @@ public class NBTReflectionUtil {
|
||||
Object pos = ObjectCreator.NMS_BLOCKPOSITION.getInstance(tile.getX(), tile.getY(), tile.getZ());
|
||||
o = ReflectionMethod.NMS_WORLD_GET_TILEENTITY.run(nmsworld, pos);
|
||||
}
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
ReflectionMethod.TILEENTITY_SET_NBT_1205.run(o, comp, registry_access);
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) {
|
||||
ReflectionMethod.TILEENTITY_SET_NBT.run(o, comp);
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_16_R1)) {
|
||||
Object blockData = ReflectionMethod.TILEENTITY_GET_BLOCKDATA.run(o);
|
||||
@@ -338,7 +404,7 @@ public class NBTReflectionUtil {
|
||||
if (!valideCompound(comp)) {
|
||||
return;
|
||||
}
|
||||
Object workingtag = gettoCompount(nbttag, comp);
|
||||
Object workingtag = getToCompount(nbttag, comp);
|
||||
try {
|
||||
ReflectionMethod.COMPOUND_SET.run(workingtag, name,
|
||||
ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz().newInstance());
|
||||
@@ -359,12 +425,12 @@ public class NBTReflectionUtil {
|
||||
if (root == null) {
|
||||
root = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance();
|
||||
}
|
||||
Object tmp = gettoCompount(root, comp);
|
||||
Object tmp = getToCompount(root, comp);
|
||||
comp.setResolvedObject(tmp);
|
||||
return tmp != null;
|
||||
}
|
||||
|
||||
protected static Object gettoCompount(Object nbttag, NBTCompound comp) {
|
||||
public static Object getToCompount(Object nbttag, NBTCompound comp) {
|
||||
Deque<String> structure = new ArrayDeque<>();
|
||||
while (comp.getParent() != null) {
|
||||
structure.add(comp.getName());
|
||||
@@ -397,7 +463,7 @@ public class NBTReflectionUtil {
|
||||
}
|
||||
if (!valideCompound(comp))
|
||||
throw new NbtApiException("The Compound wasn't able to be linked back to the root!");
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Object workingtag = getToCompount(rootnbttag, comp);
|
||||
try {
|
||||
ReflectionMethod.COMPOUND_MERGE.run(workingtag, workingtagSrc);
|
||||
comp.setCompound(rootnbttag);
|
||||
@@ -406,22 +472,6 @@ public class NBTReflectionUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content for a given key inside a Compound
|
||||
*
|
||||
* @param comp
|
||||
* @param key
|
||||
* @return Content saved under this key
|
||||
*/
|
||||
public static String getContent(NBTCompound comp, String key) {
|
||||
Object workingtag = comp.getResolvedObject();
|
||||
try {
|
||||
return ReflectionMethod.COMPOUND_GET.run(workingtag, key).toString();
|
||||
} catch (Exception e) {
|
||||
throw new NbtApiException("Exception while getting the Content for key '" + key + "'!", e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a key in a {@link NBTCompound} to a given value
|
||||
*
|
||||
@@ -441,7 +491,7 @@ public class NBTReflectionUtil {
|
||||
if (!valideCompound(comp)) {
|
||||
throw new NbtApiException("The Compound wasn't able to be linked back to the root!");
|
||||
}
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Object workingtag = getToCompount(rootnbttag, comp);
|
||||
try {
|
||||
ReflectionMethod.COMPOUND_SET.run(workingtag, key, val);
|
||||
comp.setCompound(rootnbttag);
|
||||
@@ -570,11 +620,11 @@ public class NBTReflectionUtil {
|
||||
public static void remove(NBTCompound comp, String key) {
|
||||
Object rootnbttag = comp.getCompound();
|
||||
if (rootnbttag == null) {
|
||||
rootnbttag = ObjectCreator.NMS_NBTTAGCOMPOUND.getInstance();
|
||||
return;
|
||||
}
|
||||
if (!valideCompound(comp))
|
||||
return;
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Object workingtag = getToCompount(rootnbttag, comp);
|
||||
ReflectionMethod.COMPOUND_REMOVE_KEY.run(workingtag, key);
|
||||
comp.setCompound(rootnbttag);
|
||||
}
|
||||
@@ -588,6 +638,9 @@ public class NBTReflectionUtil {
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Set<String> getKeys(NBTCompound comp) {
|
||||
Object workingtag = comp.getResolvedObject();
|
||||
if (workingtag == null) {
|
||||
return Collections.emptySet();
|
||||
}
|
||||
return (Set<String>) ReflectionMethod.COMPOUND_GET_KEYS.run(workingtag);
|
||||
}
|
||||
|
||||
@@ -610,7 +663,7 @@ public class NBTReflectionUtil {
|
||||
}
|
||||
if (!valideCompound(comp))
|
||||
throw new NbtApiException("The Compound wasn't able to be linked back to the root!");
|
||||
Object workingtag = gettoCompount(rootnbttag, comp);
|
||||
Object workingtag = getToCompount(rootnbttag, comp);
|
||||
type.run(workingtag, key, data);
|
||||
comp.setCompound(rootnbttag);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,84 @@
|
||||
package de.tr7zw.changeme.nbtapi.utils;
|
||||
|
||||
import com.mojang.datafixers.DSL.TypeReference;
|
||||
import com.mojang.datafixers.DataFixer;
|
||||
import com.mojang.serialization.Dynamic;
|
||||
import com.mojang.serialization.DynamicOps;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTCompound;
|
||||
import de.tr7zw.changeme.nbtapi.NBTContainer;
|
||||
import de.tr7zw.changeme.nbtapi.NBTReflectionUtil;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
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 DataFixerUtil {
|
||||
|
||||
public static final int VERSION1_12_2 = 1343;
|
||||
public static final int VERSION1_16_5 = 2586;
|
||||
public static final int VERSION1_17_1 = 2730;
|
||||
public static final int VERSION1_18_2 = 2975;
|
||||
public static final int VERSION1_19_2 = 3120;
|
||||
public static final int VERSION1_19_4 = 3337;
|
||||
public static final int VERSION1_20_1 = 3465;
|
||||
public static final int VERSION1_20_2 = 3578;
|
||||
public static final int VERSION1_20_4 = 3700;
|
||||
public static final int VERSION1_20_5 = 3837;
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Object fixUpRawItemData(Object nbt, int fromVersion, int toVersion)
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
DataFixer dataFixer = (DataFixer) ReflectionMethod.GET_DATAFIXER.run(null);
|
||||
TypeReference itemStackReference = (TypeReference) ClassWrapper.NMS_REFERENCES.getClazz()
|
||||
.getField(MojangToMapping.getMapping().get("net.minecraft.util.datafix.fixes.References#ITEM_STACK"))
|
||||
.get(null);
|
||||
DynamicOps<Object> nbtOps = (DynamicOps<Object>) ClassWrapper.NMS_NBTOPS.getClazz()
|
||||
.getField(MojangToMapping.getMapping().get("net.minecraft.nbt.NbtOps#INSTANCE")).get(null);
|
||||
Dynamic<Object> fixed = dataFixer.update(itemStackReference, new Dynamic<Object>(nbtOps, nbt), fromVersion,
|
||||
toVersion);
|
||||
return fixed.getValue();
|
||||
}
|
||||
|
||||
public static ReadWriteNBT fixUpItemData(ReadWriteNBT nbt, int fromVersion, int toVersion)
|
||||
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
|
||||
return new NBTContainer(fixUpRawItemData(
|
||||
NBTReflectionUtil.getToCompount(((NBTCompound) nbt).getCompound(), ((NBTCompound) nbt)), fromVersion,
|
||||
toVersion));
|
||||
}
|
||||
|
||||
/**
|
||||
* This method will return the closest or exact version number of the current
|
||||
* server. Only contains versions people will reasonably use and will fail with
|
||||
* an exception, when the target version is before 1.12.2. (Assuming no one will
|
||||
* update 1.8 items to 1.11, if so, provide the version numbers to the converter
|
||||
* method directly)
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public static int getCurrentVersion() {
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return VERSION1_20_5;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R3)) {
|
||||
return VERSION1_20_4;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R1)) {
|
||||
return VERSION1_20_1;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_19_R3)) {
|
||||
return VERSION1_19_4;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_19_R1)) {
|
||||
return VERSION1_19_2;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_18_R1)) {
|
||||
return VERSION1_18_2;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_17_R1)) {
|
||||
return VERSION1_17_1;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_16_R1)) {
|
||||
return VERSION1_16_5;
|
||||
} else if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_12_R1)) {
|
||||
return VERSION1_12_2;
|
||||
}
|
||||
throw new NbtApiException(
|
||||
"Trying to update data *to* a version before 1.12.2? Something is probably going wrong, contact the plugin author.");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
package de.tr7zw.changeme.nbtapi.utils;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import com.mojang.authlib.GameProfile;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTType;
|
||||
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.iface.ReadableNBTList;
|
||||
|
||||
public class GameprofileUtil {
|
||||
|
||||
@Nullable
|
||||
public static GameProfile readGameProfile(ReadableNBT arg) {
|
||||
String string = null;
|
||||
UUID uUID = null;
|
||||
if (arg.hasTag("Name") && arg.getType("Name") == NBTType.NBTTagString) {
|
||||
string = arg.getString("Name");
|
||||
}
|
||||
if (arg.hasTag("Id") && arg.getType("Id") == NBTType.NBTTagIntArray && arg.getIntArray("Id").length == 4) {
|
||||
uUID = arg.getUUID("Id");
|
||||
}
|
||||
try {
|
||||
GameProfile gameProfile = new GameProfile(uUID, string);
|
||||
if (arg.hasTag("Properties") && arg.getType("Properties") == NBTType.NBTTagCompound) {
|
||||
ReadableNBT compoundTag = arg.getCompound("Properties");
|
||||
for (String string2 : compoundTag.getKeys()) {
|
||||
ReadableNBTList<ReadWriteNBT> listTag = compoundTag.getCompoundList(string);
|
||||
for (int i = 0; i < listTag.size(); ++i) {
|
||||
ReadableNBT compoundTag2 = listTag.get(i);
|
||||
String string3 = compoundTag2.getString("Value");
|
||||
if (compoundTag2.hasTag("Signature")
|
||||
&& compoundTag2.getType("Signature") == NBTType.NBTTagString) {
|
||||
gameProfile.getProperties().put(string2, new com.mojang.authlib.properties.Property(string2,
|
||||
string3, compoundTag2.getString("Signature")));
|
||||
} else {
|
||||
gameProfile.getProperties().put(string2,
|
||||
new com.mojang.authlib.properties.Property(string2, string3));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return gameProfile;
|
||||
} catch (Throwable var11) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ReadWriteNBT writeGameProfile(ReadWriteNBT arg, GameProfile gameProfile) {
|
||||
if (!(gameProfile.getName() == null || gameProfile.getName().isEmpty())) {
|
||||
arg.setString("Name", gameProfile.getName());
|
||||
}
|
||||
if (gameProfile.getId() != null) {
|
||||
arg.setUUID("Id", gameProfile.getId());
|
||||
}
|
||||
if (!gameProfile.getProperties().isEmpty()) {
|
||||
ReadWriteNBT compoundTag = arg.getOrCreateCompound("Properties");
|
||||
for (String string : gameProfile.getProperties().keySet()) {
|
||||
ReadWriteNBTCompoundList list = compoundTag.getCompoundList(string);
|
||||
for (com.mojang.authlib.properties.Property property : gameProfile.getProperties().get(string)) {
|
||||
ReadWriteNBT tag = list.addCompound();
|
||||
tag.setString("Value", property.getValue());
|
||||
if (property.hasSignature()) {
|
||||
tag.setString("Signature", property.getSignature());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return arg;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
package de.tr7zw.changeme.nbtapi.utils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@@ -21,7 +23,7 @@ public enum MinecraftVersion {
|
||||
MC1_7_R4(174), MC1_8_R3(183), MC1_9_R1(191), MC1_9_R2(192), MC1_10_R1(1101), MC1_11_R1(1111), MC1_12_R1(1121),
|
||||
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_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);
|
||||
|
||||
private static MinecraftVersion version;
|
||||
private static Boolean hasGsonSupport;
|
||||
@@ -36,11 +38,25 @@ public enum MinecraftVersion {
|
||||
private static Logger logger = Logger.getLogger("NBTAPI");
|
||||
|
||||
// NBT-API Version
|
||||
protected static final String VERSION = "2.12.0-SNAPSHOT";
|
||||
protected static final String VERSION = "2.12.4";
|
||||
|
||||
private final int versionId;
|
||||
private final boolean mojangMapping;
|
||||
|
||||
// TODO: not nice
|
||||
@SuppressWarnings("serial")
|
||||
private static final Map<String, MinecraftVersion> VERSION_TO_REVISION = new HashMap<String, MinecraftVersion>() {
|
||||
{
|
||||
this.put("1.20", MC1_20_R1);
|
||||
this.put("1.20.1", MC1_20_R1);
|
||||
this.put("1.20.2", MC1_20_R2);
|
||||
this.put("1.20.3", MC1_20_R3);
|
||||
this.put("1.20.4", MC1_20_R3);
|
||||
this.put("1.20.5", MC1_20_R4);
|
||||
this.put("1.20.6", MC1_20_R4);
|
||||
}
|
||||
};
|
||||
|
||||
MinecraftVersion(int versionId) {
|
||||
this(versionId, false);
|
||||
}
|
||||
@@ -73,7 +89,11 @@ public enum MinecraftVersion {
|
||||
*/
|
||||
public String getPackageName() {
|
||||
if (this == UNKNOWN) {
|
||||
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
try {
|
||||
return Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
} catch (Exception ex) {
|
||||
// ignore, paper without remap, will fail
|
||||
}
|
||||
}
|
||||
return this.name().replace("MC", "v");
|
||||
}
|
||||
@@ -108,18 +128,22 @@ public enum MinecraftVersion {
|
||||
if (version != null) {
|
||||
return version;
|
||||
}
|
||||
final String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
logger.info("[NBTAPI] Found Spigot: " + ver + "! Trying to find NMS support");
|
||||
try {
|
||||
final String ver = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
logger.info("[NBTAPI] Found Minecraft: " + ver + "! Trying to find NMS support");
|
||||
version = MinecraftVersion.valueOf(ver.replace("v", "MC"));
|
||||
} catch (IllegalArgumentException ex) {
|
||||
version = MinecraftVersion.UNKNOWN;
|
||||
} catch (Exception ex) {
|
||||
logger.info("[NBTAPI] Found Minecraft: " + Bukkit.getServer().getBukkitVersion().split("-")[0]
|
||||
+ "! Trying to find NMS support");
|
||||
version = VERSION_TO_REVISION.getOrDefault(Bukkit.getServer().getBukkitVersion().split("-")[0],
|
||||
MinecraftVersion.UNKNOWN);
|
||||
}
|
||||
if (version != UNKNOWN) {
|
||||
logger.info("[NBTAPI] NMS support '" + version.name() + "' loaded!");
|
||||
} else {
|
||||
logger.warning("[NBTAPI] This Server-Version(" + ver + ") is not supported by this NBT-API Version("
|
||||
+ VERSION + ") located in " + VersionChecker.getPlugin()
|
||||
logger.warning("[NBTAPI] This Server-Version(" + Bukkit.getServer().getBukkitVersion()
|
||||
+ ") is not supported by this NBT-API Version(" + VERSION + ") located in "
|
||||
+ VersionChecker.getPlugin()
|
||||
+ ". The NBT-API will try to work as good as it can! Some functions may not work!");
|
||||
}
|
||||
init();
|
||||
@@ -245,8 +269,7 @@ public enum MinecraftVersion {
|
||||
return isFoliaPresent;
|
||||
}
|
||||
try {
|
||||
logger.info("[NBTAPI] Found Folia: "
|
||||
+ Class.forName("io.papermc.paper.threadedregions.RegionizedServer"));
|
||||
logger.info("[NBTAPI] Found Folia: " + Class.forName("io.papermc.paper.threadedregions.RegionizedServer"));
|
||||
isFoliaPresent = true;
|
||||
} catch (Exception ex) {
|
||||
isFoliaPresent = false;
|
||||
|
||||
@@ -8,17 +8,58 @@ import java.util.regex.Pattern;
|
||||
public final class PathUtil {
|
||||
|
||||
private static final Pattern pattern = Pattern.compile("[^\\\\](\\.)");
|
||||
private static final Pattern indexPattern = Pattern.compile(".*\\[(-?[0-9]+)\\]");
|
||||
|
||||
public static List<String> splitPath(String path) {
|
||||
List<String> list = new ArrayList<>();
|
||||
public static List<PathSegment> splitPath(String path) {
|
||||
List<PathSegment> list = new ArrayList<>();
|
||||
Matcher matcher = pattern.matcher(path);
|
||||
int startIndex = 0;
|
||||
while (matcher.find(startIndex)) {
|
||||
list.add(path.substring(startIndex, matcher.end() - 1).replace("\\.", "."));
|
||||
list.add(new PathSegment(path.substring(startIndex, matcher.end() - 1).replace("\\.", ".")));
|
||||
startIndex = matcher.end();
|
||||
}
|
||||
list.add(path.substring(startIndex).replace("\\.", "."));
|
||||
list.add(new PathSegment(path.substring(startIndex).replace("\\.", ".")));
|
||||
return list;
|
||||
}
|
||||
|
||||
public static class PathSegment {
|
||||
|
||||
private final String path;
|
||||
private final Integer index;
|
||||
|
||||
public PathSegment(String path, int index) {
|
||||
this.path = path;
|
||||
this.index = index;
|
||||
}
|
||||
|
||||
public PathSegment(String path) {
|
||||
Matcher matcher = indexPattern.matcher(path);
|
||||
if (matcher.find()) {
|
||||
this.path = path.substring(0, path.indexOf("["));
|
||||
this.index = Integer.parseInt(matcher.group(1));
|
||||
} else {
|
||||
this.path = path;
|
||||
this.index = null;
|
||||
}
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
public boolean hasIndex() {
|
||||
return index != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PathSegment [path=" + path + ", index=" + index + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+27
@@ -4,6 +4,8 @@ import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.getLogger;
|
||||
|
||||
import java.util.logging.Level;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
|
||||
/**
|
||||
@@ -18,6 +20,7 @@ public enum ClassWrapper {
|
||||
CRAFT_METAITEM(PackageWrapper.CRAFTBUKKIT, "inventory.CraftMetaItem", null, null),
|
||||
CRAFT_ENTITY(PackageWrapper.CRAFTBUKKIT, "entity.CraftEntity", null, null),
|
||||
CRAFT_WORLD(PackageWrapper.CRAFTBUKKIT, "CraftWorld", null, null),
|
||||
CRAFT_SERVER(PackageWrapper.CRAFTBUKKIT, "CraftServer", null, null),
|
||||
CRAFT_PERSISTENTDATACONTAINER(PackageWrapper.CRAFTBUKKIT, "persistence.CraftPersistentDataContainer",
|
||||
MinecraftVersion.MC1_14_R1, null),
|
||||
NMS_NBTBASE(PackageWrapper.NMS, "NBTBase", null, null, "net.minecraft.nbt", "net.minecraft.nbt.Tag"),
|
||||
@@ -62,6 +65,26 @@ public enum ClassWrapper {
|
||||
"net.minecraft.nbt.NbtUtils"),
|
||||
NMS_IBLOCKDATA(PackageWrapper.NMS, "IBlockData", MinecraftVersion.MC1_8_R3, null,
|
||||
"net.minecraft.world.level.block.state", "net.minecraft.world.level.block.state.BlockState"),
|
||||
NMS_NBTACCOUNTER(PackageWrapper.NMS, "NBTReadLimiter", MinecraftVersion.MC1_20_R3, null, "net.minecraft.nbt",
|
||||
"net.minecraft.nbt.NbtAccounter"),
|
||||
NMS_CUSTOMDATA(PackageWrapper.NMS, "CustomData", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.world.item.component", "net.minecraft.world.item.component.CustomData"),
|
||||
NMS_DATACOMPONENTTYPE(PackageWrapper.NMS, "DataComponentType", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.core.component", "net.minecraft.core.component.DataComponentType"),
|
||||
NMS_DATACOMPONENTS(PackageWrapper.NMS, "DataComponents", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.core.component", "net.minecraft.core.component.DataComponents"),
|
||||
NMS_DATACOMPONENTHOLDER(PackageWrapper.NMS, "DataComponentHolder", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.core.component", "net.minecraft.core.component.DataComponentHolder"),
|
||||
NMS_PROVIDER(PackageWrapper.NMS, "HolderLookup$a", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.core", "net.minecraft.core.HolderLookup$Provider"),
|
||||
NMS_SERVER(PackageWrapper.NMS, "MinecraftServer", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.server", "net.minecraft.server.MinecraftServer"),
|
||||
NMS_DATAFIXERS(PackageWrapper.NMS, "DataConverterRegistry", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.util.datafix", "net.minecraft.util.datafix.DataFixers"),
|
||||
NMS_REFERENCES(PackageWrapper.NMS, "DataConverterTypes", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.util.datafix.fixes", "net.minecraft.util.datafix.fixes.References"),
|
||||
NMS_NBTOPS(PackageWrapper.NMS, "DynamicOpsNBT", MinecraftVersion.MC1_20_R4, null,
|
||||
"net.minecraft.nbt", "net.minecraft.nbt.NbtOps"),
|
||||
GAMEPROFILE(PackageWrapper.NONE, "com.mojang.authlib.GameProfile", MinecraftVersion.MC1_8_R3, null);
|
||||
|
||||
private Class<?> clazz;
|
||||
@@ -99,7 +122,11 @@ public enum ClassWrapper {
|
||||
} else if (MinecraftVersion.isForgePresent() && MinecraftVersion.getVersion() == MinecraftVersion.MC1_7_R4
|
||||
&& Forge1710Mappings.getClassMappings().get(this.name()) != null) {
|
||||
clazz = Class.forName(clazzName = Forge1710Mappings.getClassMappings().get(this.name()));
|
||||
} else if (packageId == PackageWrapper.CRAFTBUKKIT) {
|
||||
// this also works for un-remapped Paper 1.20+
|
||||
clazz = Class.forName(Bukkit.getServer().getClass().getPackage().getName() + "." + clazzName);
|
||||
} else {
|
||||
// fallback for old versions pre mojmap and in the nms package
|
||||
String version = MinecraftVersion.getVersion().getPackageName();
|
||||
clazz = Class.forName(packageId.getUri() + "." + version + "." + clazzName);
|
||||
}
|
||||
|
||||
+65
-3
@@ -3,6 +3,7 @@ package de.tr7zw.changeme.nbtapi.utils.nmsmappings;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
|
||||
/**
|
||||
@@ -72,7 +73,7 @@ public class MojangToMapping {
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#saveWithId()", "n");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#getBlockState()", "q");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#load(net.minecraft.nbt.CompoundTag)", "a");
|
||||
put("net.minecraft.server.level.ServerLevel#getBlockState(net.minecraft.core.BlockPos)", "c_");
|
||||
put("net.minecraft.server.level.ServerLevel#getBlockEntity(net.minecraft.core.BlockPos)", "c_");
|
||||
}
|
||||
|
||||
};
|
||||
@@ -120,11 +121,70 @@ public class MojangToMapping {
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static Map<String, String> MC1_20R2 = new HashMap<String, String>() {
|
||||
|
||||
{
|
||||
putAll(MC1_20R1);
|
||||
|
||||
put("net.minecraft.world.entity.Entity#getEncodeId()", "bu");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static Map<String, String> MC1_20R3 = new HashMap<String, String>() {
|
||||
|
||||
{
|
||||
putAll(MC1_20R2);
|
||||
|
||||
put("net.minecraft.nbt.NbtIo#readCompressed(java.io.InputStream,net.minecraft.nbt.NbtAccounter)", "a");
|
||||
put("net.minecraft.nbt.NbtAccounter#unlimitedHeap()", "a");
|
||||
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");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
private static Map<String, String> MC1_20R4 = new HashMap<String, String>() {
|
||||
|
||||
{
|
||||
putAll(MC1_20R3);
|
||||
|
||||
put("net.minecraft.world.entity.Entity#getEncodeId()", "bC");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#getBlockState()", "n");
|
||||
put("net.minecraft.core.component.DataComponents#CUSTOM_DATA", "b");
|
||||
put("net.minecraft.core.component.DataComponentHolder#get(net.minecraft.core.component.DataComponentType)", "a");
|
||||
put("net.minecraft.world.item.component.CustomData#copyTag()", "c");
|
||||
put("net.minecraft.world.item.ItemStack#set(net.minecraft.core.component.DataComponentType,java.lang.Object)", "b");
|
||||
put("net.minecraft.world.item.ItemStack#save(net.minecraft.core.HolderLookup$Provider)", "a");
|
||||
put("net.minecraft.server.MinecraftServer#registryAccess()", "bc");
|
||||
put("net.minecraft.world.item.ItemStack#parseOptional(net.minecraft.core.HolderLookup$Provider,net.minecraft.nbt.CompoundTag)", "a");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#saveWithId(net.minecraft.core.HolderLookup$Provider)", "c");
|
||||
put("net.minecraft.world.level.block.entity.BlockEntity#loadWithComponents(net.minecraft.nbt.CompoundTag,net.minecraft.core.HolderLookup$Provider)", "c");
|
||||
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");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
public static Map<String, String> getMapping() {
|
||||
switch (MinecraftVersion.getVersion()) {
|
||||
case MC1_20_R4:
|
||||
return MC1_20R4;
|
||||
case MC1_20_R3:
|
||||
return MC1_20R3;
|
||||
case MC1_20_R2:
|
||||
return MC1_20R2;
|
||||
case MC1_20_R1:
|
||||
return MC1_20R1;
|
||||
case MC1_19_R3:
|
||||
return MC1_19R2;
|
||||
case MC1_19_R2:
|
||||
return MC1_19R2;
|
||||
case MC1_19_R1:
|
||||
@@ -133,9 +193,11 @@ public class MojangToMapping {
|
||||
return MC1_18R2;
|
||||
case MC1_18_R1:
|
||||
return MC1_18R1;
|
||||
case UNKNOWN:
|
||||
return MC1_20R2; // assume it's a future version, so try the latest known mappings
|
||||
default:
|
||||
return MC1_19R2;// throw new NbtApiException("This version of the NBTAPI is not compatible with
|
||||
// this server version!");
|
||||
// this should never happen, unless a version is forgotten here(like 1.19R3 which uses the 1.19R2 mappings)
|
||||
throw new NbtApiException("No fitting mapping found for version " + MinecraftVersion.getVersion() + ". This is a bug!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+2
-1
@@ -17,8 +17,9 @@ import static de.tr7zw.changeme.nbtapi.utils.MinecraftVersion.getLogger;
|
||||
@SuppressWarnings("javadoc")
|
||||
public enum ObjectCreator {
|
||||
NMS_NBTTAGCOMPOUND(null, null, ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()),
|
||||
NMS_CUSTOMDATA(MinecraftVersion.MC1_20_R4, null, ClassWrapper.NMS_CUSTOMDATA.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()),
|
||||
NMS_BLOCKPOSITION(null, null, ClassWrapper.NMS_BLOCKPOSITION.getClazz(), int.class, int.class, int.class),
|
||||
NMS_COMPOUNDFROMITEM(MinecraftVersion.MC1_11_R1, null, ClassWrapper.NMS_ITEMSTACK.getClazz(),
|
||||
NMS_COMPOUNDFROMITEM(MinecraftVersion.MC1_11_R1, MinecraftVersion.MC1_20_R3, ClassWrapper.NMS_ITEMSTACK.getClazz(),
|
||||
ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz()),;
|
||||
|
||||
private Constructor<?> construct;
|
||||
|
||||
+43
-9
@@ -5,6 +5,7 @@ import java.io.OutputStream;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
@@ -116,7 +117,7 @@ public enum ReflectionMethod {
|
||||
NMSITEM_GETTAG(ClassWrapper.NMS_ITEMSTACK, new Class[] {}, MinecraftVersion.MC1_7_R4,
|
||||
new Since(MinecraftVersion.MC1_7_R4, "getTag"), new Since(MinecraftVersion.MC1_18_R1, "getTag()")),
|
||||
NMSITEM_SAVE(ClassWrapper.NMS_ITEMSTACK, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "save"),
|
||||
MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_20_R3, new Since(MinecraftVersion.MC1_7_R4, "save"),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "save(net.minecraft.nbt.CompoundTag)")),
|
||||
NMSITEM_CREATESTACK(ClassWrapper.NMS_ITEMSTACK, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_10_R1, new Since(MinecraftVersion.MC1_7_R4, "createStack")),
|
||||
@@ -162,7 +163,7 @@ public enum ReflectionMethod {
|
||||
new Since(MinecraftVersion.MC1_13_R1, "get"), new Since(MinecraftVersion.MC1_18_R1, "get(int)")),
|
||||
|
||||
ITEMSTACK_SET_TAG(ClassWrapper.NMS_ITEMSTACK, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "setTag"),
|
||||
MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_20_R3, new Since(MinecraftVersion.MC1_7_R4, "setTag"),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "setTag(net.minecraft.nbt.CompoundTag)")),
|
||||
ITEMSTACK_NMSCOPY(ClassWrapper.CRAFT_ITEMSTACK, new Class[] { ItemStack.class }, MinecraftVersion.MC1_7_R4,
|
||||
new Since(MinecraftVersion.MC1_7_R4, "asNMSCopy")),
|
||||
@@ -173,7 +174,7 @@ public enum ReflectionMethod {
|
||||
new Since(MinecraftVersion.MC1_7_R4, "getHandle")),
|
||||
NMS_WORLD_GET_TILEENTITY(ClassWrapper.NMS_WORLDSERVER, new Class[] { ClassWrapper.NMS_BLOCKPOSITION.getClazz() },
|
||||
MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "getTileEntity"),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "getBlockState(net.minecraft.core.BlockPos)")),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "getBlockEntity(net.minecraft.core.BlockPos)")),
|
||||
NMS_WORLD_SET_TILEENTITY(ClassWrapper.NMS_WORLDSERVER,
|
||||
new Class[] { ClassWrapper.NMS_BLOCKPOSITION.getClazz(), ClassWrapper.NMS_TILEENTITY.getClazz() },
|
||||
MinecraftVersion.MC1_8_R3, MinecraftVersion.MC1_16_R3,
|
||||
@@ -207,7 +208,7 @@ public enum ReflectionMethod {
|
||||
TILEENTITY_GET_NBT(ClassWrapper.NMS_TILEENTITY, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_17_R1, new Since(MinecraftVersion.MC1_7_R4, "b"),
|
||||
new Since(MinecraftVersion.MC1_9_R1, "save")),
|
||||
TILEENTITY_GET_NBT_1181(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_18_R1,
|
||||
TILEENTITY_GET_NBT_1181(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_18_R1, MinecraftVersion.MC1_20_R3,
|
||||
new Since(MinecraftVersion.MC1_18_R1, "saveWithId()")),
|
||||
TILEENTITY_SET_NBT_LEGACY1151(ClassWrapper.NMS_TILEENTITY,
|
||||
new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, MinecraftVersion.MC1_7_R4,
|
||||
@@ -217,7 +218,7 @@ public enum ReflectionMethod {
|
||||
new Class[] { ClassWrapper.NMS_IBLOCKDATA.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_16_R1, MinecraftVersion.MC1_16_R3, new Since(MinecraftVersion.MC1_16_R1, "load")),
|
||||
TILEENTITY_SET_NBT(ClassWrapper.NMS_TILEENTITY, new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_17_R1, new Since(MinecraftVersion.MC1_16_R1, "load"),
|
||||
MinecraftVersion.MC1_17_R1, MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_16_R1, "load"),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "load(net.minecraft.nbt.CompoundTag)")),
|
||||
TILEENTITY_GET_BLOCKDATA(ClassWrapper.NMS_TILEENTITY, new Class[] {}, MinecraftVersion.MC1_16_R1,
|
||||
new Since(MinecraftVersion.MC1_16_R1, "getBlock"),
|
||||
@@ -237,8 +238,19 @@ public enum ReflectionMethod {
|
||||
new Since(MinecraftVersion.MC1_14_R1, "getSaveID"), new Since(MinecraftVersion.MC1_18_R1, "getEncodeId()")),
|
||||
|
||||
NBTFILE_READ(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS, new Class[] { InputStream.class },
|
||||
MinecraftVersion.MC1_7_R4, new Since(MinecraftVersion.MC1_7_R4, "a"),
|
||||
MinecraftVersion.MC1_7_R4, MinecraftVersion.MC1_20_R2, new Since(MinecraftVersion.MC1_7_R4, "a"),
|
||||
new Since(MinecraftVersion.MC1_18_R1, "readCompressed(java.io.InputStream)")),
|
||||
|
||||
NBTFILE_READV2(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS,
|
||||
new Class[] { InputStream.class, ClassWrapper.NMS_NBTACCOUNTER.getClazz() }, MinecraftVersion.MC1_20_R3,
|
||||
new Since(MinecraftVersion.MC1_20_R3,
|
||||
"readCompressed(java.io.InputStream,net.minecraft.nbt.NbtAccounter)")),
|
||||
|
||||
NBTACCOUNTER_CREATE_UNLIMITED(ClassWrapper.NMS_NBTACCOUNTER,
|
||||
new Class[] {}, MinecraftVersion.MC1_20_R3,
|
||||
new Since(MinecraftVersion.MC1_20_R3,
|
||||
"unlimitedHeap()")),
|
||||
|
||||
NBTFILE_WRITE(ClassWrapper.NMS_NBTCOMPRESSEDSTREAMTOOLS,
|
||||
new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), OutputStream.class }, MinecraftVersion.MC1_7_R4,
|
||||
new Since(MinecraftVersion.MC1_7_R4, "a"),
|
||||
@@ -271,7 +283,7 @@ public enum ReflectionMethod {
|
||||
new Since(MinecraftVersion.MC1_18_R1, "readGameProfile(net.minecraft.nbt.CompoundTag)")),
|
||||
GAMEPROFILE_SERIALIZE(ClassWrapper.NMS_GAMEPROFILESERIALIZER,
|
||||
new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), ClassWrapper.GAMEPROFILE.getClazz() },
|
||||
MinecraftVersion.MC1_8_R3, new Since(MinecraftVersion.MC1_8_R3, "serialize"),
|
||||
MinecraftVersion.MC1_8_R3, MinecraftVersion.MC1_20_R3, new Since(MinecraftVersion.MC1_8_R3, "serialize"),
|
||||
new Since(MinecraftVersion.MC1_18_R1,
|
||||
"writeGameProfile(net.minecraft.nbt.CompoundTag,com.mojang.authlib.GameProfile)")),
|
||||
|
||||
@@ -281,7 +293,29 @@ public enum ReflectionMethod {
|
||||
MinecraftVersion.MC1_14_R1, new Since(MinecraftVersion.MC1_14_R1, "getRaw")),
|
||||
CRAFT_PERSISTENT_DATA_CONTAINER_PUT_ALL(ClassWrapper.CRAFT_PERSISTENTDATACONTAINER,
|
||||
new Class[] { ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() }, MinecraftVersion.MC1_14_R1,
|
||||
new Since(MinecraftVersion.MC1_14_R1, "putAll")),;
|
||||
new Since(MinecraftVersion.MC1_14_R1, "putAll")),
|
||||
// 1.20.5+ Stuff
|
||||
NMSDATACOMPONENTHOLDER_GET(ClassWrapper.NMS_DATACOMPONENTHOLDER, new Class[] {ClassWrapper.NMS_DATACOMPONENTTYPE.getClazz()}, MinecraftVersion.MC1_20_R4,
|
||||
new Since(MinecraftVersion.MC1_20_R4, "get(net.minecraft.core.component.DataComponentType)")),
|
||||
NMSCUSTOMDATA_GETCOPY(ClassWrapper.NMS_CUSTOMDATA, new Class[] {}, MinecraftVersion.MC1_20_R4,
|
||||
new Since(MinecraftVersion.MC1_20_R4, "copyTag()")),
|
||||
NMSITEM_SET(ClassWrapper.NMS_ITEMSTACK, new Class[] {ClassWrapper.NMS_DATACOMPONENTTYPE.getClazz(), Object.class}, MinecraftVersion.MC1_20_R4,
|
||||
new Since(MinecraftVersion.MC1_20_R4, "set(net.minecraft.core.component.DataComponentType,java.lang.Object)")),
|
||||
NMSITEM_SAVE_MODERN(ClassWrapper.NMS_ITEMSTACK, new Class[] { ClassWrapper.NMS_PROVIDER.getClazz() },
|
||||
MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "save(net.minecraft.core.HolderLookup$Provider)")),
|
||||
NMSITEM_LOAD(ClassWrapper.NMS_ITEMSTACK, new Class[] { ClassWrapper.NMS_PROVIDER.getClazz(), ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz() },
|
||||
MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "parseOptional(net.minecraft.core.HolderLookup$Provider,net.minecraft.nbt.CompoundTag)")),
|
||||
NMSSERVER_GETREGISTRYACCESS(ClassWrapper.NMS_SERVER, new Class[] {},
|
||||
MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "registryAccess()")),
|
||||
NMSSERVER_GETSERVER(ClassWrapper.CRAFT_SERVER, new Class[] {},
|
||||
MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "getServer()")),
|
||||
TILEENTITY_GET_NBT_1205(ClassWrapper.NMS_TILEENTITY, new Class[] {ClassWrapper.NMS_PROVIDER.getClazz()}, MinecraftVersion.MC1_20_R4,
|
||||
new Since(MinecraftVersion.MC1_20_R4, "saveWithId(net.minecraft.core.HolderLookup$Provider)")),
|
||||
TILEENTITY_SET_NBT_1205(ClassWrapper.NMS_TILEENTITY, new Class[] {ClassWrapper.NMS_NBTTAGCOMPOUND.getClazz(), ClassWrapper.NMS_PROVIDER.getClazz() },
|
||||
MinecraftVersion.MC1_20_R4, new Since(MinecraftVersion.MC1_20_R4, "loadWithComponents(net.minecraft.nbt.CompoundTag,net.minecraft.core.HolderLookup$Provider)")),
|
||||
GET_DATAFIXER(ClassWrapper.NMS_DATAFIXERS, new Class[] {}, MinecraftVersion.MC1_20_R4,
|
||||
new Since(MinecraftVersion.MC1_20_R4, "getDataFixer()")),
|
||||
;
|
||||
|
||||
private MinecraftVersion removedAfter;
|
||||
private Since targetVersion;
|
||||
@@ -372,7 +406,7 @@ public enum ReflectionMethod {
|
||||
return method.invoke(target, args);
|
||||
} catch (Exception ex) {
|
||||
throw new NbtApiException("Error while calling the method '" + methodName + "', loaded: " + loaded
|
||||
+ ", Enum: " + this + ", Passed Class: " + (target == null ? "null" : target.getClass()), ex);
|
||||
+ ", Enum: " + this + ", Passed Class: " + (target == null ? "null" : target.getClass()) + " Args: " + (args == null ? "null" : Arrays.toString(args)), ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package de.tr7zw.changeme.nbtapi.wrapper;
|
||||
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBTCompoundList;
|
||||
|
||||
class ProxiedList<E extends NBTProxy> implements ProxyList<E> {
|
||||
|
||||
private final ReadWriteNBTCompoundList nbt;
|
||||
private final Class<E> proxy;
|
||||
|
||||
public ProxiedList(ReadWriteNBTCompoundList nbt, Class<E> proxyClass) {
|
||||
this.nbt = nbt;
|
||||
this.proxy = proxyClass;
|
||||
}
|
||||
|
||||
@Override
|
||||
public E get(int index) {
|
||||
ReadWriteNBT tag = nbt.get(index);
|
||||
return new ProxyBuilder<E>(tag,proxy).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return nbt.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove(int index) {
|
||||
nbt.remove(index);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<E> iterator() {
|
||||
return new Itr();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E addCompound() {
|
||||
ReadWriteNBT tag = nbt.addCompound();
|
||||
return new ProxyBuilder<E>(tag,proxy).build();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isEmpty() {
|
||||
return nbt.isEmpty();
|
||||
}
|
||||
|
||||
private class Itr implements Iterator<E> {
|
||||
/**
|
||||
* Index of element to be returned by subsequent call to next.
|
||||
*/
|
||||
int cursor = 0;
|
||||
|
||||
/**
|
||||
* Index of element returned by most recent call to next or
|
||||
* previous. Reset to -1 if this element is deleted by a call
|
||||
* to remove.
|
||||
*/
|
||||
int lastRet = -1;
|
||||
|
||||
public boolean hasNext() {
|
||||
return cursor != size();
|
||||
}
|
||||
|
||||
public E next() {
|
||||
try {
|
||||
int i = cursor;
|
||||
E next = get(i);
|
||||
lastRet = i;
|
||||
cursor = i + 1;
|
||||
return next;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new NoSuchElementException();
|
||||
}
|
||||
}
|
||||
|
||||
public void remove() {
|
||||
if (lastRet < 0)
|
||||
throw new IllegalStateException();
|
||||
|
||||
try {
|
||||
ProxiedList.this.remove(lastRet);
|
||||
if (lastRet < cursor)
|
||||
cursor--;
|
||||
lastRet = -1;
|
||||
} catch (IndexOutOfBoundsException e) {
|
||||
throw new ConcurrentModificationException();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package de.tr7zw.changeme.nbtapi.wrapper;
|
||||
|
||||
import java.lang.reflect.InvocationHandler;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Proxy;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
@@ -98,13 +99,24 @@ public class ProxyBuilder<T extends NBTProxy> implements InvocationHandler {
|
||||
(Class<NBTProxy>) retType).build();
|
||||
};
|
||||
}
|
||||
if (retType == ProxyList.class) {
|
||||
|
||||
Class<?> parameterType = (Class<?>) ((ParameterizedType) method.getGenericReturnType())
|
||||
.getActualTypeArguments()[0];
|
||||
if (parameterType != null && parameterType.isInterface()
|
||||
&& NBTProxy.class.isAssignableFrom(parameterType)) {
|
||||
return (arguments) -> {
|
||||
return new ProxiedList(arguments.nbt.getCompoundList(fieldName), parameterType);
|
||||
};
|
||||
}
|
||||
}
|
||||
NBTHandler<Object> handler = (NBTHandler<Object>) proxy.getHandler(retType);
|
||||
if (handler != null) {
|
||||
return (arguments) -> handler.get(arguments.nbt, fieldName);
|
||||
}
|
||||
return (arguments) -> arguments.nbt.getOrNull(fieldName, retType);
|
||||
}
|
||||
if(action == Type.HAS) {
|
||||
if (action == Type.HAS) {
|
||||
String fieldName = getNBTName(proxy.getCasing(), method);
|
||||
return (arguments) -> arguments.nbt.hasTag(fieldName);
|
||||
}
|
||||
@@ -116,7 +128,8 @@ public class ProxyBuilder<T extends NBTProxy> implements InvocationHandler {
|
||||
private static Type getAction(Method method) {
|
||||
NBTTarget target = method.getAnnotation(NBTTarget.class);
|
||||
if (target != null) {
|
||||
if (target.type() == Type.HAS && method.getParameterCount() == 0 && method.getReturnType() == boolean.class) {
|
||||
if (target.type() == Type.HAS && method.getParameterCount() == 0
|
||||
&& method.getReturnType() == boolean.class) {
|
||||
return Type.HAS;
|
||||
}
|
||||
if (target.type() == Type.GET && method.getParameterCount() == 0) {
|
||||
@@ -132,7 +145,8 @@ public class ProxyBuilder<T extends NBTProxy> implements InvocationHandler {
|
||||
if (method.getName().startsWith("get") && method.getParameterCount() == 0) {
|
||||
return Type.GET;
|
||||
}
|
||||
if (method.getName().startsWith("has") && method.getParameterCount() == 0 && method.getReturnType() == boolean.class) {
|
||||
if (method.getName().startsWith("has") && method.getParameterCount() == 0
|
||||
&& method.getReturnType() == boolean.class) {
|
||||
return Type.HAS;
|
||||
}
|
||||
return null;
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
package de.tr7zw.changeme.nbtapi.wrapper;
|
||||
|
||||
public interface ProxyList<T extends NBTProxy> extends Iterable<T> {
|
||||
|
||||
/**
|
||||
* Adds a new compound tag to the current compound tag
|
||||
*
|
||||
* @return A new instance of the class.
|
||||
*/
|
||||
T addCompound();
|
||||
|
||||
/**
|
||||
* Returns the number of elements in this collection. If this collection
|
||||
* contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE.
|
||||
*
|
||||
* @return the number of elements in this collection
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* Returns true if this collection contains no elements.
|
||||
*
|
||||
* @return true if this collection contains no elements
|
||||
*/
|
||||
boolean isEmpty();
|
||||
|
||||
/**
|
||||
* Get the object with the given id.
|
||||
*
|
||||
* @param id The id of the object to get.
|
||||
* @return The object with the given id.
|
||||
*/
|
||||
T get(int id);
|
||||
|
||||
/**
|
||||
* Removes the element at the specified position in this list
|
||||
*
|
||||
* @param i The index of the element to remove.
|
||||
*/
|
||||
void remove(int i);
|
||||
|
||||
}
|
||||
+11
-5
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
</parent>
|
||||
<artifactId>item-nbt-api-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -12,21 +12,27 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>nbt-injector</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>nbt-data-api</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.mojang</groupId>
|
||||
<artifactId>datafixerupper</artifactId>
|
||||
<version>4.0.26</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
@@ -39,7 +45,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-shade-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<version>3.5.3</version>
|
||||
<configuration>
|
||||
<createDependencyReducedPom>true</createDependencyReducedPom>
|
||||
<relocations>
|
||||
|
||||
@@ -50,6 +50,7 @@ import de.tr7zw.nbtapi.plugin.tests.tiles.TilesCustomNBTPersistentTest;
|
||||
import de.tr7zw.nbtinjector.NBTInjector;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemMergingTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.ItemStackConversionTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.LegacyItemTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.proxy.DataItemProxyTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.proxy.SimpleProxyTest;
|
||||
import de.tr7zw.nbtapi.plugin.tests.items.MetaTest;
|
||||
@@ -128,6 +129,9 @@ public class NBTAPI extends JavaPlugin {
|
||||
apiTests.add(new ItemConversionTest());
|
||||
apiTests.add(new ItemStackConversionTest());
|
||||
}
|
||||
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
apiTests.add(new LegacyItemTest());
|
||||
}
|
||||
apiTests.add(new EmptyItemTest());
|
||||
apiTests.add(new SmuggleTest());
|
||||
apiTests.add(new DataItemProxyTest());
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.compounds;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
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.ReadableNBT;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class ResolveTest implements Test {
|
||||
@@ -22,6 +26,33 @@ public class ResolveTest implements Test {
|
||||
.getInteger("other") != 123) {
|
||||
throw new NbtApiException("Wasn't able to check the value! The Item-NBT-API may not work!");
|
||||
}
|
||||
ReadWriteNBT comp = src.resolveOrCreateCompound("foo.bar.baz");
|
||||
comp.setIntArray("intarray", new int[] {1,2,3,4,5,6});
|
||||
comp.getStringList("strlist").addAll(Arrays.asList("Hello", "World!"));
|
||||
ReadWriteNBTCompoundList list = comp.getCompoundList("somelist");
|
||||
list.addCompound().setInteger("id", 1);
|
||||
list.addCompound().setInteger("id", 2);
|
||||
list.addCompound().setInteger("id", 3);
|
||||
list.addCompound().setInteger("id", 4);
|
||||
list.addCompound().setInteger("id", 5);
|
||||
list.addCompound().setInteger("id", 6);
|
||||
ReadableNBT listEntry = src.resolveCompound("foo.bar.baz.somelist[0]");
|
||||
if(listEntry == null || listEntry.getInteger("id") != 1) {
|
||||
throw new NbtApiException("The value is not what was expected! The Item-NBT-API may not work!");
|
||||
}
|
||||
ReadableNBT lastEntry = src.resolveOrCreateCompound("foo.bar.baz.somelist[-1]");
|
||||
if(lastEntry == null || lastEntry.getInteger("id") != 6) {
|
||||
throw new NbtApiException("The value is not what was expected! The Item-NBT-API may not work!");
|
||||
}
|
||||
if(src.resolveOrDefault("foo.bar.baz.intarray[1]", 0) != 2) {
|
||||
throw new NbtApiException("The value is not what was expected! The Item-NBT-API may not work!");
|
||||
}
|
||||
if(src.resolveOrNull("foo.bar.baz.intarray[2]", Integer.class) != (Integer)3) {
|
||||
throw new NbtApiException("The value is not what was expected! The Item-NBT-API may not work!");
|
||||
}
|
||||
if(!"World!".equals(src.resolveOrNull("foo.bar.baz.strlist[1]", String.class))) {
|
||||
throw new NbtApiException("The value is not what was expected! The Item-NBT-API may not work!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
+6
-1
@@ -4,22 +4,27 @@ import org.bukkit.Material;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadableNBT;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class DirectApplyMetaTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return; // skip
|
||||
}
|
||||
ItemStack baseItem = new ItemStack(Material.STONE);
|
||||
NBTItem nbti = new NBTItem(baseItem, true);
|
||||
nbti.setString("SomeKey", "SomeValue");
|
||||
nbti.modifyMeta(this::modifyMeta);
|
||||
|
||||
if (!new NBTItem(baseItem).hasTag("SomeKey") || !"SomeValue".equals(baseItem.getItemMeta().getDisplayName())) {
|
||||
throw new NbtApiException("The item was not modified correctly!");
|
||||
throw new NbtApiException("The item was not modified correctly! " + NBT.itemStackToNBT(baseItem));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,6 +20,13 @@ public class EmptyItemTest implements Test {
|
||||
if (nbti.getBoolean("test") == null || nbti.getString("test") == null || nbti.getFloatList("test") == null) {
|
||||
throw new NbtApiException("Getters return null instead of the default value");
|
||||
}
|
||||
if (!nbti.getKeys().isEmpty()) {
|
||||
throw new NbtApiException("getKeys() returned keys!");
|
||||
}
|
||||
nbti.removeKey("test");
|
||||
if (nbti.hasNBTData()) {
|
||||
throw new NbtApiException("Item reported to have data after remove");
|
||||
}
|
||||
nbti.setString("test", "test");
|
||||
if (!nbti.hasNBTData()) {
|
||||
throw new NbtApiException("Item reported to have no data");
|
||||
|
||||
@@ -6,12 +6,16 @@ import org.bukkit.inventory.meta.BookMeta;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBTItem;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.utils.MinecraftVersion;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class ItemMergingTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return; // skip, there is no vanilla nbt
|
||||
}
|
||||
ItemStack item = new ItemStack(Material.WRITTEN_BOOK);
|
||||
BookMeta bookMeta = (BookMeta) item.getItemMeta();
|
||||
bookMeta.setAuthor("Author");
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
package de.tr7zw.nbtapi.plugin.tests.items;
|
||||
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import de.tr7zw.changeme.nbtapi.NBT;
|
||||
import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
|
||||
import de.tr7zw.changeme.nbtapi.utils.DataFixerUtil;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
public class LegacyItemTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
ItemStack item = NBT
|
||||
.itemStackFromNBT(NBT.parseNBT("{id:cobblestone,Count:42,tag:{Enchantments:[{lvl:3,id:unbreaking}]}}"));
|
||||
if (item.getType() != Material.COBBLESTONE || item.getAmount() != 42
|
||||
|| item.getEnchantmentLevel(Enchantment.DURABILITY) != 3) {
|
||||
throw new NbtApiException("1.20 item didn't load correctly! " + item);
|
||||
}
|
||||
ReadWriteNBT nbt = NBT
|
||||
.parseNBT("{id:cobblestone,Count:42,tag:{display:{Name:\"test\"},ench:[{lvl:3,id:34}]}}");
|
||||
nbt = DataFixerUtil.fixUpItemData(nbt, DataFixerUtil.VERSION1_12_2, DataFixerUtil.getCurrentVersion());
|
||||
item = NBT.itemStackFromNBT(nbt);
|
||||
if (item.getType() != Material.COBBLESTONE || item.getAmount() != 42
|
||||
|| item.getEnchantmentLevel(Enchantment.DURABILITY) != 3
|
||||
|| !"test".equals(item.getItemMeta().getDisplayName())) {
|
||||
throw new NbtApiException("1.12.2 item didn't load correctly! " + item);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -6,12 +6,16 @@ import org.bukkit.inventory.ItemStack;
|
||||
|
||||
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;
|
||||
|
||||
public class MetaTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return; // skip
|
||||
}
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
NBT.modify(item, nbt -> {
|
||||
nbt.setInteger("HideFlags", 1);
|
||||
|
||||
+3
@@ -16,6 +16,9 @@ public class DataItemProxyTest implements Test {
|
||||
|
||||
@Override
|
||||
public void test() throws Exception {
|
||||
if(MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_20_R4)) {
|
||||
return; // skip
|
||||
}
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
ItemMeta meta = item.getItemMeta();
|
||||
meta.setDisplayName("Test");
|
||||
|
||||
+23
-3
@@ -9,6 +9,7 @@ import de.tr7zw.changeme.nbtapi.NbtApiException;
|
||||
import de.tr7zw.changeme.nbtapi.handler.NBTHandlers;
|
||||
import de.tr7zw.changeme.nbtapi.wrapper.NBTProxy;
|
||||
import de.tr7zw.changeme.nbtapi.wrapper.NBTTarget;
|
||||
import de.tr7zw.changeme.nbtapi.wrapper.ProxyList;
|
||||
import de.tr7zw.changeme.nbtapi.wrapper.NBTTarget.Type;
|
||||
import de.tr7zw.nbtapi.plugin.tests.Test;
|
||||
|
||||
@@ -18,12 +19,12 @@ public class SimpleProxyTest implements Test {
|
||||
public void test() throws Exception {
|
||||
ItemStack item = new ItemStack(Material.STONE);
|
||||
NBT.modify(item, TestInterface.class, ti -> {
|
||||
if(ti.hasKills()) {
|
||||
if (ti.hasKills()) {
|
||||
throw new NbtApiException("Item reported to have kills before setting data!");
|
||||
}
|
||||
ti.setKills(42);
|
||||
ti.addKill();
|
||||
if(!ti.hasKills()) {
|
||||
if (!ti.hasKills()) {
|
||||
throw new NbtApiException("Item reported to not have kills after setting data!");
|
||||
}
|
||||
if (!"{Kills:43}".equals(ti.toString())) {
|
||||
@@ -49,6 +50,23 @@ public class SimpleProxyTest implements Test {
|
||||
throw new NbtApiException("The handler in the proxy didn't work correctly!");
|
||||
}
|
||||
});
|
||||
NBT.modify(item, TestInterface.class, ti -> {
|
||||
Statistic a = ti.getStatistics().addCompound();
|
||||
a.setPoints(1);
|
||||
a.addPoint();
|
||||
a.addPoint();
|
||||
ti.getStatistics().addCompound();
|
||||
if (ti.getStatistics().size() != 2) {
|
||||
throw new NbtApiException("List size not as expected!");
|
||||
}
|
||||
ti.getStatistics().remove(1);
|
||||
if (ti.getStatistics().size() != 1) {
|
||||
throw new NbtApiException("List size not as expected!");
|
||||
}
|
||||
if (ti.getStatistics().iterator().next().getPoints() != 3) {
|
||||
throw new NbtApiException("Points not as expected!");
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public interface TestInterface extends NBTProxy {
|
||||
@@ -59,7 +77,7 @@ public class SimpleProxyTest implements Test {
|
||||
}
|
||||
|
||||
public boolean hasKills();
|
||||
|
||||
|
||||
public void setKills(int amount);
|
||||
|
||||
public int getKills();
|
||||
@@ -77,6 +95,8 @@ public class SimpleProxyTest implements Test {
|
||||
|
||||
public void setItem(ItemStack item);
|
||||
|
||||
public ProxyList<Statistic> getStatistics();
|
||||
|
||||
}
|
||||
|
||||
public interface Statistic extends NBTProxy {
|
||||
|
||||
Whitespace-only changes.
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</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.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</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.20.txt");
|
||||
File input = new File("minecraft_server.1.20.5.txt");
|
||||
List<String> lines = Files.readAllLines(input.toPath());
|
||||
|
||||
Map<String, ClassWrapper> classes = new HashMap<>();
|
||||
@@ -40,6 +40,9 @@ public class MappingsParser {
|
||||
ClassWrapper nmsWrapper = null;
|
||||
Map<ReflectionMethod, String> mappedNames = null;
|
||||
for (String line : lines) {
|
||||
if (line.startsWith("#")) {
|
||||
continue;
|
||||
}
|
||||
if (!line.startsWith(" ")) {
|
||||
if (nmsWrapper != null && mappedNames != null) {
|
||||
proccessMapping(nmsWrapper, mappedNames);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>nbt-data-api</artifactId>
|
||||
@@ -14,7 +14,7 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</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.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</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.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
<packaging>pom</packaging>
|
||||
<inceptionYear>2015</inceptionYear>
|
||||
<organization>
|
||||
@@ -90,7 +90,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.11.0</version>
|
||||
<version>3.13.0</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
@@ -99,7 +99,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.0</version>
|
||||
<version>3.3.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
@@ -112,7 +112,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<version>3.6.3</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
</configuration>
|
||||
@@ -141,13 +141,26 @@
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>license-maven-plugin</artifactId>
|
||||
<version>2.2.0</version>
|
||||
<version>2.4.0</version>
|
||||
<configuration>
|
||||
<licenseName>mit</licenseName>
|
||||
<licenseFile>LICENSE</licenseFile>
|
||||
<force>true</force>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>dev.tr7zw.nojitpack.maven</groupId>
|
||||
<artifactId>no-jitpack-maven-plugin</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>antijitpack</id>
|
||||
<goals>
|
||||
<goal>antijitpack</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.diffplug.spotless</groupId>
|
||||
<artifactId>spotless-maven-plugin</artifactId>
|
||||
|
||||
@@ -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.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</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.12.0-SNAPSHOT</version>
|
||||
<version>2.12.4</version>
|
||||
</dependency>
|
||||
...
|
||||
<plugins>
|
||||
|
||||
@@ -32,11 +32,11 @@ ReadWriteNBT nbt = NBT.parseNBT(json);
|
||||
|
||||
```java
|
||||
// Items
|
||||
ItemStack itemStack = NBT.nbtToItemStack(nbt);
|
||||
ItemStack[] itemStacks = NBT.nbtToItemStackArray(nbt);
|
||||
ItemStack itemStack = NBT.itemStackFromNBT(nbt);
|
||||
ItemStack[] itemStacks = NBT.itemStackArrayFromNBT(nbt);
|
||||
// Entity/Tiles
|
||||
In general "new NBTEntity(entity).mergeCompound(nbt)", but you might want to remove some data from the nbt first like the Location, uuid and entityId
|
||||
// Gameprofile
|
||||
GameProfile profile = NBT.nbtToGameProfile(nbt);
|
||||
GameProfile profile = NBT.gameProfileFromNBT(nbt);
|
||||
```
|
||||
|
||||
@@ -27,11 +27,11 @@ GameProfile profile = NBT.nbtToGameProfile(nbt);
|
||||
// ItemStack to NBT
|
||||
ReadWriteNBT nbt = NBT.itemStackToNBT(itemStack);
|
||||
// NBT to ItemStack
|
||||
ItemStack itemStack = NBT.nbtToItemStack(nbt);
|
||||
ItemStack itemStack = NBT.itemStackFromNBT(nbt);
|
||||
// ItemStack Array to NBT
|
||||
ReadWriteNBT nbt = NBT.itemStackArrayToNBT(itemStacks);
|
||||
// NBT to ItemStack Array
|
||||
ItemStack[] itemStacks = NBT.nbtToItemStackArray(nbt);
|
||||
ItemStack[] itemStacks = NBT.itemStackArrayFromNBT(nbt);
|
||||
// Creating a new NBT tag
|
||||
ReadWriteNBT nbt = NBT.createNBTObject();
|
||||
// Parsing a Mojang-Json String to NBT
|
||||
|
||||
Reference in new issue
Block a user