From 60ceb1b3ae20d80ed25a595089c2366a27a5ffe7 Mon Sep 17 00:00:00 2001 From: moonleay Date: Wed, 24 Apr 2024 01:18:23 +0200 Subject: [PATCH] feat: added multiplayer support --- build.gradle.kts | 2 + src/main/java/net/moonleay/gimble/Main.kt | 8 ++- .../gimble/client/editor/ClientEditor.kt | 22 ++++++- .../editormode/EnableInsertModeShortcut.kt | 2 +- .../editormode/EnableReplaceModeShortcut.kt | 2 +- .../editormode/EnableVisualModeShortcut.kt | 2 +- .../ToggleBulldozerModifierShortcut.kt | 4 +- .../ToggleForcePlaceModifierShortcut.kt | 4 +- .../ToggleNoClipModifierShortcut.kt | 4 +- .../ToggleNoUpdatesModifierShortcut.kt | 4 +- .../gimble/editor/ServerEditorManager.kt | 28 +++++++++ .../gimble/editor/state/EditorState.kt | 11 ++++ .../modes => editor/state/mode}/Mode.kt | 3 +- .../state/mode}/ModeModifier.kt | 2 +- .../net/moonleay/gimble/mixin/HudMixin.java | 4 +- .../gimble/mixin/NoClipCameraFix.java | 2 +- .../moonleay/gimble/mixin/NoClipMixin.java | 61 +++++++++++++------ .../gimble/mixin/NormalModeMixin.java | 2 +- .../gimble/mixin/ReplaceModeMixin.java | 2 +- .../gimble/networking/GimbleClient.kt | 21 +++++++ .../gimble/networking/GimbleServer.kt | 27 ++++++++ .../moonleay/gimble/networking/PacketIDs.kt | 8 +++ 22 files changed, 183 insertions(+), 42 deletions(-) create mode 100644 src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt create mode 100644 src/main/java/net/moonleay/gimble/editor/state/EditorState.kt rename src/main/java/net/moonleay/gimble/{client/editor/modes => editor/state/mode}/Mode.kt (77%) rename src/main/java/net/moonleay/gimble/{client/editor/modes => editor/state/mode}/ModeModifier.kt (87%) create mode 100644 src/main/java/net/moonleay/gimble/networking/GimbleClient.kt create mode 100644 src/main/java/net/moonleay/gimble/networking/GimbleServer.kt create mode 100644 src/main/java/net/moonleay/gimble/networking/PacketIDs.kt diff --git a/build.gradle.kts b/build.gradle.kts index 7cf8da2..00d2130 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -67,6 +67,8 @@ dependencies { modImplementation("net.fabricmc:fabric-language-kotlin:$fabricKotlinVersion") modImplementation("de.huebcraft.mod-libs:configlib:$configlibVersion") + + modImplementation("org.jetbrains.kotlinx:kotlinx-serialization-json:$serializationVersion") } val targetJavaVersion = 17 diff --git a/src/main/java/net/moonleay/gimble/Main.kt b/src/main/java/net/moonleay/gimble/Main.kt index 43e211c..5cdb815 100644 --- a/src/main/java/net/moonleay/gimble/Main.kt +++ b/src/main/java/net/moonleay/gimble/Main.kt @@ -2,12 +2,18 @@ package net.moonleay.gimble import net.moonleay.gimble.build.BuildConstants import net.fabricmc.api.ModInitializer +import net.moonleay.gimble.networking.GimbleServer import org.apache.logging.log4j.LogManager internal object Main : ModInitializer { private val LOGGER = LogManager.getLogger(BuildConstants.modName) override fun onInitialize() { - LOGGER.info("Main has been initialized") + LOGGER.info("Initializing Gimble on the common side...") + LOGGER.info("Registering packets...") + GimbleServer.registerPacketHandler() + LOGGER.info("Packets have been registered.") + LOGGER.info("Gimble has been initialized on the common side.") + LOGGER.info("${BuildConstants.modName} (${BuildConstants.modId}) v.${BuildConstants.modVersion} by moonleay") } } diff --git a/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt b/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt index fbe5422..24121a6 100644 --- a/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt +++ b/src/main/java/net/moonleay/gimble/client/editor/ClientEditor.kt @@ -2,8 +2,11 @@ package net.moonleay.gimble.client.editor import net.minecraft.client.MinecraftClient import net.moonleay.gimble.client.util.ChatUtil -import net.moonleay.gimble.client.editor.modes.Mode -import net.moonleay.gimble.client.editor.modes.ModeModifier +import net.moonleay.gimble.editor.ServerEditorManager +import net.moonleay.gimble.editor.state.EditorState +import net.moonleay.gimble.editor.state.mode.Mode +import net.moonleay.gimble.editor.state.mode.ModeModifier +import net.moonleay.gimble.networking.GimbleClient object ClientEditor { var CURRENT_MODE = Mode.NORMAL @@ -11,6 +14,15 @@ object ClientEditor { private val TEMP_DISABLED_MODIFIERS = mutableListOf() + /* + * Send an updated player state to the server + * */ + fun updateServerState() { + val state = EditorState(CURRENT_MODE, CURRENT_MODE_MODIFIER) + GimbleClient.sendEditorState(state) + ServerEditorManager.updateEditorState(MinecraftClient.getInstance().player!!.uuid, state) + } + fun toggleModifier(mod: ModeModifier) { if (CURRENT_MODE.incompatibleModifiers.contains(mod)){ if (TEMP_DISABLED_MODIFIERS.contains(mod)) @@ -33,6 +45,9 @@ object ClientEditor { checkForIncompatibleModeModifiers() } + /** + * This runs on Mode updated + */ fun checkForIncompatibleModeModifiers() { if (TEMP_DISABLED_MODIFIERS.size > 0) { CURRENT_MODE_MODIFIER.addAll( @@ -53,6 +68,9 @@ object ClientEditor { } } + // Update State + this.updateServerState() + if(TEMP_DISABLED_MODIFIERS.isNotEmpty()) { ChatUtil.addToChatHistory("The following modifiers are not supported by this editor mode and are therefore currently disabled: " + getListAsString(TEMP_DISABLED_MODIFIERS), MinecraftClient.getInstance()) diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableInsertModeShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableInsertModeShortcut.kt index 2b71b65..6ee759c 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableInsertModeShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableInsertModeShortcut.kt @@ -4,7 +4,7 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.Mode +import net.moonleay.gimble.editor.state.mode.Mode class EnableInsertModeShortcut(key: KeyBinding): GimbleShortcut(key) { diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableReplaceModeShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableReplaceModeShortcut.kt index d735f05..2d6898a 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableReplaceModeShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableReplaceModeShortcut.kt @@ -4,7 +4,7 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.Mode +import net.moonleay.gimble.editor.state.mode.Mode class EnableReplaceModeShortcut(key: KeyBinding): GimbleShortcut(key) { diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableVisualModeShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableVisualModeShortcut.kt index d5aa13c..9264561 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableVisualModeShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormode/EnableVisualModeShortcut.kt @@ -4,7 +4,7 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.Mode +import net.moonleay.gimble.editor.state.mode.Mode class EnableVisualModeShortcut(key: KeyBinding): GimbleShortcut(key) { diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleBulldozerModifierShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleBulldozerModifierShortcut.kt index b930b71..3fd31be 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleBulldozerModifierShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleBulldozerModifierShortcut.kt @@ -4,12 +4,10 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.ModeModifier +import net.moonleay.gimble.editor.state.mode.ModeModifier class ToggleBulldozerModifierShortcut(key: KeyBinding): GimbleShortcut(key) { override fun onPressed(client: MinecraftClient) { ClientEditor.toggleModifier(ModeModifier.BULLDOZER) - - ClientEditor.onModifiersUpdated() } } diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleForcePlaceModifierShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleForcePlaceModifierShortcut.kt index 1bb4b8d..eae4119 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleForcePlaceModifierShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleForcePlaceModifierShortcut.kt @@ -4,12 +4,10 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.ModeModifier +import net.moonleay.gimble.editor.state.mode.ModeModifier class ToggleForcePlaceModifierShortcut(key: KeyBinding): GimbleShortcut(key) { override fun onPressed(client: MinecraftClient) { ClientEditor.toggleModifier(ModeModifier.FORCE_PLACE) - - ClientEditor.onModifiersUpdated() } } diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoClipModifierShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoClipModifierShortcut.kt index c712ea6..40a0940 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoClipModifierShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoClipModifierShortcut.kt @@ -4,12 +4,10 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.ModeModifier +import net.moonleay.gimble.editor.state.mode.ModeModifier class ToggleNoClipModifierShortcut(key: KeyBinding): GimbleShortcut(key) { override fun onPressed(client: MinecraftClient) { ClientEditor.toggleModifier(ModeModifier.NO_CLIP) - - ClientEditor.onModifiersUpdated() } } diff --git a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoUpdatesModifierShortcut.kt b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoUpdatesModifierShortcut.kt index 0dc54f0..5380fdf 100644 --- a/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoUpdatesModifierShortcut.kt +++ b/src/main/java/net/moonleay/gimble/client/keybindings/impl/editormodemodifier/ToggleNoUpdatesModifierShortcut.kt @@ -4,12 +4,10 @@ import net.minecraft.client.MinecraftClient import net.minecraft.client.option.KeyBinding import net.moonleay.gimble.client.editor.ClientEditor import net.moonleay.gimble.client.keybindings.impl.GimbleShortcut -import net.moonleay.gimble.client.editor.modes.ModeModifier +import net.moonleay.gimble.editor.state.mode.ModeModifier class ToggleNoUpdatesModifierShortcut(key: KeyBinding): GimbleShortcut(key) { override fun onPressed(client: MinecraftClient) { ClientEditor.toggleModifier(ModeModifier.NO_UPDATES) - - ClientEditor.onModifiersUpdated() } } diff --git a/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt b/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt new file mode 100644 index 0000000..dc66198 --- /dev/null +++ b/src/main/java/net/moonleay/gimble/editor/ServerEditorManager.kt @@ -0,0 +1,28 @@ +package net.moonleay.gimble.editor + +import net.moonleay.gimble.build.BuildConstants +import net.moonleay.gimble.editor.state.EditorState +import net.moonleay.gimble.editor.state.mode.ModeModifier +import org.apache.logging.log4j.LogManager +import java.util.UUID + +object ServerEditorManager { + private val LOGGER = LogManager.getLogger(BuildConstants.modName) + + val STATEMAP = mutableMapOf() + + fun updateEditorState(playerUUID: UUID, editorState: EditorState) { + STATEMAP[playerUUID] = editorState + LOGGER.info("$playerUUID: ${editorState.editorMode} with ${editorState.editorModifier}") + } + + fun getEditorState(playerUUID: UUID): EditorState? { + return STATEMAP[playerUUID] + } + + fun playerHasModifier(playerUUID: UUID, modifier: ModeModifier): Boolean { + if (!STATEMAP.containsKey(playerUUID)) + return false + return STATEMAP[playerUUID]!!.editorModifier.contains(modifier) ?: false + } +} diff --git a/src/main/java/net/moonleay/gimble/editor/state/EditorState.kt b/src/main/java/net/moonleay/gimble/editor/state/EditorState.kt new file mode 100644 index 0000000..57e0d5f --- /dev/null +++ b/src/main/java/net/moonleay/gimble/editor/state/EditorState.kt @@ -0,0 +1,11 @@ +package net.moonleay.gimble.editor.state + +import kotlinx.serialization.Serializable +import net.moonleay.gimble.editor.state.mode.Mode +import net.moonleay.gimble.editor.state.mode.ModeModifier + +@Serializable +data class EditorState( + var editorMode: Mode, + var editorModifier: MutableList, +) diff --git a/src/main/java/net/moonleay/gimble/client/editor/modes/Mode.kt b/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt similarity index 77% rename from src/main/java/net/moonleay/gimble/client/editor/modes/Mode.kt rename to src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt index e0d1185..95d7961 100644 --- a/src/main/java/net/moonleay/gimble/client/editor/modes/Mode.kt +++ b/src/main/java/net/moonleay/gimble/editor/state/mode/Mode.kt @@ -1,6 +1,7 @@ -package net.moonleay.gimble.client.editor.modes +package net.moonleay.gimble.editor.state.mode enum class Mode(val displayName: String, val color: Int, val incompatibleModifiers: List){ + UNKNOWN("UNKNOWN", 0x000000, listOf()), // Unknown mode. This mode cannot be entered NORMAL("NORMAL", 0x90a959, listOf(ModeModifier.NO_UPDATES, ModeModifier.BULLDOZER, ModeModifier.FORCE_PLACE)), // Do nothing INSERT("INSERT", 0xf4bf75, listOf()), // Place and break blocks REPLACE("REPLACE", 0xac4242, listOf()), // Replace blocks diff --git a/src/main/java/net/moonleay/gimble/client/editor/modes/ModeModifier.kt b/src/main/java/net/moonleay/gimble/editor/state/mode/ModeModifier.kt similarity index 87% rename from src/main/java/net/moonleay/gimble/client/editor/modes/ModeModifier.kt rename to src/main/java/net/moonleay/gimble/editor/state/mode/ModeModifier.kt index 855b161..f7bc6bb 100644 --- a/src/main/java/net/moonleay/gimble/client/editor/modes/ModeModifier.kt +++ b/src/main/java/net/moonleay/gimble/editor/state/mode/ModeModifier.kt @@ -1,4 +1,4 @@ -package net.moonleay.gimble.client.editor.modes +package net.moonleay.gimble.editor.state.mode enum class ModeModifier(val displayName: String) { // NONE("None"), // No Modifiers - default behavior diff --git a/src/main/java/net/moonleay/gimble/mixin/HudMixin.java b/src/main/java/net/moonleay/gimble/mixin/HudMixin.java index 52c3dfc..eb3a060 100644 --- a/src/main/java/net/moonleay/gimble/mixin/HudMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/HudMixin.java @@ -6,8 +6,8 @@ import net.minecraft.client.gui.hud.InGameHud; import net.minecraft.client.util.math.MatrixStack; import net.minecraft.text.Text; import net.moonleay.gimble.client.editor.ClientEditor; -import net.moonleay.gimble.client.editor.modes.Mode; -import net.moonleay.gimble.client.editor.modes.ModeModifier; +import net.moonleay.gimble.editor.state.mode.Mode; +import net.moonleay.gimble.editor.state.mode.ModeModifier; import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFix.java b/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFix.java index f164836..080acfb 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFix.java +++ b/src/main/java/net/moonleay/gimble/mixin/NoClipCameraFix.java @@ -2,7 +2,7 @@ package net.moonleay.gimble.mixin; import net.minecraft.client.render.Camera; import net.moonleay.gimble.client.editor.ClientEditor; -import net.moonleay.gimble.client.editor.modes.ModeModifier; +import net.moonleay.gimble.editor.state.mode.ModeModifier; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; diff --git a/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java b/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java index dcb3ff1..b69b502 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NoClipMixin.java @@ -1,32 +1,59 @@ package net.moonleay.gimble.mixin; -import net.minecraft.entity.Entity; +import com.mojang.authlib.GameProfile; +import net.minecraft.entity.EntityPose; +import net.minecraft.entity.EntityType; +import net.minecraft.entity.LivingEntity; +import net.minecraft.entity.player.PlayerAbilities; import net.minecraft.entity.player.PlayerEntity; -import net.moonleay.gimble.client.editor.ClientEditor; -import net.moonleay.gimble.client.editor.modes.ModeModifier; +import net.minecraft.world.World; +import net.moonleay.gimble.editor.ServerEditorManager; +import net.moonleay.gimble.editor.state.mode.ModeModifier; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -@Mixin(Entity.class) -public class NoClipMixin { +import java.util.UUID; - @Shadow public boolean noClip; +@Mixin(PlayerEntity.class) +public abstract class NoClipMixin extends LivingEntity { + @Shadow public abstract GameProfile getGameProfile(); - @Inject(method = "tick", at = @At(value = "HEAD")) - private void enoClip(CallbackInfo ci) { - // TODO: Add player check, add multiplayer compat - if(!((Entity)(Object)this instanceof PlayerEntity)) // Only check on player + @Shadow public abstract PlayerAbilities getAbilities(); + + protected NoClipMixin(EntityType entityType, World world) { + super(entityType, world); + } // Server side + + + @Inject(method = "tick", at = @At(value = "FIELD", + target = "Lnet/minecraft/entity/player/PlayerEntity;noClip:Z", shift = At.Shift.AFTER) + ) private void enoClip(CallbackInfo ci) { + UUID uuid = this.getGameProfile().getId(); + + if (!ServerEditorManager.INSTANCE.playerHasModifier(uuid, ModeModifier.NO_CLIP)) { + return; // NoClip is not enabled + } + if (!this.getAbilities().flying) { return; - - if (!ClientEditor.INSTANCE.containsModifier(ModeModifier.NO_CLIP)) - return; - PlayerEntity thePlayer = (PlayerEntity)(Object)this; - if (!thePlayer.getAbilities().flying) - return; - + } + // Enable NoClip this.noClip = true; } + + @Inject(method = "updatePose", at = @At("HEAD")) + private void onUpdatePose(CallbackInfo ci) { + UUID uuid = this.getGameProfile().getId(); + + if (!ServerEditorManager.INSTANCE.playerHasModifier(uuid, ModeModifier.NO_CLIP)) + return; // NoClip is not enabled + if (!this.getAbilities().flying) + return; + + // Force standing pose in NoClip mode + + this.setPose(EntityPose.STANDING); + } } diff --git a/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java b/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java index 9cdc340..a59a026 100644 --- a/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/NormalModeMixin.java @@ -2,7 +2,7 @@ package net.moonleay.gimble.mixin; import net.minecraft.client.MinecraftClient; import net.minecraft.client.option.GameOptions; -import net.moonleay.gimble.client.editor.modes.Mode; +import net.moonleay.gimble.editor.state.mode.Mode; import net.moonleay.gimble.client.editor.ClientEditor; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java b/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java index 7e4d671..f75e219 100644 --- a/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java +++ b/src/main/java/net/moonleay/gimble/mixin/ReplaceModeMixin.java @@ -12,7 +12,7 @@ import net.minecraft.util.hit.HitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.Direction; import net.moonleay.gimble.client.editor.ClientEditor; -import net.moonleay.gimble.client.editor.modes.Mode; +import net.moonleay.gimble.editor.state.mode.Mode; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; diff --git a/src/main/java/net/moonleay/gimble/networking/GimbleClient.kt b/src/main/java/net/moonleay/gimble/networking/GimbleClient.kt new file mode 100644 index 0000000..407b1cc --- /dev/null +++ b/src/main/java/net/moonleay/gimble/networking/GimbleClient.kt @@ -0,0 +1,21 @@ +package net.moonleay.gimble.networking + +import kotlinx.serialization.ExperimentalSerializationApi +import kotlinx.serialization.cbor.Cbor +import kotlinx.serialization.encodeToByteArray +import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking +import net.fabricmc.fabric.api.networking.v1.PacketByteBufs +import net.moonleay.gimble.editor.state.EditorState + +object GimbleClient { + + /** + * 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) + } +} diff --git a/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt b/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt new file mode 100644 index 0000000..99bb164 --- /dev/null +++ b/src/main/java/net/moonleay/gimble/networking/GimbleServer.kt @@ -0,0 +1,27 @@ +package net.moonleay.gimble.networking + +import kotlinx.serialization.cbor.Cbor +import kotlinx.serialization.decodeFromByteArray +import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking +import net.minecraft.network.PacketByteBuf +import net.minecraft.server.network.ServerPlayerEntity +import net.minecraft.text.Text +import net.moonleay.gimble.editor.ServerEditorManager +import net.moonleay.gimble.editor.state.EditorState + +object GimbleServer { + + fun registerPacketHandler() { + ServerPlayNetworking + .registerGlobalReceiver(PacketIDs.UPDATE_EDITOR_STATE_ID) + { server, player, handler, buf, responseSender -> + handleStateUpdate(player, buf) + } + } + + private fun handleStateUpdate(player: ServerPlayerEntity, buf: PacketByteBuf){ + val state = Cbor.decodeFromByteArray(buf.readByteArray()) + ServerEditorManager.updateEditorState(player.uuid, state) + player.sendMessage(Text.of("Updated State (Server) ${state.editorMode} with ${state.editorModifier}")) + } +} diff --git a/src/main/java/net/moonleay/gimble/networking/PacketIDs.kt b/src/main/java/net/moonleay/gimble/networking/PacketIDs.kt new file mode 100644 index 0000000..b50148a --- /dev/null +++ b/src/main/java/net/moonleay/gimble/networking/PacketIDs.kt @@ -0,0 +1,8 @@ +package net.moonleay.gimble.networking + +import net.minecraft.util.Identifier +import net.moonleay.gimble.build.BuildConstants + +object PacketIDs { + val UPDATE_EDITOR_STATE_ID = Identifier(BuildConstants.modId, "update_editor_state") +}