Skip to content
This repository was archived by the owner on Dec 11, 2024. It is now read-only.
Syrent edited this page Jun 22, 2023 · 2 revisions

Config

The Config interface provides a standardized way to work with configuration files. It defines methods for retrieving, modifying, and saving configuration data.

Initialization

To use the Config interface, you can initialize an instance of ConfigYaml or ConfigJson.

ConfigYaml Example

import ir.syrent.origin.paper.config.Config;
import ir.syrent.origin.paper.config.Configs;
import ir.syrent.origin.paper.config.ConfigYaml;

public class ConfigExample {
    public static void main(String[] args) {
        // Initialize ConfigYaml
        Config config = ConfigYaml(File(OriginPlugin.getInstance().getDataFolder(), "config.yml"))

        // Use the Config methods
        String value = config.getString("path.to.value");
        System.out.println("Value: " + value);
    }
}

ConfigJson Example

import ir.syrent.origin.paper.config.Config;
import ir.syrent.origin.paper.config.Configs;
import ir.syrent.origin.paper.config.ConfigJson;

public class ConfigExample {
    public static void main(String[] args) {
        // Initialize ConfigJson
        Config config = ConfigJson(File(OriginPlugin.getInstance().getDataFolder(), "config.json"))

        // Use the Config methods
        String value = config.getString("path.to.value");
        System.out.println("Value: " + value);
    }
}

Usage

import ir.syrent.origin.paper.config.Config;
import ir.syrent.origin.paper.config.Configs;
import ir.syrent.origin.paper.config.ConfigYaml;

public class ConfigExample {
    public static void main(String[] args) {
        // Initialize ConfigYaml
        Config config = Configs.fromYaml("config.yml");

        // Use the Config methods

        // getFile()
        File configFile = config.getFile();
        System.out.println("Config File: " + configFile.getAbsolutePath());

        // save()
        config.save();
        System.out.println("Configuration saved.");

        // update()
        config.update();
        System.out.println("Configuration updated.");

        // reset()
        config.reset();
        System.out.println("Configuration reset.");

        // delete()
        config.delete();
        System.out.println("Configuration deleted.");

        // has(path)
        boolean hasValue = config.has("path.to.value");
        System.out.println("Has Value: " + hasValue);

        // getKeys(deep)
        List<String> keys = config.getKeys(true);
        System.out.println("Keys: " + keys);

        // get(path)
        Object value = config.get("path.to.value");
        System.out.println("Value: " + value);

        // set(path, any, replace)
        config.set("path.to.value", "New Value");
        System.out.println("Value set.");

        // getSubsection(path)
        Config subsection = config.getSubsection("path.to.subsection");
        System.out.println("Subsection: " + subsection);

        // getStringOrNull(path)
        String stringValue = config.getStringOrNull("path.to.string");
        System.out.println("String Value: " + stringValue);

        // getString(path)
        String defaultValue = config.getString("path.to.nonexistent");
        System.out.println("Default Value: " + defaultValue);

        // getIntOrNull(path)
        Integer intValue = config.getIntOrNull("path.to.integer");
        System.out.println("Integer Value: " + intValue);

        // getInt(path)
        int defaultValue = config.getInt("path.to.nonexistent");
        System.out.println("Default Integer Value: " + defaultValue);

        // getInts(path)
        List<Integer> intList = config.getInts("path.to.integers");
        System.out.println("Integer List: " + intList);

        // getDoubleOrNull(path)
        Double doubleValue = config.getDoubleOrNull("path.to.double");
        System.out.println("Double Value: " + doubleValue);

        // getDouble(path)
        double defaultDoubleValue = config.getDouble("path.to.nonexistent");
        System.out.println("Default Double Value: " + defaultDoubleValue);

        // getDoubles(path)
        List<Double> doubleList = config.getDoubles("path.to.doubles");
        System.out.println("Double List: " + doubleList);

        // getIntsOrNull(path)
        List<Integer> nullableIntList = config.getIntsOrNull("path.to.nonexistent");
        System.out.println("Nullable Integer List: " + nullableIntList);

        // getBooleanOrNull(path)
        Boolean booleanValue = config.getBooleanOrNull("path.to.boolean");
        System.out.println("Boolean Value: " + booleanValue);

        // getBoolean(path)
        boolean defaultBooleanValue = config.getBoolean("path.to.nonexistent");
        System.out.println("Default Boolean Value: " + defaultBooleanValue);

        // getStrings(path)
        List<String> stringList = config.getStrings("path.to.strings");
        System.out.println("String List: " + stringList);

        // getStringsOrNull(path)
        List<String> nullableStringList = config.getStringsOrNull("path.to.nonexistent");
        System.out.println("Nullable String List: " + nullableStringList);

        // getIntFromExpression(path, placeholders)
        int result = config.getIntFromExpression("path.to.expression", Placeholder.of("placeholder", 10));
        System.out.println("Expression Result: " + result);

        // getDoubleFromExpression(path, placeholders)
        double expressionResult = config.getDoubleFromExpression("path.to.expression", Placeholder.of("placeholder", 3.14));
        System.out.println("Expression Result: " + expressionResult);
    }
}

Clone this wiki locally