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

MyDimension-Lord

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

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

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.ItemInteractionResult;
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.FletchingTableBlock;
import net.minecraft.world.level.block.StairBlock;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import java.lang.reflect.Method;
import java.util.Arrays;

/** Protect both modern block interaction paths, including protected overrides. */
public final class BuilderInteractionPolicy {
    private static final Class<?>[] EMPTY_HAND = {
            BlockState.class, Level.class, BlockPos.class, Player.class, BlockHitResult.class};
    private static final Class<?>[] WITH_ITEM = {
            ItemStack.class, BlockState.class, Level.class, BlockPos.class,
            Player.class, InteractionHand.class, BlockHitResult.class};
    private static final ClassValue<Boolean> CUSTOM_USE_HANDLER = new ClassValue<>() {
        @Override protected Boolean computeValue(Class<?> type) {
            for (Class<?> owner = type; owner != null && owner != Block.class && owner != BlockBehaviour.class;
                 owner = owner.getSuperclass()) {
                if (owner == FletchingTableBlock.class) return false;
                if (owner == StairBlock.class) continue;
                for (Method method : owner.getDeclaredMethods()) {
                    if (method.getReturnType() == InteractionResult.class
                            && Arrays.equals(method.getParameterTypes(), EMPTY_HAND)) return true;
                    if (method.getReturnType() == ItemInteractionResult.class
                            && Arrays.equals(method.getParameterTypes(), WITH_ITEM)) return true;
                }
            }
            return false;
        }
    };
    private BuilderInteractionPolicy() {}
    public static boolean prioritizesBlock(BlockState state) {
        return prioritizesBlock(state.hasBlockEntity(), state.getBlock().getClass());
    }
    public static boolean permitsScepter(boolean shiftDown, BlockState state) {
        return permitsScepter(shiftDown, prioritizesBlock(state));
    }
    static boolean prioritizesBlock(boolean hasBlockEntity, Class<?> blockType) {
        return hasBlockEntity || CUSTOM_USE_HANDLER.get(blockType);
    }
    static boolean permitsScepter(boolean shiftDown, boolean interactionPriority) {
        return shiftDown || !interactionPriority;
    }
}