|
| 1 | +package io.github.cpearl0.ctnhcore.common.tconstruct.modifier; |
| 2 | + |
| 3 | +import io.github.cpearl0.ctnhcore.CTNHCore; |
| 4 | +import io.github.cpearl0.ctnhcore.registry.CTNHConstructModifier; |
| 5 | + |
| 6 | +import net.minecraft.nbt.CompoundTag; |
| 7 | +import net.minecraft.world.damagesource.DamageSource; |
| 8 | +import net.minecraft.world.entity.EquipmentSlot; |
| 9 | +import net.minecraft.world.entity.player.Player; |
| 10 | +import net.minecraft.world.item.ItemStack; |
| 11 | + |
| 12 | +import com.ctnhlang.CN; |
| 13 | +import com.ctnhlang.Category; |
| 14 | +import com.ctnhlang.Domain; |
| 15 | +import com.ctnhlang.EN; |
| 16 | +import com.ctnhlang.Key; |
| 17 | +import slimeknights.tconstruct.library.modifiers.Modifier; |
| 18 | +import slimeknights.tconstruct.library.modifiers.ModifierEntry; |
| 19 | +import slimeknights.tconstruct.library.modifiers.ModifierHooks; |
| 20 | +import slimeknights.tconstruct.library.modifiers.hook.armor.OnAttackedModifierHook; |
| 21 | +import slimeknights.tconstruct.library.module.ModuleHookMap; |
| 22 | +import slimeknights.tconstruct.library.tools.context.EquipmentContext; |
| 23 | +import slimeknights.tconstruct.library.tools.helper.ModifierUtil; |
| 24 | +import slimeknights.tconstruct.library.tools.nbt.IToolStackView; |
| 25 | +import tech.vixhentx.mcmod.ctnhlib.langprovider.Lang; |
| 26 | +import twilightforest.capabilities.CapabilityList; |
| 27 | + |
| 28 | +@Domain("modifier") |
| 29 | +@Category("fortification") |
| 30 | +public class FortificationModifier extends Modifier implements OnAttackedModifierHook { |
| 31 | + |
| 32 | + private static final String SHIELD_COOLDOWN = CTNHCore.MODID + ":fortification_shield_cooldown"; |
| 33 | + private static final int INITIAL_COOLDOWN = 600; |
| 34 | + |
| 35 | + @EN("Fortification") |
| 36 | + @CN("护盾") |
| 37 | + @Key("modifier.ctnhcore.fortification") |
| 38 | + static Lang name; |
| 39 | + |
| 40 | + @EN("Calls on Twilight Forest shields when hurt.") |
| 41 | + @CN("受伤时调用暮色森林护盾。") |
| 42 | + @Key("modifier.ctnhcore.fortification.description") |
| 43 | + static Lang description; |
| 44 | + |
| 45 | + @Override |
| 46 | + protected void registerHooks(ModuleHookMap.Builder hookBuilder) { |
| 47 | + hookBuilder.addHook(this, ModifierHooks.ON_ATTACKED); |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public void onAttacked(IToolStackView tool, ModifierEntry modifier, EquipmentContext context, |
| 52 | + EquipmentSlot slotType, DamageSource source, float amount, boolean isDirectDamage) { |
| 53 | + if (context.getEntity() instanceof Player player) { |
| 54 | + applyShield(player); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static void tickCooldown(Player player) { |
| 59 | + CompoundTag data = player.getPersistentData(); |
| 60 | + if (!data.contains(SHIELD_COOLDOWN)) { |
| 61 | + data.putInt(SHIELD_COOLDOWN, INITIAL_COOLDOWN); |
| 62 | + return; |
| 63 | + } |
| 64 | + int cooldown = data.getInt(SHIELD_COOLDOWN); |
| 65 | + if (cooldown > 0) { |
| 66 | + data.putInt(SHIELD_COOLDOWN, cooldown - 1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + public static void applyShield(Player player) { |
| 71 | + if (player.level().isClientSide) return; |
| 72 | + int totalLevel = getTotalLevel(player); |
| 73 | + if (totalLevel <= 0) return; |
| 74 | + |
| 75 | + CompoundTag data = player.getPersistentData(); |
| 76 | + if (data.getInt(SHIELD_COOLDOWN) > 0) return; |
| 77 | + |
| 78 | + // 迁移自 kubejs/server_scripts/src/tconstruct/modiifiers/fortification.js;直接调用暮色森林能力,替代原指令。 |
| 79 | + player.getCapability(CapabilityList.SHIELDS) |
| 80 | + .ifPresent(shields -> shields.setShields(totalLevel, true)); |
| 81 | + int cooldown = Math.max(400, (int) (100 * (1 - 0.05 * totalLevel))); |
| 82 | + data.putInt(SHIELD_COOLDOWN, cooldown); |
| 83 | + } |
| 84 | + |
| 85 | + private static int getTotalLevel(Player player) { |
| 86 | + int level = getLevel(player.getMainHandItem()) + getLevel(player.getOffhandItem()); |
| 87 | + for (EquipmentSlot slot : new EquipmentSlot[] { EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, |
| 88 | + EquipmentSlot.FEET }) { |
| 89 | + level += getLevel(player.getItemBySlot(slot)); |
| 90 | + } |
| 91 | + return level; |
| 92 | + } |
| 93 | + |
| 94 | + private static int getLevel(ItemStack stack) { |
| 95 | + if (stack.isEmpty()) return 0; |
| 96 | + return ModifierUtil.getModifierLevel(stack, CTNHConstructModifier.Ids.FORTIFICATION); |
| 97 | + } |
| 98 | +} |
0 commit comments