From 114f6c9ccd1bac7f69d2c8cf7eaa7259f24f591b Mon Sep 17 00:00:00 2001 From: Moritz Ruth Date: Thu, 10 Sep 2020 21:54:23 +0200 Subject: [PATCH] Allow configuring the displayed difficulty --- .../src/main/kotlin/space/blokk/Difficulty.kt | 10 ++++++++++ .../src/main/kotlin/space/blokk/server/Server.kt | 5 +++-- .../kotlin/space/blokk/worlds/WorldDifficulty.kt | 8 -------- .../packets/play/ServerDifficultyPacketCodec.kt | 14 +++++++------- .../net/packets/play/ServerDifficultyPacket.kt | 4 ++-- .../src/main/kotlin/space/blokk/BlokkServer.kt | 1 + .../main/kotlin/space/blokk/net/JoinProcedure.kt | 9 ++------- 7 files changed, 25 insertions(+), 26 deletions(-) create mode 100644 blokk-api/src/main/kotlin/space/blokk/Difficulty.kt delete mode 100644 blokk-api/src/main/kotlin/space/blokk/worlds/WorldDifficulty.kt diff --git a/blokk-api/src/main/kotlin/space/blokk/Difficulty.kt b/blokk-api/src/main/kotlin/space/blokk/Difficulty.kt new file mode 100644 index 0000000..ce9c54d --- /dev/null +++ b/blokk-api/src/main/kotlin/space/blokk/Difficulty.kt @@ -0,0 +1,10 @@ +package space.blokk + +enum class Difficulty { + PEACEFUL, + EASY, + NORMAL, + HARD +} + +data class DifficultyOptions(val difficulty: Difficulty, val locked: Boolean) diff --git a/blokk-api/src/main/kotlin/space/blokk/server/Server.kt b/blokk-api/src/main/kotlin/space/blokk/server/Server.kt index 6459c2f..5e5779c 100644 --- a/blokk-api/src/main/kotlin/space/blokk/server/Server.kt +++ b/blokk-api/src/main/kotlin/space/blokk/server/Server.kt @@ -2,14 +2,15 @@ package space.blokk.server import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.channels.ReceiveChannel -import space.blokk.events.EventBus +import space.blokk.DifficultyOptions import space.blokk.events.EventTarget import space.blokk.server.events.ServerEvent interface Server : EventTarget { - override val eventBus: EventBus val scope: CoroutineScope + var displayedDifficultyOptions: DifficultyOptions + /** * Creates a [ReceiveChannel] which emits [Unit] every tick. * diff --git a/blokk-api/src/main/kotlin/space/blokk/worlds/WorldDifficulty.kt b/blokk-api/src/main/kotlin/space/blokk/worlds/WorldDifficulty.kt deleted file mode 100644 index 475bee2..0000000 --- a/blokk-api/src/main/kotlin/space/blokk/worlds/WorldDifficulty.kt +++ /dev/null @@ -1,8 +0,0 @@ -package space.blokk.worlds - -enum class WorldDifficulty { - PEACEFUL, - EASY, - NORMAL, - HARD -} diff --git a/blokk-packet-codecs/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacketCodec.kt b/blokk-packet-codecs/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacketCodec.kt index c3892ee..352abe2 100644 --- a/blokk-packet-codecs/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacketCodec.kt +++ b/blokk-packet-codecs/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacketCodec.kt @@ -1,20 +1,20 @@ package space.blokk.net.packets.play import io.netty.buffer.ByteBuf +import space.blokk.Difficulty import space.blokk.net.packets.OutgoingPacketCodec -import space.blokk.worlds.WorldDifficulty object ServerDifficultyPacketCodec : OutgoingPacketCodec(0x0E, ServerDifficultyPacket::class) { override fun ServerDifficultyPacket.encode(dst: ByteBuf) { dst.writeByte( - when (difficulty) { - WorldDifficulty.PEACEFUL -> 0 - WorldDifficulty.EASY -> 1 - WorldDifficulty.NORMAL -> 2 - WorldDifficulty.HARD -> 3 + when (difficultyOptions.difficulty) { + Difficulty.PEACEFUL -> 0 + Difficulty.EASY -> 1 + Difficulty.NORMAL -> 2 + Difficulty.HARD -> 3 } ) - dst.writeBoolean(locked) + dst.writeBoolean(difficultyOptions.locked) } } diff --git a/blokk-packets/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacket.kt b/blokk-packets/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacket.kt index fe5b8f2..9e11b62 100644 --- a/blokk-packets/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacket.kt +++ b/blokk-packets/src/main/kotlin/space/blokk/net/packets/play/ServerDifficultyPacket.kt @@ -1,9 +1,9 @@ package space.blokk.net.packets.play +import space.blokk.DifficultyOptions import space.blokk.net.packets.OutgoingPacket -import space.blokk.worlds.WorldDifficulty /** * Sets the difficulty shown in the pause menu. */ -data class ServerDifficultyPacket(val difficulty: WorldDifficulty, val locked: Boolean) : OutgoingPacket() +data class ServerDifficultyPacket(val difficultyOptions: DifficultyOptions) : OutgoingPacket() diff --git a/blokk-server/src/main/kotlin/space/blokk/BlokkServer.kt b/blokk-server/src/main/kotlin/space/blokk/BlokkServer.kt index 130aa3d..ea2c65c 100644 --- a/blokk-server/src/main/kotlin/space/blokk/BlokkServer.kt +++ b/blokk-server/src/main/kotlin/space/blokk/BlokkServer.kt @@ -24,6 +24,7 @@ class BlokkServer internal constructor() : Server { override val scope = CoroutineScope(CoroutineName("BlokkServer")) override val eventBus = EventBus(ServerEvent::class, scope) + override var displayedDifficultyOptions = DifficultyOptions(Difficulty.NORMAL, true) val logger = Logger("Server") var socketServer = BlokkSocketServer(); private set diff --git a/blokk-server/src/main/kotlin/space/blokk/net/JoinProcedure.kt b/blokk-server/src/main/kotlin/space/blokk/net/JoinProcedure.kt index b2ab9c8..0eafdd1 100644 --- a/blokk-server/src/main/kotlin/space/blokk/net/JoinProcedure.kt +++ b/blokk-server/src/main/kotlin/space/blokk/net/JoinProcedure.kt @@ -1,6 +1,7 @@ package space.blokk.net import io.netty.buffer.Unpooled +import space.blokk.Blokk import space.blokk.BlokkServer import space.blokk.chat.TextComponent import space.blokk.net.MinecraftDataTypes.writeString @@ -15,7 +16,6 @@ import space.blokk.net.packets.play.ServerDifficultyPacket import space.blokk.players.GameMode import space.blokk.utils.AuthenticationHelper import space.blokk.utils.EncryptionUtils -import space.blokk.worlds.WorldDifficulty import space.blokk.worlds.WorldDimension import space.blokk.worlds.WorldType import java.security.MessageDigest @@ -121,11 +121,6 @@ class JoinProcedure(val session: BlokkSession) { } private suspend fun joinWorld() { - session.send( - ServerDifficultyPacket( - WorldDifficulty.NORMAL, - true - ) - ) + session.send(ServerDifficultyPacket(Blokk.server.displayedDifficultyOptions)) } }