feat: config & config push

This commit is contained in:
xtex 2023-07-27 10:18:55 +08:00
parent 2089a966e4
commit e70697fa6a
Signed by: xtex
GPG Key ID: B918086ED8045B91
9 changed files with 136 additions and 7 deletions

View File

@ -0,0 +1,20 @@
package quaedam.mixin;
import net.minecraft.network.Connection;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.players.PlayerList;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import quaedam.config.SimpleQuaedamConfigPush;
@Mixin(PlayerList.class)
public class MixinPlayerList {
@Inject(at = @At("RETURN"), method = "placeNewPlayer(Lnet/minecraft/network/Connection;Lnet/minecraft/server/level/ServerPlayer;)V")
public void placeNewPlayer(Connection netManager, ServerPlayer player, CallbackInfo ci) {
SimpleQuaedamConfigPush.INSTANCE.sendCurrent(player);
}
}

View File

@ -9,6 +9,7 @@ import net.minecraft.resources.ResourceLocation
import net.minecraft.world.item.CreativeModeTab
import net.minecraft.world.item.ItemStack
import org.slf4j.LoggerFactory
import quaedam.config.QuaedamConfig
import quaedam.projection.ProjectionCommand
import quaedam.projection.ProjectionEffectType
import quaedam.projection.SimpleProjectionUpdate
@ -43,6 +44,7 @@ object Quaedam {
}
fun init() {
QuaedamConfig
Projector
ProjectionEffectType
SkylightProjection

View File

@ -0,0 +1,70 @@
package quaedam.config
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import dev.architectury.event.events.client.ClientPlayerEvent
import dev.architectury.platform.Platform
import dev.architectury.utils.GameInstance
import net.minecraft.nbt.CompoundTag
import quaedam.Quaedam
import java.nio.file.Path
import kotlin.io.path.*
data class QuaedamConfig(
val projectorEffectRadius: Int = 4
) {
companion object {
const val TAG_PROJECTOR_EFFECT_RADIUS = "ProjectorEffectRadius"
private val localFile: Path = Platform.getConfigFolder().resolve("quaedam.json")
private var local0 = loadLocalConfig()
var local
get() = local0
set(value) {
local0 = value
writeLocalConfig()
}
var remote: QuaedamConfig? = null
val current get() = remote ?: local0
init {
SimpleQuaedamConfigPush
ClientPlayerEvent.CLIENT_PLAYER_QUIT.register { player ->
if (player == GameInstance.getClient().player) {
applyRemoteConfig(null)
}
}
if (localFile.notExists()) {
writeLocalConfig()
}
}
private fun loadLocalConfig(): QuaedamConfig = if (localFile.exists()) {
Gson().fromJson(localFile.readText(), QuaedamConfig::class.java)
} else {
QuaedamConfig()
}
private fun writeLocalConfig() {
localFile.writeText(GsonBuilder().serializeNulls().setPrettyPrinting().create().toJson(local0))
}
fun applyRemoteConfig(config: QuaedamConfig?) {
Quaedam.logger.info("Received remote config push: $config")
remote = config
}
fun fromPushNbt(tag: CompoundTag) = QuaedamConfig(
projectorEffectRadius = tag.getInt(TAG_PROJECTOR_EFFECT_RADIUS)
)
}
fun toPushNbt(tag: CompoundTag) {
tag.putInt(TAG_PROJECTOR_EFFECT_RADIUS, projectorEffectRadius)
}
fun toPushNbt(forPush: Boolean) = CompoundTag().also { toPushNbt(it) }
}

View File

@ -0,0 +1,34 @@
package quaedam.config
import dev.architectury.networking.NetworkManager
import io.netty.buffer.Unpooled
import net.minecraft.nbt.CompoundTag
import net.minecraft.network.FriendlyByteBuf
import net.minecraft.server.level.ServerPlayer
import quaedam.Quaedam
object SimpleQuaedamConfigPush {
val id = Quaedam.resource("simple_config_push")
init {
NetworkManager.registerReceiver(NetworkManager.Side.S2C, id, ::handle)
}
private fun handle(buf: FriendlyByteBuf, ctx: NetworkManager.PacketContext) {
val data = buf.readNbt()!!
val config = QuaedamConfig.fromPushNbt(data)
QuaedamConfig.applyRemoteConfig(config)
}
fun sendCurrent(player: ServerPlayer) = send(player, QuaedamConfig.current)
fun send(player: ServerPlayer, config: QuaedamConfig) = send(player, config.toPushNbt(forPush = true))
private fun send(player: ServerPlayer, data: CompoundTag) {
val buf = FriendlyByteBuf(Unpooled.buffer())
buf.writeNbt(data)
NetworkManager.sendToPlayer(player, id, buf)
}
}

View File

@ -19,7 +19,7 @@ abstract class ProjectionEffect : Cloneable {
abstract fun fromNbt(tag: CompoundTag, trusted: Boolean = true)
fun toNbt() = CompoundTag().apply { toNbt(this) }
fun toNbt() = CompoundTag().also { toNbt(it) }
override fun equals(other: Any?): Boolean = other === this

View File

@ -6,6 +6,7 @@ import net.minecraft.world.item.Item
import net.minecraft.world.level.Level
import net.minecraft.world.level.block.entity.BlockEntityType
import quaedam.Quaedam
import quaedam.config.QuaedamConfig
import quaedam.projection.ProjectionEffect
import quaedam.projection.ProjectionEffectType
import quaedam.utils.getChunksNearby
@ -13,7 +14,6 @@ import quaedam.utils.getChunksNearby
object Projector {
const val ID = "projector"
const val EFFECT_RADIUS = 4
val block = Quaedam.blocks.register(ID) { ProjectorBlock }!!
@ -29,7 +29,9 @@ object Projector {
BlockEntityType.Builder.of(::ProjectorBlockEntity, block.get()).build(null)
}!!
fun findNearbyProjectors(level: Level, pos: BlockPos) = level.getChunksNearby(pos, EFFECT_RADIUS)
val currentEffectRadius get() = QuaedamConfig.current.projectorEffectRadius
fun findNearbyProjectors(level: Level, pos: BlockPos) = level.getChunksNearby(pos, currentEffectRadius)
.flatMap {
it.blockEntities.filter { (_, v) -> v is ProjectorBlockEntity }
.keys

View File

@ -27,8 +27,8 @@ class ProjectorBlockEntity(pos: BlockPos, state: BlockState) :
val effectAreaChunk by lazy {
val chunk = level!!.getChunk(pos).pos
ChunkPos(chunk.x - Projector.EFFECT_RADIUS, chunk.z - Projector.EFFECT_RADIUS) to
ChunkPos(chunk.x + Projector.EFFECT_RADIUS, chunk.z + Projector.EFFECT_RADIUS)
ChunkPos(chunk.x - Projector.currentEffectRadius, chunk.z - Projector.currentEffectRadius) to
ChunkPos(chunk.x + Projector.currentEffectRadius, chunk.z + Projector.currentEffectRadius)
}
val effectArea: BoundingBox by lazy {

View File

@ -13,7 +13,7 @@ object ProjectionShell {
val item = Quaedam.items.register(ID) { ProjectionShellItem }!!
val channel = NetworkChannel.create(Quaedam.resource(ID))
val channel: NetworkChannel = NetworkChannel.create(Quaedam.resource(ID))
init {
ServerboundPSHLockAcquirePacket

View File

@ -9,7 +9,8 @@
"mixins": [
"MixinBedBlock",
"MixinBuiltInRegistries",
"MixinMinecraftServer"
"MixinMinecraftServer",
"MixinPlayerList"
],
"injectors": {
"defaultRequire": 1