Skip to content

Commit

Permalink
Fix tooltips, integrate some sort of diminishing for now
Browse files Browse the repository at this point in the history
  • Loading branch information
bibi-reden committed Jul 29, 2024
1 parent 3629ca8 commit ad163ac
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 70 deletions.
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-beta.5+1.20.1
mod_version=2.0.0-beta.6+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 @@ -8,7 +8,7 @@
*
* EntityAttribute's implement this through a mixin, and can be cast to this interface to get additional data added by Data Attributes.
*
* @author CleverNucleus
* @author Bare Minimum Studios (B.M.S), CleverNucleus
*
*/
public interface IEntityAttribute {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/**
*
* Access to an entity attribute instance.
* @author CleverNucleus
* @author Bare Minimum Studios (B.M.S), CleverNucleus
*
*/
public interface IEntityAttributeInstance {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
/**
* Implement this in your Item class.
*
* @author CleverNucleus
* @author Bare Minimum Studios (B.M.S), CleverNucleus
*
*/
public interface IItemEntityAttributeModifiers {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
package com.bibireden.data_attributes.api.attribute;

/**
* Used to factor into {@link com.bibireden.data_attributes.config.functions.AttributeFunction}'s
* to determine what operation to use when calculating how their modifier(s) are applied.
* <p/>
* @since 1.4.0
* @author CleverNucleus
* @author Bare Minimum Studios (B.M.S), CleverNucleus
*/
public enum StackingBehavior {
/** Addition of values as defined by the parent attribute. Equivalent of EntityAttributeModifier.Operation.ADDITION. */
Add((byte)0),
/** Multiplication of parent attribute. Equivalent of EntityAttributeModifier.Operation.MULTIPLY_TOTAL. */
Multiply((byte)1);

private final byte id;

StackingBehavior(final byte id) {
this.id = id;
}

public static StackingBehavior of(final byte id) {
return switch(id) {
case 0 -> Add;
case 1 -> Multiply;
default -> Add;
};
}
/**
* Addition of values as defined by the parent attribute.
* <p/>
* Equivalent to {@link net.minecraft.entity.attribute.EntityAttributeModifier.Operation#ADDITION}.
*/
Add,
/**
* Multiplication of parent attribute.
* <p>
* Equivalent to {@link net.minecraft.entity.attribute.EntityAttributeModifier.Operation#MULTIPLY_TOTAL}.
*/
Multiply;

public static StackingBehavior of(final String id) {
return id.equalsIgnoreCase("multiply") ? Multiply : Add;
}

public byte id() {
return this.id;
}

@Override
public String toString() {
return String.valueOf(this.id);
}

public String getTranslationKey() { return "text.data_attributes.stackingBehavior." + this.name().toLowerCase(); }
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
package com.bibireden.data_attributes.api.attribute

import io.wispforest.endec.Endec
import net.minecraft.util.math.MathHelper
import kotlin.math.abs
import kotlin.math.pow

// CN: ((1.0 - v2) * (1.0 - m).pow((v - v2) / m)) - ((1.0 - k2) * (1.0 - m).pow((k - k2) / m))

enum class StackingFormula(val function: (k: Double, k2: Double, v: Double, v2: Double, attribute: IEntityAttribute) -> Double) {
Flat({ k, _, v, _, _ -> k - v }),
Diminished({ k, k2, v, v2, attribute ->
val min = attribute.`data_attributes$min`()
val max = attribute.`data_attributes$max`()

val k = k / 100
val k2 = k2 / 100
val v = v / 100
val v2 = v2 / 100

val s = MathHelper.clamp(attribute.`data_attributes$smoothness`(), 0.01, 1.0)

val result1 = (1.0 - v2)
val result2 = (1.0 - s).pow((v - v2) / s)
val result3 = result1 * result2

val result4 = (1.0 - k2)
val result5 = (1.0 - s).pow((k - k2) / s)
val result6 = (result4 * result5)

(((result3-result6)) * (max-min)) - min
val base = (attribute as net.minecraft.entity.attribute.EntityAttribute).defaultValue
val smoothness = attribute.`data_attributes$smoothness`()
val result = base + (((k - base) - v) * smoothness)
result
});

companion object {
Expand All @@ -37,9 +20,34 @@ enum class StackingFormula(val function: (k: Double, k2: Double, v: Double, v2:
fun of(id: String) = if (id.equals("diminished", ignoreCase = true)) Diminished else Flat
}

val translationKey: String
get() = "text.data_attributes.stackingFormula.${this.name.lowercase()}"

fun max(current: Double, input: Double): Double = kotlin.math.max(current, abs(input))

fun stack(current: Double, input: Double): Double = current + abs(input)

fun result(k: Double, k2: Double, v: Double, v2: Double, attribute: IEntityAttribute): Double = this.function(k, k2, v, v2, attribute)
}
}


// the solution?
//val min = attribute.`data_attributes$min`()
//val max = attribute.`data_attributes$max`()
//
//val k = k / 100
//val k2 = k2 / 100
//val v = v / 100
//val v2 = v2 / 100
//
//val s = MathHelper.clamp(attribute.`data_attributes$smoothness`(), 0.01, 1.0)
//
//val result1 = (1.0 - v2)
//val result2 = (1.0 - s).pow((v - v2) / s)
//val result3 = result1 * result2
//
//val result4 = (1.0 - k2)
//val result5 = (1.0 - s).pow((k - k2) / s)
//val result6 = (result4 * result5)
//
//((((result3-result6)) * (max-min)) - min).round(2)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bibireden.data_attributes.config

import com.bibireden.data_attributes.DataAttributes
import com.bibireden.data_attributes.api.EntityInstances
import com.bibireden.data_attributes.api.attribute.IEntityAttribute
import com.bibireden.data_attributes.api.event.AttributesReloadedEvent
import com.bibireden.data_attributes.config.models.OverridesConfigModel.AttributeOverride
import com.bibireden.data_attributes.config.functions.AttributeFunction
Expand All @@ -27,7 +28,9 @@ 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(), var updateFlag: Int = 0) {
class AttributeConfigManager(var data: Data = Data(), val handler: AttributeContainerHandler = AttributeContainerHandler()) {
var updateFlag: Int = 0

@JvmRecord
data class Tuple<T>(val livingEntity: Class<out LivingEntity>, val value: T)

Expand Down Expand Up @@ -82,6 +85,18 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
)
}

/** Currently applied [AttributeOverride]'s mapped with an [EntityAttribute]'s [Identifier]. */
val overrides: Map<Identifier, AttributeOverride>
get() = this.data.overrides

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

/** Currently applied [EntityTypeData] tied to an [EntityType]'s [Identifier]. */
val entityTypes: Map<Identifier, EntityTypeData>
get() = this.data.entity_types

/**
* Increments to the next flag, usually signaling an update from the manager.
* @return [Int] The update flag's current value.
Expand Down Expand Up @@ -120,15 +135,22 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
fun onDataUpdate() {
val entityAttributeData = mutableMapOf<Identifier, EntityAttributeData>()

for ((id, value) in this.data.overrides) {
for ((id, value) in this.overrides) {
if (!Registries.ATTRIBUTE.containsId(id)) {
DataAttributes.LOGGER.warn("Attribute [$id] that was targeted for override is not registered. This has been skipped.")
continue
}
val attribute = Registries.ATTRIBUTE[id]!! as IEntityAttribute
if (value.max.isNaN()) {
value.max = attribute.`data_attributes$max_fallback`()
}
if (value.min.isNaN()) {
value.min = attribute.`data_attributes$min_fallback`()
}
entityAttributeData[id] = EntityAttributeData(value)
}

for ((id, configs) in this.data.functions) {
for ((id, configs) in this.functions) {
if (!Registries.ATTRIBUTE.containsId(id)) {
DataAttributes.LOGGER.warn("Function parent [$id] that was defined in config is not registered. This has been skipped.")
}
Expand All @@ -145,11 +167,11 @@ class AttributeConfigManager(var data: Data = Data(), val handler: AttributeCont
data.override(getAttribute(id)!!) // was already asserted to exist in L: 124
}

for ((identifier, attributeData) in entityAttributeData) {
attributeData.copy(Registries.ATTRIBUTE[identifier]!!) // was already asserted to exist in L: 124
for ((identifier, data) in entityAttributeData) {
data.copy(Registries.ATTRIBUTE[identifier]!!) // was already asserted to exist in L: 124
}

this.handler.buildContainers(this.data.entity_types)
this.handler.buildContainers(this.entityTypes)

AttributesReloadedEvent.EVENT.invoker().onReloadCompleted()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ class OverridesConfigModel {
@JvmField
var enabled: Boolean = true,
@JvmField
var min: Double = 0.0,
var min: Double = Double.NaN,
@JvmField
var max: Double = 1000.0,
var max: Double = Double.NaN,
@JvmField
var smoothness: Double = 0.01,
@JvmField
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class AttributeFunctionsProvider(val option: Option<AttributeFunctionConfig>) :
CollapsibleFoldableContainer(Sizing.content(), Sizing.content(), attributeIdentifierToText(topID), true).also { ct ->
ct.gap(15)
if (isFunctionParentUnregistered) {
ct.tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
ct.titleLayout().tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
}
functions.forEachIndexed { index, function ->
val isFunctionChildUnregistered = isAttributeUnregistered(function.id)

Containers.collapsible(Sizing.content(), Sizing.content(), attributeIdentifierToText(function.id), true).also {
it.gap(8)
if (isFunctionChildUnregistered) {
it.tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
it.titleLayout().tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
}
else {
val attribute = Registries.ATTRIBUTE[function.id]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class AttributeOverrideProvider(val option: Option<Map<Identifier, AttributeOver
Containers.collapsible(Sizing.content(), Sizing.content(), attributeIdentifierToText(id), true)
.also { topContainer ->
if (isOverrideInvalid) {
topContainer.tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
topContainer.titleLayout().tooltip(Text.translatable("text.config.data_attributes.data_entry.invalid"))
}

topContainer.child(Containers.horizontalFlow(Sizing.fill(100), Sizing.fixed(15)).also { hf ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class DataAttributesConfigScreen(val overrides: OverridesConfig, val functions:

val optionPanel = rootComponent.childById(FlowLayout::class.java, "option-panel")

if (this.client?.world != null) rootComponent.surface(Surface.blur(.55F, 2F))
if (this.client?.world != null) rootComponent.surface(Surface.VANILLA_TRANSLUCENT)

val sections = LinkedHashMap<Component, Text>()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ class CollapsibleFoldableContainer(horizontalSizing: Sizing, verticalSizing: Siz

fun getFoldableText(): MutableText {
return if (isFoldered) {
Text.translatable("ui.data_attributes.collapsiblefoldablecontainer.uncollapse_all")
Text.translatable("ui.data_attributes.cfc.uncollapse_all")
}
else {
Text.translatable("ui.data_attributes.collapsiblefoldablecontainer.collapse_all")
Text.translatable("ui.data_attributes.cfc.collapse_all")
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/assets/data_attributes/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@
{"text": "when ready.", "italic": true, "color": "gray"}
],

"ui.data_attributes.collapsiblefoldablecontainer.collapse_all": "Collapse",
"ui.data_attributes.collapsiblefoldablecontainer.uncollapse_all": "Uncollapse"
"ui.data_attributes.cfc.collapse_all": "Collapse",
"ui.data_attributes.cfc.uncollapse_all": "Uncollapse",

"text.data_attributes.stackingFormula.flat": "Flat",
"text.data_attributes.stackingFormula.diminished": "Diminished",

"text.data_attributes.stackingBehavior.add": "Add",
"text.data_attributes.stackingBehavior.multiply": "Multiply"
}

0 comments on commit ad163ac

Please sign in to comment.