NBT-API

NBT-API

98.9k Downloads

InaccessibleObjectException while using SDK 16

TonyFoster opened this issue ยท 4 comments

commented

Env:
System Info: Java 16 (Java HotSpot(TM) 64-Bit Server VM 16.0.1+9-24) Host: ***
This server is running Paper version git-Paper-105 (MC: 1.17.1) (Implementing API version 1.17.1-R0.1-SNAPSHOT) (Git: 2e37979)
Starting minecraft server version 1.17.1
[NBTAPI] Enabling NBTAPI v2.8.1-SNAPSHOT

code:
nbtItem.setObject("key", player);

get:

Caused by: de.tr7zw.nbtapi.NbtApiException: Exception while setting the Object 'CraftPlayer{name=***}'!
        at de.tr7zw.nbtapi.NBTReflectionUtil.setObject(NBTReflectionUtil.java:517) ~[?:?]
        at de.tr7zw.nbtapi.NBTCompound.setObject(NBTCompound.java:421) ~[?:?]
        at ***(***.java:59) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.17.1.jar:git-Paper-105]
        ... 21 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field private java.lang.Object java.lang.ref.Reference.referent accessible: module java.base does not "opens java.lang.ref" to unnamed module @2baf3d81
        at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:357) ~[?:?]
        at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[?:?]
        at java.lang.reflect.Field.checkCanSetAccessible(Field.java:177) ~[?:?]
        at java.lang.reflect.Field.setAccessible(Field.java:171) ~[?:?]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:157) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.getAdapter(Gson.java:423) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.CollectionTypeAdapterFactory.create(CollectionTypeAdapterFactory.java:53) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.getAdapter(Gson.java:423) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.MapTypeAdapterFactory.create(MapTypeAdapterFactory.java:126) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.getAdapter(Gson.java:423) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.createBoundField(ReflectiveTypeAdapterFactory.java:115) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.getBoundFields(ReflectiveTypeAdapterFactory.java:164) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory.create(ReflectiveTypeAdapterFactory.java:100) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.getAdapter(Gson.java:423) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.toJson(Gson.java:661) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.toJson(Gson.java:648) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.toJson(Gson.java:603) ~[patched_1.17.1.jar:git-Paper-105]
        at com.google.gson.Gson.toJson(Gson.java:583) ~[patched_1.17.1.jar:git-Paper-105]
        at de.tr7zw.nbtapi.utils.GsonWrapper.getString(GsonWrapper.java:31) ~[?:?]
        at de.tr7zw.nbtapi.NBTReflectionUtil.setObject(NBTReflectionUtil.java:514) ~[?:?]
        at de.tr7zw.nbtapi.NBTCompound.setObject(NBTCompound.java:421) ~[?:?]
        at ***(***.java:59) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.17.1.jar:git-Paper-105]
        ... 21 more

reference:
https://stackoverflow.com/questions/41265266

commented

But nbtItem.setObject("key", Instant.now()); won't work too, so I have to use java.sql.Timestamp instead.

commented

Tme should be long or a formatted String, not an Instant, Callender or Timestamp. Like I said, it uses Gson, so don't try to put stuff into there that you also woun't try to serialize directly. Especially if there is a simple format for it (for example long for time). The setObject method should only ever be used to save POJO classes.

commented

Perhaps there is nothing we can do with this matter?

commented

Hm could it be that you have reflection security features active on the java 16 jvm(try --illegal-access=permit?

Edit: Scratch that, just saw the the code you try to run nbtItem.setObject("key", player);, yea that will never work. Never pass Minecraft objects into gson (the get/set object methods are just gson wrappers), because you basically try to convert the ENTIRE running server into json and put it onto an item.