Compare commits

..
Author SHA1 Message Date
tr7zw 0228b68a7c Change NBT.modify calls on (Block-)Entities to be atomic 2023-07-03 23:47:43 +02:00
tr7zw 2a80e8ff63 Add readonly modes for Entity/TileEntity 2023-07-02 20:48:53 +02:00
tr7zw cfd04fffa6 Format 2023-06-12 18:21:35 +02:00
tr7zw c8231eb050 Testing readOnly mode for NBT.get(ItemStack) 2023-06-12 18:17:37 +02:00
tr7zw f7bceec594 Start 2.12.0-SNAPSHOT 2023-06-12 18:02:11 +02:00
tr7zw c377927ee6 Tag 2.11.3 2023-06-07 20:20:15 +02:00
tr7zw 0653f47bc1 Add 1.20 support 2023-06-07 20:14:05 +02:00
tr7zw 5588fc60b4 Merge pull request #227 from tr7zw/dependabot/maven/org.apache.maven.plugins-maven-source-plugin-3.3.0
Bump maven-source-plugin from 3.2.1 to 3.3.0
2023-05-23 10:08:50 +02:00
dependabot[bot] 7c27630c10 Bump maven-source-plugin from 3.2.1 to 3.3.0
Bumps [maven-source-plugin](https://github.com/apache/maven-source-plugin) from 3.2.1 to 3.3.0.
- [Commits](https://github.com/apache/maven-source-plugin/compare/maven-source-plugin-3.2.1...maven-source-plugin-3.3.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-source-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-22 23:58:27 +00:00
tr7zw 45dd70afad Merge pull request #224 from tom-devv/typo
Fix a small typo in nbt-injector
2023-05-10 21:50:05 +02:00
tom-devv 280ce7a50d Fix typo 2023-05-10 20:40:00 +01:00
tr7zw 175065dac4 Try to use the actual plugin name for issues.
Add a warning for using "de.tr7zw.nbtapi" as relocation target in other plugins.
Fix #206
2023-05-08 00:14:48 +02:00
tr7zw f4bfca8ba8 Disable the update check by default for shaded versions 2023-05-07 20:14:17 +02:00
tr7zw 37378b4ee1 Merge pull request #221 from tr7zw/dependabot/maven/org.codehaus.mojo-license-maven-plugin-2.0.1
Bump license-maven-plugin from 2.0.0 to 2.0.1
2023-05-06 16:16:23 +02:00
tr7zw 662c0bb92d Merge pull request #223 from SoSeDiK/tagcheck
Allow checking nbt type alongside the key
2023-05-06 16:15:57 +02:00
SoSeDiK c485f8c967 Allow checking nbt type alongside the key 2023-05-06 17:01:23 +03:00
tr7zw 5759b68d89 Merge pull request #222 from SoSeDiK/bool
Add support for boolean in getOrDefault/getOrNull
2023-05-06 15:50:28 +02:00
tr7zw c22cbaaaf0 Start next snapshot 2023-05-06 15:49:40 +02:00
SoSeDiK 4a0e1b5557 Add support for boolean in getOrDefault/getOrNull 2023-05-06 16:48:45 +03:00
dependabot[bot] 1c2e61875e Bump license-maven-plugin from 2.0.0 to 2.0.1
Bumps [license-maven-plugin](https://github.com/mojohaus/license-maven-plugin) from 2.0.0 to 2.0.1.
- [Release notes](https://github.com/mojohaus/license-maven-plugin/releases)
- [Commits](https://github.com/mojohaus/license-maven-plugin/compare/license-maven-plugin-2.0.0...2.0.1)

---
updated-dependencies:
- dependency-name: org.codehaus.mojo:license-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-05-05 23:57:37 +00:00
tr7zw 6f25881eda Add 1.19.4 as supported version. 2.11.2 2023-03-16 19:15:55 +01:00
tr7zw bd69504f54 Merge pull request #216 from tr7zw/dependabot/maven/org.apache.maven.plugins-maven-compiler-plugin-3.11.0
Bump maven-compiler-plugin from 3.10.1 to 3.11.0
2023-03-09 21:08:23 +01:00
dependabot[bot] 85d7224fdd Bump maven-compiler-plugin from 3.10.1 to 3.11.0
Bumps [maven-compiler-plugin](https://github.com/apache/maven-compiler-plugin) from 3.10.1 to 3.11.0.
- [Release notes](https://github.com/apache/maven-compiler-plugin/releases)
- [Commits](https://github.com/apache/maven-compiler-plugin/compare/maven-compiler-plugin-3.10.1...maven-compiler-plugin-3.11.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-compiler-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-02-28 00:00:07 +00:00
tr7zw 8dc51a3d30 Merge pull request #214 from tr7zw/dependabot/maven/org.apache.maven.plugins-maven-javadoc-plugin-3.5.0
Bump maven-javadoc-plugin from 3.4.1 to 3.5.0
2023-02-16 10:03:07 +01:00
dependabot[bot] 8a440889b0 Bump maven-javadoc-plugin from 3.4.1 to 3.5.0
Bumps [maven-javadoc-plugin](https://github.com/apache/maven-javadoc-plugin) from 3.4.1 to 3.5.0.
- [Release notes](https://github.com/apache/maven-javadoc-plugin/releases)
- [Commits](https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.4.1...maven-javadoc-plugin-3.5.0)

---
updated-dependencies:
- dependency-name: org.apache.maven.plugins:maven-javadoc-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-02-15 23:57:59 +00:00
tr7zw 4a93fdc869 Fix issue with ReadWriteNBT returning ReadableNBT 2023-02-15 22:06:17 +01:00
tr7zw c905de852d Merge pull request #210 from tr7zw/revert-209-dependabot/maven/com.diffplug.spotless-spotless-maven-plugin-2.31.0
Revert "Bump spotless-maven-plugin from 2.30.0 to 2.31.0"
2023-01-31 12:16:37 +01:00
tr7zw fd7da1e3fb Revert "Bump spotless-maven-plugin from 2.30.0 to 2.31.0" 2023-01-31 12:03:09 +01:00
tr7zw 1123e27b10 Merge pull request #209 from tr7zw/dependabot/maven/com.diffplug.spotless-spotless-maven-plugin-2.31.0
Bump spotless-maven-plugin from 2.30.0 to 2.31.0
2023-01-31 11:42:42 +01:00
dependabot[bot] a97ed32097 Bump spotless-maven-plugin from 2.30.0 to 2.31.0
Bumps [spotless-maven-plugin](https://github.com/diffplug/spotless) from 2.30.0 to 2.31.0.
- [Release notes](https://github.com/diffplug/spotless/releases)
- [Changelog](https://github.com/diffplug/spotless/blob/main/CHANGES.md)
- [Commits](https://github.com/diffplug/spotless/compare/lib/2.30.0...lib/2.31.0)

---
updated-dependencies:
- dependency-name: com.diffplug.spotless:spotless-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-26 23:01:52 +00:00
tr7zw 05b4249bb4 Merge pull request #207 from tr7zw/dependabot/maven/com.diffplug.spotless-spotless-maven-plugin-2.30.0
Bump spotless-maven-plugin from 2.29.0 to 2.30.0
2023-01-25 20:33:17 +01:00
tr7zw e399590d1d Bump for 2.11.2-SNAPSHOT 2023-01-25 20:32:30 +01:00
tr7zw 310682ea25 Merge pull request #208 from FREE2WIN2/master
Fix Wiki repository url for maven and gradle.
2023-01-24 11:02:17 +01:00
FREE2WIN2 6b70966926 Wiki: Fix repository url of codemc repo 2023-01-20 23:03:47 +01:00
FREE2WIN2 79275c0c19 Wiki: Fix repository url of codemc repo 2023-01-20 23:03:03 +01:00
dependabot[bot] 2b21ef46bb Bump spotless-maven-plugin from 2.29.0 to 2.30.0
Bumps [spotless-maven-plugin](https://github.com/diffplug/spotless) from 2.29.0 to 2.30.0.
- [Release notes](https://github.com/diffplug/spotless/releases)
- [Changelog](https://github.com/diffplug/spotless/blob/main/CHANGES.md)
- [Commits](https://github.com/diffplug/spotless/compare/lib/2.29.0...lib/2.30.0)

---
updated-dependencies:
- dependency-name: com.diffplug.spotless:spotless-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-16 23:03:41 +00:00
tr7zw 79e1e62862 Merge pull request #205 from tr7zw/dependabot/maven/com.diffplug.spotless-spotless-maven-plugin-2.29.0
Bump spotless-maven-plugin from 2.28.0 to 2.29.0
2023-01-03 23:51:49 +01:00
dependabot[bot] 852ddf4a22 Bump spotless-maven-plugin from 2.28.0 to 2.29.0
Bumps [spotless-maven-plugin](https://github.com/diffplug/spotless) from 2.28.0 to 2.29.0.
- [Release notes](https://github.com/diffplug/spotless/releases)
- [Changelog](https://github.com/diffplug/spotless/blob/main/CHANGES.md)
- [Commits](https://github.com/diffplug/spotless/compare/lib/2.28.0...lib/2.29.0)

---
updated-dependencies:
- dependency-name: com.diffplug.spotless:spotless-maven-plugin
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-01-02 23:03:22 +00:00
25 changed files with 315 additions and 49 deletions

No files matched your search

+1 -1
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</version>
</parent>
<artifactId>item-nbt-api</artifactId>
@@ -36,7 +36,7 @@ public class NBT {
* @return The function is being returned.
*/
public static <T> T get(ItemStack item, Function<ReadableNBT, T> getter) {
return getter.apply(new NBTItem(item));
return getter.apply(new NBTItem(item, false, true));
}
/**
@@ -48,7 +48,7 @@ public class NBT {
* @return The NBTEntity class is being returned.
*/
public static <T> T get(Entity entity, Function<ReadableNBT, T> getter) {
return getter.apply(new NBTEntity(entity));
return getter.apply(new NBTEntity(entity, true));
}
/**
@@ -62,7 +62,7 @@ public class NBT {
* @return The return type is the same as the type of the getter function.
*/
public static <T> T get(BlockState blockState, Function<ReadableNBT, T> getter) {
return getter.apply(new NBTTileEntity(blockState));
return getter.apply(new NBTTileEntity(blockState, true));
}
/**
@@ -130,7 +130,11 @@ public class NBT {
* @return The return type is the same as the return type of the function.
*/
public static <T> T modify(Entity entity, Function<ReadWriteNBT, T> function) {
return function.apply(new NBTEntity(entity));
NBTEntity nbtEnt = new NBTEntity(entity);
NBTContainer cont = new NBTContainer(nbtEnt.getCompound());
T ret = function.apply(cont);
nbtEnt.setCompound(cont.getCompound());
return ret;
}
/**
@@ -141,7 +145,10 @@ public class NBT {
* @param consumer The consumer that will be called with the NBTEntity.
*/
public static void modify(Entity entity, Consumer<ReadWriteNBT> consumer) {
consumer.accept(new NBTEntity(entity));
NBTEntity nbtEnt = new NBTEntity(entity);
NBTContainer cont = new NBTContainer(nbtEnt.getCompound());
consumer.accept(cont);
nbtEnt.setCompound(cont.getCompound());
}
/**
@@ -177,7 +184,11 @@ public class NBT {
* @return The return type is the same as the return type of the function.
*/
public static <T> T modify(BlockState blockState, Function<ReadWriteNBT, T> function) {
return function.apply(new NBTTileEntity(blockState));
NBTTileEntity blockEnt = new NBTTileEntity(blockState);
NBTContainer cont = new NBTContainer(blockEnt.getCompound());
T ret = function.apply(cont);
blockEnt.setCompound(cont);
return ret;
}
/**
@@ -190,7 +201,10 @@ public class NBT {
* takes a ReadWriteNBT and does something with it.
*/
public static void modify(BlockState blockState, Consumer<ReadWriteNBT> consumer) {
consumer.accept(new NBTTileEntity(blockState));
NBTTileEntity blockEnt = new NBTTileEntity(blockState);
NBTContainer cont = new NBTContainer(blockEnt.getCompound());
consumer.accept(cont);
blockEnt.setCompound(cont);
}
/**
@@ -600,6 +600,8 @@ public class NBTCompound implements ReadWriteNBT {
}
/**
* Checks whether the provided key exists
*
* @param key String key
* @return True if the key is set
* @deprecated Use {@link #hasTag(String)} instead
@@ -610,6 +612,8 @@ public class NBTCompound implements ReadWriteNBT {
}
/**
* Checks whether the provided key exists
*
* @param key String key
* @return true, if the key is set
*/
@@ -856,8 +860,8 @@ public class NBTCompound implements ReadWriteNBT {
* Returns the stored value if exists, or provided value otherwise.
* <p>
* Supported types:
* {@code byte/Byte, short/Short, int/Integer, long/Long, float/Float, double/Double, byte[], int[]},
* {@link String}, {@link UUID}
* {@code Boolean, Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}, and {@link Enum}
*
* @param key key
* @param defaultValue default non-null value
@@ -873,6 +877,8 @@ public class NBTCompound implements ReadWriteNBT {
return defaultValue;
Class<?> clazz = defaultValue.getClass();
if (clazz == Boolean.class)
return (T) getBoolean(key);
if (clazz == Byte.class)
return (T) getByte(key);
if (clazz == Short.class)
@@ -908,13 +914,13 @@ public class NBTCompound implements ReadWriteNBT {
* Returns the stored value if exists, or null.
* <p>
* Supported types:
* {@code Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}
* {@code Boolean, Byte, Short, Integer, Long, Float, Double, byte[], int[]},
* {@link String}, {@link UUID}, and {@link Enum}
*
* @param key key
* @param type data type
* @param <T> value type
* @return Stored or provided value
* @return Stored value or null
*/
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
@@ -924,6 +930,8 @@ public class NBTCompound implements ReadWriteNBT {
if (!hasTag(key))
return null;
if (type == Boolean.class)
return (T) getBoolean(key);
if (type == Byte.class)
return (T) getByte(key);
if (type == Short.class)
@@ -19,6 +19,26 @@ import de.tr7zw.changeme.nbtapi.utils.annotations.CheckUtil;
public class NBTEntity extends NBTCompound {
private final Entity ent;
private final boolean readonly;
private final Object compound;
/**
* @param entity Any valid Bukkit Entity
* @param readonly Readonly makes a copy at init, only reading from that copy
*/
protected NBTEntity(Entity entity, boolean readonly) {
super(null, null);
if (entity == null) {
throw new NullPointerException("Entity can't be null!");
}
this.readonly = readonly;
ent = entity;
if (readonly) {
this.compound = getCompound();
} else {
this.compound = null;
}
}
/**
* @param entity Any valid Bukkit Entity
@@ -28,11 +48,17 @@ public class NBTEntity extends NBTCompound {
if (entity == null) {
throw new NullPointerException("Entity can't be null!");
}
this.readonly = false;
this.compound = null;
ent = entity;
}
@Override
public Object getCompound() {
// this runs before async check, since it's just a copy
if (readonly && compound != null) {
return compound;
}
if (!Bukkit.isPrimaryThread())
throw new NbtApiException("Entity NBT needs to be accessed sync!");
return NBTReflectionUtil.getEntityNBTTagCompound(NBTReflectionUtil.getNMSEntity(ent));
@@ -40,6 +66,9 @@ public class NBTEntity extends NBTCompound {
@Override
protected void setCompound(Object compound) {
if (readonly) {
throw new NbtApiException("Tried setting data in read only mode!");
}
if (!Bukkit.isPrimaryThread())
throw new NbtApiException("Entity NBT needs to be accessed sync!");
NBTReflectionUtil.setEntityNBTTag(compound, NBTReflectionUtil.getNMSEntity(ent));
@@ -9,6 +9,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.nmsmappings.ClassWrapper;
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
/**
@@ -22,6 +23,7 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
private ItemStack bukkitItem;
private boolean directApply;
private boolean readOnly;
private ItemStack originalSrcStack = null;
/**
@@ -33,6 +35,30 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
this(item, false);
}
/**
* @param item
* @param directApply
* @param readOnly When turned on, no copy of the source item is created.
* Modifying the stack in that case is not valid! Also
* overwrites directApply
*/
protected NBTItem(ItemStack item, boolean directApply, boolean readOnly) {
super(null, null);
if (item == null || item.getType() == Material.AIR || item.getAmount() <= 0) {
throw new NullPointerException("ItemStack can't be null/air/amount of 0! This is not a NBTAPI bug!");
}
if (readOnly) {
bukkitItem = item;
this.readOnly = true;
} else {
this.directApply = directApply;
bukkitItem = item.clone();
if (directApply) {
this.originalSrcStack = item;
}
}
}
/**
* Constructor for NBTItems. The ItemStack will be cloned! If directApply is
* true, all changed will be mapped to the original item. Changes to the NBTItem
@@ -55,11 +81,17 @@ public class NBTItem extends NBTCompound implements ReadWriteItemNBT {
@Override
public Object getCompound() {
if (readOnly && ClassWrapper.CRAFT_ITEMSTACK.getClazz().isAssignableFrom(bukkitItem.getClass())) {
return NBTReflectionUtil.getItemRootNBTTagCompound(NBTReflectionUtil.getCraftItemHandle(bukkitItem));
}
return NBTReflectionUtil.getItemRootNBTTagCompound(ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem));
}
@Override
protected void setCompound(Object compound) {
if (readOnly) {
throw new NbtApiException("Tried setting data in read only mode!");
}
Object stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, bukkitItem);
ReflectionMethod.ITEMSTACK_SET_TAG.run(stack, compound);
bukkitItem = (ItemStack) ReflectionMethod.ITEMSTACK_BUKKITMIRROR.run(null, stack);
@@ -13,6 +13,7 @@ import java.util.UUID;
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.GsonWrapper;
@@ -32,6 +33,7 @@ import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod;
public class NBTReflectionUtil {
private static Field field_unhandledTags = null;
private static Field field_handle = null;
static {
try {
@@ -39,6 +41,12 @@ public class NBTReflectionUtil {
field_unhandledTags.setAccessible(true);
} catch (NoSuchFieldException e) {
}
try {
field_handle = ClassWrapper.CRAFT_ITEMSTACK.getClazz().getDeclaredField("handle");
field_handle.setAccessible(true);
} catch (NoSuchFieldException e) {
}
}
@@ -96,6 +104,21 @@ public class NBTReflectionUtil {
}
}
/**
* Gets the nms handle ItemStack from a CraftItemStack. Passing Spigot
* ItemStacks will cause an error!
*
* @param item
* @return
*/
public static Object getCraftItemHandle(ItemStack item) {
try {
return field_handle.get(item);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new NbtApiException("Error getting handle from " + item.getClass(), e);
}
}
/**
* Writes a Compound to an OutputStream
*
@@ -294,7 +317,7 @@ public class NBTReflectionUtil {
if ((boolean) ReflectionMethod.COMPOUND_HAS_KEY.run(compound, name)) {
return ReflectionMethod.COMPOUND_GET_COMPOUND.run(compound, name);
} else {
throw new NbtApiException("Tried getting invalide compound '" + name + "' from '" + compound + "'!");
throw new NbtApiException("Tried getting invalid compound '" + name + "' from '" + compound + "'!");
}
} catch (Exception e) {
throw new NbtApiException("Exception while getting NBT subcompounds!", e);
@@ -445,7 +468,7 @@ public class NBTReflectionUtil {
* @param key
* @param type
* @param clazz
* @return The list at that key. Null if it's an invalide type
* @return The list at that key. Null if it's an invalid type
*/
@SuppressWarnings("unchecked")
public static <T> NBTList<T> getList(NBTCompound comp, String key, NBTType type, Class<T> clazz) {
@@ -20,6 +20,26 @@ import de.tr7zw.changeme.nbtapi.utils.annotations.CheckUtil;
public class NBTTileEntity extends NBTCompound {
private final BlockState tile;
private final boolean readonly;
private final Object compound;
/**
* @param tile BlockState from any TileEntity
* @param readonly Readonly makes a copy at init, only reading from that copy
*/
public NBTTileEntity(BlockState tile, boolean readonly) {
super(null, null);
if (tile == null || (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_8_R3) && !tile.isPlaced())) {
throw new NullPointerException("Tile can't be null/not placed!");
}
this.tile = tile;
this.readonly = readonly;
if (readonly) {
this.compound = getCompound();
} else {
this.compound = null;
}
}
/**
* @param tile BlockState from any TileEntity
@@ -29,11 +49,17 @@ public class NBTTileEntity extends NBTCompound {
if (tile == null || (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_8_R3) && !tile.isPlaced())) {
throw new NullPointerException("Tile can't be null/not placed!");
}
this.readonly = false;
this.compound = null;
this.tile = tile;
}
@Override
public Object getCompound() {
// this runs before async check, since it's just a copy
if (readonly && compound != null) {
return compound;
}
if (!Bukkit.isPrimaryThread())
throw new NbtApiException("BlockEntity NBT needs to be accessed sync!");
return NBTReflectionUtil.getTileEntityNBTTagCompound(tile);
@@ -41,6 +67,9 @@ public class NBTTileEntity extends NBTCompound {
@Override
protected void setCompound(Object compound) {
if (readonly) {
throw new NbtApiException("Tried setting data in read only mode!");
}
if (!Bukkit.isPrimaryThread())
throw new NbtApiException("BlockEntity NBT needs to be accessed sync!");
NBTReflectionUtil.setTileEntityNBTTagCompound(tile, compound);
@@ -129,7 +129,13 @@ public interface ReadWriteNBT extends ReadableNBT {
* @param name
* @return
*/
ReadableNBT getOrCreateCompound(String name);
ReadWriteNBT getOrCreateCompound(String name);
/**
* @param name
* @return The Compound instance or null
*/
ReadWriteNBT getCompound(String name);
/**
* Set a key to the given Enum value. It gets stored as a String. Passing null
@@ -132,11 +132,24 @@ public interface ReadableNBT {
UUID getUUID(String key);
/**
* Checks whether the provided key exists
*
* @param key String key
* @return true, if the key is set
*/
boolean hasTag(String key);
/**
* Checks whether the provided key exists and has the specified type
*
* @param key String key
* @param type nbt tag type
* @return whether the key is set and has the specified type
*/
default boolean hasTag(String key, NBTType type) {
return hasTag(key) && getType(key) == type;
}
/**
* @return Set of all stored Keys
*/
@@ -19,21 +19,21 @@ 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_R2(1192, true), MC1_19_R3(1193, true), MC1_20_R1(1201, true);
private static MinecraftVersion version;
private static Boolean hasGsonSupport;
private static Boolean isForgePresent;
private static boolean bStatsDisabled = false;
private static boolean disablePackageWarning = false;
private static boolean updateCheckDisabled = false;
private static boolean updateCheckDisabled = true;
/**
* Logger used by the api
*/
private static Logger logger = Logger.getLogger("NBTAPI");
// NBT-API Version
protected static final String VERSION = "2.11.1";
protected static final String VERSION = "2.12.0-SNAPSHOT";
private final int versionId;
private final boolean mojangMapping;
@@ -116,7 +116,7 @@ public enum MinecraftVersion {
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 at " + MinecraftVersion.class.getName()
+ VERSION + ") located in " + VersionChecker.getPlugin()
+ ". The NBT-API will try to work as good as it can! Some functions may not work!");
}
init();
@@ -156,6 +156,18 @@ public enum MinecraftVersion {
logger.warning(
"#########################################- NBTAPI -#########################################");
}
if (!disablePackageWarning && !"NBTAPI".equals(VersionChecker.getPlugin())
&& MinecraftVersion.class.getPackage().getName().equals("de.tr7zw.nbtapi.utils")) {
logger.warning(
"#########################################- NBTAPI -#########################################");
logger.warning(
"The NBT-API inside " + VersionChecker.getPlugin() + " is located at 'de.tr7zw.nbtapi.utils'!");
logger.warning(
"This package name is reserved for the official NBTAPI plugin, and not intended to be used for shading!");
logger.warning("Please change the relocate to something else. For example: com.example.util.nbtapi");
logger.warning(
"#########################################- NBTAPI -#########################################");
}
}
/**
@@ -210,6 +222,14 @@ public enum MinecraftVersion {
updateCheckDisabled = true;
}
/**
* Enables the update check. Uses Spiget to get the current version and prints a
* warning when outdated.
*/
public static void enableUpdateCheck() {
updateCheckDisabled = false;
}
/**
* Forcefully disables the log message for plugins not shading the API to
* another location. This may be helpful for networks or development
@@ -1,11 +1,13 @@
package de.tr7zw.changeme.nbtapi.utils;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import org.bukkit.configuration.file.YamlConfiguration;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@@ -44,7 +46,7 @@ public class VersionChecker {
int versionDifference = getVersionDifference(latest.get("name").getAsString());
if (versionDifference == -1) { // Outdated
MinecraftVersion.getLogger().log(Level.WARNING,
"[NBTAPI] The NBT-API located at '" + NBTItem.class.getPackage() + "' seems to be outdated!");
"[NBTAPI] The NBT-API in '" + getPlugin() + "' seems to be outdated!");
MinecraftVersion.getLogger().log(Level.WARNING, "[NBTAPI] Current Version: '" + MinecraftVersion.VERSION
+ "' Newest Version: " + latest.get("name").getAsString() + "'");
MinecraftVersion.getLogger().log(Level.WARNING,
@@ -54,7 +56,7 @@ public class VersionChecker {
if (!hideOk)
MinecraftVersion.getLogger().log(Level.INFO, "[NBTAPI] The NBT-API seems to be up-to-date!");
} else if (versionDifference == 1) {
MinecraftVersion.getLogger().log(Level.INFO, "[NBTAPI] The NBT-API at '" + NBTItem.class.getPackage()
MinecraftVersion.getLogger().log(Level.INFO, "[NBTAPI] The NBT-API in '" + getPlugin()
+ "' seems to be a future Version, not yet released on Spigot/CurseForge! This is not an error!");
MinecraftVersion.getLogger().log(Level.INFO, "[NBTAPI] Current Version: '" + MinecraftVersion.VERSION
+ "' Newest Version: " + latest.get("name").getAsString() + "'");
@@ -105,4 +107,19 @@ public class VersionChecker {
return 1;
}
protected static String getPlugin() {
ClassLoader classLoader = VersionChecker.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("plugin.yml");
if (inputStream != null) {
try (InputStreamReader reader = new InputStreamReader(inputStream)) {
YamlConfiguration pluginYml = YamlConfiguration.loadConfiguration(reader);
return pluginYml.getString("name");
} catch (IOException e) {
// ignored
}
}
return NBTItem.class.getPackage().getName();
}
}
@@ -41,7 +41,6 @@ public class MojangToMapping {
put("net.minecraft.nbt.CompoundTag#getShort(java.lang.String)", "g");
put("net.minecraft.nbt.CompoundTag#putByte(java.lang.String,byte)", "a");
put("net.minecraft.nbt.CompoundTag#getAllKeys()", "d");
put("net.minecraft.nbt.CompoundTag#getAllKeys()", "d");
put("net.minecraft.nbt.CompoundTag#putUUID(java.lang.String,java.util.UUID)", "a");
put("net.minecraft.nbt.CompoundTag#putShort(java.lang.String,short)", "a");
put("net.minecraft.nbt.CompoundTag#getLong(java.lang.String)", "i");
@@ -108,8 +107,22 @@ public class MojangToMapping {
};
@SuppressWarnings("serial")
private static Map<String, String> MC1_20R1 = new HashMap<String, String>() {
{
putAll(MC1_19R2);
put("net.minecraft.world.entity.Entity#getEncodeId()", "br");
put("net.minecraft.world.item.ItemStack#getTag()", "v");
}
};
public static Map<String, String> getMapping() {
switch (MinecraftVersion.getVersion()) {
case MC1_20_R1:
return MC1_20R1;
case MC1_19_R2:
return MC1_19R2;
case MC1_19_R1:
+4 -4
View File
@@ -4,7 +4,7 @@
<parent>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</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.11.1</version>
<version>2.12.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>nbt-injector</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>nbt-data-api</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
@@ -27,6 +27,7 @@ import de.tr7zw.nbtapi.plugin.tests.compounds.InterfaceTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.IteratorTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.ListTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.MergeTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.ModernSubCompoundsTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.RemovingKeys;
import de.tr7zw.nbtapi.plugin.tests.compounds.StreamTest;
import de.tr7zw.nbtapi.plugin.tests.compounds.SubCompoundsTest;
@@ -77,6 +78,8 @@ public class NBTAPI extends JavaPlugin {
if (!getConfig().getBoolean("updateCheck.enabled")) {
getLogger().info("Update check disabled");
MinecraftVersion.disableUpdateCheck();
} else {
MinecraftVersion.enableUpdateCheck();
}
// Disabled by default since 2.1. Enable it yourself by calling
@@ -101,6 +104,7 @@ public class NBTAPI extends JavaPlugin {
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_8_R3)) // 1.7.10 list support is not complete at all
apiTests.add(new ListTest());
apiTests.add(new SubCompoundsTest());
apiTests.add(new ModernSubCompoundsTest());
if (MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_8_R3)) // 1.7.10 not a thing
apiTests.add(new MergeTest());
apiTests.add(new ForEachTest());
@@ -0,0 +1,56 @@
package de.tr7zw.nbtapi.plugin.tests.compounds;
import de.tr7zw.changeme.nbtapi.NBT;
import de.tr7zw.changeme.nbtapi.NbtApiException;
import de.tr7zw.changeme.nbtapi.iface.ReadWriteNBT;
import de.tr7zw.nbtapi.plugin.tests.Test;
public class ModernSubCompoundsTest implements Test {
private static final String COMP_TEST_KEY = "componentTest";
private static final String STRING_TEST_KEY = "stringTest";
private static final String INT_TEST_KEY = "intTest";
private static final String DOUBLE_TEST_KEY = "doubleTest";
private static final String BOOLEAN_TEST_KEY = "booleanTest";
private static final String STRING_TEST_VALUE = "TestString";
private static final int INT_TEST_VALUE = 42;
private static final double DOUBLE_TEST_VALUE = 1.5d;
private static final boolean BOOLEAN_TEST_VALUE = true;
@Override
public void test() throws Exception {
ReadWriteNBT cont = NBT.createNBTObject();
ReadWriteNBT comp = cont.getOrCreateCompound(COMP_TEST_KEY);
comp.setString(STRING_TEST_KEY, STRING_TEST_VALUE + "2");
comp.setInteger(INT_TEST_KEY, INT_TEST_VALUE * 2);
comp.setDouble(DOUBLE_TEST_KEY, DOUBLE_TEST_VALUE * 2);
if (cont.getCompound("invalide") != null) {
throw new NbtApiException("An invalide compound did not return null!");
}
comp = null;
comp = cont.getCompound(COMP_TEST_KEY);
if (comp == null) {
throw new NbtApiException("Wasn't able to get the NBTCompound!");
}
if (!comp.hasTag(STRING_TEST_KEY)) {
throw new NbtApiException("Wasn't able to check a compound key!");
}
if (!(STRING_TEST_VALUE + "2").equals(comp.getString(STRING_TEST_KEY))
|| comp.getInteger(INT_TEST_KEY) != INT_TEST_VALUE * 2
|| comp.getDouble(DOUBLE_TEST_KEY) != DOUBLE_TEST_VALUE * 2
|| comp.getBoolean(BOOLEAN_TEST_KEY) == BOOLEAN_TEST_VALUE) {
throw new NbtApiException("One key does not equal the original compound value!");
}
// Using getOrCreateCompound twice
comp.getOrCreateCompound("someName").setString("test", "abc");
if (!comp.getOrCreateCompound("someName").getString("test").equals("abc")) {
throw new NbtApiException("getOrCreateCompound did not return the same compound!");
}
}
}
@@ -23,6 +23,8 @@ public class DirectApplyTest implements Test {
nbt.setString("SomeKey", "SomeValue");
return nbt.getString("SomeKey");
});
baseItem = NBT.itemStackFromNBT(NBT.itemStackToNBT(baseItem)); // trick to force the item to be "real", not a
// Spigot only item
String outside = NBT.get(baseItem, nbt -> nbt.getString("SomeKey"));
if (!new NBTItem(baseItem).hasTag("SomeKey")) {
throw new NbtApiException("The data was not applied!");
+2 -2
View File
@@ -4,7 +4,7 @@
<parent>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</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.11.1</version>
<version>2.12.0-SNAPSHOT</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.18.2.txt");
File input = new File("minecraft_server.1.20.txt");
List<String> lines = Files.readAllLines(input.toPath());
Map<String, ClassWrapper> classes = new HashMap<>();
+2 -2
View File
@@ -5,7 +5,7 @@
<parent>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</version>
</parent>
<artifactId>nbt-data-api</artifactId>
@@ -14,7 +14,7 @@
<dependency>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-api</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
+2 -2
View File
@@ -4,7 +4,7 @@
<parent>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</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.11.1</version>
<version>2.12.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
@@ -52,7 +52,7 @@ public class NBTInjector {
if (MinecraftVersion.getVersion() == MinecraftVersion.MC1_7_R4
|| MinecraftVersion.isAtLeastVersion(MinecraftVersion.MC1_16_R1)) {
logger.warning(
"[NBTINJECTOR] The NBT-Injector is not compatibel with this Minecraft Version! For 1.16+ please use the persistent storage API.");
"[NBTINJECTOR] The NBT-Injector is not compatible with this Minecraft Version! For 1.16+ please use the persistent storage API.");
return;
}
isInjected = true;
+6 -6
View File
@@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>de.tr7zw</groupId>
<artifactId>item-nbt-parent</artifactId>
<version>2.11.1</version>
<version>2.12.0-SNAPSHOT</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.10.1</version>
<version>3.11.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.2.1</version>
<version>3.3.0</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.4.1</version>
<version>3.5.0</version>
<configuration>
<source>8</source>
</configuration>
@@ -154,7 +154,7 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>license-maven-plugin</artifactId>
<version>2.0.0</version>
<version>2.0.1</version>
<configuration>
<licenseName>mit</licenseName>
<licenseFile>LICENSE</licenseFile>
@@ -164,7 +164,7 @@
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
<version>2.28.0</version>
<version>2.30.0</version>
<configuration>
<formats>
<format>
+2 -2
View File
@@ -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.11.1</version>
<version>2.12.0-SNAPSHOT</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.11.1</version>
<version>2.12.0-SNAPSHOT</version>
</dependency>
...
<plugins>
+3 -3
View File
@@ -11,7 +11,7 @@ repositories {
...
maven {
name = "CodeMC"
url = uri("https://repo.codemc.org/repository/maven-public/")
url = uri("https://repo.codemc.io/repository/maven-public/")
}
...
}
@@ -46,7 +46,7 @@ repositories {
...
maven {
name = "CodeMC"
url = uri("https://repo.codemc.org/repository/maven-public/")
url = uri("https://repo.codemc.io/repository/maven-public/")
}
...
}
@@ -64,4 +64,4 @@ If you want to run the shadowJar task when the build task is executed, you can w
build {
dependsOn(shadowJar)
}
```
```
+3 -3
View File
@@ -15,7 +15,7 @@ Add the following Entries to your pom at the correct location:
<!-- CodeMC -->
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
<url>https://repo.codemc.io/repository/maven-public/</url>
<layout>default</layout>
</repository>
...
@@ -42,7 +42,7 @@ Add the following Entries to your pom at the correct location:
<!-- CodeMC -->
<repository>
<id>codemc-repo</id>
<url>https://repo.codemc.org/repository/maven-public/</url>
<url>https://repo.codemc.io/repository/maven-public/</url>
<layout>default</layout>
</repository>
...
@@ -73,4 +73,4 @@ Add the following Entries to your pom at the correct location:
</configuration>
</plugin>
</plugins>
```
```