XFE Git
XFE Studio Git
Git 首页 全局搜索
XFE 主站 文档 NuGet

MyDimension-Lord

【我的世界】我的维度附属模组:维度之主

公开
关注 0 Fork 0 Star 0
UTF-8
package com.xfestudio.mydimensionlord.equipment;

import com.xfestudio.mydimensionlord.platform.EquipmentAttributes;
import com.xfestudio.mydimensionlord.platform.EquipmentAttributes.Bonus;
import com.xfestudio.mydimensionlord.platform.EquipmentCombat;
import com.xfestudio.mydimensionlord.registry.EquipmentItems;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.monster.Enemy;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import javax.annotation.Nullable;

public final class EquipmentPowers {
    public static final EquipmentSlot[] ARMOR_SLOTS = {EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET};
    private EquipmentPowers() {}
    @Nullable public static GearSet pieceSet(Item item) {
        for (var set : GearSet.values()) for (var slot : ARMOR_SLOTS) if (item == EquipmentItems.armorItem(set, slot)) return set;
        return null;
    }
    @Nullable public static GearSet fullSet(Player player) {
        for (var set : GearSet.values()) {
            boolean full = true;
            for (var slot : ARMOR_SLOTS) if (!player.getItemBySlot(slot).is(EquipmentItems.armorItem(set, slot))) { full = false; break; }
            if (full) return set;
        }
        return null;
    }
    public static void update(Player player) {
        GearSet set = fullSet(player);
        LordFlight.update(player);
        EquipmentAttributes.apply(player, Bonus.VITALITY, set == GearSet.LORD ? 12 : 0);
        EquipmentAttributes.apply(player, Bonus.ANCHOR, set == GearSet.LORD ? 0.30 : 0);
        EquipmentAttributes.apply(player, Bonus.POWER, set == GearSet.LORD ? 8 : 0);
        EquipmentAttributes.apply(player, Bonus.HASTE, set == GearSet.LORD ? 0.40 : 0);
        EquipmentAttributes.apply(player, Bonus.STRIDE, set == GearSet.LORD ? 0.20 : 0);
        if (player.getHealth() > player.getMaxHealth()) player.setHealth(player.getMaxHealth());
        if (!player.level().isClientSide() && player.isAlive() && !player.isSpectator()
                && EquipmentRules.repairTick(player.tickCount, player.level().dimension().location().getNamespace())) {
            repairEquipped(player);
        }
    }
    /** Held tools and worn armor only; inventory stacks never repair automatically. */
    public static int repairEquipped(Player player) {
        if (player.level().isClientSide() || !player.isAlive() || player.isSpectator()
                || !"mydimension".equals(player.level().dimension().location().getNamespace())) return 0;
        int repaired = repair(player.getMainHandItem(), "mydimension");
        for (var slot : ARMOR_SLOTS) repaired += repair(player.getItemBySlot(slot), "mydimension");
        return repaired;
    }
    public static int repair(ItemStack stack, String dimensionNamespace) {
        if ("mydimension".equals(dimensionNamespace) && stack.isDamaged()
                && (stack.is(EquipmentItems.DIMENSION_BLADE.get()) || stack.is(EquipmentItems.DIMENSION_PICKAXE.get())
                    || pieceSet(stack.getItem()) != null)) {
            stack.setDamageValue(stack.getDamageValue() - 1);
            return 1;
        }
        return 0;
    }
    public static int slash(Player player, ItemStack blade, InteractionHand hand) {
        return slash(player, blade, hand, 0);
    }
    public static int slash(Player player, ItemStack blade, InteractionHand hand, int chargeTicks) {
        if (!(player.level() instanceof ServerLevel level) || !blade.is(EquipmentItems.DIMENSION_BLADE.get())
                || hand != InteractionHand.MAIN_HAND || player.getItemInHand(hand) != blade
                || EquipmentCombat.cooling(player, blade) || !player.isAlive() || player.isSpectator()) return 0;
        double range = EquipmentRules.slashRange(chargeTicks);
        BladeCombatEffects.slash(level, player, chargeTicks);
        player.swing(hand, true);
        int hit = 0;
        for (var target : level.getEntitiesOfClass(LivingEntity.class, player.getBoundingBox().inflate(range),
                target -> target instanceof Enemy && target.isAlive() && !player.isAlliedTo(target))) {
            var offset = target.position().subtract(player.position());
            if (!EquipmentRules.withinSlash(offset.lengthSqr(), player.getLookAngle().dot(offset.normalize()), chargeTicks) || !player.hasLineOfSight(target)) continue;
            if (!EquipmentCombat.hurt(player, target, EquipmentRules.slashDamage(chargeTicks))) continue;
            hit++;
        }
        if (hit > 0) {
            EquipmentCombat.spend(player, blade, hand);
        }
        return hit;
    }
}