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.MyDimension;
import com.xfestudio.mydimensionlord.registry.EquipmentItems;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.tags.TagKey;
import net.minecraft.world.entity.item.ItemEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import java.util.*;

/** Extra blocks use the player's normal game mode, including every protection/loot hook. */
public final class PickaxePowers {
    public static final int DURABILITY = 4096;
    public static final int VEIN_LIMIT = 64;
    public static final int VEIN_RADIUS = 8;
    public static final double MAGNET_RADIUS = 6;
    public static final TagKey<Block> VEIN_MINEABLE = TagKey.create(Registries.BLOCK, new ResourceLocation(MyDimension.MOD_ID, "vein_mineable"));
    private static final String MODE_TAG = "LordPickaxeVein";
    private record Pending(ItemStack tool, BlockPos origin, Block block, ResourceKey<Level> dimension) {}
    private static final Map<ServerPlayer, List<Pending>> PENDING = new WeakHashMap<>();
    private static final ThreadLocal<Boolean> CHAINING = ThreadLocal.withInitial(() -> false);
    private PickaxePowers() {}

    public static boolean veinEnabled(ItemStack stack) { return stack.getOrCreateTag().getBoolean(MODE_TAG); }
    public static void toggle(Player player, ItemStack stack) {
        boolean enabled = !veinEnabled(stack);
        stack.getOrCreateTag().putBoolean(MODE_TAG, enabled);
        player.displayClientMessage(Component.translatable("message.mydimension_lord.pickaxe." + (enabled ? "vein" : "single")), true);
    }
    public static void queue(ServerPlayer player, ItemStack tool, BlockPos pos, BlockState state) {
        if (CHAINING.get() || !tool.is(EquipmentItems.DIMENSION_PICKAXE.get()) || !veinEnabled(tool) || !state.is(VEIN_MINEABLE)) return;
        List<Pending> pending = PENDING.computeIfAbsent(player, ignored -> new ArrayList<>());
        if (pending.size() < 4) pending.add(new Pending(tool, pos.immutable(), state.getBlock(), player.level().dimension()));
    }
    public static void update(ServerPlayer player) {
        drain(player);
        ItemStack tool = player.getMainHandItem();
        if (!player.isAlive() || player.isSpectator() || !tool.is(EquipmentItems.DIMENSION_PICKAXE.get())) return;
        if (player.tickCount % 5 == 0) magnet(player);
    }
    public static int repair(ItemStack tool, String dimensionNamespace) {
        if (tool.is(EquipmentItems.DIMENSION_PICKAXE.get()) && tool.isDamaged() && "mydimension".equals(dimensionNamespace)) {
            tool.setDamageValue(tool.getDamageValue() - 1);
            return 1;
        }
        return 0;
    }
    public static int magnet(ServerPlayer player) {
        if (!player.isAlive() || player.isSpectator() || !player.getMainHandItem().is(EquipmentItems.DIMENSION_PICKAXE.get())) return 0;
        int picked = 0, inspected = 0;
        for (var item : player.level().getEntitiesOfClass(ItemEntity.class, player.getBoundingBox().inflate(MAGNET_RADIUS))) {
            if (++inspected > 64) break;
            var owner = item.getOwner();
            if (owner instanceof Player && owner != player || item.hasPickUpDelay() || !item.isAlive()
                    || player.distanceToSqr(item) > MAGNET_RADIUS * MAGNET_RADIUS || !player.hasLineOfSight(item)) continue;
            int before = item.getItem().getCount();
            item.playerTouch(player); // Vanilla pickup delay, ownership, inventory and loader hooks remain authoritative.
            picked += item.isRemoved() ? before : before - item.getItem().getCount();
        }
        return picked;
    }
    public static int drain(ServerPlayer player) {
        List<Pending> pending = PENDING.remove(player);
        if (pending == null || !player.isAlive() || player.isSpectator() || player.isCreative()) return 0;
        int broken = 0;
        CHAINING.set(true);
        try {
            for (Pending request : pending) {
                var level = player.serverLevel();
                if (broken >= VEIN_LIMIT - 1) break;
                if (player.getMainHandItem() != request.tool() || !veinEnabled(request.tool())
                        || !request.tool().is(EquipmentItems.DIMENSION_PICKAXE.get()) || !level.dimension().equals(request.dimension())
                        || !level.hasChunkAt(request.origin()) || level.getBlockState(request.origin()).is(request.block())) continue;
                var visited = new HashSet<BlockPos>();
                var frontier = new ArrayDeque<BlockPos>();
                visited.add(request.origin()); frontier.add(request.origin());
                while (!frontier.isEmpty() && broken < VEIN_LIMIT - 1 && player.getMainHandItem() == request.tool()
                        && request.tool().is(EquipmentItems.DIMENSION_PICKAXE.get())) {
                    BlockPos center = frontier.remove();
                    for (Direction direction : Direction.values()) {
                        if (broken >= VEIN_LIMIT - 1 || !request.tool().is(EquipmentItems.DIMENSION_PICKAXE.get())) break;
                        BlockPos next = center.relative(direction);
                        if (!visited.add(next) || !withinVein(request.origin(), next) || !level.hasChunkAt(next)
                                || !level.getWorldBorder().isWithinBounds(next) || !level.mayInteract(player, next)) continue;
                        BlockState state = level.getBlockState(next);
                        if (!state.is(request.block()) || state.getDestroySpeed(level, next) < 0 || level.getBlockEntity(next) != null) continue;
                        if (player.gameMode.destroyBlock(next) && !level.getBlockState(next).is(request.block())) {
                            broken++; frontier.add(next);
                        }
                    }
                }
            }
        } finally { CHAINING.remove(); }
        return broken;
    }
    private static boolean withinVein(BlockPos origin, BlockPos next) {
        return Math.abs(next.getX() - origin.getX()) <= VEIN_RADIUS && Math.abs(next.getY() - origin.getY()) <= VEIN_RADIUS
                && Math.abs(next.getZ() - origin.getZ()) <= VEIN_RADIUS;
    }
}