Skip to content

benjaminwied/Prototype

Repository files navigation

Contributors Forks Stargazers Issues GPL License


Prototype

A small library for loading prototypes from lua files in java.

View Code · Report Bug · Request Feature

Built With

Usage

Prototypes can be used to build various objects from properties. Fist, create your desired type just as a normal java class:

public class MyClass {
  ...
  public MyClass(String a, boolean b, long createTime) {
    ...
  }
}

Then write your prototype class (this will be deserialized usin Jackson):

public class MyPrototype extends Prototype<MyClass> {
    public String a;
    public boolean b;
    
    @Override
    public MyClass build() {
        return new MyClass(a, b, System.currentTimeMillis());
    }
}

Define the values in lua:

prototypes["examplePrototype"] = {
    class = "com.example.MyPrototype",
    data = {
        a = "abc",
        b = true
    }
}

To load and use prototypes, do the following:

Path rootPath = Path.of("path/to/your/prototypes");
Map<String, Object> context = Map.of("exampleKey", "42");
PrototypeManager.loadPrototypes(rootPath, context);

// optional: verify integrity
IntegrityChecker.verifyIntegrity();

// get a prototype
Prototype<MyClass, ? extends Prototype<MyClass>> prototype = PrototypeManager.getPrototype("examplePrototype");

// build the type
MyClass myObject = prototype.build();

// or, if you only need the type
myObject = PrototypeManager.createType("examplePrototype");

Prototypes can use the context to inject additional data.

local valA = exampleKey;

prototypes["anotherPrototype"] = {
    class = "com.example.MyPrototype",
    data = {
        a = valA,
        b = false
    }
}

-- Using the above loading code, "a" would have be "42"

Installation

Use gradle

implementation 'io.github.benjaminwied:prototype:0.3.0'

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

Distributed under the MIT License. See LICENSE.txt for more information.

Contact

Project Link: https://github.com/benjaminwied/Prototype

Acknowledgments

About

Prototype is a library for defining and loading prototypes from lua files.

Resources

License

Stars

Watchers

Forks

Languages