diff --git a/CHANGELOG.md b/CHANGELOG.md index 3a8dd2f..627c578 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,5 @@ ## Additions 💫 -- Diminishing setting should be implemented. It works somewhat differently compared to the wiki, so it will require documentation which will be supplimented in the future. - - To use Diminishing behavior, click on the **Flat** button to toggle between Diminishing behavior for an attribute. - - Adjust the smoothness (previously increment value) to your liking. \ No newline at end of file +- Added the `DefaultAttributeFactory`, which can be used in mods to add extra defaults to the config. + - Currently, it is limited to hard-code, +- Added `EntityAttributeSupplier` to the mod again. + - Its purpose is just to be a wrapper on lazily evaluated `EntityAttribute` values. \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 630f5d3..eacd571 100644 --- a/gradle.properties +++ b/gradle.properties @@ -9,7 +9,7 @@ loom_version=1.7-SNAPSHOT minecraft_version=1.20.1 fabric_kotlin_version=1.11.0+kotlin.2.0.0 fabric_api_version=0.92.2+1.20.1 -mod_version=2.0.0-alpha.7+1.20.1 +mod_version=2.0.0-alpha.8+1.20.1 loader=fabric yarn_mappings=1.20.1+build.10 diff --git a/src/main/kotlin/com/bibireden/data_attributes/api/DataAttributesAPI.kt b/src/main/kotlin/com/bibireden/data_attributes/api/DataAttributesAPI.kt index ed7c5ff..5601996 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/api/DataAttributesAPI.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/api/DataAttributesAPI.kt @@ -3,6 +3,7 @@ package com.bibireden.data_attributes.api import net.minecraft.entity.LivingEntity import net.minecraft.entity.attribute.EntityAttribute import java.util.* +import java.util.function.Supplier object DataAttributesAPI { @JvmStatic @@ -32,9 +33,9 @@ object DataAttributesAPI { * - The attribute is registered to the game * - The attribute is **present** on the given [LivingEntity]. */ - fun getValue(supplier: () -> EntityAttribute?, entity: LivingEntity): Optional { + fun getValue(supplier: Supplier, entity: LivingEntity): Optional { val container = entity.attributes - val attribute = supplier() + val attribute = supplier.get() return if (attribute != null && container.hasAttribute(attribute)) { Optional.of(container.getValue(attribute)) diff --git a/src/main/kotlin/com/bibireden/data_attributes/api/attribute/EntityAttributeSupplier.kt b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/EntityAttributeSupplier.kt new file mode 100644 index 0000000..06494b0 --- /dev/null +++ b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/EntityAttributeSupplier.kt @@ -0,0 +1,13 @@ +package com.bibireden.data_attributes.api.attribute + +import net.minecraft.entity.attribute.EntityAttribute +import net.minecraft.registry.Registries +import net.minecraft.util.Identifier +import java.util.function.Supplier + +/** + * Supplier classes to provide dynamic attribute references. + */ +class EntityAttributeSupplier(val id: Identifier) : Supplier { + override fun get() = Registries.ATTRIBUTE[this.id] +} \ No newline at end of file diff --git a/src/main/kotlin/com/bibireden/data_attributes/api/factory/DefaultAttributeFactory.kt b/src/main/kotlin/com/bibireden/data_attributes/api/factory/DefaultAttributeFactory.kt new file mode 100644 index 0000000..1a15ab5 --- /dev/null +++ b/src/main/kotlin/com/bibireden/data_attributes/api/factory/DefaultAttributeFactory.kt @@ -0,0 +1,39 @@ +package com.bibireden.data_attributes.api.factory + +import com.bibireden.data_attributes.DataAttributes +import com.bibireden.data_attributes.config.functions.AttributeFunction +import com.bibireden.data_attributes.config.models.OverridesConfigModel.AttributeOverride +import com.bibireden.data_attributes.data.EntityTypeData +import net.minecraft.util.Identifier + +/** + * Meant to register attributes into the DataAttributes config primarily after it is initialized. + * + * This is useful for mods that wish to implement their own defaults, so they can be applied to the world. + * Ensure that it is not done through static initialization, the config is not guaranteed to exist at that time. Instead, register afterward, such as on **mod initialization**. + */ +object DefaultAttributeFactory { + /** Registers default [AttributeOverride]'s to the config if they are not present currently within the config. */ + fun registerOverrides(overrides: Map) { + val current = DataAttributes.OVERRIDES_CONFIG.overrides.toMutableMap() + overrides.forEach { (id, ao) -> current.computeIfAbsent(id) { ao } } + DataAttributes.OVERRIDES_CONFIG.overrides = current + DataAttributes.OVERRIDES_CONFIG.save() + } + + /** Registers default [AttributeFunction]'s to the config if they are not present currently within the config. */ + fun registerFunctions(functions: Map>) { + val current = DataAttributes.FUNCTIONS_CONFIG.functions.data.toMutableMap() + functions.forEach { (id, af) -> current.computeIfAbsent(id) { af } } + DataAttributes.FUNCTIONS_CONFIG.functions.data = current + DataAttributes.FUNCTIONS_CONFIG.save() + } + + /** Registers default [EntityTypeData]'s to the config if they are not present currently within the config. */ + fun registerEntityTypes(entityTypes: Map) { + val current = DataAttributes.ENTITY_TYPES_CONFIG.entity_types.toMutableMap() + entityTypes.forEach { (id, types) -> current.computeIfAbsent(id) { types } } + DataAttributes.ENTITY_TYPES_CONFIG.entity_types = current + DataAttributes.ENTITY_TYPES_CONFIG.save() + } +} \ No newline at end of file