Skip to content

Repository files navigation

Beanification

All Rights Reserved

  • This library may only be used as a dependency in software projects.
  • Modification is prohibited. (Pull Requests are allowed)
  • This library is allowed to be repackaged, unmodified, into your own binaries for distribution.
    • Repackaging as shown below with the Gradle Shadow plugin is allowed, and encouraged.
  • Redistribution of this library as a standalone product is prohibited.
  • Attribution is not required.

Version Table

MC VersionVersionTested
26.11.9.X
1.21.5 - 1.21.111.6.X
1.21.1 - 1.21.41.6.X
1.20.2 - 1.21.01.6.X

Gradle

  • ModDevGradle is recommended.
    • (version 2.0.76 is being used for this example)
  • The gradle shadow plugin is recommended.
plugins {
...
id 'net.neoforged.moddev' version "${mdg_version}"
id 'com.gradleup.shadow' version '8.3.3'
}
...
configurations {
shade
}
...
shadowJar {
archiveClassifier.set('')
configurations = [project.configurations.shade]
relocate 'tamaized.beanification', 'your.package.beanification'
}
...
repositories {
...
maven {
name 'Beanification'
url 'https://maven.tamaized.com/releases/'
}
}
...
dependencies {
...
def beanification ="tamaized:beanification:${project.beanification_version}"
implementation beanification
shade beanification
testImplementation "${beanification}:tests"
testCompileOnly "${beanification}:test-sources"
}
...
build.dependsOn shadowJar

Setup

In your main @Mod class

@Mod("modid")
publicclassYourMod {
static {
// (Optional) Configure the BeanContext, must be called before #initBeanContext.configure()
.loggingSettings().enableInjectInto();
// General SetupBeanContext.init();
}
@AutowiredprivateYourComponentyourComponent; // Injected at runtime, not actually null.@Autowired("someName")
privateYourComponentyourNamedComponent; // Will be an instance of YourExtendedComponentpublicYourMod() {
// Enables @Autowired to function with non-static fields in the main @Mod classBeanContext.injectInto(this);
// You could instead use `@Configurable` to apply this automatically, more information below.
}
@BeanprivatestaticYourComponentyourComponent() {
returnnewYourComponent();
}
@Bean("someName")
privatestaticYourComponentyourExtendedComponent() {
returnnewYourExtendedComponent();
}
}

For further details, view the javadoc for:

Enable @Configurable

ModDevGradle 2.0.X is required

A gradle plugin is required for this annotation to work as it modifies bytecode during compile time to inject BeanContext.injectInto(this) into every constructor.

It even works if the class does not have any constructor defined.

The injection happens before each RETURN (b1) (177) opcode

buildscript {
repositories{
maven {
name 'Beanification'
url 'https://maven.tamaized.com/releases/'
}
}
dependencies {
classpath "tamaized:beanification:${beanification_version}:gradle"
}
}
plugins {
...
}
apply plugin: 'tamaized.beanification'

This only works for Intellij run configs and the gradle classes (build) task! Other IDEs are unsupported at this time.

@ConfigurablepublicclassMyItemextendsItem {
@AutowiredprivateMyComponentmyComponent;
publicMyItem() {
super();
z();
// BeanContext.injectInto(this) <- compile time injection here
}
protectedMyItem(intx) {
this();
x.y();
z();
// BeanContext.injectInto(this) <- compile time injection here
}
privatevoidz() {
// Will NPE// The injection happens at the very end of constructors// If you need beans during constructors themselves, use constructor injectionmyComponent.apply();
}
publicvoidw() {
// Will not NPE// Assuming this is invoked after the object constructor has finishedmyComponent.apply();
}
}

Donations

https://ko-fi.com/tamaized

https://www.patreon.com/Tamaized

About

Spring like Dependency Injection for Modded Minecraft

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages