CobbleSync Box Update
This commit is contained in:
@@ -1,19 +1,24 @@
|
|||||||
package co.sirblob
|
package co.sirblob
|
||||||
|
|
||||||
import com.cobblemon.mod.common.api.text.red
|
import com.cobblemon.mod.common.api.text.red
|
||||||
|
import com.cobblemon.mod.common.api.text.green
|
||||||
|
import com.cobblemon.mod.common.api.text.blue
|
||||||
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
|
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
|
||||||
import com.cobblemon.mod.common.api.pokemon.stats.Stat
|
import com.cobblemon.mod.common.api.pokemon.stats.Stat
|
||||||
import com.cobblemon.mod.common.api.pokemon.stats.Stats
|
import com.cobblemon.mod.common.api.pokemon.stats.Stats
|
||||||
|
import com.cobblemon.mod.common.api.storage.pc.PCBox
|
||||||
import com.cobblemon.mod.common.util.pc
|
import com.cobblemon.mod.common.util.pc
|
||||||
import com.cobblemon.mod.common.pokemon.Pokemon
|
import com.cobblemon.mod.common.pokemon.Pokemon
|
||||||
import com.cobblemon.mod.common.pokemon.Species
|
import com.cobblemon.mod.common.pokemon.Species
|
||||||
import com.cobblemon.mod.common.Cobblemon
|
import com.cobblemon.mod.common.Cobblemon
|
||||||
|
import com.cobblemon.mod.relocations.oracle.truffle.regex.tregex.util.json.Json
|
||||||
import org.json.JSONObject
|
import org.json.JSONObject
|
||||||
import com.mojang.brigadier.Command
|
import com.mojang.brigadier.Command
|
||||||
import com.mojang.brigadier.arguments.IntegerArgumentType
|
import com.mojang.brigadier.arguments.IntegerArgumentType
|
||||||
import com.mojang.brigadier.builder.LiteralArgumentBuilder
|
import com.mojang.brigadier.builder.LiteralArgumentBuilder
|
||||||
import com.mojang.brigadier.context.CommandContext
|
import com.mojang.brigadier.context.CommandContext
|
||||||
import com.google.gson.JsonObject
|
import com.google.gson.JsonObject
|
||||||
|
import com.google.gson.JsonParser
|
||||||
import net.fabricmc.api.ModInitializer
|
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
|
||||||
@@ -21,137 +26,125 @@ import net.minecraft.network.chat.Component
|
|||||||
import net.minecraft.core.RegistryAccess
|
import net.minecraft.core.RegistryAccess
|
||||||
import net.minecraft.resources.ResourceLocation
|
import net.minecraft.resources.ResourceLocation
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import SyncMon
|
|
||||||
|
|
||||||
object CobbleSync : ModInitializer {
|
object CobbleSync : ModInitializer {
|
||||||
|
|
||||||
private val logger = LoggerFactory.getLogger("cobblesync")
|
private val logger = LoggerFactory.getLogger("cobblesync")
|
||||||
private val request = Request("http://localhost:5173")
|
private val request = Request("http://localhost:5173")
|
||||||
|
|
||||||
|
|
||||||
private fun Pokemon.toSyncJSONObject(): JSONObject {
|
|
||||||
val statsJson = JSONObject()
|
|
||||||
.put("hp", this.maxHealth)
|
|
||||||
.put("attack", this.attack)
|
|
||||||
.put("defense", this.defence)
|
|
||||||
.put("specialAttack", this.specialAttack)
|
|
||||||
.put("specialDefense", this.specialDefence)
|
|
||||||
.put("speed", this.speed)
|
|
||||||
|
|
||||||
return JSONObject()
|
|
||||||
.put("species", this.species.name)
|
|
||||||
.put("level", this.level)
|
|
||||||
.put("nickname", this.nickname ?: "")
|
|
||||||
.put("shiny", this.shiny)
|
|
||||||
.put("stats", statsJson)
|
|
||||||
.put("ivs", this.ivs.map { (stat, value) ->
|
|
||||||
JSONObject().put(stat.toString(), value)
|
|
||||||
})
|
|
||||||
.put("friendship", this.friendship)
|
|
||||||
.put("nature", this.nature.toString())
|
|
||||||
.put("ability", this.ability)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
override fun onInitialize() {
|
override fun onInitialize() {
|
||||||
|
|
||||||
CommandRegistrationCallback.EVENT.register(CommandRegistrationCallback { dispatcher, _, _ ->
|
CommandRegistrationCallback.EVENT.register(CommandRegistrationCallback { dispatcher, _, _ ->
|
||||||
dispatcher.register(
|
dispatcher.register(
|
||||||
LiteralArgumentBuilder.literal<CommandSourceStack>("cobblesync")
|
LiteralArgumentBuilder.literal<CommandSourceStack>("cobblesync")
|
||||||
.then(
|
.then(
|
||||||
LiteralArgumentBuilder.literal<CommandSourceStack>("sync")
|
LiteralArgumentBuilder.literal<CommandSourceStack>("sync")
|
||||||
.executes(Command<CommandSourceStack> { context: CommandContext<CommandSourceStack> ->
|
.executes(Command<CommandSourceStack> { context: CommandContext<CommandSourceStack> ->
|
||||||
|
|
||||||
val player = context.source.playerOrException
|
val player = context.source.playerOrException
|
||||||
|
|
||||||
val pc = player.pc()
|
|
||||||
|
|
||||||
player.sendSystemMessage(Component.literal("Syncing box...").red())
|
player.sendSystemMessage(Component.literal("Syncing box...").red())
|
||||||
|
|
||||||
val box30 = pc.boxes.get(29)
|
val box30 = player.pc().boxes.get(29)
|
||||||
|
|
||||||
var pokemonArry = ArrayList<Pokemon>()
|
|
||||||
var pokemonCount = 0
|
var pokemonCount = 0
|
||||||
|
|
||||||
box30.filterNotNull().forEach { pokemon ->
|
box30.filterNotNull().forEach { pokemon ->
|
||||||
pokemonArry.add(pokemon)
|
player.sendSystemMessage(
|
||||||
logger.info("Found pokemon: ${pokemon.species.name}")
|
Component.literal("Syncing Pokémon: ${pokemon.species.name} (Level ${pokemon.level})")
|
||||||
|
.blue()
|
||||||
|
)
|
||||||
pokemonCount++
|
pokemonCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
if (pokemonCount < 1) {
|
if (pokemonCount < 1) {
|
||||||
player.sendSystemMessage(Component.literal("Box 30 is empty!").red())
|
player.sendSystemMessage(Component.literal("[Sync Failed] Box 1 is empty!").red())
|
||||||
return@Command 1
|
return@Command 1
|
||||||
} else if (pokemonCount > 12) {
|
} else if (pokemonCount > 12) {
|
||||||
player.sendSystemMessage(Component.literal("Box 30 has too many pokemon!").red())
|
player.sendSystemMessage(Component.literal("[Sync Failed] Box 1 has too many pokemon!").red())
|
||||||
return@Command 1
|
return@Command 1
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
val pokemonJsonList = pokemonArry.map { pokemon ->
|
var obj = box30.saveToJSON(JsonObject(), player.registryAccess())
|
||||||
pokemon.toSyncJSONObject()
|
|
||||||
}
|
|
||||||
|
|
||||||
val payload = JSONObject()
|
val payload = JSONObject()
|
||||||
.put("pokemon", pokemonJsonList)
|
.put("pokemon", obj.toString())
|
||||||
.put("count", pokemonCount)
|
.put("count", pokemonCount)
|
||||||
|
|
||||||
var response = request.POST("/api/cobblesync/" + player.uuid.toString(), payload)
|
var response = request.POST("/api/cobblesync/" + player.uuid.toString(), payload)
|
||||||
|
|
||||||
logger.info(response.toString())
|
|
||||||
|
|
||||||
if (response.getInt("status") != 200) {
|
if (response.getInt("status") != 200) {
|
||||||
player.sendSystemMessage(Component.literal("Failed to sync box 30!").red())
|
player.sendSystemMessage(Component.literal("Failed to sync box 1!").red())
|
||||||
return@Command 1
|
return@Command 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear Box 30 before adding new Pokémon
|
player.sendSystemMessage(Component.literal("Box 1 synced successfully!").green())
|
||||||
// for (i in 0 until box30.count()) {
|
|
||||||
// box30.set(i, null)
|
|
||||||
// }
|
|
||||||
|
|
||||||
val newPokemonsArray = response.optJSONArray("pokemon")
|
|
||||||
if (newPokemonsArray == null) {
|
|
||||||
player.sendSystemMessage(Component.literal("No Pokémon data received in server response.").red())
|
|
||||||
// Box is now empty, which might be the intended state if server sends no pokemon
|
|
||||||
return@Command 1
|
|
||||||
}
|
|
||||||
|
|
||||||
// Iterate through the received Pokémon and add them to Box 30 in an open slot
|
|
||||||
|
|
||||||
for (i in 0 until newPokemonsArray.length()) {
|
|
||||||
val pokemonJson = newPokemonsArray.getJSONObject(i)
|
|
||||||
val newPokemon = SyncMon().createFromJSON(pokemonJson)
|
|
||||||
|
|
||||||
// Find the first empty slot in Box 30
|
|
||||||
|
|
||||||
logger.info(newPokemon.toSyncJSONObject().toString())
|
|
||||||
|
|
||||||
for (j in 0 until box30.count()) {
|
|
||||||
if (box30.get(j) == null) {
|
|
||||||
box30.pc.add(newPokemon)
|
|
||||||
logger.info("Added ${newPokemon.species.name} to Box 30 at slot $j")
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (e: HTTPException) {
|
} catch (e: HTTPException) {
|
||||||
logger.error("HTTP Exception: ${e.message}")
|
logger.error("HTTP Exception: ${e.message}")
|
||||||
player.sendSystemMessage(Component.literal("Error syncing box 30!").red())
|
player.sendSystemMessage(Component.literal("Error syncing box 1!").red())
|
||||||
|
return@Command 1
|
||||||
|
} catch (e: Exception) {
|
||||||
|
logger.error("Exception: ${e.message}")
|
||||||
|
player.sendSystemMessage(Component.literal("An unexpected error occurred!").red())
|
||||||
|
return@Command 1
|
||||||
|
}
|
||||||
|
|
||||||
|
1
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.then(
|
||||||
|
LiteralArgumentBuilder.literal<CommandSourceStack>("load")
|
||||||
|
.executes(Command<CommandSourceStack> { context: CommandContext<CommandSourceStack> ->
|
||||||
|
val player = context.source.playerOrException
|
||||||
|
|
||||||
|
val pc = player.pc()
|
||||||
|
player.sendSystemMessage(Component.literal("Syncing box...").green())
|
||||||
|
|
||||||
|
var box1 = pc.boxes.get(0)
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
|
var response = request.GET("/api/cobblesync/" + player.uuid.toString())
|
||||||
|
logger.info(response.toString())
|
||||||
|
|
||||||
|
if (response.getInt("status") != 200) {
|
||||||
|
player.sendSystemMessage(Component.literal("Failed to load box 1!").red())
|
||||||
|
return@Command 1
|
||||||
|
}
|
||||||
|
|
||||||
|
var obj = JsonParser.parseString(response.getString("pokemon")).asJsonObject
|
||||||
|
|
||||||
|
var newBox = box1.loadFromJSON(obj, player.registryAccess())
|
||||||
|
|
||||||
|
newBox.pc.filterNotNull().forEach { pokemon ->
|
||||||
|
player.sendSystemMessage(
|
||||||
|
Component.literal("Received Pokémon: ${pokemon.species.name} (Level ${pokemon.level})")
|
||||||
|
.blue()
|
||||||
|
)
|
||||||
|
|
||||||
|
box1.pc.add(pokemon)
|
||||||
|
}
|
||||||
|
|
||||||
|
player.sendSystemMessage(Component.literal("Box 1 loaded successfully!").green())
|
||||||
|
|
||||||
|
} catch (e: HTTPException) {
|
||||||
|
logger.error("HTTP Exception: ${e.message}")
|
||||||
|
player.sendSystemMessage(Component.literal("Error loading box 1!").red())
|
||||||
return@Command 1
|
return@Command 1
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logger.error("Exception: ${e.message}")
|
logger.error("Exception: ${e.message}")
|
||||||
player.sendSystemMessage(Component.literal("An unexpected error occurred!").red())
|
player.sendSystemMessage(Component.literal("An unexpected error occurred!").red())
|
||||||
return@Command 1
|
return@Command 1
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendSystemMessage(Component.literal("Box 30 synced successfully!").red())
|
|
||||||
1
|
1
|
||||||
})
|
}
|
||||||
)
|
)
|
||||||
)
|
|
||||||
|
))
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
|
|
||||||
import com.cobblemon.mod.common.api.pokemon.PokemonSpecies
|
|
||||||
import com.cobblemon.mod.common.api.pokemon.stats.Stats
|
|
||||||
import com.cobblemon.mod.common.api.pokemon.Natures
|
|
||||||
import com.cobblemon.mod.common.pokemon.Pokemon
|
|
||||||
import com.cobblemon.mod.common.pokemon.Nature
|
|
||||||
import org.json.JSONObject
|
|
||||||
import net.minecraft.network.chat.Component
|
|
||||||
|
|
||||||
|
|
||||||
class SyncMon: Pokemon {
|
|
||||||
|
|
||||||
constructor() : super()
|
|
||||||
|
|
||||||
public fun toSyncJSONObject(): JSONObject {
|
|
||||||
val statsJson = JSONObject()
|
|
||||||
.put("hp", this.maxHealth)
|
|
||||||
.put("attack", this.attack)
|
|
||||||
.put("defense", this.defence)
|
|
||||||
.put("specialAttack", this.specialAttack)
|
|
||||||
.put("specialDefense", this.specialDefence)
|
|
||||||
.put("speed", this.speed)
|
|
||||||
|
|
||||||
return JSONObject()
|
|
||||||
.put("species", this.species.name)
|
|
||||||
.put("level", this.level)
|
|
||||||
.put("nickname", this.nickname ?: "")
|
|
||||||
.put("shiny", this.shiny)
|
|
||||||
.put("stats", statsJson)
|
|
||||||
.put("ivs", this.ivs.map { (stat, value) ->
|
|
||||||
JSONObject().put(stat.toString(), value)
|
|
||||||
})
|
|
||||||
.put("friendship", this.friendship)
|
|
||||||
.put("nature", this.nature.toString())
|
|
||||||
.put("ability", this.ability)
|
|
||||||
}
|
|
||||||
|
|
||||||
public fun createFromJSON(json: JSONObject): Pokemon {
|
|
||||||
val speciesName = json.getString("species")
|
|
||||||
val species = PokemonSpecies.getByName(speciesName.lowercase()) ?: throw IllegalArgumentException("Unknown species: $speciesName")
|
|
||||||
|
|
||||||
val pokemon = Pokemon()
|
|
||||||
pokemon.species = species
|
|
||||||
pokemon.level = json.getInt("level")
|
|
||||||
pokemon.shiny = json.getBoolean("shiny")
|
|
||||||
pokemon.setFriendship(json.getInt("friendship"))
|
|
||||||
super.nickname = Component.literal(json.getString("nickname"))
|
|
||||||
pokemon.nature = Natures.getNature(json.getString("nature")) ?: Natures.GENTLE
|
|
||||||
|
|
||||||
//val statsJson = json.getJSONObject("stats")
|
|
||||||
|
|
||||||
val ivsJson = json.getJSONArray("ivs")
|
|
||||||
for (i in 0 until ivsJson.length()) {
|
|
||||||
val ivJson = ivsJson.getJSONObject(i)
|
|
||||||
for (key in ivJson.keys()) {
|
|
||||||
val stat = Stats.getStat(key)
|
|
||||||
pokemon.setIV(stat, ivJson.getInt(key))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pokemon
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user