Merge branch 'data-sync-improvements' into 'main'

Fixed syncing of learnset, stabilize registry sync order, fix client logging...

See merge request cable-mc/pokemon-cobbled!186
This commit is contained in:
Licious
2022-11-04 14:09:49 +00:00
8 changed files with 51 additions and 4 deletions
@@ -43,6 +43,7 @@ open class MoveTemplate(
val effectChance: Double,
val effectStatus: String
) {
var id = 0
val displayName: MutableText
get() = lang("move.$name")
val description: MutableText
@@ -42,18 +42,31 @@ object Moves : JsonDataRegistry<MoveTemplate> {
override val observable = SimpleObservable<Moves>()
private val allMoves = mutableMapOf<String, MoveTemplate>()
private val idMapping = mutableMapOf<Int, MoveTemplate>()
override fun reload(data: Map<Identifier, MoveTemplate>) {
this.allMoves.clear()
data.forEach { (identifier, moveTemplate) -> this.allMoves[identifier.path] = moveTemplate }
applyIDs()
Pokemod.LOGGER.info("Loaded {} moves", this.allMoves.size)
this.observable.emit(this)
}
private fun applyIDs() {
var id = 0
this.allMoves.values
.sortedBy { it.name }
.forEach {
it.id = id++
idMapping[it.id] = it
}
}
override fun sync(player: ServerPlayerEntity) {
MovesRegistrySyncPacket().sendToPlayer(player)
}
fun getByName(name: String) = allMoves[name.lowercase()]
fun getByNumericalId(id: Int) = idMapping[id]!!
fun getByNameOrDummy(name: String) = allMoves[name.lowercase()] ?: MoveTemplate.dummy(name.lowercase())
fun getExceptional() = getByName("tackle") ?: allMoves.values.random()
fun count() = allMoves.size
@@ -10,8 +10,12 @@ package com.cablemc.pokemod.common.api.pokemon.moves
import com.cablemc.pokemod.common.api.moves.MoveTemplate
import com.cablemc.pokemod.common.api.moves.Moves
import com.cablemc.pokemod.common.net.IntSize
import com.cablemc.pokemod.common.util.isInt
import com.cablemc.pokemod.common.util.readSizedInt
import com.cablemc.pokemod.common.util.writeSizedInt
import com.google.gson.JsonElement
import net.minecraft.network.PacketByteBuf
open class Learnset {
class Interpreter(
@@ -87,4 +91,27 @@ open class Learnset {
.sortedBy { it.key }
.flatMap { it.value }
.toSet()
fun encodeLevelUpMoves(buffer: PacketByteBuf) {
buffer.writeSizedInt(IntSize.U_BYTE, levelUpMoves.size)
for ((level, moves) in levelUpMoves) {
buffer.writeSizedInt(IntSize.U_SHORT, level)
buffer.writeSizedInt(IntSize.U_BYTE, moves.size)
for (move in moves) {
buffer.writeSizedInt(IntSize.U_SHORT, move.id)
}
}
}
fun decodeLevelUpMoves(buffer: PacketByteBuf) {
repeat(times = buffer.readSizedInt(IntSize.U_BYTE)) {
val level = buffer.readSizedInt(IntSize.U_SHORT)
val moves = mutableListOf<MoveTemplate>()
repeat(times = buffer.readSizedInt(IntSize.U_BYTE)) {
val move = Moves.getByNumericalId(buffer.readSizedInt(IntSize.U_SHORT))
moves.add(move)
}
levelUpMoves[level] = moves
}
}
}
@@ -42,6 +42,7 @@ class PokemonClientDelegate : PoseableEntityState<PokemonEntity>(), PokemonSideD
private val minimumFallSpeed = -0.1F
private val intensityVelocityCap = -0.5F
override fun changePokemon(pokemon: Pokemon) {
pokemon.isClient = true
entity.subscriptions.add(entity.species.subscribeIncludingCurrent {
currentPose = null
entity.pokemon.species = PokemonSpecies.getByIdentifier(Identifier(it))!! // TODO exception handling
@@ -27,7 +27,7 @@ internal object CobbledDataProvider : DataProvider {
// Both Forge n Fabric keep insertion order so if a registry depends on another simply register it after
var canReload = true
private val registries = hashMapOf<Identifier, DataRegistry>()
private val registries = linkedMapOf<Identifier, DataRegistry>()
fun registerDefaults() {
this.register(Moves)
@@ -13,12 +13,12 @@ import com.cablemc.pokemod.common.api.drop.DropTable
import com.cablemc.pokemod.common.api.moves.MoveTemplate
import com.cablemc.pokemod.common.api.net.Decodable
import com.cablemc.pokemod.common.api.net.Encodable
import com.cablemc.pokemod.common.api.pokemon.moves.Learnset
import com.cablemc.pokemod.common.api.pokemon.effect.ShoulderEffect
import com.cablemc.pokemod.common.api.pokemon.egg.EggGroup
import com.cablemc.pokemod.common.api.pokemon.evolution.Evolution
import com.cablemc.pokemod.common.api.pokemon.evolution.PreEvolution
import com.cablemc.pokemod.common.api.pokemon.experience.ExperienceGroup
import com.cablemc.pokemod.common.api.pokemon.moves.Learnset
import com.cablemc.pokemod.common.api.pokemon.stats.Stat
import com.cablemc.pokemod.common.api.pokemon.stats.Stats
import com.cablemc.pokemod.common.api.types.ElementalType
@@ -62,7 +62,7 @@ class FormData(
@SerializedName("shoulderEffects")
private val _shoulderEffects: MutableList<ShoulderEffect>? = null,
@SerializedName("moves")
private val _moves: Learnset? = null,
private var _moves: Learnset? = null,
@SerializedName("evolutions")
private val _evolutions: MutableSet<Evolution>? = null,
@SerializedName("abilities")
@@ -230,6 +230,8 @@ class FormData(
buffer.writeNullable(this._height) { pb, height -> pb.writeFloat(height) }
buffer.writeNullable(this._weight) { pb, weight -> pb.writeFloat(weight) }
buffer.writeNullable(this._pokedex) { pb1, pokedex -> pb1.writeCollection(pokedex) { pb2, line -> pb2.writeString(line) } }
buffer.writeBoolean(_moves != null)
buffer.writeNullable(_moves) { buf, moves -> moves.encodeLevelUpMoves(buf)}
}
override fun decode(buffer: PacketByteBuf) {
@@ -243,5 +245,6 @@ class FormData(
this._height = buffer.readNullable { pb -> pb.readFloat() }
this._weight = buffer.readNullable { pb -> pb.readFloat() }
this._pokedex = buffer.readNullable { pb -> pb.readList { it.readString() } }
this._moves = buffer.readNullable { pb -> Learnset().also { it.decodeLevelUpMoves(pb) }}
}
}
@@ -332,7 +332,7 @@ open class Pokemon {
}
}
private var isClient = false
internal var isClient = false
val storeCoordinates = SettableObservable<StoreCoordinates<*>?>(null)
// We want non-optional evolutions to trigger first to avoid unnecessary packets and any cost associate with an optional one that would just be lost
@@ -150,6 +150,7 @@ class Species : ClientDataSynchronizer<Species> {
buffer.writeNullable(this.secondaryType) { pb, type -> pb.writeString(type.name) }
buffer.writeCollection(this.pokedex) { pb, line -> pb.writeString(line) }
buffer.writeCollection(this.forms) { pb, form -> form.encode(pb) }
moves.encodeLevelUpMoves(buffer)
}
override fun decode(buffer: PacketByteBuf) {
@@ -164,6 +165,7 @@ class Species : ClientDataSynchronizer<Species> {
pokedex += buffer.readList { pb -> pb.readString() }
forms.clear()
forms += buffer.readList{ pb -> FormData().apply { decode(pb) } }.filterNotNull()
moves.decodeLevelUpMoves(buffer)
}
}