I'm making a KTOR server that aims to allow the player to buy IG items with IG money. For this, I use the spigot API in 1.20. I also use this class, but I get the error below when I use the getItemMeta() method or in Kotlin .itemMeta. I'd like to know how to get rid of this error so that the fromJson(string: String?):ItemStack? method works normally. Note that I also get a NullPointerException when I use the toJson(itemStack: ItemStack) method: String if needed here is my project that I put on GitHub. Thank you in advance for your time in finding a solution.
java.lang.NullPointerException: null
at org.bukkit.Bukkit.getItemFactory(Bukkit.java:1608)
at org.bukkit.inventory.ItemStack.getItemMeta(ItemStack.java:550)
at fr.plaglefleau.serialization.JsonItemStack.fromJson(JsonItemStack.kt:278)
at fr.plaglefleau.bdd_MySql.Gestion.updateInventory(Gestion.kt:815)
at fr.plaglefleau.plugins.RoutingKt$configureRouting$1$3$1$1$7.invokeSuspend(Routing.kt:588)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
at kotlinx.coroutines.internal.DispatchedContinuation.resumeWith(DispatchedContinuation.kt:205)
at io.ktor.util.pipeline.SuspendFunctionGun.resumeRootWith(SuspendFunctionGun.kt:135)
at io.ktor.util.pipeline.SuspendFunctionGun.loop(SuspendFunctionGun.kt:109)
at io.ktor.util.pipeline.SuspendFunctionGun.access$loop(SuspendFunctionGun.kt:11)
at io.ktor.util.pipeline.SuspendFunctionGun$continuation$1.resumeWith(SuspendFunctionGun.kt:59)
at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:46)
at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
at io.netty.util.concurrent.AbstractEventExecutor.runTask(AbstractEventExecutor.java:174)
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:167)
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:470)
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:569)
at io.netty.util.concurrent.SingleThreadEventExecutor$4.run(SingleThreadEventExecutor.java:997)
at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
at io.ktor.server.netty.EventLoopGroupProxy$Companion.create$lambda$1$lambda$0(NettyApplicationEngine.kt:296)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:834)
2023-07-09 03:46:51.495 [eventLoopGroupProxy-4-1] TRACE io.ktor.server.sessions.Sessions - Sending session data for /api/client/updateInventory: connect
2023-07-09 03:46:51.496 [eventLoopGroupProxy-4-1] TRACE i.k.s.p.c.ContentNegotiation - Skipping response body transformation from HttpStatusCode to OutgoingContent for the PUT /api/client/updateInventory request because the HttpStatusCode type is ignored. See [ContentNegotiationConfig::ignoreType].
Gestion updateInventory:
fun updateInventory(inventory: ArrayList<ItemSlotPair>, username: String) {
val laConnexion = Connexion(url, this.username, password)
var preparedStatement = laConnexion.getConnexion().prepareStatement(
"SELECT id FROM user WHERE username = ?"
)
preparedStatement.setString(1,username)
var rs = preparedStatement.executeQuery()
val userID = if(rs.next()) {
rs.getInt("id")
} else {
null
}
if(userID == null) {
return
}
for(i in 0..inventory.size) {
val item = JsonItemStack.fromJson(inventory[i].item) ?: return
addItemIfNotExist(item)
preparedStatement = laConnexion.getConnexion().prepareStatement(
"UPDATE inventaire SET article_id = ?, quantité = ? WHERE slot_number = ? AND user_id = ?"
)
preparedStatement.setInt(1,item.type.id)
preparedStatement.setInt(2,item.amount)
preparedStatement.setInt(3, inventory[i].slot)
preparedStatement.setInt(4,userID)
preparedStatement.executeUpdate()
}
laConnexion.fermerConnexion()
}
JsonItemStack.kt:218
val meta = itemStack.itemMeta
Routing.kt:put("updateInventory")
put("updateInventory") {
val receive = call.receive<InventoryReceive>()
gestion.updateInventory(receive.inventory, receive.username)
call.respond(HttpStatusCode.OK, "Good")
}
I think I have found where the problem may come from so to get the itemmeta of an itemStack we need the ItemFactory interfaces but to get the ItemFactory interface we need to do Server.getItemFactory() and here is my probleme i don't run it on a minecraft server so if I want the ItemMeta i need a minecraft serveur to run the spigot api so the solution to use a class that convert to json an itemStack don't work I will probably use a custom class to stock the data of my ItemStack.