Coder Social home page Coder Social logo

annotated-di's Introduction

Annotated DI

CurseForge Maven Central

Annotated DI adds Dependency Injection for Minecraft mods. It includes Guice for the basic DI system, and adds a custom annotation to allow easily setting up a DI system configured only by annotations.

Setup

To use this with your mod, include the following in build.gradle:

dependencies {
  modImplementation "dev.the-fireplace:Annotated-DI:${project.annotateddi_version}"
  annotationProcessor "dev.the-fireplace:Annotated-DI:${project.annotateddi_version}:processor"
}

And in gradle.properties:

annotateddi_version=<mod version>+<minecraft version>

After that, edit your fabric.mod.json to use di-main entrypoint and implement your main entrypoint with DIModInitializer instead of ModInitializer

Usage

A simple example of how the custom @Implementation annotation is used:

NetworkInterface.java

public interface NetworkInterface {
    ByteBuf getBuffer();
}

BufferFactory.java

@Implementation
public class BufferFactory implements NetworkInterface {
    @Override
    public ByteBuf getBuffer() {
        // do stuff
    }
}

SomeOtherFile.java (Using Constructor Injection)

public class SomeOtherFile {
    private final NetworkInterface networkInterface;
    
    @Inject
    public SomeOtherFile(NetworkInterface interface) {
        this.networkInterface = interface;
    }
    
    public void doStuff() {
        ByteBuf buffer = this.networkInterface.getBuffer();
    }
}

DiEntrypoint.java

public class DiEntrypoint implements DIModInitializer {
    public void onInitialize(Injector diContainer) {
        SomeOtherFile otherFile = diContainer.getInstance(SomeOtherFile.class);
        otherFile.doStuff();
    }
}

@Implementation annotation with multiple interfaces:

NetworkInterface.java

public interface NetworkInterface {
    ByteBuf getBuffer();
}

BufferFactory.java

@Implementation({NetworkInterface.class})
public class BufferFactory implements NetworkInterface, SomeOtherInterface {
    @Override
    public ByteBuf getBuffer() {
        // do stuff
    }
}

SomeOtherFile.java (Using Constructor Injection)

public class SomeOtherFile {
    private final NetworkInterface networkInterface;
    
    @Inject
    public SomeOtherFile(NetworkInterface interface) {
        this.networkInterface = interface;
    }
    
    public void doStuff() {
        ByteBuf buffer = this.networkInterface.getBuffer();
    }
}

annotated-di's People

Contributors

meister1593 avatar the-fireplace 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.