Skip to content

Commit

Permalink
Implement alpha.8 (DefaultAttributeFactory) (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden authored Jul 20, 2024
1 parent 5cd62a9 commit 1ca077c
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 6 deletions.
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
- 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.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<Double> {
fun getValue(supplier: Supplier<EntityAttribute?>, entity: LivingEntity): Optional<Double> {
val container = entity.attributes
val attribute = supplier()
val attribute = supplier.get()

return if (attribute != null && container.hasAttribute(attribute)) {
Optional.of(container.getValue(attribute))
Expand Down
Original file line number Diff line number Diff line change
@@ -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<EntityAttribute?> {
override fun get() = Registries.ATTRIBUTE[this.id]
}
Original file line number Diff line number Diff line change
@@ -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<Identifier, AttributeOverride>) {
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<Identifier, List<AttributeFunction>>) {
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<Identifier, EntityTypeData>) {
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()
}
}

0 comments on commit 1ca077c

Please sign in to comment.