方块如何使用一个自定义工具进行挖掘?

GhostSkyV9


版本信息
你使用的IDE:IntelliJ IDEA
你使用的IDE版本:2019.1.3
Forge版本: 14.23.5.2768
Minecraft版本: 1.12


**相关代码**
「public class ObsidianBlock extends Block implements IHasModel {
    public ObsidianBlock(String name) {
        super(Material.IRON);
        setRegistryName(name);
        setUnlocalizedName(name);
        setHardness(3.0F);
        setHarvestLevel("iceaxe",3);
        setCreativeTab(Util.creativeTabs);
        ModBlocks.BLOCKS.add(this);
    }

    @Override
    public void registryItemRender() {
        InitHelper.itemModelRegistry(Item.getItemFromBlock(this));
    }
}
」
public class ObsidianIceaxe extends ItemTool implements IHasModel {
    private static final Set<Block> EFFECTIVE_ON = Sets.newHashSet(Blocks.PLANKS, Blocks.BOOKSHELF, Blocks.LOG, Blocks.LOG2, Blocks.CHEST, Blocks.PUMPKIN, Blocks.LIT_PUMPKIN, Blocks.MELON_BLOCK, Blocks.LADDER, Blocks.WOODEN_BUTTON, Blocks.WOODEN_PRESSURE_PLATE,Blocks.ACTIVATOR_RAIL, Blocks.COAL_ORE, Blocks.COBBLESTONE, Blocks.DETECTOR_RAIL, Blocks.DIAMOND_BLOCK, Blocks.DIAMOND_ORE, Blocks.DOUBLE_STONE_SLAB, Blocks.GOLDEN_RAIL, Blocks.GOLD_BLOCK, Blocks.GOLD_ORE, Blocks.ICE, Blocks.IRON_BLOCK, Blocks.IRON_ORE, Blocks.LAPIS_BLOCK, Blocks.LAPIS_ORE, Blocks.LIT_REDSTONE_ORE, Blocks.MOSSY_COBBLESTONE, Blocks.NETHERRACK, Blocks.PACKED_ICE, Blocks.RAIL, Blocks.REDSTONE_ORE, Blocks.SANDSTONE, Blocks.RED_SANDSTONE, Blocks.STONE, Blocks.STONE_SLAB, Blocks.STONE_BUTTON, Blocks.STONE_PRESSURE_PLATE);
    public ObsidianIceaxe(String name,ToolMaterial materialIn) {
        super(materialIn,EFFECTIVE_ON);
        InitHelper.itemInit(this,name, Util.creativeTabs);
    }
    @Override
    public Set<String> getToolClasses(ItemStack stack) {
        return Collections.singleton("iceaxe");
    }

GhostSkyV9


我这样写的代码,进入游戏后,并没有效果。。。


FledgeXu


你也没有自定义工具啊。

setHarvestLevel("iceaxe",3);

这里的iceaxe是什么?


GhostSkyV9


ObsidianIceaxe这个类不是自定义工具嘛


FledgeXu


在你的工具类里添加

    @Override
    public Set<String> getToolClasses(ItemStack stack) {
        return Collections.singleton("iceaxe");
    }

    @Override
    public int getHarvestLevel(ItemStack stack, String toolClass, @Nullable EntityPlayer player, @Nullable IBlockState blockState) {
        return 4;
    }

然后在你的方块里添加:

setHarvestLevel("iceaxe",4);

顺别一提,请不要复制粘贴我的代码,哪怕要抄也要手打。


system


该主题在最后一个回复创建后7天后自动关闭。不再允许新的回复。