CobbleSync Json Parser Fix
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -40,3 +40,4 @@ replay_*.log
|
|||||||
*.jfr
|
*.jfr
|
||||||
|
|
||||||
remappedSrc/
|
remappedSrc/
|
||||||
|
.kotlin/
|
||||||
@@ -4,6 +4,7 @@ import co.sirblob.network.ServerPacketHandler
|
|||||||
import com.cobblemon.mod.common.api.text.blue
|
import com.cobblemon.mod.common.api.text.blue
|
||||||
import com.cobblemon.mod.common.api.text.green
|
import com.cobblemon.mod.common.api.text.green
|
||||||
import com.cobblemon.mod.common.api.text.red
|
import com.cobblemon.mod.common.api.text.red
|
||||||
|
import com.cobblemon.mod.common.pokemon.Pokemon
|
||||||
import com.cobblemon.mod.common.util.pc
|
import com.cobblemon.mod.common.util.pc
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
@@ -14,6 +15,7 @@ import net.fabricmc.api.ModInitializer
|
|||||||
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
|
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback
|
||||||
import net.minecraft.commands.CommandSourceStack
|
import net.minecraft.commands.CommandSourceStack
|
||||||
import net.minecraft.network.chat.Component
|
import net.minecraft.network.chat.Component
|
||||||
|
import net.minecraft.world.item.ItemStack
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
@@ -92,6 +94,13 @@ object CobbleSync : ModInitializer {
|
|||||||
player.registryAccess()
|
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 =
|
val payload =
|
||||||
JSONObject()
|
JSONObject()
|
||||||
.put(
|
.put(
|
||||||
@@ -185,6 +194,16 @@ object CobbleSync : ModInitializer {
|
|||||||
)
|
)
|
||||||
logger.info(response.toString())
|
logger.info(response.toString())
|
||||||
|
|
||||||
|
if (response.getInt("status") == 403) {
|
||||||
|
player.sendSystemMessage(
|
||||||
|
Component.literal(
|
||||||
|
"Player already synced/loaded"
|
||||||
|
)
|
||||||
|
.red()
|
||||||
|
)
|
||||||
|
return@Command 1
|
||||||
|
}
|
||||||
|
|
||||||
if (response.getInt("status") !=
|
if (response.getInt("status") !=
|
||||||
200
|
200
|
||||||
) {
|
) {
|
||||||
@@ -205,21 +224,16 @@ object CobbleSync : ModInitializer {
|
|||||||
)
|
)
|
||||||
.asJsonObject
|
.asJsonObject
|
||||||
|
|
||||||
// Use a
|
val pokemonToLoad = mutableListOf<Pokemon>()
|
||||||
// temporary
|
obj.entrySet().forEach { entry ->
|
||||||
// box to
|
if (entry.key.startsWith("Slot")) {
|
||||||
// parse the
|
val pokemonJson = entry.value.asJsonObject
|
||||||
// JSON
|
val pokemon = Pokemon.loadFromJSON(player.registryAccess()!!, pokemonJson)
|
||||||
val tempBox =
|
if (pokemon != null) {
|
||||||
pc.boxes[0]
|
pokemonToLoad.add(pokemon)
|
||||||
.loadFromJSON(
|
}
|
||||||
obj,
|
}
|
||||||
player.registryAccess()
|
}
|
||||||
)
|
|
||||||
val pokemonToLoad =
|
|
||||||
tempBox.pc
|
|
||||||
.filterNotNull()
|
|
||||||
.toMutableList()
|
|
||||||
|
|
||||||
if (pokemonToLoad.isEmpty()) {
|
if (pokemonToLoad.isEmpty()) {
|
||||||
player.sendSystemMessage(
|
player.sendSystemMessage(
|
||||||
|
|||||||
@@ -2,12 +2,17 @@ package co.sirblob.network
|
|||||||
|
|
||||||
import co.sirblob.HTTPException
|
import co.sirblob.HTTPException
|
||||||
import co.sirblob.Request
|
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.cobblemon.mod.common.util.pc
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
import com.google.gson.JsonParser
|
import com.google.gson.JsonParser
|
||||||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry
|
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry
|
||||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
|
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
|
||||||
import net.minecraft.server.level.ServerPlayer
|
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.json.JSONObject
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
|
|
||||||
@@ -71,6 +76,13 @@ object ServerPacketHandler {
|
|||||||
|
|
||||||
val obj = box30.saveToJSON(JsonObject(), player.registryAccess())
|
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)
|
val payload = JSONObject().put("pokemon", obj.toString()).put("count", pokemonCount)
|
||||||
|
|
||||||
logger.info("/api/cobblesync/${player.uuid}")
|
logger.info("/api/cobblesync/${player.uuid}")
|
||||||
@@ -96,6 +108,11 @@ object ServerPacketHandler {
|
|||||||
val response = request.GET("/api/cobblesync/${player.uuid}")
|
val response = request.GET("/api/cobblesync/${player.uuid}")
|
||||||
logger.info(response.toString())
|
logger.info(response.toString())
|
||||||
|
|
||||||
|
if (response.getInt("status") == 403) {
|
||||||
|
sendResponse(player, false, "Player already synced/loaded")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (response.getInt("status") != 200) {
|
if (response.getInt("status") != 200) {
|
||||||
sendResponse(player, false, "No saved Pokémon found!")
|
sendResponse(player, false, "No saved Pokémon found!")
|
||||||
return
|
return
|
||||||
@@ -103,9 +120,16 @@ object ServerPacketHandler {
|
|||||||
|
|
||||||
val obj = JsonParser.parseString(response.getString("pokemon")).asJsonObject
|
val obj = JsonParser.parseString(response.getString("pokemon")).asJsonObject
|
||||||
|
|
||||||
// Use a temporary box to parse the JSON data
|
val pokemonToLoad = mutableListOf<Pokemon>()
|
||||||
val tempBox = pc.boxes[0].loadFromJSON(obj, player.registryAccess())
|
obj.entrySet().forEach { entry ->
|
||||||
val pokemonToLoad = tempBox.pc.filterNotNull().toMutableList()
|
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()) {
|
if (pokemonToLoad.isEmpty()) {
|
||||||
sendResponse(player, false, "No Pokémon to load!")
|
sendResponse(player, false, "No Pokémon to load!")
|
||||||
|
|||||||
Reference in New Issue
Block a user