如何在 Forge 中将 setBlock() 与自定义方块一起使用?

问题描述 投票:0回答:1

所以,我正在制作一个模组,我想使用 level.setBlock(),我是这样使用的:

level.setBlock(pos, Blocks.DIRT.defaultBlockState(), 1);
并且它有效但是当我想设置我的自定义块时,我有这个错误:
The method setBlock(BlockPos, BlockState, int) in the type Level is not applicable for the arguments (BlockPos, RegistryObject<Block>, int).

问题是我不能将 defaultBlockState() 与我的自定义块一起使用,但我想这样做

level.setBlock(pos, ModBlocks.lock, 1)
.

ModBlocks.java:

package com.redmecanics.villagedestroyer.block;

import java.util.function.Supplier;

import com.redmecanics.villagedestroyer.VillageDestroyer;
import com.redmecanics.villagedestroyer.item.ModCreativeModeTab;
import com.redmecanics.villagedestroyer.item.ModItems;

import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;

public class ModBlocks {
    public static final BooleanProperty defState = BooleanProperty.create("defaultBlockState");
    
    public static final DeferredRegister<Block> BLOCKS =
            DeferredRegister.create(ForgeRegistries.BLOCKS, VillageDestroyer.MOD_ID);

    public static final RegistryObject<Block> aluminium_block = registerBlock("aluminium_block",
            () -> new Block(BlockBehaviour.Properties.of(Material.METAL)
                    .strength(3f).requiresCorrectToolForDrops()), ModCreativeModeTab.villagedestroyer_tab);

    public static final RegistryObject<Block> aluminium_ore = registerBlock("aluminium_ore",
            () -> new Block(BlockBehaviour.Properties.of(Material.STONE)
                    .strength(3f).requiresCorrectToolForDrops()), ModCreativeModeTab.villagedestroyer_tab);
    
    public static final RegistryObject<Block> deepslate_aluminium_ore = registerBlock("deepslate_aluminium_ore",
            () -> new Block(BlockBehaviour.Properties.of(Material.STONE)
                    .strength(3f).requiresCorrectToolForDrops()), ModCreativeModeTab.villagedestroyer_tab);
    
    public static final RegistryObject<Block> lock = registerBlock("lock",
            () -> new Block(BlockBehaviour.Properties.of(Material.METAL)
                    .strength(-1f)), ModCreativeModeTab.villagedestroyer_tab);


    private static <T extends Block> RegistryObject<T> registerBlock(String name, Supplier<T> block, CreativeModeTab tab) {
        RegistryObject<T> toReturn = BLOCKS.register(name, block);
        registerBlockItem(name, toReturn, tab);
        return toReturn;
    }

    private static <T extends Block> RegistryObject<Item> registerBlockItem(String name, RegistryObject<T> block,
                                                                            CreativeModeTab tab) {
        return ModItems.ITEMS.register(name, () -> new BlockItem(block.get(), new Item.Properties().tab(tab)));
    }

    public static void register(IEventBus eventBus) {
        BLOCKS.register(eventBus);
    }
}

KeyItem.java:

package com.redmecanics.villagedestroyer.item.custom;

import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;

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

    @Override
    public InteractionResult useOn(UseOnContext ctx) {
        Level level = ctx.getLevel();
        BlockPos pos = ctx.getClickedPos();
        Block btc = level.getBlockState(pos).getBlock();
        
        if (!level.isClientSide) {
            if (btc == Blocks.OAK_DOOR) {
                level.removeBlock(pos, true);
                level.setBlock(pos, ModBlocks.lock, 1);
                System.out.println("door locked");
            }
        }
        
        return super.useOn(ctx);
    }
}
minecraft minecraft-forge
1个回答
0
投票

所以我发现我能做到:

level.setBlock(pos, ModBlocks.lock.get().defaultBlockState(), 1);
© www.soinside.com 2019 - 2024. All rights reserved.