今天我们在1.16.5的模组中尝试实现几种特殊的buff效果
1.在开发包中新建effects包 -> effects包中新建一个BsaeEffect类作为我们效果的基类:
BaseEffect.java
public class BaseEffect extends Effect {
private boolean instant;
private boolean isRegistered = false;
public BaseEffect(EffectType type, int color, boolean isInstant) {
super(type, color);
this.instant = isInstant;
}
public boolean isInstantenous() {
return false;
}
@Override
public boolean isDurationEffectTick(int remainingTicks, int level) {
if (isInstantenous()) {
return true;
}
return canApplyEffect(remainingTicks, level);
}
protected boolean canApplyEffect(int remainingTicks, int level) {
if (!isInstantenous()) {
//Log.w("Non instant effects should override canApplyEffect!");
Thread.dumpStack();
}
return false;
}
@Override
public void applyEffectTick(LivingEntity entity, int amplifier) {
if (isInstantenous()) {
applyInstantenousEffect(null, null, entity, amplifier, 1.0d);
}
}
public BaseEffect onRegister() {
isRegistered = true;
return this;
}
public boolean isRegistered() {
return isRegistered;
}
}
2.在effects包中定义一个我们的效果类EffectErosion,让其继承我们的效果基类:
EffectErosion.java
package com.joy187.re8joymod.common.effects;
import net.minecraft.entity.LivingEntity;
import net.minecraft.inventory.EquipmentSlotType;
import net.minecraft.item.ItemStack;
import net.minecraft.potion.EffectType;
import java.util.Random;
public class EffectErosion extends BaseEffect {
public EffectErosion(EffectType type, int color, boolean isInstant) {
super(type, color, isInstant);
}
@Override
protected boolean canApplyEffect(int remainingTicks, int level) {
return remainingTicks % 7 == 0;
}
//这个是buff在身上持续作用的效果函数
@Override
public void applyEffectTick(LivingEntity living, int amplified) {
amplified ++;
Random ran = new Random();
int co = ran.nextInt(5);
for (EquipmentSlotType slot: EquipmentSlotType.values()) {
DamageItemInSlot(slot, living, co*amplified);
}
}
//我们的效果是要让装备盔甲值快速减少,所以增加一个Damage函数
public void DamageItemInSlot(EquipmentSlotType slot, LivingEntity livingBase, int amount)
{
ItemStack stack = livingBase.getItemBySlot(slot);
stack.hurtAndBreak(1, livingBase, (p_220287_1_) -> {
p_220287_1_.broadcastBreakEvent(slot);
});
}
//声明buff是好buff还是debuff
@Override
public boolean isBeneficial() {
return false;
}
}
3.在init包中新建一个ModEffects类,用于将我们所有的效果进行注册:
ModEffects.java
package com.joy187.re8joymod.common.init;
import com.joy187.re8joymod.common.effects.EffectDeadly;
import com.joy187.re8joymod.common.effects.EffectErosion;
import net.minecraft.potion.Effect;
import net.minecraft.potion.EffectType;
import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
public class ModEffects {
public static final DeferredRegister<Effect> EFFECTS = DeferredRegister.create(ForgeRegistries.POTIONS,"re8joymod");
//buff1
public static RegistryObject<Effect> EROSION = EFFECTS.register("erosion",()->
{
//3个参数:药水类型(好/坏/中)、药水粒子颜色、是否为即时效果(类似于瞬间伤害效果)
return new EffectErosion(EffectType.HARMFUL, 0x660033, false);
});
//buff2
//public static RegistryObject<Effect> DEADLY = EFFECTS.register("deadly",()->
//{
// return new EffectDeadly(EffectType.NEUTRAL, 0x333333, false);
//});
//buff3...
}
在Main类中将我们的ModEffects加入到初始化函数中:
Main.java
中的Main()函数
public Main(){
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
bus.addListener(this::setup);
bus.addListener(this::doClientStuff);
SoundInit.SOUND_TYPES.register(bus);
EntityInit.ENTITY_TYPES.register(bus);
ModItems.ITEMS.register(bus);
ModBlocks.BLOCKS.register(bus);
//将我们的buff效果在bus中注册
ModEffects.EFFECTS.register(bus);
ModTileEntities.register(bus);
ModContainers.register(bus);
ModStructures.register(bus);
SurfaceBuilderInit.SURFACE_BUILDERS.register(bus);
BiomeInit.BIOMES.register(bus);
BiomeInit.registerBiomes();
ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON,SPEC,"re8joymod-common.toml");
MinecraftForge.EVENT_BUS.register(this);
}
4.在resources包中的lang文件中添加我们药水的名称
en_us.json
中添加:
"effect.re8joymod.erosion": "Erosion Biohazard",
在textures/mob_effect中添加我们所有buff的贴图,名称与第3步中的效果注册名称相同
5.保存项目 -> 启动游戏调试
我们可以用指令来给予自己的buff效果:
/effect give @p modid:buff名称
以/effect give @p re8joymod:erosion
为例: