Record Class DiminishingReturnsValue

java.lang.Object
java.lang.Record
johnsmith.enchantmentcore.api.enchantment.value.DiminishingReturnsValue
Record Components:
base - The initial value added at the first level (Level 1).
decrement - The amount by which the value added is reduced for each subsequent level.
minimum - The floor value. The added value per level will never drop below this amount.
All Implemented Interfaces:
net.minecraft.world.item.enchantment.LevelBasedValue

public record DiminishingReturnsValue(float base, float decrement, float minimum) extends Record implements net.minecraft.world.item.enchantment.LevelBasedValue
A specialized implementation of LevelBasedValue that calculates a total value based on an arithmetic progression where the increment decreases (diminishes) for each subsequent level until a defined minimum value is reached (the "floor").

The value added at level $N$ is calculated as: $\max(\text{base} - (N - 1) \cdot \text{decrement}, \text{minimum})$. The final result is the sum of these diminishing terms up to the given level.

  • Nested Class Summary

    Nested classes/interfaces inherited from interface net.minecraft.world.item.enchantment.LevelBasedValue

    net.minecraft.world.item.enchantment.LevelBasedValue.Clamped, net.minecraft.world.item.enchantment.LevelBasedValue.Constant, net.minecraft.world.item.enchantment.LevelBasedValue.Fraction, net.minecraft.world.item.enchantment.LevelBasedValue.LevelsSquared, net.minecraft.world.item.enchantment.LevelBasedValue.Linear, net.minecraft.world.item.enchantment.LevelBasedValue.Lookup
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final com.mojang.serialization.MapCodec<DiminishingReturnsValue>
    The codec responsible for serializing and deserializing instances of this record from JSON.
    static final String
    Registry path identifier for this level based value.

    Fields inherited from interface net.minecraft.world.item.enchantment.LevelBasedValue

    DISPATCH_CODEC
  • Constructor Summary

    Constructors
    Constructor
    Description
    DiminishingReturnsValue(float base, float decrement, float minimum)
    Creates an instance of a DiminishingReturnsValue record class.
  • Method Summary

    Modifier and Type
    Method
    Description
    float
    Returns the value of the base record component.
    float
    calculate(int level)
    Calculates the cumulative total value up to the given enchantment level using diminishing returns.
    @NotNull com.mojang.serialization.MapCodec<? extends net.minecraft.world.item.enchantment.LevelBasedValue>
    float
    Returns the value of the decrement record component.
    final boolean
    Indicates whether some other object is "equal to" this one.
    final int
    Returns a hash code value for this object.
    float
    Returns the value of the minimum record component.
    final String
    Returns a string representation of this record class.

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • KEY

      public static final String KEY
      Registry path identifier for this level based value.
      See Also:
    • CODEC

      public static final com.mojang.serialization.MapCodec<DiminishingReturnsValue> CODEC
      The codec responsible for serializing and deserializing instances of this record from JSON.
  • Constructor Details

    • DiminishingReturnsValue

      public DiminishingReturnsValue(float base, float decrement, float minimum)
      Creates an instance of a DiminishingReturnsValue record class.
      Parameters:
      base - the value for the base record component
      decrement - the value for the decrement record component
      minimum - the value for the minimum record component
  • Method Details

    • calculate

      public float calculate(int level)
      Calculates the cumulative total value up to the given enchantment level using diminishing returns.

      For efficiency, this method uses the closed-form formula for the sum of an arithmetic progression for the initial terms (where reduction occurs) and then adds the constant floor value for any remaining terms. If the decrement is zero or negative, it falls back to an iterative calculation to ensure correct non-diminishing behavior.

      Specified by:
      calculate in interface net.minecraft.world.item.enchantment.LevelBasedValue
      Parameters:
      level - The enchantment level for which to calculate the cumulative value.
      Returns:
      The total float value. Returns 0 if level <= 0.
    • codec

      @NotNull public @NotNull com.mojang.serialization.MapCodec<? extends net.minecraft.world.item.enchantment.LevelBasedValue> codec()
      Specified by:
      codec in interface net.minecraft.world.item.enchantment.LevelBasedValue
      Returns:
      The MapCodec for this class.
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with '=='.
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • base

      public float base()
      Returns the value of the base record component.
      Returns:
      the value of the base record component
    • decrement

      public float decrement()
      Returns the value of the decrement record component.
      Returns:
      the value of the decrement record component
    • minimum

      public float minimum()
      Returns the value of the minimum record component.
      Returns:
      the value of the minimum record component