mirror of
https://codeberg.org/moonleay/Gimbal.git
synced 2025-07-06 23:25:46 +02:00
chore: renamed to Gimbal, because I cannot spell apparently
Signed-off-by: moonleay <contact@moonleay.net>
This commit is contained in:
parent
1c7138bb58
commit
c14cdffeed
50 changed files with 322 additions and 279 deletions
62
src/main/java/net/moonleay/gimbal/networking/GimbalClient.kt
Normal file
62
src/main/java/net/moonleay/gimbal/networking/GimbalClient.kt
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Gimbal
|
||||
* Copyright (C) 2024 moonleay
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.gimbal.networking
|
||||
|
||||
import kotlinx.serialization.ExperimentalSerializationApi
|
||||
import kotlinx.serialization.cbor.Cbor
|
||||
import kotlinx.serialization.decodeFromByteArray
|
||||
import kotlinx.serialization.encodeToByteArray
|
||||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
|
||||
import net.minecraft.network.PacketByteBuf
|
||||
import net.moonleay.gimbal.client.editor.ClientEditor
|
||||
import net.moonleay.gimbal.editor.state.EditorState
|
||||
import net.moonleay.gimbal.editor.util.GimbalPolicy
|
||||
|
||||
object GimbalClient {
|
||||
|
||||
fun registerPacketHandlers() {
|
||||
ClientPlayNetworking.registerGlobalReceiver(PacketIDs.TRANSFER_GIMBLE_POLICY_ID) { _, _, buf, _ ->
|
||||
onAllowedCheck(buf)
|
||||
}
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
private fun onAllowedCheck(buf: PacketByteBuf) {
|
||||
val policy = Cbor.decodeFromByteArray<GimbalPolicy>(buf.readByteArray())
|
||||
ClientEditor.onAllowedCheck(policy) // Update the client's policy
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the given [EditorState] to the server.
|
||||
*/
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun sendEditorState(state: EditorState) {
|
||||
val buf = PacketByteBufs.create()
|
||||
buf.writeByteArray(Cbor.encodeToByteArray(state))
|
||||
ClientPlayNetworking.send(PacketIDs.UPDATE_EDITOR_STATE_ID, buf)
|
||||
}
|
||||
|
||||
@OptIn(ExperimentalSerializationApi::class)
|
||||
fun checkIfServerHasGimble(state: EditorState) {
|
||||
val buf = PacketByteBufs.create()
|
||||
buf.writeByteArray(Cbor.encodeToByteArray(state))
|
||||
ClientPlayNetworking.send(PacketIDs.GIMBLE_PRERENCE_CHECK_ID, buf)
|
||||
}
|
||||
}
|
62
src/main/java/net/moonleay/gimbal/networking/GimbalServer.kt
Normal file
62
src/main/java/net/moonleay/gimbal/networking/GimbalServer.kt
Normal file
|
@ -0,0 +1,62 @@
|
|||
/*
|
||||
* Gimbal
|
||||
* Copyright (C) 2024 moonleay
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.gimbal.networking
|
||||
|
||||
import kotlinx.serialization.cbor.Cbor
|
||||
import kotlinx.serialization.decodeFromByteArray
|
||||
import kotlinx.serialization.encodeToByteArray
|
||||
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
|
||||
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
|
||||
import net.minecraft.network.PacketByteBuf
|
||||
import net.minecraft.server.network.ServerPlayerEntity
|
||||
import net.moonleay.gimbal.editor.ServerEditorManager
|
||||
import net.moonleay.gimbal.editor.state.EditorState
|
||||
import net.moonleay.gimbal.editor.state.GimbalPolicyType
|
||||
import net.moonleay.gimbal.editor.util.GimbalPolicy
|
||||
|
||||
object GimbalServer {
|
||||
|
||||
fun registerPacketHandler() {
|
||||
ServerPlayNetworking
|
||||
.registerGlobalReceiver(PacketIDs.UPDATE_EDITOR_STATE_ID)
|
||||
{ server, player, handler, buf, responseSender ->
|
||||
handleStateUpdate(player, buf)
|
||||
}
|
||||
ServerPlayNetworking
|
||||
.registerGlobalReceiver(PacketIDs.GIMBLE_PRERENCE_CHECK_ID)
|
||||
{ server, player, handler, buf, responseSender ->
|
||||
handlePresenceCheck(player, buf)
|
||||
}
|
||||
}
|
||||
|
||||
private fun handleStateUpdate(player: ServerPlayerEntity, buf: PacketByteBuf){
|
||||
val state = Cbor.decodeFromByteArray<EditorState>(buf.readByteArray())
|
||||
ServerEditorManager.updateEditorState(player.uuid, state)
|
||||
// player.sendMessage(Text.of("Mode: ${state.editorMode} with ${state.editorModifier}"))
|
||||
}
|
||||
|
||||
private fun handlePresenceCheck(player: ServerPlayerEntity, buf: PacketByteBuf) {
|
||||
val state = Cbor.decodeFromByteArray<EditorState>(buf.readByteArray())
|
||||
ServerEditorManager.updateEditorState(player.uuid, state)
|
||||
|
||||
val buffer = PacketByteBufs.create()
|
||||
buffer.writeByteArray(Cbor.encodeToByteArray(GimbalPolicy(GimbalPolicyType.ALLOWED)))
|
||||
ServerPlayNetworking.send(player, PacketIDs.TRANSFER_GIMBLE_POLICY_ID, buffer)
|
||||
}
|
||||
}
|
29
src/main/java/net/moonleay/gimbal/networking/PacketIDs.kt
Normal file
29
src/main/java/net/moonleay/gimbal/networking/PacketIDs.kt
Normal file
|
@ -0,0 +1,29 @@
|
|||
/*
|
||||
* Gimbal
|
||||
* Copyright (C) 2024 moonleay
|
||||
*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package net.moonleay.gimbal.networking
|
||||
|
||||
import net.minecraft.util.Identifier
|
||||
import net.moonleay.gimbal.build.BuildConstants
|
||||
|
||||
object PacketIDs {
|
||||
val UPDATE_EDITOR_STATE_ID = Identifier(BuildConstants.modId, "update_editor_state")
|
||||
val GIMBLE_PRERENCE_CHECK_ID = Identifier(BuildConstants.modId, "gimble_preference_check")
|
||||
val TRANSFER_GIMBLE_POLICY_ID = Identifier(BuildConstants.modId, "gimble_is_present")
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue