A small library for loading prototypes from lua files in java.
View Code
·
Report Bug
·
Request Feature
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"
Use gradle
implementation 'io.github.benjaminwied:prototype:0.3.0'
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!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE.txt
for more information.
Project Link: https://github.com/benjaminwied/Prototype
- Othneil Drew for his README-Template.