User Tools

Site Tools


tutorial:screenhandler

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:screenhandler [2020/08/15 19:45] technici4ntutorial:screenhandler [2022/11/20 06:57] – [Registering our Screen and ScreenHandler] netuserget
Line 1: Line 1:
-====== Creating a Container Block (DRAFT) (NEW) ======+====== Creating a Container Block======
 In this tutorial we will create simple storage block similar to a dispenser, explaining how to build a user interface with the ''ScreenHandler'' API from Fabric and Vanilla Minecraft along the way. In this tutorial we will create simple storage block similar to a dispenser, explaining how to build a user interface with the ''ScreenHandler'' API from Fabric and Vanilla Minecraft along the way.
  
Line 23: Line 23:
  
     @Override     @Override
-    public BlockEntity createBlockEntity(BlockView world) { +    public BlockEntity createBlockEntity(BlockPos pos, BlockState state) { 
-        return new BoxBlockEntity();+        return new BoxBlockEntity(pos, state);
     }     }
  
Line 36: Line 36:
     public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {     public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
         if (!world.isClient) {         if (!world.isClient) {
-            //This will call the createScreenHandlerFactory method from blockWithEntity, which will return our blockEntity casted +            //This will call the createScreenHandlerFactory method from BlockWithEntity, which will return our blockEntity casted to 
-            //to a namedScreenHandlerFactory+            //a namedScreenHandlerFactory. If your block class does not extend BlockWithEntity, it needs to implement createScreenHandlerFactory.
             NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);             NamedScreenHandlerFactory screenHandlerFactory = state.createScreenHandlerFactory(world, pos);
  
Line 70: Line 70:
     @Override     @Override
     public int getComparatorOutput(BlockState state, World world, BlockPos pos) {     public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
-        return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos))+        return ScreenHandler.calculateComparatorOutput(world.getBlockEntity(pos));
     }     }
 } }
Line 81: Line 81:
     private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(9, ItemStack.EMPTY);     private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(9, ItemStack.EMPTY);
  
-    public BoxBlockEntity() { +    public BoxBlockEntity(BlockPos pos, BlockState state) { 
-        super(ExampleMod.BOX_BLOCK_ENTITY);+        super(ExampleMod.BOX_BLOCK_ENTITY, pos, state);
     }     }
  
Line 111: Line 111:
          
     @Override     @Override
-    public void fromTag(BlockState state, CompoundTag tag) { +    public void readNbt(NbtCompound nbt) { 
-        super.fromTag(state, tag); +        super.readNbt(nbt); 
-        inventory = DefaultedList.ofSize(invsize, ItemStack.EMPTY); +        Inventories.readNbt(nbt, this.inventory);
-        Inventories.fromTag(tag, this.inventory);+
     }     }
  
     @Override     @Override
-    public CompoundTag toTag(CompoundTag tag) { +    public NbtCompound writeNbt(NbtCompound nbt) { 
-        super.toTag(tag); +        super.writeNbt(nbt); 
-        Inventories.toTag(tag, this.inventory); +        Inventories.writeNbt(nbt, this.inventory); 
-        return tag;+        return nbt;
     }     }
 } }
Line 147: Line 146:
  
         //The parameter of build at the very end is always null, do not worry about it         //The parameter of build at the very end is always null, do not worry about it
 +        // pre-1.17
         BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, BOX, BlockEntityType.Builder.create(BoxBlockEntity::new, BOX_BLOCK).build(null));         BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, BOX, BlockEntityType.Builder.create(BoxBlockEntity::new, BOX_BLOCK).build(null));
 +        // In 1.17 use FabricBlockEntityTypeBuilder instead of BlockEntityType.Builder
 +        BOX_BLOCK_ENTITY = Registry.register(Registry.BLOCK_ENTITY_TYPE, BOX, FabricBlockEntityTypeBuilder.create(BoxBlockEntity::new, BOX_BLOCK).build(null));
     }     }
  
Line 241: Line 243:
  
 <code java [enable_line_numbers="true"] BoxScreen.java> <code java [enable_line_numbers="true"] BoxScreen.java>
-public class BoxScreen extends HandledScreen<ScreenHandler> {+public class BoxScreen extends HandledScreen<BoxScreenHandler> {
     //A path to the gui texture. In this example we use the texture from the dispenser     //A path to the gui texture. In this example we use the texture from the dispenser
     private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/container/dispenser.png");     private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/container/dispenser.png");
  
-    public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) {+    public BoxScreen(BoxScreenHandler handler, PlayerInventory inventory, Text title) {
         super(handler, inventory, title);         super(handler, inventory, title);
     }     }
Line 251: Line 253:
     @Override     @Override
     protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {     protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {
-        RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); +        RenderSystem.setShader(GameRenderer::getPositionTexShader); 
-        client.getTextureManager().bindTexture(TEXTURE);+        RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); 
 +        RenderSystem.setShaderTexture(0, TEXTURE);
         int x = (width - backgroundWidth) / 2;         int x = (width - backgroundWidth) / 2;
         int y = (height - backgroundHeight) / 2;         int y = (height - backgroundHeight) / 2;
Line 285: Line 288:
     @Override     @Override
     public void onInitializeClient() {     public void onInitializeClient() {
-        ScreenRegistry.register(ExampleMod.BOX_SCREEN_HANDLER, BoxScreen::new);+        HandledScreens.register(ExampleMod.BOX_SCREEN_HANDLER, BoxScreen::new);
     }     }
 } }
  
 +</code>
 +
 +Don't forget to register this entrypoint in ''fabric.mod.json'' if you haven't done it yet:
 +<code json>
 +/* ... */
 +  "entrypoints": {
 +    /* ... */
 +    "client": [
 +      "tutorial.path.to.ExampleModClient"
 +    ]
 +  },
 </code> </code>
  
tutorial/screenhandler.txt · Last modified: 2024/02/19 02:51 by netuserget