Skip to content

Commit

Permalink
refactor: GearyContexts, reworked Addon system etc (#30)
Browse files Browse the repository at this point in the history
* feat: Add support for generating typescript definitions from all registered prefabs

* refactor: Swap to using proper dependency injection throughout in anticipation of multi-world ticking support

* chore: Bump geary

* chore: Bump geary

* chore: Update .gitignore

* chore: Remove print statement

* feat: allow baseMaterial and itemPredicates.customModelData to inherit from set.item component

* chore: Continue work upating geary for per world ticking

* refactor: Continue migrating to geary di update

* chore: Get compiling

* fix: Move features to load after geary starts, add GearyWorldLoadEvent in preparation for future refactor

* fix(commands): Use namespaced keys for prefab arguments instead of strings

* chore: Bump ido commands

* chore: Bump deps

* fix: Get building after merge

* feat: Add helpers for getting geary world off Bukkit classes

* chore: Bump geary with prefab loading changes

* refactor: pass initial argument in withGeary block

* chore: Bump geary

* fix: BlockTracking tracking after all entities have been set

* chore: bump idofront

---------

Co-authored-by: Danielle Voznyy <[email protected]>
  • Loading branch information
Boy0000 and 0ffz authored Oct 30, 2024
1 parent e2fffea commit 9886885
Show file tree
Hide file tree
Showing 105 changed files with 1,267 additions and 683 deletions.
1 change: 1 addition & 0 deletions .gitignore
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ eclipse

logs
/truckconfig.json
.kotlin
Empty file modified LICENSE
100755 → 100644
Empty file.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ allprojects {
freeCompilerArgs.addAll(
"-opt-in=kotlinx.serialization.ExperimentalSerializationApi",
"-opt-in=kotlin.ExperimentalUnsignedTypes",
"-Xcontext-receivers"
)
}
}
Expand Down
1 change: 1 addition & 0 deletions geary-papermc-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ plugins {
dependencies {
api(libs.geary.core)
api(libs.geary.serialization)
api(libs.geary.prefabs)
compileOnly(libs.geary.actions)

// MineInAbyss platform
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.idofront.plugin.listeners
import kotlinx.coroutines.Job
import org.bukkit.Bukkit
Expand All @@ -9,7 +10,7 @@ import org.bukkit.event.Listener
abstract class Feature(context: FeatureContext) {
open val name = this::class.simpleName
val plugin = context.plugin
val logger = context.logger
open val logger = context.logger
val listeners = mutableListOf<Listener>()
val tasks = mutableListOf<Job>()
private var pluginDeps = listOf<String>()
Expand Down Expand Up @@ -39,6 +40,7 @@ abstract class Feature(context: FeatureContext) {
}
return canLoad()
}

fun defaultCanEnable(): Boolean {
val unmet = pluginDeps.filter { !plugin.server.pluginManager.isPluginEnabled(it) }
if (unmet.isNotEmpty()) {
Expand Down Expand Up @@ -89,4 +91,6 @@ abstract class Feature(context: FeatureContext) {
fun task(task: Job) {
tasks.add(task)
}

fun subFeatures(vararg features: FeatureBuilder) = Features(plugin, *features)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.idofront.messaging.ComponentLogger
import org.bukkit.plugin.Plugin

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.idofront.messaging.error
import com.mineinabyss.idofront.messaging.injectedLogger
import com.mineinabyss.idofront.messaging.success
Expand Down Expand Up @@ -28,7 +29,7 @@ class Features(
isFirstEnable = false
}

fun load(builder: FeatureBuilder): Result<Feature> {
fun load(builder: FeatureBuilder): Result<Feature> = runCatching {
val feature = builder(context)
featuresByClass[feature::class] = builder
if (!feature.canLoad()) return Result.failure(IllegalStateException("Feature ${feature.name} could not be loaded"))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.addons.dsl.AddonSetup
import com.mineinabyss.geary.modules.Geary

class GearyPaper(
val plugin: GearyPlugin,
world: Geary,
) : Geary by world

inline fun AddonSetup<*>.onPluginEnable(crossinline run: GearyPaper.() -> Unit) {
onStart {
GearyPaper(application.koin.get<GearyPlugin>(), this).run()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.mineinabyss.geary.papermc
import ca.spottedleaf.moonrise.common.util.TickThread
import co.touchlab.kermit.Severity
import com.charleskorn.kaml.YamlComment
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.geary
import kotlinx.serialization.Serializable
import org.bukkit.entity.EntityType
Expand Down Expand Up @@ -42,7 +43,7 @@ class Catching(
when (type) {
CatchType.ERROR -> AsyncCatcher.catchOp(message)
CatchType.WARN -> if (!TickThread.isTickThread()) {
geary.logger.w(message)
Geary.w(message)
IllegalStateException("(Ignoring) $message").printStackTrace()
}
CatchType.IGNORE -> Unit
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.GearySetup
import com.mineinabyss.geary.modules.UninitializedGearyModule
import com.mineinabyss.idofront.config.IdofrontConfig
import com.mineinabyss.idofront.di.DI
import com.mineinabyss.idofront.messaging.ComponentLogger
Expand All @@ -13,4 +15,8 @@ interface GearyPaperModule {
val config: GearyPaperConfig
val logger: ComponentLogger
val features: Features
val gearyModule: UninitializedGearyModule
val worldManager: WorldManager
}

inline fun GearyPaperModule.configure(configure: GearySetup.() -> Unit) = gearyModule.configure(configure)
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.prefabs.PrefabKey
import com.mineinabyss.geary.prefabs.entityOfOrNull
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.block.Block
import org.bukkit.block.TileState

context(Geary)
fun PrefabKey.toEntityOrNull() = entityOfOrNull(this)

inline fun <T, R : BukkitEntity> R.withGeary(run: Geary.(R) -> T) = with(world.toGeary()) { run(this@withGeary) }

inline fun <T, R : TileState> R.withGeary(run: Geary.(R) -> T) = with(world.toGeary()) { run(this@withGeary) }

inline fun <T, R : Block> R.withGeary(run: Geary.(R) -> T) = with(world.toGeary()) { run(this@withGeary) }
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mineinabyss.geary.papermc

import com.charleskorn.kaml.*
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer
import com.mineinabyss.geary.serialization.serializers.PolymorphicListAsMapSerializer.Companion.provideConfig
Expand Down Expand Up @@ -36,12 +37,12 @@ class MultiEntryYamlReader<T>(
nodes[nameStr] = decoded
}
.onSuccess {
geary.logger.d { "Read entry $nameStr entry from $path" }
Geary.d { "Read entry $nameStr entry from $path" }
}
.onFailure {
geary.logger.w { "Failed to read entry $nameStr entry from $path" }
geary.logger.w { it.localizedMessage }
geary.logger.d { it.stackTraceToString() }
Geary.w { "Failed to read entry $nameStr entry from $path" }
Geary.w { it.localizedMessage }
Geary.d { it.stackTraceToString() }
}
}

Expand Down Expand Up @@ -107,7 +108,6 @@ class MultiEntryYamlReader<T>(

// If original is not a list, but we ask to inherit, remove these tags from the override
override is YamlList -> {
println("Overriding! $override")
YamlList(override.items.filter { (it as? YamlScalar)?.content?.contains(specialMergeTags) != true }, override.path)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mineinabyss.geary.papermc

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.idofront.typealiases.BukkitEntity
import org.bukkit.World

// TODO per world engine support in the future
class WorldManager {
private var _globalEngine: Geary? = null

fun getGearyWorld(world: World): Geary? = _globalEngine

fun setGlobalEngine(engine: Geary) {
_globalEngine = engine
}

val global get() = _globalEngine ?: error("No global Geary engine set")
}

fun World.toGeary() = gearyPaper.worldManager.getGearyWorld(this) ?: error("No Geary engine found for world $name")
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.mineinabyss.geary.papermc.events

import com.mineinabyss.geary.addons.dsl.createAddon
import com.mineinabyss.geary.modules.GearySetup
import org.bukkit.event.Event
import org.bukkit.event.HandlerList

data class GearyWorldLoadEvent(
val setup: GearySetup,
) : Event() {
fun configure(run: GearySetup.() -> Unit) {
run.invoke(setup)
}

companion object {
@JvmStatic
private val HANDLER_LIST = HandlerList()

@JvmStatic
fun getHandlerList() = HANDLER_LIST
}

override fun getHandlers() = HANDLER_LIST

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package com.mineinabyss.geary.papermc.datastore
import com.mineinabyss.geary.components.relations.InstanceOf
import com.mineinabyss.geary.datatypes.GearyEntity
import com.mineinabyss.geary.helpers.component
import com.mineinabyss.geary.helpers.toGeary
import com.mineinabyss.geary.modules.geary
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.serialization.components.Persists
import com.mineinabyss.geary.serialization.getAllPersisting
import com.mineinabyss.geary.serialization.setAllPersisting
Expand All @@ -14,7 +13,7 @@ import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.persistence.PersistentDataHolder

/** Encodes this entity's persisting components into a [PersistentDataContainer] */
fun GearyEntity.encodeComponentsTo(pdc: PersistentDataContainer) {
fun GearyEntity.encodeComponentsTo(pdc: PersistentDataContainer) = with(world) {
val persisting = getAllPersisting()
if (persisting.isEmpty() && getRelations<InstanceOf?, Any?>().isEmpty()) {
pdc.hasComponentsEncoded = false
Expand All @@ -27,20 +26,22 @@ fun GearyEntity.encodeComponentsTo(pdc: PersistentDataContainer) {
pdc.encodeComponents(persisting, type)
}

context(Geary)
fun GearyEntity.encodeComponentsTo(holder: PersistentDataHolder) {
val bukkitHolder = holder as? BukkitEntity
geary.logger.v { "Encoding components for bukkit entity $id (${bukkitHolder?.type} ${bukkitHolder?.uniqueId})" }
world.logger.v { "Encoding components for bukkit entity $id (${bukkitHolder?.type} ${bukkitHolder?.uniqueId})" }
encodeComponentsTo(holder.persistentDataContainer)
}

context(Geary)
fun GearyEntity.encodeComponentsTo(item: ItemStack) {
item.editMeta { encodeComponentsTo(it.persistentDataContainer) }
}


/** Decodes a [PersistentDataContainer]'s components, adding them to this entity and its list of persisting components */
fun GearyEntity.loadComponentsFrom(pdc: PersistentDataContainer) {
loadComponentsFrom(pdc.decodeComponents())
loadComponentsFrom(with(world) { pdc.decodeComponents() })
}

fun GearyEntity.loadComponentsFrom(decodedEntityData: DecodedEntityData) {
Expand All @@ -49,11 +50,15 @@ fun GearyEntity.loadComponentsFrom(decodedEntityData: DecodedEntityData) {
// Components written to this entity's PDC will override the ones defined in type
setAllPersisting(components)
//TODO this should just add the id and a listener handle what addPrefab currently does
type.forEach { extend(it.toGeary()) }
with(world) {
type.forEach { extend(it.toGeary()) }
}
}

context(Geary)
fun PersistentDataHolder.decodeComponents(): DecodedEntityData =
persistentDataContainer.decodeComponents()

context(Geary)
fun ItemStack.decodeComponents(): DecodedEntityData =
itemMeta.decodeComponents()
Loading

0 comments on commit 9886885

Please sign in to comment.