CobbleSync Json Parser Fix
This commit is contained in:
@@ -2,12 +2,17 @@ package co.sirblob.network
|
||||
|
||||
import co.sirblob.HTTPException
|
||||
import co.sirblob.Request
|
||||
import com.cobblemon.mod.common.api.text.red
|
||||
import com.cobblemon.mod.common.pokemon.Pokemon
|
||||
import com.cobblemon.mod.common.util.pc
|
||||
import com.google.gson.JsonObject
|
||||
import com.google.gson.JsonParser
|
||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
|
||||
import net.minecraft.server.level.ServerPlayer
|
||||
import net.minecraft.world.item.ItemStack
|
||||
import net.minecraft.network.chat.Component
|
||||
import com.cobblemon.mod.common.api.text.red
|
||||
import org.json.JSONObject
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
@@ -70,6 +75,13 @@ object ServerPacketHandler {
|
||||
}
|
||||
|
||||
val obj = box30.saveToJSON(JsonObject(), player.registryAccess())
|
||||
|
||||
// Remove held items from the JSON to prevent duping/transferring items
|
||||
obj.entrySet().forEach { entry ->
|
||||
if (entry.value.isJsonObject) {
|
||||
entry.value.asJsonObject.remove("HeldItem")
|
||||
}
|
||||
}
|
||||
|
||||
val payload = JSONObject().put("pokemon", obj.toString()).put("count", pokemonCount)
|
||||
|
||||
@@ -96,6 +108,11 @@ object ServerPacketHandler {
|
||||
val response = request.GET("/api/cobblesync/${player.uuid}")
|
||||
logger.info(response.toString())
|
||||
|
||||
if (response.getInt("status") == 403) {
|
||||
sendResponse(player, false, "Player already synced/loaded")
|
||||
return
|
||||
}
|
||||
|
||||
if (response.getInt("status") != 200) {
|
||||
sendResponse(player, false, "No saved Pokémon found!")
|
||||
return
|
||||
@@ -103,9 +120,16 @@ object ServerPacketHandler {
|
||||
|
||||
val obj = JsonParser.parseString(response.getString("pokemon")).asJsonObject
|
||||
|
||||
// Use a temporary box to parse the JSON data
|
||||
val tempBox = pc.boxes[0].loadFromJSON(obj, player.registryAccess())
|
||||
val pokemonToLoad = tempBox.pc.filterNotNull().toMutableList()
|
||||
val pokemonToLoad = mutableListOf<Pokemon>()
|
||||
obj.entrySet().forEach { entry ->
|
||||
if (entry.key.startsWith("Slot")) {
|
||||
val pokemonJson = entry.value.asJsonObject
|
||||
val pokemon = Pokemon.loadFromJSON(player.registryAccess()!!, pokemonJson)
|
||||
if (pokemon != null) {
|
||||
pokemonToLoad.add(pokemon)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (pokemonToLoad.isEmpty()) {
|
||||
sendResponse(player, false, "No Pokémon to load!")
|
||||
|
||||
Reference in New Issue
Block a user