From 67722f8e78cea003752c41ff748470269e43e728 Mon Sep 17 00:00:00 2001 From: Moritz Zwerger Date: Thu, 14 Dec 2023 13:37:05 +0100 Subject: [PATCH] dynamic vibration sources Done with factories now --- .../particle/data/VibrationParticleData.kt | 7 +++- .../particle/data/vibration/BlockSource.kt | 33 +++++++++++++++++ .../particle/data/vibration/EntitySource.kt | 35 +++++++++++++++++++ .../data/vibration/VibrationFactory.kt | 22 ++++++++++++ .../data/vibration/VibrationSource.kt | 16 +++++++++ .../data/vibration/VibrationSources.kt | 30 ++++++++++++++++ .../packets/s2c/play/world/VibrationS2CP.kt | 5 +-- .../protocol/buffers/play/PlayInByteBuffer.kt | 13 +++---- 8 files changed, 147 insertions(+), 14 deletions(-) create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/BlockSource.kt create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/EntitySource.kt create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationFactory.kt create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSource.kt create mode 100644 src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSources.kt diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/VibrationParticleData.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/VibrationParticleData.kt index dd4aecb51..514aaef06 100644 --- a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/VibrationParticleData.kt +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/VibrationParticleData.kt @@ -13,9 +13,14 @@ package de.bixilon.minosoft.data.registries.particle.data import de.bixilon.minosoft.data.registries.particle.ParticleType +import de.bixilon.minosoft.data.registries.particle.data.vibration.VibrationSource import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer -class VibrationParticleData(val source: Any, val arrival: Int, type: ParticleType) : ParticleData(type) { +class VibrationParticleData( + val source: VibrationSource, + val arrival: Int, + type: ParticleType, +) : ParticleData(type) { override fun toString(): String { return "$type: $source in $arrival" diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/BlockSource.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/BlockSource.kt new file mode 100644 index 000000000..6aa8668e5 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/BlockSource.kt @@ -0,0 +1,33 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.particle.data.vibration + +import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft +import de.bixilon.minosoft.data.world.positions.BlockPosition +import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer + +class BlockSource( + val position: BlockPosition, +) : VibrationSource { + + companion object : VibrationFactory { + override val identifier = minecraft("block") + + override fun read(buffer: PlayInByteBuffer): BlockSource { + val position = buffer.readBlockPosition() + + return BlockSource(position) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/EntitySource.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/EntitySource.kt new file mode 100644 index 000000000..f285feea6 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/EntitySource.kt @@ -0,0 +1,35 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.particle.data.vibration + +import de.bixilon.minosoft.data.registries.identified.Namespaces.minecraft +import de.bixilon.minosoft.protocol.protocol.ProtocolVersions.V_1_20_1 +import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer + +class EntitySource( + val entityId: Int, + val yOffset: Float = 0.0f, +) : VibrationSource { + + companion object : VibrationFactory { + override val identifier = minecraft("entity") + + override fun read(buffer: PlayInByteBuffer): EntitySource { + val entityId = buffer.readEntityId() + val yOffset = if (buffer.versionId >= V_1_20_1) buffer.readFloat() else 0.0f // TODO: version guessed. Present in 1.20.2 + + return EntitySource(entityId, yOffset) + } + } +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationFactory.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationFactory.kt new file mode 100644 index 000000000..6cb0c4214 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationFactory.kt @@ -0,0 +1,22 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.particle.data.vibration + +import de.bixilon.minosoft.data.registries.identified.Identified +import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer + +interface VibrationFactory : Identified { + + fun read(buffer: PlayInByteBuffer): V +} diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSource.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSource.kt new file mode 100644 index 000000000..e46c3efd7 --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSource.kt @@ -0,0 +1,16 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.particle.data.vibration + +interface VibrationSource diff --git a/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSources.kt b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSources.kt new file mode 100644 index 000000000..01b33bccf --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/data/registries/particle/data/vibration/VibrationSources.kt @@ -0,0 +1,30 @@ +/* + * Minosoft + * 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 distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along with this program. If not, see . + * + * This software is not affiliated with Mojang AB, the original developer of Minecraft. + */ + +package de.bixilon.minosoft.data.registries.particle.data.vibration + +import de.bixilon.minosoft.data.registries.factory.DefaultFactory +import de.bixilon.minosoft.protocol.protocol.buffers.play.PlayInByteBuffer + +object VibrationSources : DefaultFactory>( + BlockSource, + EntitySource, +) { + + fun read(buffer: PlayInByteBuffer): VibrationSource { + val type = buffer.readResourceLocation() + val factory = this[type] ?: throw IllegalArgumentException("Can not find vibration source: $type") + + return factory.read(buffer) + } +} diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/world/VibrationS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/world/VibrationS2CP.kt index 029939257..a23ca1458 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/world/VibrationS2CP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/world/VibrationS2CP.kt @@ -25,9 +25,6 @@ class VibrationS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket { val arrival: Int = buffer.readVarInt() override fun log(reducedLog: Boolean) { - Log.log( - LogMessageType.NETWORK_IN, - level = LogLevels.VERBOSE - ) { "Vibration signal (sourcePosition=$sourcePosition, target=$target, arrival=$arrival)" } + Log.log(LogMessageType.NETWORK_IN, level = LogLevels.VERBOSE) { "Vibration signal (sourcePosition=$sourcePosition, target=$target, arrival=$arrival)" } } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt b/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt index a11a0a106..44bdf5a7a 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/protocol/buffers/play/PlayInByteBuffer.kt @@ -28,6 +28,8 @@ import de.bixilon.minosoft.data.entities.entities.player.properties.textures.Pla import de.bixilon.minosoft.data.registries.chat.ChatParameter import de.bixilon.minosoft.data.registries.particle.ParticleType import de.bixilon.minosoft.data.registries.particle.data.* +import de.bixilon.minosoft.data.registries.particle.data.vibration.VibrationSource +import de.bixilon.minosoft.data.registries.particle.data.vibration.VibrationSources import de.bixilon.minosoft.data.registries.registries.registry.AbstractRegistry import de.bixilon.minosoft.data.registries.registries.registry.EnumRegistry import de.bixilon.minosoft.data.registries.registries.registry.Registry @@ -375,15 +377,8 @@ class PlayInByteBuffer : InByteBuffer { return Vec3d(readShort(), readShort(), readShort()) / ProtocolDefinition.VELOCITY_NETWORK_DIVIDER } - fun readVibrationSource(): Any { - val type = readResourceLocation() - val source: Any = when (type.toString()) { // TODO: dynamic, factories - "minecraft:block" -> readBlockPosition() - "minecraft:entity" -> readEntityId() - else -> error("Unknown vibration source: $type") - } - - return source + fun readVibrationSource(): VibrationSource { + return VibrationSources.read(this) } fun readLegacyBitSet(bytes: Int): BitSet {