状态效果,又称效果,是一种可以影响实体的条件。 它们的性质可以是正面的,负面的或者中性的。 游戏本体通过许多不同的方式应用这些效果,如食物和药水等等。
命令 /effect 可被用于给实体应用效果。
自定义状态效果
在这篇教程中我们将加入一个叫 土豆 的新状态效果,它会每游戏刻给你 1 点经验。
继承 StatusEffect
让我们通过继承所有状态效果的基类 StatusEffect 来创建一个自定义状态效果类。
java
public class TaterEffect extends MobEffect {
protected TaterEffect() {
// category: StatusEffectCategory - describes if the effect is helpful (BENEFICIAL), harmful (HARMFUL) or useless (NEUTRAL)
// color: int - Color is the color assigned to the effect (in RGB)
super(MobEffectCategory.BENEFICIAL, 0xe9b8b3);
}
// Called every tick to check if the effect can be applied or not
@Override
public boolean shouldApplyEffectTickThisTick(int duration, int amplifier) {
// In our case, we just make it return true so that it applies the effect every tick
return true;
}
// Called when the effect is applied.
@Override
public boolean applyEffectTick(ServerLevel world, LivingEntity entity, int amplifier) {
if (entity instanceof Player) {
((Player) entity).giveExperiencePoints(1 << amplifier); // Higher amplifier gives you experience faster
}
return super.applyEffectTick(world, entity, amplifier);
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
注册你的自定义状态效果
与注册方块和物品类似,我们使用 Registry.register 将我们的自定义状态效果注册到 STATUS_EFFECT 注册表。 这可以在我们的模组入口点内完成。 这可以在我们的模组入口点内完成。
java
public class ExampleModEffects implements ModInitializer {
public static final Holder<MobEffect> TATER =
Registry.registerForHolder(BuiltInRegistries.MOB_EFFECT, ResourceLocation.fromNamespaceAndPath(ExampleMod.MOD_ID, "tater"), new TaterEffect());
@Override
public void onInitialize() {
// ...
}
}1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
本地化与纹理
您可以为您的状态效果指定名称并提供一个纹理(texture)图标,这将显示在玩家背包中。
纹理
状态效果图标是 18x18 的 PNG。 将您的自定义图标放在: 将您的自定义图标放在:
resources/assets/example-mod/textures/mob_effect/tater.png
翻译
像其它翻译一样,您可以添加一个 ID 格式的条目 "effect.example-mod.<effect-identifier>": "Value" 到语言文件中。





