Aluminis Inject
一个基于依赖注入的框架模组,旨在简化 Minecraft 模组的开发。 要理解如何使用此模组,需要您具备 面向对象编程 和 依赖注入 的基础知识。
为什么选择它
在深入功能之前,让我们先谈谈为什么您可能想要使用这个模组……而不是标准的 Fabric 开发方式。Fabric 的一个不足之处在于没有自动注入:Fabric 不会将对象“注入”到您的类中。 您需要负责显式地实例化和注册所有内容。基本上,您需要创建许多不同的类,然后手动实例化它们,最后将所有内容连接在一起。这对于不需要大量连接/注册的小型模组来说没问题,但当你处理更大、更复杂的模组时……情况可能会迅速变得杂乱无章且令人困惑。
1. 手动样板代码
在 Fabric 中,你必须显式地注册每一个物品、方块、事件监听器等,没有任何抽象层。 这意味着会有更多的重复代码,尤其是当你的模组规模扩大时。
2. 扩展性痛点
小型 Fabric 模组感觉干净且简单。
大型 Fabric 模组呢?现在你要处理数十甚至数百个手动的 Registry.register() 调用、
事件注册等。如果没有中央容器或自动化系统(如 DI 容器),扩展到大型项目就需要构建自己的抽象层。
核心功能
自动依赖注入(自动布线)
自动布线是一种特性,它可以自动将依赖项注入到类中,而无需显式配置或手动布线。你不必编写配置代码来指定应注入哪个实现,该框架会根据某些规则(在本例中为注解)自动确定。
当然,如果你希望使用非基于类的提供程序,可能仍需要进行一些配置。更多信息,请参阅 Google Guice Wiki。
入门指南
首先,设置名为 aluminis-inject:bootstrap 的 Fabric 入口点,指向一个继承自
net.aluminis.inject.model.AbstractModule 的类。Aluminis Inject 将实例化此类,并使用它来自动加载你的服务。
"aluminis-inject:bootstrap": [
"net.aluminis.inject.test.module.TestModule"
]
在你的模块入口点中,务必实现 provideModId() 方法以提供你的 modId。
这可以被注入。
import com.google.inject.Provides;
import net.aluminis.inject.api.annotation.ModId;
import net.aluminis.inject.api.model.AbstractModule;
public class TestModule extends AbstractModule {
@Provides // 可选(告诉自动加载器将此值作为提供程序)
@ModId // 可选(告诉自动加载器使此值可通过 @ModId 注解访问)
public String provideModId() {
return "aluminis-inject";
}
public TestModule() {
LoggerFactory.getLogger(MOD_ID).info("TestModuleInit worked!");
}
}
创建你的服务!任何带有 @AutoLoad 注解的类都将被自动加载到模组容器中。
@AutoLoad()
public class TestService {
public TestService() {
}
}
服务可以注入其他服务或提供程序。自动布线是自动完成的!更多信息, 请参阅 Google Guice Wiki。
@AutoLoad()
public class TestService {
@Inject
public TestService(@ModId String modId) {
LoggerFactory.getLogger(modId).info("TestService worked!");
}
}
Aluminis Inject
A dependency-injection based framework mod for simplifying creation of minecraft mods. To understand how to use this mod, it is expected to have a basic understanding of Object-Oriented Programming and Dependency Injection
Why
Before we can dive into features, let's talk a little bit about why you might want to use this mod... rather than standard fabric development. A downfall of fabric, there is no automatic injection: Fabric doesn’t "inject" objects into your classes. You’re responsible for instantiating and registering everything explicitly. Basically, you make many different classes then instantiate them manually and finally wire it all together. This is fine for small mods, where not much wiring/registration is required, but when you're working on larger, more complex mods... it can become disorganized and confusing fast.
1. Manual Boilerplate
In Fabric, you have to explicitly register every single item, block, event listener, etc. with no abstraction layer. This means there is more repetitive code, especially as your mod scales up.
2. Scaling Pain
A small Fabric mod feels clean and simple.
A large Fabric mod? Now you’re wrangling dozens or hundreds of manual Registry.register() calls, event registrations, etc. Without a central container or automation system (like a DI container), scaling to big projects requires building your own abstractions.
Core Features
Automatic dependency injection (Autowiring)
Autowiring is a feature that automatically injects dependencies into a class without requiring explicit configuration or manual wiring. Instead of writing configuration code to specify which implementation should be injected, the framework automatically figures it out based on certain rules (in this case, an annotation).
Of course, some configuration may still be needed for non-class based providers, if you want to use them. For more information, view the Google Guice Wiki.
Getting Started
First, set up the fabric entrypoint named aluminis-inject:bootstrap pointing to a class that extends
net.aluminis.inject.model.AbstractModule. Aluminis inject will instantiate this class, and
use it to autoload your services.
"aluminis-inject:bootstrap": [
"net.aluminis.inject.test.module.TestModule"
]
In your module entrypoint, be sure to implement the provideModId() method, to provide your modId.
This can be injected
import com.google.inject.Provides;
import net.aluminis.inject.api.annotation.ModId;
import net.aluminis.inject.api.model.AbstractModule;
public class TestModule extends AbstractModule {
@Provides // Optional (Tells autoloader to make this value a provider)
@ModId // Optional (Tells autoloader to make this value available with the annotation @ModId)
public String provideModId() {
return "aluminis-inject";
}
public TestModule() {
LoggerFactory.getLogger(MOD_ID).info("TestModuleInit worked!");
}
}
Make your services! Any class with the @AutoLoad annotation will be automatically loaded to the mod container.
@AutoLoad()
public class TestService {
public TestService() {
}
}
Services can be injected with other services, or providers too. Autowiring is done automatically! For more information, view the Google Guice Wiki
@AutoLoad()
public class TestService {
@Inject
public TestService(@ModId String modId) {
LoggerFactory.getLogger(modId).info("TestService worked!");
}
}
下载 Aluminis Inject
下载 Aluminis Inject
下载 Aluminis Inject
下载 Aluminis Inject
下载 Aluminis Inject
下载 Aluminis Inject
为按键绑定和事件监听器添加了抽象层
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
一个用于收集生物掉落物的多区块模组。
模组
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
模组
在游戏画面角落显示附近世界地形、玩家、生物和实体的地图。可创建路径点,帮助你找到标记过的位置。
模组
查看物品与配方
模组
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
模组
AE2:一款流行的自动化与存储模组
评论 0
登录后参与评分
还没有评论,来抢沙发吧