java.lang.Object
org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers

public class ValueHelpers extends Object
A collection of helpers for variables, values and value types.
  • Constructor Details

    • ValueHelpers

      public ValueHelpers()
  • Method Details

    • from

      public static IValueType[] from(IVariable... variables)
      Create a new value type array from the given variable array element-wise. If a variable would be null, that corresponding value type would be null as well.
      Parameters:
      variables - The variables.
      Returns:
      The value types array corresponding element-wise to the variables array.
    • from

      public static IValueType<?>[] from(IVariableFacade... variableFacades)
      Create a new value type array from the given variableFacades array element-wise. If a variableFacade would be null, that corresponding value type would be null as well.
      Parameters:
      variableFacades - The variables facades.
      Returns:
      The value types array corresponding element-wise to the variables array.
    • from

      public static net.minecraft.network.chat.Component[] from(IValueType<?>... valueTypes)
      Create a new unlocalized name array from the given variableFacades array element-wise.
      Parameters:
      valueTypes - The value types.
      Returns:
      The unlocalized names array corresponding element-wise to the value types array.
    • areValuesEqual

      public static boolean areValuesEqual(@Nullable IValue v1, @Nullable IValue v2)
      Check if the two given values are equal. If they are both null, they are also considered equal.
      Parameters:
      v1 - Value one
      v2 - Value two
      Returns:
      If they are equal.
    • correspondsTo

      public static boolean correspondsTo(IValueType t1, IValueType t2)
      Bidirectional checking of correspondence.
      Parameters:
      t1 - First type.
      t2 - Second type.
      Returns:
      If they correspond to each other in some direction.
    • correspondsTo

      public static boolean correspondsTo(IVariable<?> variable, IValueType type)
      Check if the variable corresponds to the given type.
      Parameters:
      variable - Variable.
      type - First type.
      Returns:
      If they correspond to each.
    • evaluateOperator

      public static IValue evaluateOperator(IOperator operator, IValue... values) throws EvaluationException
      Evaluate an operator for the given values.
      Parameters:
      operator - The operator.
      values - The values.
      Returns:
      The resulting value.
      Throws:
      EvaluationException - If something went wrong during operator evaluation.
    • evaluateOperator

      public static IValue evaluateOperator(IOperator operator, IVariable... variables) throws EvaluationException
      Evaluate an operator for the given variables.
      Parameters:
      operator - The operator.
      variables - The variables.
      Returns:
      The resulting value.
      Throws:
      EvaluationException - If something went wrong during operator evaluation.
    • serializeRaw

      public static net.minecraft.nbt.Tag serializeRaw(ValueDeseralizationContext valueDeseralizationContext, IValue value)
      Serialize the given value to a raw tag without its value type.
      Parameters:
      valueDeseralizationContext - The deserialization context.
      value - The value.
      Returns:
      The NBT tag.
    • serialize

      public static net.minecraft.nbt.CompoundTag serialize(ValueDeseralizationContext valueDeseralizationContext, IValue value)
      Serialize the given value to NBT.
      Parameters:
      valueDeseralizationContext - The deserialization context.
      value - The value.
      Returns:
      The NBT tag.
    • deserialize

      public static IValue deserialize(ValueDeseralizationContext valueDeseralizationContext, net.minecraft.nbt.CompoundTag tag)
      Deserialize the given NBT tag to a value.
      Parameters:
      valueDeseralizationContext - The deserialization context.
      tag - The NBT tag containing a value.
      Returns:
      The value.
    • deserializeRaw

      public static <T extends IValue> T deserializeRaw(ValueDeseralizationContext valueDeseralizationContext, IValueType<T> valueType, net.minecraft.nbt.Tag valueString)
      Deserialize the given value string to a value.
      Type Parameters:
      T - The type of value.
      Parameters:
      valueDeseralizationContext - The deserialization context.
      valueType - The value type to deserialize for.
      valueString - The value tag.
      Returns:
      The value.
    • toString

      public static <T extends IValue> String toString(T value)
      Get the string representation of the given value. This is useful for cases when the value needs to be edited in a GUI. This corresponds to parseString(IValueType, String).
      Type Parameters:
      T - The value type.
      Parameters:
      value - A value.
      Returns:
      A string representation of the given value.
    • parseString

      public static <T extends IValue> T parseString(IValueType<T> valueType, String value) throws EvaluationException
      Parse the given string representation of a value. This corresponds to toString(IValue).
      Type Parameters:
      T - The value type.
      Parameters:
      valueType - The value type to parse by.
      value - A string representation of a value.
      Returns:
      A value.
      Throws:
      EvaluationException - If parsing failed.
    • validatePredicateOutput

      public static void validatePredicateOutput(IOperator predicate, IValue result) throws EvaluationException
      Check if the given result (from the given operator) is a boolean.
      Parameters:
      predicate - A predicate, used for error logging.
      result - A result from the given predicate
      Throws:
      EvaluationException - If the value was not a boolean.
    • getSafeReadableValue

      public static org.apache.commons.lang3.tuple.Pair<net.minecraft.network.chat.MutableComponent,Integer> getSafeReadableValue(@Nullable IVariable variable)
      Get the human readable value of the given value in a safe way.
      Parameters:
      variable - A nullable variable.
      Returns:
      A pair of a string and color.
    • createResourceLocationInEvaluation

      public static net.minecraft.resources.ResourceLocation createResourceLocationInEvaluation(String value) throws EvaluationException
      Create a ResourceLocation from the given value. Any ResourceLocationExceptions will be emitted as EvaluationException.
      Parameters:
      value - A ResourceLocation value.
      Returns:
      A ResourceLocation
      Throws:
      EvaluationException - If a ResourceLocationException was thrown.
    • variableUnpackAnyType

      public static <V extends IValue, C extends IValueType<V>> C variableUnpackAnyType(IVariable variable, IOperator operator, IValueTypeCategory<V> category, Class<? super C> categoryClazz) throws EvaluationException
      If the given variable has type ANY, attempt to cast the type to the given category type, or throw.
      Type Parameters:
      V - The value type.
      C - The category type.
      Parameters:
      variable - The variable.
      operator - An operator to include in the error message.
      category - The category to check.
      categoryClazz - The category class.
      Returns:
      The cast value type.
      Throws:
      EvaluationException - If casting failed.