You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Minecraft 1.16.5 Forge模组开发:右键无法替换方块问题求助

问题原因

你当前的代码存在4处核心问题导致方块替换不生效:

  • 两个抛光方法仅声明了替换用的方块变量,没有实际调用world.setBlock执行替换逻辑
  • 花岗岩对应的抛光方法错误引用了抛光闪长岩的方块对象
  • onItemUseFirst方法处理完自定义逻辑后返回了父类方法的结果,会导致自定义操作被覆盖
  • 代码中的lambda表达式存在转义错误,运行时会编译报错

修复后的完整代码

package com.technowin.beeutil.item.custom;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemUseContext;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

import java.util.Objects;

public class Waxer extends Item {
    public Waxer(Properties properties) {
        super(properties);
    }

    @Override
    public ActionResultType onItemUseFirst(ItemStack stack, ItemUseContext context) {
        World world = context.getWorld();

        if(!world.isRemote) {
            PlayerEntity playerEntity = Objects.requireNonNull(context.getPlayer());
            BlockState clickedBlock = world.getBlockState(context.getPos());
            BlockPos clickedPos = context.getPos();

            // 匹配方块执行对应抛光逻辑
            if (clickedBlock.getBlock() == Blocks.DIORITE) {
                world.setBlockState(clickedPos, Blocks.POLISHED_DIORITE.getDefaultState(), 3);
                stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
                return ActionResultType.SUCCESS;
            } else if (clickedBlock.getBlock() == Blocks.GRANITE) {
                world.setBlockState(clickedPos, Blocks.POLISHED_GRANITE.getDefaultState(), 3);
                stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
                return ActionResultType.SUCCESS;
            } else if (clickedBlock.getBlock() == Blocks.ANDESITE) {
                world.setBlockState(clickedPos, Blocks.POLISHED_ANDESITE.getDefaultState(), 3);
                stack.damageItem(1, playerEntity, player -> player.sendBreakAnimation(context.getHand()));
                return ActionResultType.SUCCESS;
            }
        }
        return ActionResultType.PASS;
    }
}

关键逻辑说明

  • 1.16.5 Forge版本替换方块统一使用world.setBlockState方法,第三个参数3是标准更新标记,代表方块修改后同步到客户端且触发状态更新,避免出现客户端服务端状态不一致的问题
  • 每次成功执行抛光后直接返回ActionResultType.SUCCESS,终止后续逻辑执行,避免父类方法覆盖操作
  • 这里把之前分散的校验、抛光逻辑合并简化,降低新手阶段的维护成本,你后续可以根据需求再拆分封装
  • 额外补充了安山岩的抛光逻辑,和你最初的需求匹配

内容的提问来源于stack exchange,提问作者Techo WinMC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.30 11:06:04