From e7bce40a56b875a297ad955f40f958ea85aebc31 Mon Sep 17 00:00:00 2001 From: PssbleTrngle Date: Thu, 18 Aug 2022 21:39:50 +0200 Subject: [PATCH] sync server config --- .../create_jetpack/CreateJetpackMod.java | 3 ++ .../create_jetpack/Content.kt | 3 ++ .../create_jetpack/capability/JetpackLogic.kt | 2 - .../create_jetpack/config/Configs.kt | 44 ++++++++++++++-- .../create_jetpack/config/ServerConfig.kt | 49 +++++++++++++++--- .../config/SyncConfigMessage.kt | 51 +++++++++++++++++++ .../create_jetpack/item/BronzeJetpack.kt | 14 ++--- 7 files changed, 146 insertions(+), 20 deletions(-) create mode 100644 src/main/kotlin/com/possible_triangle/create_jetpack/config/SyncConfigMessage.kt diff --git a/src/main/java/com/possible_triangle/create_jetpack/CreateJetpackMod.java b/src/main/java/com/possible_triangle/create_jetpack/CreateJetpackMod.java index 00bad50..59716c9 100644 --- a/src/main/java/com/possible_triangle/create_jetpack/CreateJetpackMod.java +++ b/src/main/java/com/possible_triangle/create_jetpack/CreateJetpackMod.java @@ -2,12 +2,15 @@ import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; @Mod.EventBusSubscriber @Mod(CreateJetpackMod.MOD_ID) public class CreateJetpackMod { public static final String MOD_ID = "create_jetpack"; + public static final Logger LOGGER = LogManager.getLogger(); public CreateJetpackMod() { var eventBus = FMLJavaModLoadingContext.get().getModEventBus(); diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/Content.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/Content.kt index 08b62f6..64a3005 100644 --- a/src/main/kotlin/com/possible_triangle/create_jetpack/Content.kt +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/Content.kt @@ -131,6 +131,8 @@ object Content { LOADING_CONTEXT.registerConfig(ModConfig.Type.COMMON, Configs.SERVER_SPEC) LOADING_CONTEXT.registerConfig(ModConfig.Type.CLIENT, Configs.CLIENT_SPEC) + Configs.Network.register() + modBus.addListener { _: FMLCommonSetupEvent -> ModNetwork.init() } @@ -149,6 +151,7 @@ object Content { FORGE_BUS.addListener(ControlManager::onLogout) FORGE_BUS.addListener(JetpackLogic::tick) + FORGE_BUS.addListener(Configs::syncConfig) FORGE_BUS.addGenericListener(ItemStack::class.java) { event: AttachCapabilitiesEvent -> attachCapabilities(event.`object`, event::addCapability) } diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/capability/JetpackLogic.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/capability/JetpackLogic.kt index bd9970f..e448e70 100644 --- a/src/main/kotlin/com/possible_triangle/create_jetpack/capability/JetpackLogic.kt +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/capability/JetpackLogic.kt @@ -21,12 +21,10 @@ import net.minecraft.world.phys.Vec3 import net.minecraftforge.common.ForgeMod import net.minecraftforge.common.capabilities.ICapabilityProvider import net.minecraftforge.event.TickEvent -import net.minecraftforge.fml.common.Mod import java.util.* import kotlin.math.max import kotlin.math.min -@Mod.EventBusSubscriber object JetpackLogic { private val DIRECTIONS = listOf( diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/config/Configs.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/config/Configs.kt index 412640d..bd42823 100644 --- a/src/main/kotlin/com/possible_triangle/create_jetpack/config/Configs.kt +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/config/Configs.kt @@ -1,13 +1,24 @@ package com.possible_triangle.create_jetpack.config +import com.possible_triangle.create_jetpack.CreateJetpackMod +import com.possible_triangle.create_jetpack.CreateJetpackMod.MOD_ID +import net.minecraft.resources.ResourceLocation +import net.minecraft.server.level.ServerPlayer import net.minecraftforge.common.ForgeConfigSpec +import net.minecraftforge.event.entity.player.PlayerEvent +import net.minecraftforge.network.NetworkRegistry +import net.minecraftforge.network.PacketDistributor object Configs { var SERVER_SPEC: ForgeConfigSpec private set - var SERVER: ServerConfig - private set + + private var LOCAL_SERVER: ServerConfig + internal var SYNCED_SERVER: IServerConfig? = null + + val SERVER: IServerConfig + get() = SYNCED_SERVER ?: LOCAL_SERVER var CLIENT_SPEC: ForgeConfigSpec private set @@ -16,7 +27,7 @@ object Configs { init { with(ForgeConfigSpec.Builder().configure { ServerConfig(it) }) { - SERVER = left + LOCAL_SERVER = left SERVER_SPEC = right } @@ -26,4 +37,31 @@ object Configs { } } + fun syncConfig(event: PlayerEvent.PlayerLoggedInEvent) { + val player = event.player + if (player !is ServerPlayer) return + CreateJetpackMod.LOGGER.debug("Sending server config to ${player.scoreboardName}") + Network.CHANNEL.send(PacketDistributor.PLAYER.with { player }, SyncConfigMessage(LOCAL_SERVER)) + } + + object Network { + private const val version = "1.0" + internal val CHANNEL = NetworkRegistry.newSimpleChannel( + ResourceLocation(MOD_ID, "configs"), + { version }, + version::equals, + version::equals + ) + + fun register() { + CHANNEL.registerMessage( + 0, + SyncConfigMessage::class.java, + SyncConfigMessage::encode, + SyncConfigMessage::decode, + SyncConfigMessage::handle + ) + } + } + } \ No newline at end of file diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/config/ServerConfig.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/config/ServerConfig.kt index 8e41e6b..aa33787 100644 --- a/src/main/kotlin/com/possible_triangle/create_jetpack/config/ServerConfig.kt +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/config/ServerConfig.kt @@ -2,15 +2,48 @@ package com.possible_triangle.create_jetpack.config import net.minecraftforge.common.ForgeConfigSpec -class ServerConfig(builder: ForgeConfigSpec.Builder) { +interface IServerConfig { + val usesPerTank: Int + val usesPerTankHover: Int + val horizontalSpeed: Double + val verticalSpeed: Double + val acceleration: Double + val hoverSpeed: Double + val swimModifier: Double +} - val USES_PER_TANK = builder.defineInRange("air.uses_per_tank", 2048, 1, Integer.MAX_VALUE) - val USES_PER_TANK_HOVER = builder.defineInRange("air.uses_per_tank_hover", 2048 * 10, 1, Integer.MAX_VALUE) +data class SyncedConfig( + override val usesPerTank: Int, + override val usesPerTankHover: Int, + override val horizontalSpeed: Double, + override val verticalSpeed: Double, + override val acceleration: Double, + override val hoverSpeed: Double, + override val swimModifier: Double, +) : IServerConfig - val HORIZONTAL_SPEED = builder.defineInRange("speed.horizontal", 0.02, 0.01, 100.0) - val VERTICAL_SPEED = builder.defineInRange("speed.vertical", 0.4, 0.01, 100.0) - val ACCELERATION = builder.defineInRange("speed.acceleration", 0.6, 0.01, 100.0) - val HOVER_SPEED = builder.defineInRange("speed.hover_descend", -0.03, -100.0, 0.0) - val SWIM_MODIFIER = builder.defineInRange("speed.swim_modifier", 1.8, 0.0, 100.0) +class ServerConfig(builder: ForgeConfigSpec.Builder) : IServerConfig { + + private val usesPerTankValue = builder.defineInRange("air.uses_per_tank", 2048, 1, Integer.MAX_VALUE) + override val usesPerTank get() = usesPerTankValue.get()!! + + private val usesPerTankHoverValue = + builder.defineInRange("air.uses_per_tank_hover", 2048 * 10, 1, Integer.MAX_VALUE) + override val usesPerTankHover get() = usesPerTankHoverValue.get()!! + + private val horizontalSpeedValue = builder.defineInRange("speed.horizontal", 0.02, 0.01, 100.0) + override val horizontalSpeed get() = horizontalSpeedValue.get()!! + + private val verticalSpeedValue = builder.defineInRange("speed.vertical", 0.4, 0.01, 100.0) + override val verticalSpeed get() = verticalSpeedValue.get()!! + + private val accelerationValue = builder.defineInRange("speed.acceleration", 0.6, 0.01, 100.0) + override val acceleration get() = accelerationValue.get()!! + + private val hoverSpeedValue = builder.defineInRange("speed.hover_descend", -0.03, -100.0, 0.0) + override val hoverSpeed get() = hoverSpeedValue.get()!! + + private val swimModifierValue = builder.defineInRange("speed.swim_modifier", 1.8, 0.0, 100.0) + override val swimModifier get() = swimModifierValue.get()!! } \ No newline at end of file diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/config/SyncConfigMessage.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/config/SyncConfigMessage.kt new file mode 100644 index 0000000..bacdb2c --- /dev/null +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/config/SyncConfigMessage.kt @@ -0,0 +1,51 @@ +package com.possible_triangle.create_jetpack.config + +import com.possible_triangle.create_jetpack.CreateJetpackMod +import net.minecraft.network.FriendlyByteBuf +import net.minecraftforge.network.NetworkDirection +import net.minecraftforge.network.NetworkEvent.Context +import java.util.function.Supplier + +class SyncConfigMessage (private val config: IServerConfig) { + + companion object { + + fun decode(buf: FriendlyByteBuf): SyncConfigMessage { + val config = SyncedConfig( + usesPerTank = buf.readInt(), + usesPerTankHover = buf.readInt(), + horizontalSpeed = buf.readDouble(), + verticalSpeed = buf.readDouble(), + acceleration = buf.readDouble(), + hoverSpeed = buf.readDouble(), + swimModifier = buf.readDouble(), + ) + return SyncConfigMessage(config) + } + } + + fun encode(buf: FriendlyByteBuf) { + buf.writeInt(config.usesPerTank) + buf.writeInt(config.usesPerTankHover) + buf.writeDouble(config.horizontalSpeed) + buf.writeDouble(config.verticalSpeed) + buf.writeDouble(config.acceleration) + buf.writeDouble(config.hoverSpeed) + buf.writeDouble(config.swimModifier) + } + + fun handle(context: Supplier) { + with(context.get()) { + enqueueWork { + if (direction == NetworkDirection.PLAY_TO_CLIENT) { + CreateJetpackMod.LOGGER.debug("Hover speed: ${config.hoverSpeed}") + Configs.SYNCED_SERVER = config + } else { + CreateJetpackMod.LOGGER.debug("Received server config of $direction") + } + } + packetHandled = true + } + } + +} \ No newline at end of file diff --git a/src/main/kotlin/com/possible_triangle/create_jetpack/item/BronzeJetpack.kt b/src/main/kotlin/com/possible_triangle/create_jetpack/item/BronzeJetpack.kt index 45f2f2a..11c0cfd 100644 --- a/src/main/kotlin/com/possible_triangle/create_jetpack/item/BronzeJetpack.kt +++ b/src/main/kotlin/com/possible_triangle/create_jetpack/item/BronzeJetpack.kt @@ -20,11 +20,11 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry { this } override fun hoverSpeed(context: Context): Double { - return Configs.SERVER.HOVER_SPEED.get() + return Configs.SERVER.hoverSpeed } override fun verticalSpeed(context: Context): Double { - return Configs.SERVER.VERTICAL_SPEED.get() + return Configs.SERVER.verticalSpeed } override fun activeType(context: Context): ControlType { @@ -36,15 +36,15 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry @@ -59,8 +59,8 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry