mirror of
https://codeberg.org/moonleay/Gimbal.git
synced 2025-04-09 07:54:14 +02:00
74 lines
3 KiB
Kotlin
74 lines
3 KiB
Kotlin
/*
|
|
* 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 me.lucko.fabric.api.permissions.v0.Permissions
|
|
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.build.BuildConstants
|
|
import net.moonleay.gimbal.constants.PacketIDs
|
|
import net.moonleay.gimbal.constants.PermissionIDs
|
|
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.state.GimbalServerState
|
|
import org.apache.logging.log4j.LogManager
|
|
|
|
object GimbalServer {
|
|
private val LOGGER = LogManager.getLogger(BuildConstants.modName)
|
|
|
|
fun registerPacketHandler() {
|
|
ServerPlayNetworking
|
|
.registerGlobalReceiver(PacketIDs.UPDATE_EDITOR_STATE_ID)
|
|
{ _, player, _, buf, _ ->
|
|
handleStateUpdate(player, buf)
|
|
}
|
|
}
|
|
|
|
@OptIn(ExperimentalSerializationApi::class)
|
|
fun notifyPlayerOfPolicy(player: ServerPlayerEntity) {
|
|
LOGGER.info("Sending policy to " + player.name.string)
|
|
val buf = PacketByteBufs.create()
|
|
buf.writeByteArray(
|
|
Cbor.encodeToByteArray(
|
|
GimbalServerState(
|
|
BuildConstants.protocolVersion.toInt(),
|
|
if (this.isPlayerAllowedToUseGimbal(player)) GimbalPolicyType.ALLOWED else GimbalPolicyType.DENIED
|
|
)
|
|
)
|
|
)
|
|
ServerPlayNetworking.send(player, PacketIDs.GIMBAL_POLICY_PACKET_ID, 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 isPlayerAllowedToUseGimbal(player: ServerPlayerEntity): Boolean {
|
|
return player.hasPermissionLevel(2) || Permissions.check(player, PermissionIDs.GIMBAL_USAGE_PERMISSION)
|
|
}
|
|
}
|