HytaleTalk – Hytale Servers, Mods & Community Forum

Welcome to HytaleTalk — the community hub for all Hytale fans! You're currently viewing the forum as a guest. By creating a free account, you unlock full access: post your own topics, share creations, join discussions, and connect with other members through private messages. Ready to join the adventure?

Countdown until official launch!

The Forum is officially released!

Plugin Custom Configuration Files

Adding in a custom configuration file isn't a super complex process yet it can be confusing when setting default values.

The first step to creating a custom config file is to create a Java Class that will hold all the config data and builder.

Within the following code you will see a very basic config value called enabled, this value will be used to test if the config works and is a base to look on from.
the Append function works by creating a new KeyedCodec<>("path_name", Codec.TypeOfVariable) then follow along with the code to set the value of the private variable.

Java:
public class MyPluginConfig {

   // Create Private variable to store the config value
    private boolean enabled = true;
   
    // Create public get function for other classes to access
    public boolean isEnabled() {return enabled;}
    
    // Create the Builder for the config value
    // Append a new value to the Builder then use the .add() function to add it
    // After all values are added use .build() to create the file
    public static final BuilderCodec<PluginConfig> CODEC = BuilderCodec.builder(MyPluginConfig.class, MyPluginConfig::new)
            .append(new KeyedCodec<>("MOTD_Enabled", Codec.BOOLEAN),
                    (config, value, info) -> config.enabled = value, config -> config.enabled)
            .add()
            .build();
}

In your main.java class create a new variable of type Config<MyPluginConfig> this is the variable that you will access the config through in other classes.
Then within the init function create the config file load and then save it.
Java:
public final Config<PluginConfig> cfg;

public ExamplePlugin(@Nonnull JavaPluginInit init) {
    super(init);
    // withConfig("Config File Name", Config Class . CODEC
    this.cfg = this.withConfig("ExamplePlugin", PluginConfig.CODEC);
    this.cfg.load();
    this.cfg.save();
}

To then access the values in another class make a call to your main class and use
Java:
// Call to main class
private static final ExamplePlugin main = ExamplePlugin.get();

// Get the cfg file and isEnabled value and set it to a variable that can then be used
public boolean enabled = main.cfg.get().isEnabled()
 
Back
Top