From 8bf6d53a486f0e1f8ab9d46062d14a140921b563 Mon Sep 17 00:00:00 2001 From: Sollace Date: Sun, 30 Jun 2024 14:15:44 +0100 Subject: [PATCH] Implement proper formatting for ranged spell attributes --- .../spell/attribute/AttributeFormat.java | 10 +++++++- .../magic/spell/attribute/SpellAttribute.java | 25 ++++++++++++++++--- .../magic/spell/effect/FireBoltSpell.java | 10 +++++--- 3 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/AttributeFormat.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/AttributeFormat.java index 2312e8f0..17ec7d67 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/AttributeFormat.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/AttributeFormat.java @@ -32,6 +32,14 @@ public enum AttributeFormat { public abstract String formatValue(float value); + public MutableText getBase(Text attributeName, float value, float max, String comparison, Formatting color) { + return formatAttributeLine(Text.translatable( + "attribute.modifier." + comparison + ".0", + Text.translatable("item.unicopia.magic_staff.enchanted", formatValue(value), formatValue(max)), + attributeName + ).formatted(color)); + } + public MutableText getBase(Text attributeName, float value, String comparison, Formatting color) { return formatAttributeLine(Text.translatable("attribute.modifier." + comparison + ".0", formatValue(value), attributeName).formatted(color)); } @@ -40,7 +48,7 @@ public enum AttributeFormat { return getBase(attributeName, value, "equals", Formatting.LIGHT_PURPLE); } - public Text getRelative(Text attributeName, float baseValue, float currentValue, boolean detrimental) { + public Text getRelative(float baseValue, float currentValue, boolean detrimental) { float difference = currentValue - baseValue; return Text.literal(" (" + (difference > 0 ? "+" : "-") + formatValue(this == PERCENTAGE ? difference / baseValue : difference) + ")").formatted((detrimental ? difference : -difference) < 0 ? Formatting.DARK_GREEN : Formatting.RED); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/SpellAttribute.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/SpellAttribute.java index 0baacd85..00ba94f0 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/SpellAttribute.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/attribute/SpellAttribute.java @@ -6,6 +6,7 @@ import java.util.function.BiFunction; import java.util.function.Function; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import com.minelittlepony.unicopia.ability.magic.spell.effect.CustomisedSpellType; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; @@ -51,14 +52,29 @@ public record SpellAttribute ( } public static SpellAttribute create(SpellAttributeType id, AttributeFormat baseFormat, AttributeFormat relativeFormat, Trait trait, BiFunction valueGetter, boolean detrimental) { - return new SpellAttribute<>(trait, valueGetter, (CustomisedSpellType type, List tooltip) -> { + return createRanged(id, baseFormat, relativeFormat, trait, null, valueGetter, detrimental); + } + + public static SpellAttribute createRanged(SpellAttributeType id, AttributeFormat baseFormat, AttributeFormat relativeFormat, Trait trait, + @Nullable SpellAttribute maxValueGetter, + BiFunction valueGetter, + boolean detrimental) { + final BiFunction clampedValueGetter = maxValueGetter == null ? valueGetter : (traits, f) -> { + T t = valueGetter.apply(traits, f); + T max = maxValueGetter.get(traits); + return max.floatValue() > 0 && t.floatValue() > max.floatValue() ? max : t; + }; + return new SpellAttribute<>(trait, clampedValueGetter, (CustomisedSpellType type, List tooltip) -> { float traitAmount = type.traits().get(trait); float traitDifference = type.relativeTraits().get(trait); - float value = valueGetter.apply(type.traits(), traitAmount).floatValue(); + float max = maxValueGetter == null ? 0 : maxValueGetter.get(type.traits()).floatValue(); + float value = clampedValueGetter.apply(type.traits(), traitAmount).floatValue(); - var b = baseFormat.getBase(id.name(), value, "equals", Formatting.LIGHT_PURPLE); + var b = max > 0 + ? baseFormat.getBase(id.name(), value, max, "equals", Formatting.LIGHT_PURPLE) + : baseFormat.getBase(id.name(), value, "equals", Formatting.LIGHT_PURPLE); if (traitDifference != 0) { - tooltip.add(b.append(relativeFormat.getRelative(Text.empty(), valueGetter.apply(type.traits(), traitAmount - traitDifference).floatValue(), value, detrimental))); + tooltip.add(b.append(relativeFormat.getRelative(valueGetter.apply(type.traits(), traitAmount - traitDifference).floatValue(), value, detrimental))); tooltip.add(AttributeFormat.formatTraitDifference(trait, traitDifference)); } else { tooltip.add(b); @@ -66,6 +82,7 @@ public record SpellAttribute ( }); } + public static SpellAttribute createConditional(SpellAttributeType id, Trait trait, Float2ObjectFunction valueGetter) { return createConditional(id, trait, (traits, value) -> valueGetter.get(value.floatValue())); } diff --git a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java index 346a652f..9075457a 100644 --- a/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java +++ b/src/main/java/com/minelittlepony/unicopia/ability/magic/spell/effect/FireBoltSpell.java @@ -19,7 +19,6 @@ import net.minecraft.item.Items; import net.minecraft.nbt.NbtCompound; import net.minecraft.predicate.entity.EntityPredicates; import net.minecraft.util.hit.EntityHitResult; -import net.minecraft.util.math.MathHelper; public class FireBoltSpell extends AbstractSpell implements HomingSpell, ProjectileDelegate.ConfigurationListener, ProjectileDelegate.EntityHitListener { @@ -40,10 +39,13 @@ public class FireBoltSpell extends AbstractSpell implements HomingSpell, private static final SpellAttribute PROJECTILE_COUNT = SpellAttribute.create(SpellAttributeType.PROJECTILE_COUNT, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.EARTH, earth -> 11 + (int)earth * 3); private static final SpellAttribute FOLLOWS_TARGET = SpellAttribute.createConditional(SpellAttributeType.FOLLOWS_TARGET, Trait.FOCUS, focus -> focus >= 50); private static final SpellAttribute FOLLOW_RANGE = SpellAttribute.create(SpellAttributeType.FOLLOW_RANGE, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.FOCUS, focus -> Math.max(0F, focus - 49)); - private static final SpellAttribute MAX_EXPLOSION_STRENGTH = SpellAttribute.create(SpellAttributeType.EXPLOSION_STRENGTH, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.FOCUS, focus -> focus >= 50 ? 10F : 1F); - private static final SpellAttribute EXPLOSION_STRENGTH = SpellAttribute.create(SpellAttributeType.EXPLOSION_STRENGTH, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.POWER, (traits, focus) -> MathHelper.clamp(focus / 50, 0, MAX_EXPLOSION_STRENGTH.get(traits))); + private static final SpellAttribute EXPLOSION_STRENGTH = SpellAttribute.createRanged(SpellAttributeType.EXPLOSION_STRENGTH, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.POWER, + SpellAttribute.create(SpellAttributeType.EXPLOSION_STRENGTH, AttributeFormat.REGULAR, AttributeFormat.PERCENTAGE, Trait.FOCUS, focus -> focus >= 50 ? 10F : 1F), + (traits, power) -> Math.max(power / 50F, 0F), + false + ); - static final TooltipFactory TOOLTIP = TooltipFactory.of(MAX_EXPLOSION_STRENGTH, EXPLOSION_STRENGTH, VELOCITY, PROJECTILE_COUNT, FOLLOWS_TARGET, FOLLOW_RANGE.conditionally(FOLLOWS_TARGET::get)); + static final TooltipFactory TOOLTIP = TooltipFactory.of(EXPLOSION_STRENGTH, VELOCITY, PROJECTILE_COUNT, FOLLOWS_TARGET, FOLLOW_RANGE.conditionally(FOLLOWS_TARGET::get)); private final EntityReference target = new EntityReference<>();