refactor InitializeS2CP, RespawnS2CP, remove RespawnEvent

This commit is contained in:
Bixilon 2023-01-04 10:59:31 +01:00
parent 4fee62885f
commit e6bc1babc2
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 48 additions and 60 deletions

View File

@ -19,7 +19,7 @@ import de.bixilon.minosoft.data.registries.registries.registry.codec.ResourceLoc
data class Dimension( data class Dimension(
override val identifier: ResourceLocation, override val identifier: ResourceLocation,
val type: DimensionProperties, val properties: DimensionProperties,
) : RegistryItem() { ) : RegistryItem() {
override fun toString(): String { override fun toString(): String {
@ -30,7 +30,7 @@ data class Dimension(
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: Map<String, Any>): Dimension { override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: Map<String, Any>): Dimension {
return Dimension( return Dimension(
identifier = resourceLocation, identifier = resourceLocation,
type = DimensionProperties.deserialize(data) properties = DimensionProperties.deserialize(data)
) )
} }
} }

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -25,8 +25,7 @@ import de.bixilon.minosoft.gui.rendering.gui.gui.ElementStates
import de.bixilon.minosoft.gui.rendering.gui.gui.GUIBuilder import de.bixilon.minosoft.gui.rendering.gui.gui.GUIBuilder
import de.bixilon.minosoft.gui.rendering.gui.gui.LayoutedGUIElement import de.bixilon.minosoft.gui.rendering.gui.gui.LayoutedGUIElement
import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.Menu import de.bixilon.minosoft.gui.rendering.gui.gui.screen.menu.Menu
import de.bixilon.minosoft.modding.event.events.RespawnEvent import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionStates
import de.bixilon.minosoft.modding.event.listener.CallbackEventListener.Companion.listen
import de.bixilon.minosoft.protocol.packets.c2s.play.ClientActionC2SP import de.bixilon.minosoft.protocol.packets.c2s.play.ClientActionC2SP
class RespawnMenu(guiRenderer: GUIRenderer) : Menu(guiRenderer) { class RespawnMenu(guiRenderer: GUIRenderer) : Menu(guiRenderer) {
@ -72,10 +71,12 @@ class RespawnMenu(guiRenderer: GUIRenderer) : Menu(guiRenderer) {
guiRenderer.gui.pop(element) guiRenderer.gui.pop(element)
} }
} }
guiRenderer.connection.events.listen<RespawnEvent> { guiRenderer.connection::state.observe(this) {
if (it != PlayConnectionStates.SPAWNING) return@observe
val element = guiRenderer.gui[this] val element = guiRenderer.gui[this]
if (element.state == ElementStates.CLOSED) { if (element.state == ElementStates.CLOSED) {
return@listen return@observe
} }
element.layout.canPop = true element.layout.canPop = true
guiRenderer.gui.pop(element) guiRenderer.gui.pop(element)

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -20,7 +20,7 @@ import de.bixilon.minosoft.gui.rendering.util.VecUtil.inSectionHeight
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.chunkPosition import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.chunkPosition
import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.inChunkSectionPosition import de.bixilon.minosoft.gui.rendering.util.vec.vec3.Vec3iUtil.inChunkSectionPosition
import de.bixilon.minosoft.gui.rendering.world.WorldRenderer import de.bixilon.minosoft.gui.rendering.world.WorldRenderer
import de.bixilon.minosoft.modding.event.events.RespawnEvent import de.bixilon.minosoft.modding.event.events.DimensionChangeEvent
import de.bixilon.minosoft.modding.event.events.blocks.BlockDataChangeEvent import de.bixilon.minosoft.modding.event.events.blocks.BlockDataChangeEvent
import de.bixilon.minosoft.modding.event.events.blocks.BlockSetEvent import de.bixilon.minosoft.modding.event.events.blocks.BlockSetEvent
import de.bixilon.minosoft.modding.event.events.blocks.BlocksSetEvent import de.bixilon.minosoft.modding.event.events.blocks.BlocksSetEvent
@ -122,7 +122,7 @@ object WorldRendererChangeListener {
listenBlockSet(renderer) listenBlockSet(renderer)
listenBlocksSet(renderer) listenBlocksSet(renderer)
events.listen<RespawnEvent> { if (it.dimensionChange) renderer.unloadWorld() } events.listen<DimensionChangeEvent> { renderer.unloadWorld() }
events.listen<ChunkDataChangeEvent> { renderer.master.tryQueue(it.chunk) } events.listen<ChunkDataChangeEvent> { renderer.master.tryQueue(it.chunk) }
events.listen<LightChangeEvent> { events.listen<LightChangeEvent> {

View File

@ -1,6 +1,6 @@
/* /*
* Minosoft * Minosoft
* Copyright (C) 2020-2022 Moritz Zwerger * Copyright (C) 2020-2023 Moritz Zwerger
* *
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
* *
@ -15,8 +15,6 @@ package de.bixilon.minosoft.modding.event.events
import de.bixilon.minosoft.modding.event.events.connection.play.PlayConnectionEvent import de.bixilon.minosoft.modding.event.events.connection.play.PlayConnectionEvent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
@Deprecated("Observables") class DimensionChangeEvent(
class RespawnEvent(
connection: PlayConnection, connection: PlayConnection,
val dimensionChange: Boolean,
) : PlayConnectionEvent(connection) ) : PlayConnectionEvent(connection)

View File

@ -44,21 +44,17 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
val gamemode: Gamemodes val gamemode: Gamemodes
var world: ResourceLocation? = null var world: ResourceLocation? = null
private set private set
var dimensionProperties: DimensionProperties? = null var dimension: DimensionProperties? = null
private set private set
var dimensionType: ResourceLocation? = null var dimensionName: ResourceLocation? = null
private set private set
var difficulty: Difficulties = Difficulties.NORMAL var difficulty: Difficulties = Difficulties.NORMAL
private set private set
var viewDistance = 0 var viewDistance = 0
private set private set
var maxPlayers = 0 var reducedDebugScreen = false
private set private set
var levelType: String? = null var respawnScreen = true
private set
var isReducedDebugScreen = false
private set
var isEnableRespawnScreen = true
private set private set
var hashedSeed: Long = 0L var hashedSeed: Long = 0L
private set private set
@ -83,32 +79,32 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} }
if (buffer.versionId < ProtocolVersions.V_1_9_1) { if (buffer.versionId < ProtocolVersions.V_1_9_1) {
dimensionProperties = buffer.connection.registries.dimension[buffer.readByte().toInt()].type dimension = buffer.connection.registries.dimension[buffer.readByte().toInt()].properties
difficulty = Difficulties[buffer.readUnsignedByte()] difficulty = Difficulties[buffer.readUnsignedByte()]
maxPlayers = buffer.readUnsignedByte() buffer.readUnsignedByte() // max players
if (buffer.versionId >= ProtocolVersions.V_13W42B) { if (buffer.versionId >= ProtocolVersions.V_13W42B) {
levelType = buffer.readString() buffer.readString() // level type
} }
if (buffer.versionId >= ProtocolVersions.V_14W29A) { if (buffer.versionId >= ProtocolVersions.V_14W29A) {
isReducedDebugScreen = buffer.readBoolean() reducedDebugScreen = buffer.readBoolean()
} }
} else { } else {
if (buffer.versionId >= ProtocolVersions.V_1_16_PRE6) { if (buffer.versionId >= ProtocolVersions.V_1_16_PRE6) {
buffer.readByte() // previous game mode buffer.readByte() // previous game mode
} }
if (buffer.versionId >= ProtocolVersions.V_20W22A) { if (buffer.versionId >= ProtocolVersions.V_20W22A) {
buffer.readArray { buffer.readResourceLocation() } // list of dimensions buffer.readArray { buffer.readResourceLocation() } // list of worlds
} }
if (buffer.versionId < ProtocolVersions.V_20W21A) { if (buffer.versionId < ProtocolVersions.V_20W21A) {
dimensionProperties = buffer.connection.registries.dimension[buffer.readInt()].type dimension = buffer.connection.registries.dimension[buffer.readInt()].properties
} else { } else {
registries = buffer.readNBT().asJsonObject() registries = buffer.readNBT().asJsonObject()
if (buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A) { if (buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A) {
dimensionType = buffer.readResourceLocation() // dimension type dimensionName = buffer.readResourceLocation() // dimension type
} else { } else {
dimensionProperties = DimensionProperties.deserialize(buffer.readNBT().asJsonObject()) dimension = DimensionProperties.deserialize(buffer.readNBT().asJsonObject())
} }
this.world = buffer.readResourceLocation() // dimension id this.world = buffer.readResourceLocation()
} }
if (buffer.versionId >= ProtocolVersions.V_19W36A) { if (buffer.versionId >= ProtocolVersions.V_19W36A) {
@ -117,13 +113,13 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
if (buffer.versionId < ProtocolVersions.V_19W11A) { if (buffer.versionId < ProtocolVersions.V_19W11A) {
difficulty = Difficulties[buffer.readUnsignedByte()] difficulty = Difficulties[buffer.readUnsignedByte()]
} }
maxPlayers = if (buffer.versionId < ProtocolVersions.V_1_16_2_RC1) { if (buffer.versionId < ProtocolVersions.V_1_16_2_RC1) {
buffer.readByte().toInt() buffer.readUnsignedByte() // max players
} else { } else {
buffer.readVarInt() buffer.readVarInt() // max players
} }
if (buffer.versionId < ProtocolVersions.V_20W20A) { if (buffer.versionId < ProtocolVersions.V_20W20A) {
levelType = buffer.readString() buffer.readString() // level type
} }
if (buffer.versionId >= ProtocolVersions.V_19W13A) { if (buffer.versionId >= ProtocolVersions.V_19W13A) {
viewDistance = buffer.readVarInt() viewDistance = buffer.readVarInt()
@ -133,13 +129,11 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} }
if (buffer.versionId >= ProtocolVersions.V_20W20A) { if (buffer.versionId >= ProtocolVersions.V_20W20A) {
buffer.readBoolean() // isDebug buffer.readBoolean() // isDebug
if (buffer.readBoolean()) { buffer.readBoolean() // flat
levelType = "flat"
}
} }
isReducedDebugScreen = buffer.readBoolean() reducedDebugScreen = buffer.readBoolean()
if (buffer.versionId >= ProtocolVersions.V_19W36A) { if (buffer.versionId >= ProtocolVersions.V_19W36A) {
isEnableRespawnScreen = buffer.readBoolean() respawnScreen = buffer.readBoolean()
} }
if (buffer.versionId >= ProtocolVersions.V_1_19_PRE2) { if (buffer.versionId >= ProtocolVersions.V_1_19_PRE2) {
lastDeathPosition = buffer.readPlayOptional { GlobalPositionEntityDataType.read(this) } lastDeathPosition = buffer.readPlayOptional { GlobalPositionEntityDataType.read(this) }
@ -160,7 +154,7 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
connection.world.hardcore = isHardcore connection.world.hardcore = isHardcore
registries?.let { connection.registries.update(it) } registries?.let { connection.registries.update(it) }
connection.world.dimension = dimensionProperties ?: connection.registries.dimension[dimensionType]?.type ?: throw NullPointerException("Can not find dimension: $dimensionType") connection.world.dimension = dimension ?: connection.registries.dimension[dimensionName]?.properties ?: throw NullPointerException("Can not find dimension: $dimensionName")
connection.world.name = world connection.world.name = world
@ -184,7 +178,7 @@ class InitializeS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Initialize (entityId=$entityId, gamemode=$gamemode, dimensionType=$dimensionProperties, difficulty=$difficulty, hardcore=$isHardcore, viewDistance=$viewDistance)" } Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Initialize (entityId=$entityId, gamemode=$gamemode, dimension=$dimension, difficulty=$difficulty, hardcore=$isHardcore, viewDistance=$viewDistance)" }
} }
companion object : PacketErrorHandler { companion object : PacketErrorHandler {

View File

@ -19,7 +19,7 @@ import de.bixilon.minosoft.data.entities.data.types.GlobalPositionEntityDataType
import de.bixilon.minosoft.data.registries.ResourceLocation import de.bixilon.minosoft.data.registries.ResourceLocation
import de.bixilon.minosoft.data.registries.dimension.DimensionProperties import de.bixilon.minosoft.data.registries.dimension.DimensionProperties
import de.bixilon.minosoft.data.world.difficulty.Difficulties import de.bixilon.minosoft.data.world.difficulty.Difficulties
import de.bixilon.minosoft.modding.event.events.RespawnEvent import de.bixilon.minosoft.modding.event.events.DimensionChangeEvent
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection import de.bixilon.minosoft.protocol.network.connection.play.PlayConnection
import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionStates import de.bixilon.minosoft.protocol.network.connection.play.PlayConnectionStates
import de.bixilon.minosoft.protocol.packets.factory.LoadPacket import de.bixilon.minosoft.protocol.packets.factory.LoadPacket
@ -37,14 +37,8 @@ class RespawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
var difficulty: Difficulties = Difficulties.NORMAL var difficulty: Difficulties = Difficulties.NORMAL
private set private set
val gamemode: Gamemodes val gamemode: Gamemodes
var levelType: String? = null
private set
var hashedSeed = 0L var hashedSeed = 0L
private set private set
var isDebug = false
private set
var isFlat = false
private set
var keepAttributes = false var keepAttributes = false
private set private set
var keepFlags: Byte = 0xFF.toByte() var keepFlags: Byte = 0xFF.toByte()
@ -57,20 +51,19 @@ class RespawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
init { init {
dimension = when { dimension = when {
buffer.versionId < ProtocolVersions.V_20W21A -> { buffer.versionId < ProtocolVersions.V_20W21A -> {
buffer.connection.registries.dimension[if (buffer.versionId < ProtocolVersions.V_1_8_9) { // ToDo: this should be 108 but wiki.vg is wrong. In 1.8 it is an int. val id = if (buffer.versionId < ProtocolVersions.V_1_8_9) { // ToDo: this should be 108 but wiki.vg is wrong. In 1.8 it is an int.
buffer.readByte().toInt() buffer.readByte().toInt()
} else { } else {
buffer.readInt() buffer.readInt()
}].type }
buffer.connection.registries.dimension[id].properties
} }
buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A -> { buffer.versionId < ProtocolVersions.V_1_16_2_PRE3 || buffer.versionId >= ProtocolVersions.V_22W19A -> {
buffer.readLegacyRegistryItem(buffer.connection.registries.dimension)!!.type buffer.readLegacyRegistryItem(buffer.connection.registries.dimension)!!.properties
} }
else -> { else -> DimensionProperties.deserialize(buffer.readNBT().asJsonObject()) // current dimension data
DimensionProperties.deserialize(buffer.readNBT().asJsonObject()) // current dimension data
}
} }
if (buffer.versionId < ProtocolVersions.V_19W11A) { if (buffer.versionId < ProtocolVersions.V_19W11A) {
difficulty = Difficulties[buffer.readUnsignedByte()] difficulty = Difficulties[buffer.readUnsignedByte()]
@ -86,11 +79,11 @@ class RespawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
buffer.readByte() // previous game mode buffer.readByte() // previous game mode
} }
if (buffer.versionId >= ProtocolVersions.V_13W42B && buffer.versionId < ProtocolVersions.V_20W20A) { if (buffer.versionId >= ProtocolVersions.V_13W42B && buffer.versionId < ProtocolVersions.V_20W20A) {
levelType = buffer.readString() buffer.readString() // level type
} }
if (buffer.versionId >= ProtocolVersions.V_20W20A) { if (buffer.versionId >= ProtocolVersions.V_20W20A) {
isDebug = buffer.readBoolean() buffer.readBoolean() // debug
isFlat = buffer.readBoolean() buffer.readBoolean() // flat
} }
if (buffer.versionId >= ProtocolVersions.V_20W18A) { if (buffer.versionId >= ProtocolVersions.V_20W18A) {
if (buffer.versionId >= ProtocolVersions.V_1_19_3_RC3) { if (buffer.versionId >= ProtocolVersions.V_1_19_3_RC3) {
@ -115,10 +108,12 @@ class RespawnS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket {
connection.world.name = world connection.world.name = world
connection.state = PlayConnectionStates.SPAWNING connection.state = PlayConnectionStates.SPAWNING
connection.events.fire(RespawnEvent(connection, dimensionChange)) if (dimensionChange) {
connection.events.fire(DimensionChangeEvent(connection))
}
} }
override fun log(reducedLog: Boolean) { override fun log(reducedLog: Boolean) {
Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Respawn (dimension=$dimension, difficulty=$difficulty, gamemode=$gamemode, levelType=$levelType)" } Log.log(LogMessageType.NETWORK_PACKETS_IN, level = LogLevels.VERBOSE) { "Respawn (dimension=$dimension, difficulty=$difficulty, gamemode=$gamemode)" }
} }
} }