Skip to content

Commit

Permalink
sync server config
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Aug 18, 2022
1 parent 53c8c6a commit e7bce40
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
Expand All @@ -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<ItemStack> ->
attachCapabilities(event.`object`, event::addCapability)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,7 +27,7 @@ object Configs {

init {
with(ForgeConfigSpec.Builder().configure { ServerConfig(it) }) {
SERVER = left
LOCAL_SERVER = left
SERVER_SPEC = right
}

Expand All @@ -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
)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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()!!

}
Original file line number Diff line number Diff line change
@@ -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<Context>) {
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
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry<CopperBacktankB
private val capability = LazyOptional.of<IJetpack> { 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 {
Expand All @@ -36,15 +36,15 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry<CopperBacktankB
}

override fun horizontalSpeed(context: Context): Double {
return Configs.SERVER.HORIZONTAL_SPEED.get()
return Configs.SERVER.horizontalSpeed
}

override fun acceleration(context: Context): Double {
return Configs.SERVER.ACCELERATION.get()
return Configs.SERVER.acceleration
}

override fun swimModifier(context: Context): Double {
return Configs.SERVER.SWIM_MODIFIER.get()
return Configs.SERVER.swimModifier
}

private val thrusters = listOf(-0.35, 0.35).map { offset ->
Expand All @@ -59,8 +59,8 @@ class BronzeJetpack(properties: Properties, blockItem: ItemEntry<CopperBacktankB
}

private fun usesPerTank(context: Context): Int {
return if (isHovering(context)) Configs.SERVER.USES_PER_TANK_HOVER.get()
else Configs.SERVER.USES_PER_TANK.get()
return if (isHovering(context)) Configs.SERVER.usesPerTankHover
else Configs.SERVER.usesPerTank
}

override fun isValid(context: Context): Boolean {
Expand Down

0 comments on commit e7bce40

Please sign in to comment.