From c7b8aa4094ce2c9925dc3c45c070ca80542ac68b Mon Sep 17 00:00:00 2001 From: Bixilon Date: Fri, 23 Apr 2021 16:08:47 +0200 Subject: [PATCH] bitbyte: use own infix functions --- .../gui/input/key/RenderWindowInputHandler.kt | 9 ++-- .../packets/c2s/play/ToggleFlyC2SP.kt | 4 +- .../packets/s2c/play/PlayerAbilitiesS2CP.kt | 20 +++---- .../de/bixilon/minosoft/util/BitByte.java | 45 ---------------- .../java/de/bixilon/minosoft/util/BitByte.kt | 54 +++++++++++++++++++ 5 files changed, 71 insertions(+), 61 deletions(-) delete mode 100644 src/main/java/de/bixilon/minosoft/util/BitByte.java create mode 100644 src/main/java/de/bixilon/minosoft/util/BitByte.kt diff --git a/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt b/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt index 6345ef90f..1a63b05b1 100644 --- a/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt +++ b/src/main/java/de/bixilon/minosoft/gui/input/key/RenderWindowInputHandler.kt @@ -44,11 +44,12 @@ class RenderWindowInputHandler( init { registerKeyCallback(KeyBindingsNames.DEBUG_MOUSE_CATCH) { - if (it) { - glfwSetInputMode(renderWindow.windowId, GLFW_CURSOR, GLFW_CURSOR_DISABLED) + val newCursorMode = if (it) { + GLFW_CURSOR_DISABLED } else { - glfwSetInputMode(renderWindow.windowId, GLFW_CURSOR, GLFW_CURSOR_NORMAL) + GLFW_CURSOR_NORMAL } + glfwSetInputMode(renderWindow.windowId, GLFW_CURSOR, newCursorMode) renderWindow.sendDebugMessage("Toggled mouse catch!") } } @@ -179,7 +180,7 @@ class RenderWindowInputHandler( continue } - // Log.debug("Changing $resourceLocation because of $keyCode -> $combinationDown") + // Log.debug("Changing $resourceLocation because of $keyCode -> $thisKeyBindingDown") for (callback in pair.callback) { callback.invoke(thisKeyBindingDown) } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/c2s/play/ToggleFlyC2SP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/c2s/play/ToggleFlyC2SP.kt index ddf34c9ce..49e9e4269 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/c2s/play/ToggleFlyC2SP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/c2s/play/ToggleFlyC2SP.kt @@ -27,8 +27,8 @@ class ToggleFlyC2SP(val flying: Boolean) : PlayC2SPacket { buffer.writeByte(flags) if (buffer.versionId < ProtocolVersions.V_1_16_PRE4) { // only fly matters, everything else ignored - buffer.writeFloat(0.0f) // flyingSpeed - buffer.writeFloat(0.0f) // walkingSpeed + buffer.writeFloat(1.0f) // flyingSpeed + buffer.writeFloat(1.0f) // walkingSpeed } } diff --git a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerAbilitiesS2CP.kt b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerAbilitiesS2CP.kt index 918b923e2..df595049e 100644 --- a/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerAbilitiesS2CP.kt +++ b/src/main/java/de/bixilon/minosoft/protocol/packets/s2c/play/PlayerAbilitiesS2CP.kt @@ -16,7 +16,7 @@ import de.bixilon.minosoft.protocol.network.connection.PlayConnection import de.bixilon.minosoft.protocol.packets.s2c.PlayS2CPacket import de.bixilon.minosoft.protocol.protocol.PlayInByteBuffer import de.bixilon.minosoft.protocol.protocol.ProtocolVersions -import de.bixilon.minosoft.util.BitByte +import de.bixilon.minosoft.util.BitByte.isBit import de.bixilon.minosoft.util.logging.Log class PlayerAbilitiesS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() { @@ -29,17 +29,17 @@ class PlayerAbilitiesS2CP(buffer: PlayInByteBuffer) : PlayS2CPacket() { val walkingSpeed: Float init { - val flags = buffer.readByte() + val flags = buffer.readUnsignedByte() if (buffer.versionId < ProtocolVersions.V_14W03B) { // ToDo: Find out correct version - isInvulnerable = BitByte.isBitSet(flags.toInt(), 0) - isFlying = BitByte.isBitSet(flags.toInt(), 1) - canFly = BitByte.isBitSet(flags.toInt(), 2) - canInstantBuild = BitByte.isBitSet(flags.toInt(), 3) + isInvulnerable = flags isBit (0) + isFlying = flags.isBit(1) + canFly = flags.isBit(2) + canInstantBuild = flags.isBit(3) } else { - canInstantBuild = BitByte.isBitSet(flags.toInt(), 0) - isFlying = BitByte.isBitSet(flags.toInt(), 1) - canFly = BitByte.isBitSet(flags.toInt(), 2) - isInvulnerable = BitByte.isBitSet(flags.toInt(), 3) + canInstantBuild = flags.isBit(0) + isFlying = flags.isBit(1) + canFly = flags.isBit(2) + isInvulnerable = flags.isBit(3) } flyingSpeed = buffer.readFloat() walkingSpeed = buffer.readFloat() diff --git a/src/main/java/de/bixilon/minosoft/util/BitByte.java b/src/main/java/de/bixilon/minosoft/util/BitByte.java deleted file mode 100644 index 318ca125b..000000000 --- a/src/main/java/de/bixilon/minosoft/util/BitByte.java +++ /dev/null @@ -1,45 +0,0 @@ -/* - * Minosoft - * Copyright (C) 2020 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.util; - -public final class BitByte { - public static boolean isBitSet(long in, int pos) { - long mask = 1L << pos; - return ((in & mask) == mask); - } - - public static boolean isBitSet(int in, int pos) { - int mask = 1 << pos; - return ((in & mask) == mask); - } - - public static boolean isBitMask(int in, int mask) { - return ((in & mask) == mask); - } - - public static byte getBitCount(long input) { - byte ret = 0; - for (byte i = 0; i < Long.SIZE; i++) { - if (isBitSet(input, i)) { - ret++; - } - } - return ret; - } - - public static boolean isBitSetShort(short in, int pos) { - int mask = 1 << pos; - return ((in & mask) == mask); - } -} diff --git a/src/main/java/de/bixilon/minosoft/util/BitByte.kt b/src/main/java/de/bixilon/minosoft/util/BitByte.kt new file mode 100644 index 000000000..d90dd28fe --- /dev/null +++ b/src/main/java/de/bixilon/minosoft/util/BitByte.kt @@ -0,0 +1,54 @@ +/* + * Minosoft + * Copyright (C) 2020 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.util + +object BitByte { + fun isBitSet(`in`: Long, pos: Int): Boolean { + val mask = 1L shl pos + return `in` and mask == mask + } + + fun isBitSet(`in`: Int, pos: Int): Boolean { + val mask = 1 shl pos + return `in` and mask == mask + } + + @JvmStatic + fun isBitMask(`in`: Int, mask: Int): Boolean { + return `in` and mask == mask + } + + fun getBitCount(input: Long): Byte { + var ret: Byte = 0 + for (i in 0 until java.lang.Long.SIZE) { + if (isBitSet(input, i)) { + ret++ + } + } + return ret + } + + fun isBitSetShort(`in`: Short, pos: Int): Boolean { + val mask = 1 shl pos + return `in`.toInt() and mask == mask + } + + infix fun Int.isBit(bit: Int): Boolean { + return isBitSet(this, bit) + } + + @JvmName("isBitMask1") + infix fun Int.isBitMask(bitMask: Int): Boolean { + return isBitMask(this, bitMask) + } +}