From 26f33b7be5c96c9cbeed5e3a1484ba6ca6708f92 Mon Sep 17 00:00:00 2001 From: Data Encoded <81109563+DataEncoded@users.noreply.github.com> Date: Mon, 12 Aug 2024 16:33:30 -0500 Subject: [PATCH] merge 1.20.1/fabric dev formatting (#29) * Implement Interface Injection, bump version (#22) * [init] initial commit of formatting * [fix] Add endec suppor tto attribute format * [format] change formatting of percentages * [semver] bump version to beta-12 * [feat] Add getFormattedValue to API * [chore] update changelog --------- Co-authored-by: Bibi Reden --- CHANGELOG.md | 4 +- gradle.properties | 4 +- .../api/attribute/IEntityAttribute.java | 3 +- .../mixin/ClampedEntityAttributeMixin.java | 4 +- .../mixin/EntityAttributeMixin.java | 12 +++++- .../data_attributes/api/DataAttributesAPI.kt | 29 ++++++++++++- .../api/attribute/AttributeFormat.kt | 14 ++++++ .../config/functions/AttributeFunction.kt | 1 + .../config/models/OverridesConfigModel.kt | 8 +++- .../providers/AttributeOverrideProvider.kt | 43 +++++++++++++++++++ .../assets/data_attributes/lang/en_us.json | 13 ++++++ 11 files changed, 125 insertions(+), 10 deletions(-) create mode 100644 src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index efcf815..e63dfc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,2 @@ -## Changes 🌽 -- Resolved an issue with entity-types/functions not applying properly if a mod/data-pack got to the entity-type id first. \ No newline at end of file +## Additions 🍎 +- Added a condensed way to format attribute values into the API through `getFormattedValue`. \ No newline at end of file diff --git a/gradle.properties b/gradle.properties index 437684e..fb4bacb 100644 --- a/gradle.properties +++ b/gradle.properties @@ -11,9 +11,9 @@ maven_group=com.bibireden.data_attributes loom_version=1.7-SNAPSHOT minecraft_version=1.20.1 -fabric_kotlin_version=1.12.0+kotlin.2.0.10 +fabric_kotlin_version=1.11.0+kotlin.2.0.0 fabric_api_version=0.92.2+1.20.1 -mod_version=2.0.0+1.20.1-beta.11 +mod_version=2.0.0+1.20.1-beta.12 loader=fabric # Mappings diff --git a/src/main/java/com/bibireden/data_attributes/api/attribute/IEntityAttribute.java b/src/main/java/com/bibireden/data_attributes/api/attribute/IEntityAttribute.java index 8e849a9..daa1ea6 100644 --- a/src/main/java/com/bibireden/data_attributes/api/attribute/IEntityAttribute.java +++ b/src/main/java/com/bibireden/data_attributes/api/attribute/IEntityAttribute.java @@ -37,7 +37,8 @@ public interface IEntityAttribute { * @return The attribute's {@link StackingFormula}. */ default StackingFormula data_attributes$formula() { return null; } - + + default AttributeFormat data_attributes$format() { return null; } /** * @return An immutable map of the function-parents attached to this attribute. * @since 1.4.0 diff --git a/src/main/java/com/bibireden/data_attributes/mixin/ClampedEntityAttributeMixin.java b/src/main/java/com/bibireden/data_attributes/mixin/ClampedEntityAttributeMixin.java index ffa05fe..04066ff 100644 --- a/src/main/java/com/bibireden/data_attributes/mixin/ClampedEntityAttributeMixin.java +++ b/src/main/java/com/bibireden/data_attributes/mixin/ClampedEntityAttributeMixin.java @@ -1,5 +1,7 @@ package com.bibireden.data_attributes.mixin; +import com.bibireden.data_attributes.api.attribute.AttributeFormat; +import com.bibireden.data_attributes.config.models.OverridesConfigModel; import com.bibireden.data_attributes.config.models.OverridesConfigModel.AttributeOverride; import com.llamalad7.mixinextras.injector.ModifyReturnValue; import com.llamalad7.mixinextras.sugar.Local; @@ -26,7 +28,7 @@ abstract class ClampedEntityAttributeMixin extends EntityAttributeMixin { @Inject(method = "", at = @At("TAIL")) private void data_attributes$init(String translationKey, double fallback, double min, double max, CallbackInfo ci) { - this.data_attributes$override(new AttributeOverride(false, minValue, maxValue, min, max, 0.0, StackingFormula.Flat)); + this.data_attributes$override(new AttributeOverride(false, minValue, maxValue, min, max, 0.0, StackingFormula.Flat, AttributeFormat.Whole)); } @ModifyReturnValue(method = "getMinValue", at = @At("RETURN")) diff --git a/src/main/java/com/bibireden/data_attributes/mixin/EntityAttributeMixin.java b/src/main/java/com/bibireden/data_attributes/mixin/EntityAttributeMixin.java index 2e6ca51..2203e01 100644 --- a/src/main/java/com/bibireden/data_attributes/mixin/EntityAttributeMixin.java +++ b/src/main/java/com/bibireden/data_attributes/mixin/EntityAttributeMixin.java @@ -2,6 +2,8 @@ import java.util.Map; +import com.bibireden.data_attributes.api.attribute.AttributeFormat; +import com.bibireden.data_attributes.config.models.OverridesConfigModel; import com.bibireden.data_attributes.config.models.OverridesConfigModel.AttributeOverride; import com.bibireden.data_attributes.config.functions.AttributeFunction; import com.llamalad7.mixinextras.injector.ModifyReturnValue; @@ -28,6 +30,7 @@ abstract class EntityAttributeMixin implements MutableEntityAttribute { @Unique private Map data_attributes$parents, data_attributes$children; @Unique protected StackingFormula data_attributes$formula; + @Unique protected AttributeFormat data_attributes$format; @Unique protected boolean data_attributes$enabled; @Unique protected double data_attributes$min, data_attributes$max, data_attributes$smoothness; @@ -41,6 +44,7 @@ abstract class EntityAttributeMixin implements MutableEntityAttribute { this.data_attributes$min = Double.MIN_VALUE; this.data_attributes$max = Double.MAX_VALUE; this.data_attributes$formula = StackingFormula.Flat; + this.data_attributes$format = AttributeFormat.Whole; this.data_attributes$parents = new Object2ObjectArrayMap<>(); this.data_attributes$children = new Object2ObjectArrayMap<>(); } @@ -68,6 +72,7 @@ abstract class EntityAttributeMixin implements MutableEntityAttribute { this.data_attributes$max = override.max; this.data_attributes$smoothness = override.smoothness; this.data_attributes$formula = override.formula; + this.data_attributes$format = override.format; } @Override @@ -90,7 +95,7 @@ abstract class EntityAttributeMixin implements MutableEntityAttribute { @Override public void data_attributes$clear() { - this.data_attributes$override(new AttributeOverride(this.data_attributes$enabled, this.fallback, this.fallback, this.fallback, this.fallback, 0.0D, StackingFormula.Flat)); + this.data_attributes$override(new AttributeOverride(this.data_attributes$enabled, this.fallback, this.fallback, this.fallback, this.fallback, 0.0D, StackingFormula.Flat, AttributeFormat.Whole)); this.data_attributes$clearDescendants(); } @@ -130,6 +135,11 @@ abstract class EntityAttributeMixin implements MutableEntityAttribute { return this.data_attributes$formula; } + @Override + public AttributeFormat data_attributes$format() { + return this.data_attributes$format; + } + @Override public Map data_attributes$parents() { return ImmutableMap.copyOf(this.data_attributes$parents); 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 f98e7b0..4457316 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 com.bibireden.data_attributes.DataAttributes import com.bibireden.data_attributes.DataAttributesClient import com.bibireden.data_attributes.api.attribute.EntityAttributeSupplier +import com.bibireden.data_attributes.api.attribute.IEntityAttribute import com.bibireden.data_attributes.config.AttributeConfigManager import net.minecraft.entity.LivingEntity import net.minecraft.entity.attribute.EntityAttribute @@ -52,7 +53,7 @@ object DataAttributesAPI { } /** - * Tries to obtain a [EntityAttribute] value off a [LivingEntity] based on a supplier implementation. + * Tries to obtain a [EntityAttribute] formatted value off a [LivingEntity]. * Certain requirements must be met in order for the value to be present: * * - The attribute is registered to the game @@ -62,4 +63,30 @@ object DataAttributesAPI { fun getValue(supplier: Supplier>, entity: LivingEntity): Optional { return supplier.get().filter(entity.attributes::hasAttribute).map(entity.attributes::getValue) } + + /** + * Tries to obtain a [EntityAttribute] formatted value off a [LivingEntity]. + * Certain requirements must be met in order for the value to be present: + * + * - The attribute is registered to the game + * - The attribute is **present** on the given [LivingEntity]. + */ + @JvmStatic + fun getFormattedValue(attribute: EntityAttribute, entity: LivingEntity): String { + val attr = (attribute as IEntityAttribute) + return attr.`data_attributes$format`().function(attr.`data_attributes$min`(), attr.`data_attributes$max`(), getValue(attribute, entity).orElse(0.0)) + } + + /** + * Tries to obtain a [EntityAttribute] formatted value off a [LivingEntity] based on a supplier implementation. + * Certain requirements must be met in order for the value to be present: + * + * - The attribute is registered to the game + * - The attribute is **present** on the given [LivingEntity]. + */ + @JvmStatic + fun getFormattedValue(supplier: Supplier>, entity: LivingEntity): String + { + return supplier.get().filter(entity.attributes::hasAttribute).map { getFormattedValue(it, entity) }.orElse("N/A") + } } \ No newline at end of file diff --git a/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt new file mode 100644 index 0000000..c899348 --- /dev/null +++ b/src/main/kotlin/com/bibireden/data_attributes/api/attribute/AttributeFormat.kt @@ -0,0 +1,14 @@ +package com.bibireden.data_attributes.api.attribute + +import io.wispforest.endec.Endec + +enum class AttributeFormat(val function: (min: Double, max: Double, value: Double) -> String) { + Percentage ({min, max, value -> "%.2f".format((value-min)/((max-min)/100))+"%" }), + Whole({_, _, value -> "$value"} ); + + companion object { + val ENDEC: Endec = Endec.STRING.xmap(AttributeFormat::of) { it.name } + + fun of(id: String) = if (id.equals("percentage", ignoreCase = true)) Percentage else Whole + } +} \ No newline at end of file diff --git a/src/main/kotlin/com/bibireden/data_attributes/config/functions/AttributeFunction.kt b/src/main/kotlin/com/bibireden/data_attributes/config/functions/AttributeFunction.kt index 1cf17bd..7d03b0e 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/config/functions/AttributeFunction.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/config/functions/AttributeFunction.kt @@ -1,6 +1,7 @@ package com.bibireden.data_attributes.config.functions import com.bibireden.data_attributes.api.attribute.StackingBehavior +import com.bibireden.data_attributes.api.attribute.AttributeFormat import com.bibireden.data_attributes.endec.Endecs import com.bibireden.data_attributes.serde.IdentifierSerializer import io.wispforest.endec.Endec diff --git a/src/main/kotlin/com/bibireden/data_attributes/config/models/OverridesConfigModel.kt b/src/main/kotlin/com/bibireden/data_attributes/config/models/OverridesConfigModel.kt index b761592..9ee64c9 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/config/models/OverridesConfigModel.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/config/models/OverridesConfigModel.kt @@ -2,6 +2,7 @@ package com.bibireden.data_attributes.config.models import com.bibireden.data_attributes.DataAttributes import com.bibireden.data_attributes.api.attribute.StackingFormula +import com.bibireden.data_attributes.api.attribute.AttributeFormat import com.bibireden.data_attributes.mutable.MutableEntityAttribute import io.wispforest.endec.Endec import io.wispforest.endec.impl.StructEndecBuilder @@ -36,7 +37,9 @@ class OverridesConfigModel { @JvmField var max_fallback: Double = 20.0, @JvmField - var formula: StackingFormula = StackingFormula.Flat + var formula: StackingFormula = StackingFormula.Flat, + @JvmField + var format: AttributeFormat = AttributeFormat.Whole, ) { /** Calls an override of an `MutableEntityAttribute`. */ fun override(attribute: EntityAttribute) { @@ -52,7 +55,8 @@ class OverridesConfigModel { Endec.DOUBLE.optionalFieldOf("smoothness", { it.smoothness }, 0.01), Endec.DOUBLE.optionalFieldOf("min_fallback", { it.min_fallback }, 0.0), Endec.DOUBLE.optionalFieldOf("max_fallback", { it.max_fallback }, 1_000_000.0), - StackingFormula.ENDEC.fieldOf("formula") { it.formula }, + StackingFormula.ENDEC.optionalFieldOf("formula", { it.formula }, StackingFormula.Flat), + AttributeFormat.ENDEC.optionalFieldOf("format", { it.format }, AttributeFormat.Whole), ::AttributeOverride, ) } diff --git a/src/main/kotlin/com/bibireden/data_attributes/config/providers/AttributeOverrideProvider.kt b/src/main/kotlin/com/bibireden/data_attributes/config/providers/AttributeOverrideProvider.kt index 842d6cc..f945de3 100644 --- a/src/main/kotlin/com/bibireden/data_attributes/config/providers/AttributeOverrideProvider.kt +++ b/src/main/kotlin/com/bibireden/data_attributes/config/providers/AttributeOverrideProvider.kt @@ -1,5 +1,6 @@ package com.bibireden.data_attributes.config.providers +import com.bibireden.data_attributes.api.attribute.AttributeFormat import com.bibireden.data_attributes.api.DataAttributesAPI import com.bibireden.data_attributes.api.attribute.StackingFormula import com.bibireden.data_attributes.config.DataAttributesConfigProviders.attributeIdentifierToText @@ -157,6 +158,48 @@ class AttributeOverrideProvider(val option: Option + hf.verticalAlignment(VerticalAlignment.CENTER) + hf.gap(8) + hf.child( + Components.label(Text.translatable("text.config.data_attributes.data_entry.overrides.format")) + .sizing(Sizing.content(), Sizing.fixed(20)) + ) + hf.child( + Components.button(Text.translatable("text.config.data_attributes.enum.format.${override.format.name.lowercase()}"), { + override.format = when (override.format) { + AttributeFormat.Percentage -> AttributeFormat.Whole + AttributeFormat.Whole -> AttributeFormat.Percentage + } + it.message = Text.translatable("text.config.data_attributes.enum.format.${override.format.name.lowercase()}") + this.backing.replace(id, override.copy(formula = override.formula)) + }) + .renderer(ButtonRenderers.STANDARD) + .positioning(Positioning.relative(100, 0)).horizontalSizing(Sizing.fixed(65)) + ) + }) +/* + topContainer.child(Containers.horizontalFlow(Sizing.fill(100), Sizing.fixed(20))).also { hf -> + hf.verticalAlignment(VerticalAlignment.CENTER) + hf.gap(8) + hf.child( + Components.label(Text.translatable("text.config.data_attributes.data_entry.overrides.format")) + .sizing(Sizing.content(), Sizing.fixed(20)) + ) + hf.child( + Components.button(Text.translatable("text.config.data_attributes.enum.format.${override.format.name.lowercase()}"), { + override.format = when (override.format) { + AttributeFormat.Whole -> AttributeFormat.Percentage + AttributeFormat.Percentage -> AttributeFormat.Whole + } + it.message = Text.translatable("text.config.data_attributes.enum.format.${override.format.name.lowercase()}") + this.backing.replace(id, override.copy(format = override.format)) + }) + .renderer(ButtonRenderers.STANDARD) + .positioning(Positioning.relative(100, 0)).horizontalSizing(Sizing.fixed(65)) + ) + }*/ } .also(this::child) } diff --git a/src/main/resources/assets/data_attributes/lang/en_us.json b/src/main/resources/assets/data_attributes/lang/en_us.json index 36771e1..2751540 100644 --- a/src/main/resources/assets/data_attributes/lang/en_us.json +++ b/src/main/resources/assets/data_attributes/lang/en_us.json @@ -31,6 +31,15 @@ {"text": "Multiply", "color": "white"} ], + "text.config.data_attributes.enum.format.whole": [ + {"text": "ℝ ", "color": "#ffffff"}, + {"text": "Whole", "color": "white"} + ], + "text.config.data_attributes.enum.format.percentage": [ + {"text": "% ", "color": "#ffffff"}, + {"text": "Percentage", "color": "white"} + ], + "text.config.data_attributes.category.overrides": "Overrides", "text.config.data_attributes.category.functions": "Functions", @@ -63,6 +72,10 @@ {"text": "\uD83E\uDDEA ", "color": "#83DEB7"}, {"text": "Stacking Formula", "color": "white"} ], + "text.config.data_attributes.data_entry.overrides.format" : [ + {"text": "\uD835\uDC53 ", "color": "#83DEB7"}, + {"text": "Format", "color": "white"} + ], "text.config.data_attributes.data_entry.functions.behavior": [ {"text": "\uD835\uDC53 ", "color": "#83DEB7"}, {"text": "Behavior", "color": "white"}