Add floorMod function for Float
This commit is contained in:
parent
4fafdcbbc5
commit
ab09228565
10 changed files with 53 additions and 10 deletions
|
@ -14,6 +14,7 @@ import space.uranos.net.event.SessionAfterLoginEvent
|
||||||
import space.uranos.player.GameMode
|
import space.uranos.player.GameMode
|
||||||
import space.uranos.plugin.Plugin
|
import space.uranos.plugin.Plugin
|
||||||
import space.uranos.testplugin.anvil.AnvilWorld
|
import space.uranos.testplugin.anvil.AnvilWorld
|
||||||
|
import space.uranos.util.RGBColor
|
||||||
import space.uranos.util.secondsToTicks
|
import space.uranos.util.secondsToTicks
|
||||||
import space.uranos.world.Biome
|
import space.uranos.world.Biome
|
||||||
import space.uranos.world.Chunk
|
import space.uranos.world.Chunk
|
||||||
|
@ -21,7 +22,6 @@ import space.uranos.world.Dimension
|
||||||
import space.uranos.world.VoxelLocation
|
import space.uranos.world.VoxelLocation
|
||||||
import space.uranos.world.block.GreenWoolBlock
|
import space.uranos.world.block.GreenWoolBlock
|
||||||
import space.uranos.world.block.RedWoolBlock
|
import space.uranos.world.block.RedWoolBlock
|
||||||
import java.awt.Color
|
|
||||||
|
|
||||||
class TestPlugin: Plugin("Test", "1.0.0") {
|
class TestPlugin: Plugin("Test", "1.0.0") {
|
||||||
override fun onEnable() {
|
override fun onEnable() {
|
||||||
|
@ -35,10 +35,10 @@ class TestPlugin: Plugin("Test", "1.0.0") {
|
||||||
val biome = Biome(
|
val biome = Biome(
|
||||||
"test:test",
|
"test:test",
|
||||||
Biome.Precipitation.NONE,
|
Biome.Precipitation.NONE,
|
||||||
Color.GREEN,
|
RGBColor(0xFF0000),
|
||||||
Color.BLACK,
|
RGBColor(0x000000),
|
||||||
Color.ORANGE,
|
RGBColor(0x00FF00),
|
||||||
Color.GRAY,
|
RGBColor(0x444444),
|
||||||
Biome.MoodSound(2, 0.0, "minecraft:entity.pig.ambient", 1),
|
Biome.MoodSound(2, 0.0, "minecraft:entity.pig.ambient", 1),
|
||||||
0.0f,
|
0.0f,
|
||||||
10f
|
10f
|
||||||
|
|
|
@ -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
|
||||||
|
}
|
|
@ -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<ArithmeticException> { floorMod(0f, 0f) }
|
||||||
|
expectThrows<ArithmeticException> { floorMod(20f, 0f) }
|
||||||
|
expectThrows<ArithmeticException> { 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)
|
||||||
|
}
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ package space.uranos.net.packet.play
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
import space.uranos.Position
|
import space.uranos.Position
|
||||||
import space.uranos.net.packet.IncomingPacketCodec
|
import space.uranos.net.packet.IncomingPacketCodec
|
||||||
|
import space.uranos.util.floorMod
|
||||||
|
|
||||||
object IncomingPlayerPositionPacketCodec :
|
object IncomingPlayerPositionPacketCodec :
|
||||||
IncomingPacketCodec<IncomingPlayerPositionPacket>(0x13, IncomingPlayerPositionPacket::class) {
|
IncomingPacketCodec<IncomingPlayerPositionPacket>(0x13, IncomingPlayerPositionPacket::class) {
|
||||||
|
@ -16,8 +17,7 @@ object IncomingPlayerPositionPacketCodec :
|
||||||
msg.readDouble(),
|
msg.readDouble(),
|
||||||
msg.readDouble(),
|
msg.readDouble(),
|
||||||
msg.readDouble(),
|
msg.readDouble(),
|
||||||
Math.floorMod((360 - msg.readFloat()).toInt(), 360)
|
floorMod(msg.readFloat(), 360f),
|
||||||
.toFloat(), // TODO: Higher precision (do not use Math.floorMod)
|
|
||||||
msg.readFloat()
|
msg.readFloat()
|
||||||
),
|
),
|
||||||
msg.readBoolean()
|
msg.readBoolean()
|
||||||
|
|
|
@ -7,12 +7,12 @@ package space.uranos.net.packet.play
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf
|
import io.netty.buffer.ByteBuf
|
||||||
import space.uranos.net.packet.IncomingPacketCodec
|
import space.uranos.net.packet.IncomingPacketCodec
|
||||||
|
import space.uranos.util.floorMod
|
||||||
|
|
||||||
object PlayerOrientationPacketCodec :
|
object PlayerOrientationPacketCodec :
|
||||||
IncomingPacketCodec<PlayerOrientationPacket>(0x14, PlayerOrientationPacket::class) {
|
IncomingPacketCodec<PlayerOrientationPacket>(0x14, PlayerOrientationPacket::class) {
|
||||||
override fun decode(msg: ByteBuf): PlayerOrientationPacket = PlayerOrientationPacket(
|
override fun decode(msg: ByteBuf): PlayerOrientationPacket = PlayerOrientationPacket(
|
||||||
Math.floorMod((360 - msg.readFloat()).toInt(), 360)
|
floorMod(msg.readFloat(), 360f),
|
||||||
.toFloat(), // TODO: Higher precision (do not use Math.floorMod),
|
|
||||||
msg.readFloat(),
|
msg.readFloat(),
|
||||||
msg.readBoolean()
|
msg.readBoolean()
|
||||||
)
|
)
|
||||||
|
|
|
@ -156,7 +156,7 @@ class UranosSession(val channel: io.netty.channel.Channel, val server: UranosSer
|
||||||
scope.launch {
|
scope.launch {
|
||||||
if (state == State.Disconnected) return@launch
|
if (state == State.Disconnected) return@launch
|
||||||
if (!expected && currentProtocol != HandshakingProtocol && currentProtocol != StatusProtocol)
|
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()
|
packetsAdapter.stopProcessingIncomingPackets()
|
||||||
coroutineContext.cancel(DisconnectedCancellationException())
|
coroutineContext.cancel(DisconnectedCancellationException())
|
||||||
|
|
Reference in a new issue