Compare commits
18
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
dc333ae25a | ||
|
|
d18adb459d | ||
|
|
bc914b73bc | ||
|
|
60172ea2a7 | ||
|
|
7b30c1533e | ||
|
|
a1c90658f3 | ||
|
|
0b0fdeaa60 | ||
|
|
d989797453 | ||
|
|
c70aefe670 | ||
|
|
7d0fc09470 | ||
|
|
427113e6f0 | ||
|
|
20ae1cb178 | ||
|
|
83b011fafa | ||
|
|
3affeee151 | ||
|
|
06da2114ad | ||
|
|
a3efdc622b | ||
|
|
9b60978af6 | ||
|
|
598db19d8d |
No files matched your search
@@ -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>.
|
||||
@@ -5,7 +5,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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!");
|
||||
|
||||
@@ -254,7 +254,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.
|
||||
*/
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
@@ -406,22 +407,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
|
||||
*
|
||||
@@ -570,7 +555,7 @@ 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;
|
||||
@@ -588,6 +573,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -21,7 +21,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(1201, true);
|
||||
|
||||
private static MinecraftVersion version;
|
||||
private static Boolean hasGsonSupport;
|
||||
@@ -36,7 +36,7 @@ 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.0";
|
||||
|
||||
private final int versionId;
|
||||
private final boolean mojangMapping;
|
||||
|
||||
@@ -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 + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+14
-1
@@ -120,9 +120,22 @@ 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");
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
public static Map<String, String> getMapping() {
|
||||
switch (MinecraftVersion.getVersion()) {
|
||||
case MC1_20_R2:
|
||||
return MC1_20R2;
|
||||
case MC1_20_R1:
|
||||
return MC1_20R1;
|
||||
case MC1_19_R2:
|
||||
@@ -134,7 +147,7 @@ public class MojangToMapping {
|
||||
case MC1_18_R1:
|
||||
return MC1_18R1;
|
||||
default:
|
||||
return MC1_19R2;// throw new NbtApiException("This version of the NBTAPI is not compatible with
|
||||
return MC1_20R2;// throw new NbtApiException("This version of the NBTAPI is not compatible with
|
||||
// this server version!");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</version>
|
||||
</parent>
|
||||
<artifactId>item-nbt-api-plugin</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
@@ -12,19 +12,19 @@
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-api</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>nbt-injector</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>nbt-data-api</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
@@ -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!");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
+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 {
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
<parent>
|
||||
<groupId>de.tr7zw</groupId>
|
||||
<artifactId>item-nbt-parent</artifactId>
|
||||
<version>2.12.0-SNAPSHOT</version>
|
||||
<version>2.12.0</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.0</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.2.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.0</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.0</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.0</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.0</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.0</version>
|
||||
<packaging>pom</packaging>
|
||||
<inceptionYear>2015</inceptionYear>
|
||||
<organization>
|
||||
@@ -112,7 +112,7 @@
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.5.0</version>
|
||||
<version>3.6.0</version>
|
||||
<configuration>
|
||||
<source>8</source>
|
||||
</configuration>
|
||||
@@ -148,6 +148,19 @@
|
||||
<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.0</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.0</version>
|
||||
</dependency>
|
||||
...
|
||||
<plugins>
|
||||
|
||||
Reference in new issue
Block a user