通知
你通常不需要单独下载这个模组,因为依赖它的模组通常会将其嵌入到自身内部。此页面主要用于提供教程链接以及查看最新版本
Consumable Chemicals 是一个库模组,提供了向游戏中添加可被玩家追踪的化学物质的功能
额外功能
/chemicals命令用于设置、获取和更改玩家的化学物质水平
Forge 版本?
没有,我没有时间、注意力和动力去学习一个全新的加载器
整合包?
完全没问题,只要你注明我的名字并链接回此模组在 Modrinth 或 GitHub 上的页面即可。
用法
Gradle 配置
build.gradle
repositories {
maven { url = "https://pluto-mod-maven.web.app/maven" }
}
dependencies {
include(modImplementation("ml.pluto7073:chemicals:${minecraft_version}+${chemicals_version}"))
}
gradle.properties
chemicals_version=SEE_MODRINTH_FOR_VERSIONS
创建 ChemicalHandler
在创建化学处理器时,你可以通过扩展 ConsumableChemicalHandler 从零开始创建自己的处理器,或者从下面三个预制的抽象处理器中选择一个:
HalfLifeChemicalHandler
使用构造函数中指定的半衰期(以刻为单位),通过指数衰减的方式减少玩家体内的化学物质数量
LinearChemicalHandler
每个刻从玩家体内移除特定数量的化学物质,直到数量降为零
StaticChemicalHandler
任何被消耗的化学物质都会保留在玩家体内,直到达到最大数量(如果指定了的话)或者玩家死亡。 当达到此最大数量时,可以定义自定义操作
示例
在此示例中,我们将使用 HalfLifeChemicalHandler
假设我们希望我们的化学物质具有 5 分钟的半衰期,即 6000 刻。这将得到以下代码
public class ExampleHandler extends HalfLifeChemicalHandler {
public ExampleHandler() {
super(6000); // 5分钟 * 60秒/分 * 20刻/秒
}
}
如果你使用的是预制的抽象处理器,那么只需要实现一个方法:getEffectsForAmount()
(如果你扩展的是 ConsumableChemicalHandler,那么你还需要实现 tickPlayer(),该方法应在每个刻处理化学物质,通常情况下每次移除少量物质直到耗尽)
该方法接收表示数量的 float 参数和当前的 Level。它应该返回一个 Mob Effect 实例列表(例如隐身效果),拥有该数量化学物质的玩家应具有这些效果。
在此示例中,假设我们希望当玩家拥有超过 10 单位的这种化学物质时获得迅捷 I 效果,而当拥有超过 50 单位时获得缓速 III 效果。
public class ExampleHandler extends HalfLifeChemicalHandler {
[...]
@Override
public Collection<MobEffectInstance> getEffectsForAmount(float amount, Level level) {
ArrayList<MobEffectInstance> effects = new ArrayList<>();
if (amount >= 10) {
effects.add(new MobEffectInstance(MobEffects.DIG_SPEED, 600));
}
if (amount >= 50) {
effects.add(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 600, 2));
}
return effects;
}
}
注册 ChemicalHandler
一旦你的处理器类创建完成,就需要对其进行注册。
这可以通过 Registry.register(Chemicals.REGISTRY, ID, HANDLER) 来完成,并且应该在模组初始化时进行,就像其他任何注册一样。
示例
ExampleHandler.java
public class ExampleHandler extends HalfLifeChemicalHandler {
public static final ExampleHandler INSTANCE = new ExampleHandler();
[...]
public static void init() {
Registry.register(Chemicals.REGISTRY, new ResourceLocation("modid", "example_chemical"), INSTANCE);
}
}
ExampleMod.java
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
ExampleHandler.init();
}
}
将化学物质添加到食物中
你已经将化学物质添加到了游戏中!你可以使用 /chemicals 命令来编辑玩家的化学物质水平以进行测试,但现在你需要一种在游戏内消耗该化学物质的方法。最简单的方法是将其添加到 FoodProperties 实例中。
FoodProperties 构建器中添加了一个新方法,用于将化学物质添加到食物中。该方法接收代表化学物质(或 ChemicalHandler 本身)的 ResourceLocation 以及数量。
ExampleItems.java
public class ExampleItems {
public static final FoodProperties CHEMICAL_FOOD = new FoodProperties.Builder()
.alwaysEat()
.fast()
.addChemical(ExampleChemical.INSTANCE, 5f)
.build();
public static final Item CHEMICAL_PILL = new Item(new Item.Properties().food(CHEMICAL_FOOD));
public static void init() {
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation("modid", "chemical_pill"), CHEMICAL_PILL);
}
}
ExampleMod.java
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
[...]
ExampleItems.init(); // 物品应在化学物质之后注册
}
}
在 1.20.5+ 中将化学物质添加到食物中
在 1.20.5+ 版本中,食物数据由物品组件(Item Components)管理。化学物质现在也通过 ChemicalMap.COMPONENT_TYPE 组件添加到物品中,其 ID 为 chemicals:chemicals。
如果你想在游戏中向现有的食物或可饮用物品(如药水)添加化学物质,可以使用简单的命令来实现。
/give @s minecraft:cooked_beef[chemicals:chemicals={"modid:example_chemical":100}]
上述命令给予我们一块牛排,其中包含上面示例中的 100 单位化学物质,这在食用后会被添加到玩家体内。
化学药丸示例
如果你希望模组中的自定义物品像上面 1.20.4 及更早版本的示例那样,默认包含一定量的化学物质,你可以在注册该物品时使用 ChemicalMap.COMPONENT_TYPE 组件和 ChemicalMap.Builder。
public static final FoodProperties CHEMICAL_FOOD = new FoodProperties.Builder()
.alwaysEat().fast().build(); // 不包含化学物质的食物数据
public static final Item CHEMICAL_PILL = new Item(new Item.Properties().food(CHEMICAL_FOOD)
.component(ChemicalMap.COMPONENT_TYPE, new ChemicalMap.Builder().add(EXAMPLE_CHEMICAL, 10).build()));
public static void init() {
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation("modid", "chemical_pill"), CHEMICAL_PILL);
}
此示例将为我们创建一个默认的含有 10 单位示例化学物质的化学药丸物品。
更深入的化学物品实现
如果你的物品可以包含可变数量的化学物质(例如使用 NBT 数据存储),最好在物品的类中实现 ChemicalContaining 接口。该接口包含一个方法 getChemicalContent(),它接收化学物质的 ResourceLocation 和 ItemStack 作为参数。该方法应计算当前物品栈中指定化学物质的数量并返回。
在此示例中,该物品中的所有化学物质都存储在物品 NBT 的 Chemicals 对象中。
ExampleItem.java
public class ExampleItem extends Item implements ChemicalContaining {
public ExampleItem(Properties properties) {
super(properties);
}
@Override
public float getChemicalContent(ResourceLocation id, ItemStack stack) {
CompoundTag chemicals = stack.getOrCreateTagElement("Chemicals");
if (chemicals.contains(id.toString())) {
return chemicals.getFloat(id.toString());
}
return 0;
}
}
NOTICE
You shouldn't have to download this mod individually, as mods that depend on it are meant to embed it within the mod itself. This page is mainly here for links to tutorials and to see most recent versions
Consumable Chemicals is a library mod that provides functionality for adding chemicals to the game that can be tracked in the player
Additional Features
/chemicalscommand to set, get, and change a player's chemical levels
Forge Version?
No, I lack the time, attention span, and motivation to learn an entirely new modloader
Modpacks?
Completely fine as long as you credit me and link back to the Modrinth or GitHub page for this mod.
Usage
Gradle Configuration
build.gradle
repositories {
maven { url = "https://pluto-mod-maven.web.app/maven" }
}
dependencies {
include(modImplementation("ml.pluto7073:chemicals:${minecraft_version}+${chemicals_version}"))
}
gradle.properties
chemicals_version=SEE_MODRINTH_FOR_VERSIONS
Creating a ChemicalHandler
When creating a Chemical Handler, you can either create your own from scratch by extending ConsumableChemicalHandler or choose
from one of the three pre-made abstract handlers below:
HalfLifeChemicalHandler
The amount of the chemical is exponentially ticked down in the player using a half life in ticks, specified in the constructor
LinearChemicalHandler
A specific amount of the chemical is removed from the player each tick, until the amount reaches zero
StaticChemicalHandler
Any chemical consumed remains in the player, until a maximum amount is reached (if specified) or the player dies. A custom action can be defined for when this maximum amount is hit
Example
For this example we will use HalfLifeChemicalHandler
Let's say we want our chemical to have a half-life of 5 minutes, or 6000 ticks. This would get us the below code
public class ExampleHandler extends HalfLifeChemicalHandler {
public ExampleHandler() {
super(6000); // 5 minutes * 60 sec/min * 20 ticks/sec
}
}
If you are using a pre-made abstract handler, then there's only one method to implement. getEffectsForAmount()
(If you are extending ConsumableChemicalHandler, then you will also need to implement tickPlayer(), which should process
the chemical each tick, most of the time removing a small amount until there's none left)
This takes in a float of the amount and the current Level. This should return a list of Mob Effect Instances (e.g. Invisibility)
that a player with that amount of the chemical should have.
For this example let's say we want the player to have Haste I if they have over 10 of this chemical and Slowness III if they have over 50.
public class ExampleHandler extends HalfLifeChemicalHandler {
[...]
@Override
public Collection<MobEffectInstance> getEffectsForAmount(float amount, Level level) {
ArrayList<MobEffectInstance> effects = new ArrayList<>();
if (amount >= 10) {
effects.add(new MobEffectInstance(MobEffects.DIG_SPEED, 600));
}
if (amount >= 50) {
effects.add(new MobEffectInstance(MobEffects.MOVEMENT_SLOWDOWN, 600, 2));
}
return effects;
}
}
Registering the ChemicalHandler
Once your handler class is made, it needs to be registered.
This can be done using Registry.register(Chemicals.REGISTRY, ID, HANDLER) and should be done upon mod initialization, like any
other registration being done.
Example
ExampleHandler.java
public class ExampleHandler extends HalfLifeChemicalHandler {
public static final ExampleHandler INSTANCE = new ExampleHandler();
[...]
public static void init() {
Registry.register(Chemicals.REGISTRY, new ResourceLocation("modid", "example_chemical"), INSTANCE);
}
}
ExampleMod.java
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
ExampleHandler.init();
}
}
Adding Chemicals to Foods
You have just added your chemical to the game! You can use the /chemicals command to edit a player's chemical levels to test it out,
but now you need an in-game way to consume your chemical. The simplest way to do this is to add it to a FoodProperties instance.
A new method has been added to the FoodProperties builder to add a chemical to the food. This takes in a ResourceLocation representing
the chemical (or the ChemicalHandler itself) and the amount.
ExampleItems.java
public class ExampleItems {
public static final FoodProperties CHEMICAL_FOOD = new FoodProperties.Builder()
.alwaysEat()
.fast()
.addChemical(ExampleChemical.INSTANCE, 5f)
.build();
public static final Item CHEMICAL_PILL = new Item(new Item.Properties().food(CHEMICAL_FOOD));
public static void init() {
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation("modid", "chemical_pill"), CHEMICAL_PILL);
}
}
ExampleMod.java
public class ExampleMod implements ModInitializer {
@Override
public void onInitialize() {
[...]
ExampleItems.init(); // Items should be registered after chemicals
}
}
Adding Chemicals to Foods in 1.20.5+
In 1.20.5+, Food data is managed by Item Components. Chemicals now too are added to items via the ChemicalMap.COMPONENT_TYPE component, with the ID of chemicals:chemicals.
If you want to add chemicals to an existing food or drinkable item like a potion in game, this can be done with a simple command.
/give @s minecraft:cooked_beef[chemicals:chemicals={"modid:example_chemical":100}]
The above command gives us a piece of Steak with 100 units of our example chemical from above, which will be added to the player upon consuming.
Chemical Pill Example
If you want a custom item in a mod to have a default amount of the chemical like the above example for 1.20.4 and before, you can use the ChemicalMap.COMPONENT_TYPE component and ChemicalMap.Builder when registering the item.
public static final FoodProperties CHEMICAL_FOOD = new FoodProperties.Builder()
.alwaysEat().fast().build(); // Our food data without the chemicals
public static final Item CHEMICAL_PILL = new Item(new Item.Properties().food(CHEMICAL_FOOD)
.component(ChemicalMap.COMPONENT_TYPE, new ChemicalMap.Builder().add(EXAMPLE_CHEMICAL, 10).build()));
public static void init() {
Registry.register(BuiltInRegistries.ITEM, new ResourceLocation("modid", "chemical_pill"), CHEMICAL_PILL);
}
This example gets us a chemical pill item with 10 units of our example chemical by default.
More In-Depth Chemical Items
If you have an item that can have a variable amount of a chemical in it, using NBT data for example, it is best
to implement ChemicalContaining in your Item Class. This has one method, getChemicalContent(), which takes
the ResourceLocation of the chemical and the ItemStack. This should calculate the amount of the specified chemical
in this stack and return it.
In this example, any chemicals in this item are stored in the Chemicals object in the item's NBT
ExampleItem.java
public class ExampleItem extends Item implements ChemicalContaining {
public ExampleItem(Properties properties) {
super(properties);
}
@Override
public float getChemicalContent(ResourceLocation id, ItemStack stack) {
CompoundTag chemicals = stack.getOrCreateTagElement("Chemicals");
if (chemicals.contains(id.toString())) {
return chemicals.getFloat(id.toString());
}
return 0;
}
}
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
下载 ConsumableChemicals
需要联机服务器吗?
稳定流畅、一键部署,几分钟即可和朋友联机开服
最低仅需 8 元/月 即可畅快联机
更新内容
ChemicalContaining.getConsumedChemicalContent现在接受 Player 作为参数
关于这个资源的讨论、问题反馈与使用心得
还没有讨论,来发第一帖吧
你是这个资源的作者?登录后可认领此资源。
相似资源
模组
一个用于收集生物掉落物的多区块模组。
模组
显示你正在注视的方块的信息。(适用于 Minecraft 1.16+ 的 Hwyla/Waila 分支)
模组
查看物品与配方
模组
使用 JavaScript 编辑配方、添加自定义物品、编写世界事件!
模组
适用于 MC 1.18+ 的 Effekseer 加载器
模组
那是什么?一个 Waila/Hwyla 的分支。
评论 0
登录后参与评分
还没有评论,来抢沙发吧