Skip to content

Custom Parameters (Variant Loader)

Aram edited this page Nov 22, 2024 · 7 revisions

This variant loader has been designed with Geckolib in mind; therefore, using Geckolib is recommended.


Custom Parameters

Overview

Steps

  1. Register Your Entity:

    • Follow the instructions outlined in the Getting Started section to register your entity with the Variant Loader.
  2. Create Your Entity:

    • Follow the instructions outlined in the Adding Entities section to create your entity.
  3. Have at least one Variant:

    • It’s essential to define at least one variant for your entity. Follow the Adding Variants section for guidance on setting up a variant.
    • Completing these steps is crucial for ensuring that you can add custom parameters to your entity.

Getting Started

For the steps below is assumed that you have implemented the Variant Loader and have at least 1 Variant for the Entity you want to add Custom Parameters to.

Custom parameters allow you to specify additional data for each entity variant within your JSON configuration file. These parameters can include various data types (e.g., strings, integers, booleans, arrays) to enrich your entity’s configuration.

Expanding JSON Configuration Files

You can update your JSON configuration files to define custom parameters. Each entity variant can now hold custom data values.

Example Configuration

{
  "variantOne": [
    {
      "string": "customValue1",
      "int": 1,
      "bool": true,
      "array": [
        "inputOne",
        "inputTwo"
      ],
      "somethingRandom": "It Recognises it is a String"
    }
  ]
}
  • variantOne: Refers to the variant name.
  • string, int, bool, array: These fields correspond to the parameters. Each entry under entityOne provides values for these parameters, allowing for customized behaviors or properties.

Note: Due to JSON limitations, all values are stored as strings, and arrays are represented as comma-separated strings. Parsing is recommended to convert them back to their original types when used.

Using the Custom Parameters

Without Parsing

Since all values in the JSON configuration are stored as strings, you can check for each parameter using an .equals:

if (Objects.equals(ParameterUtils.getCustomParameterForVariant(entityName,this.getVariantName(), "bool"), "true")) {
    BaseLogger.log(BaseLogLevel.INFO, "The Boolean in the Custom Parameter of this Variant is true!");
}
With Parsing

Since all values in the JSON configuration are stored as strings, you may want to convert each parameter to its intended type within your code. Here’s an example showing how to parse parameters from strings to their respective data types:

String variantName = this.getVariantName();

// Retrieve parameters
String stringParam = ParameterUtils.getCustomParameterForVariant(entityName, variantName, "string");
int intParam = Integer.parseInt(ParameterUtils.getCustomParameterForVariant(entityName, variantName, "int"));
boolean boolParam = Boolean.parseBoolean(ParameterUtils.getCustomParameterForVariant(entityName, variantName, "bool"));
String[] arrayParam = ParameterUtils.getCustomParameterForVariant(entityName, variantName, "array").split(",");

// Example usage
if (boolParam) {
    BaseLogger.log(BaseLogLevel.INFO, "The Boolean in the Custom Parameter of this Variant is true!");
}
BaseLogger.log(BaseLogLevel.INFO, "Integer value: " + intParam);
BaseLogger.log(BaseLogLevel.INFO, "Array values: " + Arrays.toString(arrayParam));

Key Points

  • Modular Configuration: Custom parameters allow modular and reusable configurations for your entity variants.
  • Enhanced Data Storage: Custom parameters let you store various data types within the JSON configuration, adding flexibility for each variant.
  • Expandability: This approach makes it easier to expand the parameters as needed, depending on the requirements of your mod or application.

For further information and advanced customization options, refer to the official Discord community where you can seek help, share experiences, and learn from other developers. You may also refer to our API Documentation.