feat: Refactor Pokémon loading to include empty slot checks and add a new README file.
This commit is contained in:
@@ -91,16 +91,8 @@ object ServerPacketHandler {
|
||||
private fun handleLoadRequest(player: ServerPlayer) {
|
||||
try {
|
||||
val pc = player.pc()
|
||||
val box1 = pc.boxes[0]
|
||||
|
||||
var pokemonCount = 0
|
||||
box1.filterNotNull().forEach { _ -> pokemonCount++ }
|
||||
|
||||
if (pokemonCount > 0) {
|
||||
sendResponse(player, false, "Box 1 is not empty!")
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch synced Pokemon from the server
|
||||
val response = request.GET("/api/cobblesync/${player.uuid}")
|
||||
logger.info(response.toString())
|
||||
|
||||
@@ -110,16 +102,56 @@ object ServerPacketHandler {
|
||||
}
|
||||
|
||||
val obj = JsonParser.parseString(response.getString("pokemon")).asJsonObject
|
||||
val newBox = box1.loadFromJSON(obj, player.registryAccess())
|
||||
|
||||
var loadedCount = 0
|
||||
newBox.pc.filterNotNull().forEach { pokemon ->
|
||||
logger.info("Loading Pokémon: ${pokemon.species.name} (Level ${pokemon.level})")
|
||||
box1.pc.add(pokemon)
|
||||
loadedCount++
|
||||
// Use a temporary box to parse the JSON data
|
||||
val tempBox = pc.boxes[0].loadFromJSON(obj, player.registryAccess())
|
||||
val pokemonToLoad = tempBox.pc.filterNotNull().toMutableList()
|
||||
|
||||
if (pokemonToLoad.isEmpty()) {
|
||||
sendResponse(player, false, "No Pokémon to load!")
|
||||
return
|
||||
}
|
||||
|
||||
sendResponse(player, true, "Successfully loaded $loadedCount Pokémon!")
|
||||
// Count available empty slots across all boxes
|
||||
var emptySlots = 0
|
||||
for (box in pc.boxes) {
|
||||
for (slot in 0 until 30) { // Each box has 30 slots
|
||||
if (box[slot] == null) {
|
||||
emptySlots++
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (emptySlots < pokemonToLoad.size) {
|
||||
sendResponse(
|
||||
player,
|
||||
false,
|
||||
"Not enough empty slots! Need ${pokemonToLoad.size}, have $emptySlots"
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
// Add Pokemon to empty slots across all boxes
|
||||
var loadedCount = 0
|
||||
for (pokemon in pokemonToLoad) {
|
||||
var placed = false
|
||||
for (box in pc.boxes) {
|
||||
if (placed) break
|
||||
for (slot in 0 until 30) {
|
||||
if (box[slot] == null) {
|
||||
box[slot] = pokemon
|
||||
logger.info(
|
||||
"Loaded Pokémon: ${pokemon.species.name} (Level ${pokemon.level}) to slot $slot"
|
||||
)
|
||||
loadedCount++
|
||||
placed = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sendResponse(player, true, "Successfully loaded $loadedCount Pokémon to empty slots!")
|
||||
} catch (e: HTTPException) {
|
||||
logger.error("HTTP Exception: ${e.message}")
|
||||
sendResponse(player, false, "Server error: ${e.message}")
|
||||
|
||||
Reference in New Issue
Block a user