本次我们在模组中添加一个自定义药水效果
演示效果 演示效果 演示效果
1.在Java包中新建一个effect包 -> 包中新建一个效果类EffectMimic
EffectMimic.java
package net.joy187.joyggd.effect;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
public class EffectMimic extends StatusEffect {
public EffectMimic(StatusEffectCategory statusEffectCategory, int color) {
super(statusEffectCategory, color);
}
//实际药水效果
@Override
public void applyUpdateEffect(LivingEntity living, int pAmplifier) {
//我们需要这个效果可以让生物对玩家失去敌意,当判断到生物要攻击玩家时,把该生物的目标设置为空
if(living instanceof MobEntity)
{
if(((MobEntity) living).getTarget() instanceof PlayerEntity)
{
((MobEntity) living).setTarget(null);
}
}
}
//每隔多久药水生效一次
@Override
public boolean canApplyUpdateEffect(int pDuration, int pAmplifier) {
return pDuration % 2 == 0;
}
}
2.在init包中新建一个效果注册类EffectInit
:
EffectInit.java
package net.joy187.joyggd.init;
import net.joy187.joyggd.ModMain;
import net.joy187.joyggd.effect.EffectMimic;
import net.minecraft.entity.effect.StatusEffect;
import net.minecraft.entity.effect.StatusEffectCategory;
import net.minecraft.util.Identifier;
import net.minecraft.util.registry.Registry;
public class EffectInit {
public static StatusEffect MIMIC;
//注册我们上一步中的效果
public static StatusEffect registerStatusEffects(String name) {
return Registry.register(Registry.STATUS_EFFECT, new Identifier(ModMain.MOD_ID, name),
new EffectMimic(StatusEffectCategory.BENEFICIAL, 16777215));
}
public static void registerEffects() {
MIMIC = registerStatusEffects("mimic");
}
}
在项目主类中将该效果注册类进行注册:
ModMain.java
public class ModMain implements ModInitializer {
public static final String MOD_ID = "joyggd";
public static final Logger LOGGER = LoggerFactory.getLogger(MOD_ID);
public static final ItemGroup ITEMTAB = FabricItemGroupBuilder.build(
new Identifier(MOD_ID, "itemtab"), () -> new ItemStack(ItemInit.SHERIFF));
@Override
public void onInitialize() {
//添加该注册类
EffectInit.registerEffects();
}
}
3.在resources\assets\你的moid\textures\mob_effect
中放入我们的效果贴图:
4.进入游戏调试
我们本次的物品逻辑为使用该物品后,判定在一定范围内的敌人丧失敌意,该物品代码如下:
ItemU.java
package net.joy187.joyggd.items;
import net.joy187.joyggd.config.ModConfigs;
import net.joy187.joyggd.init.EffectInit;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.item.TooltipContext;
import net.minecraft.entity.Entity;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
import java.util.List;
import java.util.function.Predicate;
public class ItemU extends Item {
private static final Predicate<Entity> MIMIC_AREA = (entity) -> {
return entity.isAlive() && !(entity instanceof PlayerEntity);
};
public ItemU(Settings settings) {
super(settings.maxDamage(50));
}
@Override
public TypedActionResult<ItemStack> use(World level, PlayerEntity playerIn, Hand hand) {
ItemStack itemstack = playerIn.getMainHandStack();
//当物品不在cd时,可使用该物品
if(!playerIn.getItemCooldownManager().isCoolingDown(itemstack.getItem()))
{
for (LivingEntity livingentity : playerIn.world.getEntitiesByClass(LivingEntity.class, playerIn.getBoundingBox().expand(10), MIMIC_AREA)) {
if(livingentity instanceof MobEntity)
{
if(((MobEntity) livingentity).getTarget()==playerIn)
{
//给范围内的生物赋予一个效果,使其丧失敌意
livingentity.addStatusEffect(new StatusEffectInstance(EffectInit.MIMIC, 10 * 20, 0, true, true));
}
}
}
//使用过后给物品一个cd
playerIn.getItemCooldownManager().set(this,17*20);
// playerIn.getMainHandStack().damage(1, playerIn, (p_40665_) -> {
// p_40665_. .broadcastBreakEvent(playerIn.getMainHandStack());
// });
playerIn.getMainHandStack().damage(1, playerIn,
(p_40665_) -> p_40665_.sendToolBreakStatus(playerIn.getActiveHand()));
}
return TypedActionResult.success(itemstack);
}
//给物品加上物品信息
@Override
public void appendTooltip(ItemStack stack, World worldIn, List<Text> tooltip, TooltipContext flagIn) {
if(Screen.hasShiftDown()) {
tooltip.add(Text.translatable("tooltip.mimic"));
}
else tooltip.add(Text.translatable("tooltip.shift"));
}
}