跳到主要内容

17满护甲效果

具体代码

/**
* @author houyunfei
*/
public class ModArmorItem extends ArmorItem {
public ModArmorItem(ArmorMaterial material, Type type, Settings settings) {
super(material, type, settings);
}

private static final Map<ArmorMaterial, StatusEffectInstance> MATERIAL_STATUS_EFFECT_INSTANCE_MAP =
(new ImmutableMap.Builder<ArmorMaterial, StatusEffectInstance>())
.put(ModArmorMaterials.RUBY, new StatusEffectInstance(StatusEffects.HASTE, 400, 1,
false, false, true)).build();

/**
* 这个方法会在玩家每次更新物品栏时调用
*/
@Override
public void inventoryTick(ItemStack stack, World world, Entity entity, int slot, boolean selected) {
if (!world.isClient) {
if (entity instanceof PlayerEntity player && hasFullSuitOfArmorOn(player)) {
evaluateArmorEffects(player);
}
}
super.inventoryTick(stack, world, entity, slot, selected);
}

/**
* 评估护甲效果
*/
private void evaluateArmorEffects(PlayerEntity player) {
for (Map.Entry<ArmorMaterial, StatusEffectInstance> entry : MATERIAL_STATUS_EFFECT_INSTANCE_MAP.entrySet()) {
ArmorMaterial mapArmorMaterial = entry.getKey();
StatusEffectInstance mapStatusEffect = entry.getValue();
if (hasCorrectArmorOn(mapArmorMaterial, player)) {
addStatusEffectForMaterial(player, mapArmorMaterial, mapStatusEffect);
}
}

}

/**
* 为玩家添加护甲效果
*/
private void addStatusEffectForMaterial(PlayerEntity player, ArmorMaterial mapArmorMaterial, StatusEffectInstance mapStatusEffect) {
boolean hasPlayerEffect = player.hasStatusEffect(mapStatusEffect.getEffectType());
if (hasCorrectArmorOn(mapArmorMaterial, player) && !hasPlayerEffect) {
player.addStatusEffect(new StatusEffectInstance(mapStatusEffect));
}
}

/**
* 判断玩家是否穿戴了正确的盔甲
*/
private boolean hasCorrectArmorOn(ArmorMaterial mapArmorMaterial, PlayerEntity player) {
for (ItemStack armorStack : player.getInventory().armor) {
if (!(armorStack.getItem() instanceof ArmorItem)) {
return false;
}
}

ArmorItem boots = (ArmorItem) player.getInventory().getArmorStack(0).getItem();
ArmorItem leggings = (ArmorItem) player.getInventory().getArmorStack(1).getItem();
ArmorItem breastplate = (ArmorItem) player.getInventory().getArmorStack(2).getItem();
ArmorItem helmet = (ArmorItem) player.getInventory().getArmorStack(3).getItem();

return boots.getMaterial() == mapArmorMaterial
&& leggings.getMaterial() == mapArmorMaterial
&& breastplate.getMaterial() == mapArmorMaterial
&& helmet.getMaterial() == mapArmorMaterial;
}

/**
* 判断玩家是否有全套盔甲
*/
private boolean hasFullSuitOfArmorOn(PlayerEntity player) {
ItemStack boots = player.getInventory().getArmorStack(0);
ItemStack leggings = player.getInventory().getArmorStack(1);
ItemStack breastplate = player.getInventory().getArmorStack(2);
ItemStack helmet = player.getInventory().getArmorStack(3);

return !helmet.isEmpty() && !breastplate.isEmpty()
&& !leggings.isEmpty() && !boots.isEmpty();
}
}

ModItems:

public static final Item RUBY_HELMET = registerItem("ruby_helmet",
new ModArmorItem(ModArmorMaterials.RUBY, ArmorItem.Type.HELMET, new FabricItemSettings()));

效果

当我们穿上了全套的护甲之后,会有一个急迫的效果

image-20240824220544126