Skip to content

Commit

Permalink
[ref] Make List<AttributeFunction> into Map with identifier
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Oct 3, 2024
1 parent 25a4cf8 commit 443d420
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 107 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
- e.g., looking up `playerex:luck`, or `Luck` should work.
- You can now enable/disable Attribute Functions.
## Changes 🌽

- **[BREAKING]** Changed overall structure of config related class definitions. This will affect your config file considerably.
- **[BREAKING]** Removed `DefaultAttributeFactory`.
- **[BREAKING]** Changed `Map<Identifier, List<AttributeFunction>>` -> `Map<Identifier, Map<Identifier, AttributeFunction>>`
- This existed to avoid an odd situation that does not exist anymore.
- Made some changes to certain logic internally and micro-optimizations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import net.minecraft.util.Identifier
/**
* Used to manage config data, and contains an [AttributeContainerHandler] to build related [EntityTypeData].
*/
class AttributeConfigManager(var data: Data = Data(), val handler: AttributeContainerHandler = AttributeContainerHandler()) {
class AttributeConfigManager(var data: Data = Data(), private val handler: AttributeContainerHandler = AttributeContainerHandler()) {
var updateFlag: Int = 0

var defaults: DefaultAttributesReloadListener.Cache = DefaultAttributesReloadListener.Cache()
Expand All @@ -55,14 +55,14 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
*/
data class Data(
var overrides: Map<Identifier, AttributeOverride> = mapOf(),
var functions: Map<Identifier, List<AttributeFunction>> = mapOf(),
var functions: Map<Identifier, Map<Identifier, AttributeFunction>> = mapOf(),
var entity_types: Map<Identifier, EntityTypeData> = mapOf()
)
{
companion object {
val ENDEC = StructEndecBuilder.of(
Endecs.IDENTIFIER.keyOf(AttributeOverride.ENDEC).fieldOf("overrides") { it.overrides },
Endecs.IDENTIFIER.keyOf(AttributeFunction.ENDEC.listOf()).fieldOf("functions") { it.functions },
Endecs.IDENTIFIER.keyOf(Endecs.IDENTIFIER.keyOf(AttributeFunction.ENDEC)).fieldOf("functions") { it.functions },
Endecs.IDENTIFIER.keyOf(EntityTypeData.ENDEC).fieldOf("entity_types") { it.entity_types },
::Data
)
Expand Down Expand Up @@ -93,7 +93,7 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
get() = this.data.overrides

/** Currently applied [AttributeFunction]'s tied to the parent [EntityAttribute]'s [Identifier]. */
val functions: Map<Identifier, List<AttributeFunction>>
val functions: Map<Identifier, Map<Identifier, AttributeFunction>>
get() = this.data.functions

/** Currently applied [EntityTypeData] tied to an [EntityType]'s [Identifier]. */
Expand Down Expand Up @@ -179,7 +179,7 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
}
}

private fun insertFunctions(store: Map<Identifier, List<AttributeFunction>>, data: MutableMap<Identifier, EntityAttributeData>) {
private fun insertFunctions(store: Map<Identifier, Map<Identifier, AttributeFunction>>, data: MutableMap<Identifier, EntityAttributeData>) {
for ((id, functions) in store) {
if (!Registries.ATTRIBUTE.containsId(id)) {
DataAttributes.LOGGER.warn("Function parent [$id] that was defined in config is not registered. This has been skipped.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,22 @@ import net.minecraft.util.Identifier
object ConfigMerger {
fun mergeOverrides(values: Map<Identifier, AttributeOverride>): Map<Identifier, AttributeOverride> {
val entries = values.toMutableMap()
for ((id, override) in DataAttributes.OVERRIDES_CONFIG.overrides) {
for ((id, override) in DataAttributes.OVERRIDES_CONFIG.entries) {
entries[id] = override
}
return entries
}

fun mergeFunctions(values: Map<Identifier, List<AttributeFunction>>): Map<Identifier, List<AttributeFunction>> {
fun mergeFunctions(values: Map<Identifier, Map<Identifier, AttributeFunction>>): Map<Identifier, Map<Identifier, AttributeFunction>> {
val entries = values.toMutableMap()
for ((primaryId, primaryFunctions) in DataAttributes.FUNCTIONS_CONFIG.functions.data) {
val entriesMap = entries[primaryId]
if (entriesMap == null) {
entries[primaryId] = primaryFunctions
for ((primaryId, primaryEntry) in DataAttributes.FUNCTIONS_CONFIG.entries.data) {
val secondaryEntry = entries[primaryId]?.toMutableMap()
if (secondaryEntry == null) {
entries[primaryId] = primaryEntry
}
else {
val secondaryEntry = entriesMap.toMutableList()
primaryFunctions.forEach { primaryFunction ->
var replaced = false
entriesMap.forEachIndexed { index, entry ->
if (entry.id == primaryFunction.id) {
secondaryEntry.removeAt(index)
secondaryEntry.add(index, primaryFunction)
replaced = true
}
}
if (!replaced) {
secondaryEntry.add(primaryFunction)
}
for ((id, value) in primaryEntry) {
secondaryEntry[id] = value
}
entries[primaryId] = secondaryEntry
}
Expand All @@ -45,7 +34,7 @@ object ConfigMerger {

fun mergeEntityTypes(values: Map<Identifier, Map<Identifier, Double>>): Map<Identifier, EntityTypeData> {
val entries = values.toMutableMap()
for ((primaryId, primaryEntry) in DataAttributes.ENTITY_TYPES_CONFIG.entity_types) {
for ((primaryId, primaryEntry) in DataAttributes.ENTITY_TYPES_CONFIG.entries) {
val secondaryEntry = entries[primaryId]?.toMutableMap()
if (secondaryEntry == null) {
entries[primaryId] = primaryEntry.data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class DefaultAttributesReloadListener : SimpleResourceReloadListener<DefaultAttr
@Serializable
data class Overrides(var entries: LinkedHashMap<Identifier, AttributeOverride> = LinkedHashMap())
@Serializable
data class Functions(var entries: LinkedHashMap<Identifier, List<AttributeFunction>> = LinkedHashMap())
data class Functions(var entries: LinkedHashMap<Identifier, LinkedHashMap<Identifier, AttributeFunction>> = LinkedHashMap())
@Serializable
data class EntityTypes(var entries: LinkedHashMap<Identifier, LinkedHashMap<Identifier, Double>> = LinkedHashMap())

Expand Down Expand Up @@ -61,7 +61,7 @@ class DefaultAttributesReloadListener : SimpleResourceReloadListener<DefaultAttr
cache.functions.entries.remove(id)
return@forEach
}
cache.functions.entries[id] = functions.filter { f -> Registries.ATTRIBUTE.containsId(f.id) }
cache.functions.entries[id] = LinkedHashMap(functions.filter { (id2) -> Registries.ATTRIBUTE.containsId(id2) })
}
cache.types.entries.forEach { (id, data) ->
if (!Registries.ENTITY_TYPE.containsId(id)) {
Expand Down Expand Up @@ -105,12 +105,10 @@ class DefaultAttributesReloadListener : SimpleResourceReloadListener<DefaultAttr
cache.functions.entries[id] = entry
}
else {
val mutablePresentEntry = presentEntry.toMutableList()
for (func in entry) {
if (presentEntry.find { it.id == func.id } != null) continue
mutablePresentEntry.add(func)
for ((secondaryId, secondaryValue) in entry) {
presentEntry.computeIfAbsent(secondaryId) { secondaryValue }
}
cache.functions.entries[id] = mutablePresentEntry
cache.functions.entries[id] = presentEntry
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,19 @@ import net.minecraft.util.Identifier
* */
@Serializable
data class AttributeFunction(
@Serializable(with = IdentifierSerializer::class)
var id: Identifier,
var enabled: Boolean,
var behavior: StackingBehavior,
var value: Double,
var enabled: Boolean
var value: Double
) {
@Suppress("UNUSED")
constructor() : this(Identifier("unknown"), StackingBehavior.Add, 0.0, true)
constructor() : this(true, StackingBehavior.Add, 0.0)

companion object {
@JvmField
val ENDEC = StructEndecBuilder.of(
Endecs.IDENTIFIER.fieldOf("id") { it.id },
Endec.BOOLEAN.optionalFieldOf("enabled", { it.enabled }, true),
Endec.STRING.xmap(StackingBehavior::of) { x -> x.name.uppercase() }.fieldOf("behavior") { it.behavior },
Endec.DOUBLE.fieldOf("value") { it.value },
Endec.BOOLEAN.optionalFieldOf("enabled", { it.enabled }, true),
::AttributeFunction
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import net.minecraft.util.Identifier
/**
* Container for data that applies modifiers to specific [EntityAttribute]'s based on a [AttributeFunction].
*/
data class AttributeFunctionConfig(var data: Map<Identifier, List<AttributeFunction>> = mapOf()) {
data class AttributeFunctionConfig(
val data: MutableMap<Identifier, MutableMap<Identifier, AttributeFunction>> = mutableMapOf()
) {
companion object {
@JvmField
val ENDEC = Endecs.IDENTIFIER.keyOf(AttributeFunction.ENDEC.listOf()).xmap(::AttributeFunctionConfig) { it.data }
val ENDEC = Endecs.IDENTIFIER.keyOf(Endecs.IDENTIFIER.keyOf(AttributeFunction.ENDEC)).xmap(::AttributeFunctionConfig) { it.data }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bibireden.data_attributes.config.models

import blue.endless.jankson.Comment
import com.bibireden.data_attributes.DataAttributes
import com.bibireden.data_attributes.data.EntityTypeData
import io.wispforest.owo.config.Option.SyncMode
Expand All @@ -17,5 +18,6 @@ class EntityTypesConfigModel {

@JvmField
@Hook
var entity_types: Map<Identifier, EntityTypeData> = mapOf()
@Comment("entity types are able to target specific entities in-game to attach certain attributes to them.")
var entries: Map<Identifier, EntityTypeData> = mapOf()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bibireden.data_attributes.config.models

import blue.endless.jankson.Comment
import com.bibireden.data_attributes.DataAttributes
import com.bibireden.data_attributes.config.functions.AttributeFunctionConfig
import io.wispforest.owo.config.Option.SyncMode
Expand All @@ -16,5 +17,6 @@ class FunctionsConfigModel {

@JvmField
@Hook
var functions: AttributeFunctionConfig = AttributeFunctionConfig()
@Comment("attribute functions are able to compute child attributes based on an increment/decrement, or multiplication of a parent attribute.")
var entries: AttributeFunctionConfig = AttributeFunctionConfig()
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.bibireden.data_attributes.config.models

import blue.endless.jankson.Comment
import com.bibireden.data_attributes.DataAttributes
import com.bibireden.data_attributes.api.attribute.StackingFormula
import com.bibireden.data_attributes.api.attribute.AttributeFormat
Expand All @@ -20,7 +21,8 @@ class OverridesConfigModel {

@JvmField
@Hook
var overrides: Map<Identifier, AttributeOverride> = mapOf()
@Comment("attribute overrides can change the range of the attribute, and can apply different formulas to modify its behavior when computed.")
var entries: Map<Identifier, AttributeOverride> = mapOf()

@Serializable
data class AttributeOverride(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@ class EntityAttributeData(val override: AttributeOverride? = null, val functions
}
}

/** Joins a [List] of [AttributeFunction]'s with the data in this class. */
fun putFunctions(functions: List<AttributeFunction>) {
functions.forEach { (id, behavior, value, enabled) ->
/** Joins a [Map] of [AttributeFunction]'s with the data in this class. */
fun putFunctions(functions: Map<Identifier, AttributeFunction>) {
for ((id, function) in functions) {
if (!Registries.ATTRIBUTE.containsId(id)) {
DataAttributes.LOGGER.warn("The attribute function child [$id] does not seem to be registered. This could allude to a missing mod or registered attribute.")
}
this.functions[id] = AttributeFunction(id, behavior, value, enabled)
this.functions[id] = function
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ object JanksonBuilders {
marshaller.serialize(cfg.data)
}
builder.registerDeserializer(JsonObject::class.java, AttributeFunctionConfig::class.java) { obj, marshaller ->
AttributeFunctionConfig(marshaller.marshall<Map<String, JsonArray>>(Map::class.java, obj).entries
.associate { (id, array) ->
Identifier.tryParse(id)!! to array.map { marshaller.marshall(AttributeFunction::class.java, it) }
}
AttributeFunctionConfig(marshaller.marshall<Map<String, Map<String, JsonObject>>>(Map::class.java, obj).entries
.associate { (a, b) -> Identifier.tryParse(a)!! to b.entries
.associate { (k, v) -> Identifier.tryParse(k)!! to marshaller.marshallCarefully(AttributeFunction::class.java, v) }.toMutableMap()
}.toMutableMap()
)
}
}
Expand Down
Loading

0 comments on commit 443d420

Please sign in to comment.