Skip to content

Commit

Permalink
merge 1.20.1/fabric dev formatting (#29)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
DataEncoded and bibi-reden authored Aug 12, 2024
1 parent f9b9cf4 commit 26f33b7
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 10 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
## Additions 🍎
- Added a condensed way to format attribute values into the API through `getFormattedValue`.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -26,7 +28,7 @@ abstract class ClampedEntityAttributeMixin extends EntityAttributeMixin {

@Inject(method = "<init>", 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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,6 +30,7 @@
abstract class EntityAttributeMixin implements MutableEntityAttribute {
@Unique private Map<IEntityAttribute, AttributeFunction> 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;

Expand All @@ -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<>();
}
Expand Down Expand Up @@ -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
Expand All @@ -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();
}

Expand Down Expand Up @@ -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<IEntityAttribute, AttributeFunction> data_attributes$parents() {
return ImmutableMap.copyOf(this.data_attributes$parents);
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 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
Expand Down Expand Up @@ -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
Expand All @@ -62,4 +63,30 @@ object DataAttributesAPI {
fun getValue(supplier: Supplier<Optional<EntityAttribute>>, entity: LivingEntity): Optional<Double> {
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<Optional<EntityAttribute>>, entity: LivingEntity): String
{
return supplier.get().filter(entity.attributes::hasAttribute).map { getFormattedValue(it, entity) }.orElse("N/A")
}
}
Original file line number Diff line number Diff line change
@@ -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<AttributeFormat> = Endec.STRING.xmap(AttributeFormat::of) { it.name }

fun of(id: String) = if (id.equals("percentage", ignoreCase = true)) Percentage else Whole
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -157,6 +158,48 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver
.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.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)
}
Expand Down
13 changes: 13 additions & 0 deletions src/main/resources/assets/data_attributes/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",

Expand Down Expand Up @@ -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"}
Expand Down

0 comments on commit 26f33b7

Please sign in to comment.