User Tools

Site Tools


ko_kr:tutorial:blocks

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Last revisionBoth sides next revision
ko_kr:tutorial:blocks [2021/04/07 04:54] – created themovieaytko_kr:tutorial:blocks [2021/04/07 05:14] – external edit 127.0.0.1
Line 1: Line 1:
 ====== 블록 추가하기 ====== ====== 블록 추가하기 ======
-블록을 추가하는 방법은 아이템을 추가하는 것과 비슷합니다.+블록을 추가하는 방법은 [[tutorial:items|아이템을 추가]]하는 것과 비슷합니다. 클래스를 생성해서 블록의 인스턴스를 만들고, ''Registry.BLOCK''에 블록을 레지스터 하시면 됩니다. 그리고 블록의 생김새를 위해 블록 택스쳐와 블록 모델을 추가해야 합니다. 블록 모델 구성의 자세한 정보는 [[https://minecraft.gamepedia.com/Model|마인크레프트 위키 모델 페이지]]를 참조하세요. 
 + 
 +===== 블록 만들기 ===== 
 +Start by creating an instance of ''Block''. It can be stored at any location, but we will start at the top of your ''ModInitializer''. The ''Block'' constructor requires an ''AbstractBlock.Settings'' instance, which is a builder for configuring block properties. Fabric provides a ''FabricBlockSettings'' builder class with more available options. 
 + 
 +블록 인스턴스를 먼저 만듭니다.  
 + 
 +<code java [enable_line_numbers="true"]> 
 +public class ExampleMod implements ModInitializer { 
 + 
 +    /* Declare and initialize our custom block instance. 
 +       We set our block material to `METAL`, which requires a pickaxe to efficiently break. 
 +        
 +       `strength` sets both the hardness and the resistance of a block to the same value. 
 +       Hardness determines how long the block takes to break, and resistance determines how strong the block is against blast damage (e.g. explosions). 
 +       Stone has a hardness of 1.5f and a resistance of 6.0f, while Obsidian has a hardness of 50.0f and a resistance of 1200.0f. 
 +        
 +       You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks. 
 +    */ 
 +    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +     
 +    @Override 
 +    public void onInitialize() { 
 +         
 +    } 
 +
 +</code> 
 + 
 +==== Registering your Block ==== 
 + 
 +Blocks should be registered under the ''Registry.BLOCK'' registry. Call //Registry.register// and pass in the appropriate arguments. 
 + 
 +<code java [enable_line_numbers="true",highlight_lines_extra="11"]> 
 +public class ExampleMod implements ModInitializer { 
 + 
 +    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +     
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +    } 
 +
 +</code> 
 + 
 +Your custom block will //not// be accessible as an item yet, but it can be seen in-game by using the command ''/setblock tutorial:example_block''
 + 
 +==== Registering an Item for your Block ==== 
 + 
 +In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block. 
 + 
 +<code java [enable_line_numbers="true",highlight_lines_extra="12"]> 
 +public class ExampleMod implements ModInitializer { 
 + 
 +    public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f)); 
 +     
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +        Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings().group(ItemGroup.MISC))); 
 +    } 
 +
 +</code> 
 + 
 +===== Giving your Block Visuals ===== 
 + 
 +At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft's way of showing you that something went wrong while loading the block's assets (or visuals). A full list of issues will be printed to your log when you run your client. You will need these files to give your block visuals: 
 +  * A blockstate file 
 +  * A block model file 
 +  * A texture 
 +  * An item model file (if the block has an item associated with it). 
 + 
 +The files should be located here: 
 + 
 +  Blockstate: src/main/resources/assets/tutorial/blockstates/example_block.json 
 +  Block Model: src/main/resources/assets/tutorial/models/block/example_block.json 
 +  Item Model: src/main/resources/assets/tutorial/models/item/example_block.json 
 +  Block Texture: src/main/resources/assets/tutorial/textures/block/example_block.png 
 + 
 +The blockstate file determines which model a block should use depending on its blockstate. Our block doesn't have any potential states, so we cover everything with ''""''.  
 + 
 +<code JavaScript src/main/resources/assets/tutorial/blockstates/example_block.json> 
 +
 +  "variants":
 +    "": { "model": "tutorial:block/example_block"
 +  } 
 +
 +</code> 
 + 
 +The block model file defines the shape and texture of your block. Our model will have ''block/cube_all''as a parent, which applies the texture ''all'' to //all// sides of the block. 
 + 
 +<code JavaScript src/main/resources/assets/tutorial/models/block/example_block.json> 
 +
 +  "parent": "block/cube_all", 
 +  "textures":
 +    "all": "tutorial:block/example_block" 
 +  } 
 +
 +</code> 
 + 
 +In most cases, you will want the block to look the same in item form. You can make an item model that has the block model file as a parent, which makes it appear exactly like the block: 
 + 
 +<code JavaScript src/main/resources/assets/tutorial/models/item/example_block.json> 
 +
 +  "parent": "tutorial:block/example_block" 
 +
 +</code> 
 + 
 +Load up Minecraft and your block should have visuals! 
 + 
 +===== Configuring Block Drops ===== 
 + 
 +To make your block drop items when broken, you will need a //loot table//. The following file will cause your block to drop its respective item form when broken:  
 + 
 +<code JavaScript src/main/resources/data/tutorial/loot_tables/blocks/example_block.json> 
 +
 +  "type": "minecraft:block", 
 +  "pools":
 +    { 
 +      "rolls": 1, 
 +      "entries":
 +        { 
 +          "type": "minecraft:item", 
 +          "name": "tutorial:example_block" 
 +        } 
 +      ], 
 +      "conditions":
 +        { 
 +          "condition": "minecraft:survives_explosion" 
 +        } 
 +      ] 
 +    } 
 +  ] 
 +
 +</code> 
 + 
 +===== Creating a Custom Block Class ===== 
 + 
 +The above approach works well for simple items but falls short when you want a block with //unique// mechanics. We'll create a //separate// class that extends ''Block'' to do this. The class needs a constructor that takes in an ''AbstractBlock.Settings'' argument: 
 + 
 +<code java [enable_line_numbers="true"]> 
 +public class ExampleBlock extends Block { 
 + 
 +    public ExampleBlock(Settings settings) { 
 +        super(settings); 
 +    } 
 +
 +</code> 
 + 
 +You can override methods in the block class for custom functionality. Here's an implementation of the ''onUse'' method, which is called when you right-click the block. We check if the interaction is occurring on the server, and then send the player a message saying, //"Hello, world!"// 
 + 
 +<code java [enable_line_numbers="true",highlight_lines_extra="8,9,10,11,12,13,14,15"]> 
 +public class ExampleBlock extends Block { 
 + 
 +    public ExampleBlock(Settings settings) { 
 +        super(settings); 
 +    } 
 + 
 +    @Override 
 +    public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { 
 +        if (!world.isClient) { 
 +            player.sendMessage(new LiteralText("Hello, world!"), false); 
 +        } 
 + 
 +        return ActionResult.SUCCESS; 
 +    } 
 +
 +</code> 
 + 
 +To use your custom block class, replace //new Block// with //new ExampleBlock//: 
 + 
 +<code java [enable_line_numbers="true",highlight_lines_extra="3"]> 
 +public class ExampleMod implements ModInitializer { 
 + 
 +    public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE).hardness(4.0f)); 
 +     
 +    @Override 
 +    public void onInitialize() { 
 +        Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK); 
 +        Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC))); 
 +    } 
 +
 +</code> 
 + 
 +==== Custom VoxelShape ==== 
 + 
 +When using block models that do not //entirely// fill the block (eg. Anvil, Slab, Stairs), adjacent blocks hide their faces: 
 + 
 +{{:tutorial:voxelshape_wrong.png?200|}} 
 + 
 +To fix this, we have to define the ''VoxelShape'' of the new block: 
 + 
 +<code> 
 + @Override 
 + public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) { 
 +     return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f); 
 + } 
 +</code> 
 + 
 +Note that the //collision shape// of the block defaults to the outline shape if it is not specified. 
 + 
 +{{:tutorial:voxelshape_fixed.png?200|}} 
 + 
 +===== Next Steps ===== 
 +[[tutorial:blockstate|Adding simple state to a block, like ints and booleans]].  
 + 
 +[[tutorial:blockentity|Giving blocks a block entity so they can have advanced state like inventories]]. Also needed for many things like GUI and custom block rendering.
ko_kr/tutorial/blocks.txt · Last modified: 2023/08/14 16:13 by namutree0345