diff --git a/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt b/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt index 88fbebb..2b1fa06 100644 --- a/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt +++ b/test-plugin/src/main/kotlin/space/uranos/testplugin/TestPlugin.kt @@ -14,6 +14,7 @@ import space.uranos.net.event.SessionAfterLoginEvent import space.uranos.player.GameMode import space.uranos.plugin.Plugin import space.uranos.testplugin.anvil.AnvilWorld +import space.uranos.util.RGBColor import space.uranos.util.secondsToTicks import space.uranos.world.Biome import space.uranos.world.Chunk @@ -21,7 +22,6 @@ import space.uranos.world.Dimension import space.uranos.world.VoxelLocation import space.uranos.world.block.GreenWoolBlock import space.uranos.world.block.RedWoolBlock -import java.awt.Color class TestPlugin: Plugin("Test", "1.0.0") { override fun onEnable() { @@ -35,10 +35,10 @@ class TestPlugin: Plugin("Test", "1.0.0") { val biome = Biome( "test:test", Biome.Precipitation.NONE, - Color.GREEN, - Color.BLACK, - Color.ORANGE, - Color.GRAY, + RGBColor(0xFF0000), + RGBColor(0x000000), + RGBColor(0x00FF00), + RGBColor(0x444444), Biome.MoodSound(2, 0.0, "minecraft:entity.pig.ambient", 1), 0.0f, 10f diff --git a/uranos-api/src/main/kotlin/space/uranos/util/FloatFloorMod.kt b/uranos-api/src/main/kotlin/space/uranos/util/FloatFloorMod.kt new file mode 100644 index 0000000..804defc --- /dev/null +++ b/uranos-api/src/main/kotlin/space/uranos/util/FloatFloorMod.kt @@ -0,0 +1,15 @@ +/* + * Copyright 2020-2021 Moritz Ruth and Uranos contributors + * Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file + */ + +package space.uranos.util + +fun floorMod(dividend: Float, divisor: Float): Float { + if (divisor == 0f) throw ArithmeticException("divisor cannot be 0") + + val mod = dividend % divisor + + if (mod != 0f && ((mod < 0 && divisor > 0) || (mod > 0 && divisor < 0))) return mod + divisor + return mod +} diff --git a/uranos-api/src/test/kotlin/space/blokk/chat/LegacyFormattingCodeTest.kt b/uranos-api/src/test/kotlin/space/uranos/chat/LegacyFormattingCodeTest.kt similarity index 100% rename from uranos-api/src/test/kotlin/space/blokk/chat/LegacyFormattingCodeTest.kt rename to uranos-api/src/test/kotlin/space/uranos/chat/LegacyFormattingCodeTest.kt diff --git a/uranos-api/src/test/kotlin/space/uranos/util/FloatFloorModTest.kt b/uranos-api/src/test/kotlin/space/uranos/util/FloatFloorModTest.kt new file mode 100644 index 0000000..2f1be14 --- /dev/null +++ b/uranos-api/src/test/kotlin/space/uranos/util/FloatFloorModTest.kt @@ -0,0 +1,28 @@ +package space.uranos.util + +import org.junit.jupiter.api.Test +import strikt.api.expectThat +import strikt.api.expectThrows +import strikt.assertions.isEqualTo +import strikt.assertions.isTrue + +class FloatFloorModTest { + @Test + fun `throws an error when divisor is 0`() { + expectThrows { floorMod(0f, 0f) } + expectThrows { floorMod(20f, 0f) } + expectThrows { floorMod(-20f, 0f) } + } + + @Test + fun `returns correct results`() { + expectThat(floorMod(0f, 1f) == 0f).isTrue() + expectThat(floorMod(20f, 1f) == 0f).isTrue() + expectThat(floorMod(-20f, 1f) == 0f).isTrue() + + expectThat(floorMod(110f, 100f)).isEqualTo(10f) + expectThat(floorMod(-110f, 100f)).isEqualTo(90f) + expectThat(floorMod(110f, -100f)).isEqualTo(-90f) + expectThat(floorMod(-110f, -100f)).isEqualTo(-10f) + } +} diff --git a/uranos-api/src/test/kotlin/space/blokk/util/TicksTest.kt b/uranos-api/src/test/kotlin/space/uranos/util/TicksTest.kt similarity index 100% rename from uranos-api/src/test/kotlin/space/blokk/util/TicksTest.kt rename to uranos-api/src/test/kotlin/space/uranos/util/TicksTest.kt diff --git a/uranos-api/src/test/kotlin/space/blokk/world/block/BlockCodecTest.kt b/uranos-api/src/test/kotlin/space/uranos/world/block/BlockCodecTest.kt similarity index 100% rename from uranos-api/src/test/kotlin/space/blokk/world/block/BlockCodecTest.kt rename to uranos-api/src/test/kotlin/space/uranos/world/block/BlockCodecTest.kt diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/IncomingPlayerPositionPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/IncomingPlayerPositionPacketCodec.kt index 0875c25..ec56b75 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/IncomingPlayerPositionPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/IncomingPlayerPositionPacketCodec.kt @@ -8,6 +8,7 @@ package space.uranos.net.packet.play import io.netty.buffer.ByteBuf import space.uranos.Position import space.uranos.net.packet.IncomingPacketCodec +import space.uranos.util.floorMod object IncomingPlayerPositionPacketCodec : IncomingPacketCodec(0x13, IncomingPlayerPositionPacket::class) { @@ -16,8 +17,7 @@ object IncomingPlayerPositionPacketCodec : msg.readDouble(), msg.readDouble(), msg.readDouble(), - Math.floorMod((360 - msg.readFloat()).toInt(), 360) - .toFloat(), // TODO: Higher precision (do not use Math.floorMod) + floorMod(msg.readFloat(), 360f), msg.readFloat() ), msg.readBoolean() diff --git a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/PlayerOrientationPacketCodec.kt b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/PlayerOrientationPacketCodec.kt index 9e42ee5..a0186eb 100644 --- a/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/PlayerOrientationPacketCodec.kt +++ b/uranos-packet-codecs/src/main/kotlin/space/uranos/net/packet/play/PlayerOrientationPacketCodec.kt @@ -7,12 +7,12 @@ package space.uranos.net.packet.play import io.netty.buffer.ByteBuf import space.uranos.net.packet.IncomingPacketCodec +import space.uranos.util.floorMod object PlayerOrientationPacketCodec : IncomingPacketCodec(0x14, PlayerOrientationPacket::class) { override fun decode(msg: ByteBuf): PlayerOrientationPacket = PlayerOrientationPacket( - Math.floorMod((360 - msg.readFloat()).toInt(), 360) - .toFloat(), // TODO: Higher precision (do not use Math.floorMod), + floorMod(msg.readFloat(), 360f), msg.readFloat(), msg.readBoolean() ) diff --git a/uranos-packet-codecs/src/test/kotlin/space/blokk/net/packet/ProtocolValidationTest.kt b/uranos-packet-codecs/src/test/kotlin/space/uranos/net/packet/ProtocolValidationTest.kt similarity index 100% rename from uranos-packet-codecs/src/test/kotlin/space/blokk/net/packet/ProtocolValidationTest.kt rename to uranos-packet-codecs/src/test/kotlin/space/uranos/net/packet/ProtocolValidationTest.kt diff --git a/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt b/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt index 1047d61..e4817f3 100644 --- a/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt +++ b/uranos-server/src/main/kotlin/space/uranos/net/UranosSession.kt @@ -156,7 +156,7 @@ class UranosSession(val channel: io.netty.channel.Channel, val server: UranosSer scope.launch { if (state == State.Disconnected) return@launch if (!expected && currentProtocol != HandshakingProtocol && currentProtocol != StatusProtocol) - logger trace "The client disconnected unexpectedly" + logger trace "The client disconnected unexpectedly" // TODO: This is sometimes logged multiple times packetsAdapter.stopProcessingIncomingPackets() coroutineContext.cancel(DisconnectedCancellationException())