Coder Social home page Coder Social logo

liteconfig's Introduction

LiteConfig

Honestly, you could just copy/paste ConfigManager.java into your project; it's literally just a single class.

A very small and lightweight Java configuration manager.

Features

  • Smol - only one functional class (+1 interface, technically), artifact size 5 kB
  • Simple - just do a new ConfigManager( [...] ) and it handles everything for you
  • No dependencies!

How to use

  1. Create (public, optionally static) fields to hold configurable values in a class of your choice (e.g. MainClass)
public class MainClass [
  public int settingA = 4; // Default value: 4
  public booelan settingB = false; // Default value: false
  ...
  1. (optional) Create a template config file (to be embedded inside the JAR) in your resource folder (src/main/resources/ for Gradle)
  2. Initialise a new ConfigManager instance in your entrypoint
import me.av306.liteconfig.ConfigManager;
...
public class MainClass {
  ...
  private ConfigManager cm;
  ...
  public void entrypoint() {
    ...
    cm = new ConfigManager(
      "Test App", // Name of program, for logging
      Path.of( "path/to/config/file/directory/" ),
      "testapp_config.properties", // Name of config file (with extension), should be the same as the one you created in step 2
      this.getClass().getResourceAsStream( "/default_config.properties" ), // Can omit if you did step 2, otherwise pass in the InputStream from your desired file
      this.getClass(), // or MainClass.class if static context
      this // or null if static fields are used
    );
    ...
  1. The constructor handles the initial creation and reading of the config file
  2. To update configs, call ConfigManager#readConfigFile()
  3. To save configs if they were changed during runtime, call ConfigManager#saveConfigFile()

That's it!

Note

You have to provide a default config file for ConfigManager, although very technically we could create one by inferring from the fields in the configurable class, but then we'd need annotations and stuff and that's way too complex for this. If you need those, try one of the better config libraries out there, like CompleteConfig (for Minecraft).

License: MIT

liteconfig's People

Contributors

av306 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.