User Tools

Site Tools


tutorial:containers

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
tutorial:containers [2020/08/14 07:03] – screen handlers part 2, needs extra testing leocth2tutorial:containers [2022/05/27 15:57] (current) solidblock
Line 50: Line 50:
             BlockEntity blockEntity = world.getBlockEntity(pos);             BlockEntity blockEntity = world.getBlockEntity(pos);
             if (blockEntity instanceof BiggerChestBlockEntity) {             if (blockEntity instanceof BiggerChestBlockEntity) {
-                ItemScatterer.spawn(world, (BlockPos)pos, (Inventory)((BiggerChestBlockEntity)blockEntity));+                ItemScatterer.spawn(world, pos, (BiggerChestBlockEntity)blockEntity);
                 // update comparators                 // update comparators
                 world.updateHorizontalAdjacent(pos, this);                 world.updateHorizontalAdjacent(pos, this);
Line 81: Line 81:
     public static final Identifier BIGGER_CHEST = new Identifier(MOD_ID, "bigger_chest_block");     public static final Identifier BIGGER_CHEST = new Identifier(MOD_ID, "bigger_chest_block");
  
-    public static final Block BIGGER_CHEST_BLOCK = new BiggerChestBlock(FabricBlockSettings.of(Material.METAL).build());+    public static final Block BIGGER_CHEST_BLOCK = new BiggerChestBlock(FabricBlockSettings.of(Material.METAL));
        
     @Override     @Override
Line 87: Line 87:
     {     {
         Registry.register(Registry.BLOCK, BIGGER_CHEST, BIGGER_CHEST_BLOCK);         Registry.register(Registry.BLOCK, BIGGER_CHEST, BIGGER_CHEST_BLOCK);
-        Registry.register(Registry.BLOCK, BIGGER_CHEST, new BlockItem(BIGGER_CHEST_BLOCK, new Item.Settings().group(ItemGroup.REDSTONE)));+        Registry.register(Registry.ITEM, BIGGER_CHEST, new BlockItem(BIGGER_CHEST_BLOCK, new Item.Settings().group(ItemGroup.REDSTONE)));
     }     }
 } }
Line 109: Line 109:
     @Override     @Override
     protected Text getContainerName() {     protected Text getContainerName() {
 +        // versions 1.18.2 and below
         return new TranslatableText("container.chest");         return new TranslatableText("container.chest");
 +        // versions since 1.19
 +        return Text.translatable("container.chest");
     }     }
  
     @Override     @Override
-    protected Container createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity playerEntity) { +    protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) { 
-        return new BiggerChestContainer(syncId, playerInventory, (Inventory) this);+        return new BiggerChestScreenHandler(syncId, playerInventory, (Inventory) this);
     }     }
  
Line 128: Line 131:
  
     @Override     @Override
-    public int getInvSize() {+    public int size() {
         return INVENTORY_SIZE;         return INVENTORY_SIZE;
     }     }
Line 135: Line 138:
     public void fromTag(CompoundTag tag) {     public void fromTag(CompoundTag tag) {
         super.fromTag(tag);         super.fromTag(tag);
-        this.inventory = DefaultedList.ofSize(this.getInvSize(), ItemStack.EMPTY);+        this.inventory = DefaultedList.ofSize(this.size(), ItemStack.EMPTY);
         if (!this.deserializeLootTable(tag)) {         if (!this.deserializeLootTable(tag)) {
             Inventories.fromTag(tag, this.inventory);             Inventories.fromTag(tag, this.inventory);
Line 274: Line 277:
     public void onInitializeClient() {     public void onInitializeClient() {
         [...]         [...]
-        ScreenProviderRegistry.INSTANCE.<BiggerChestContainer>registerFactory(ExampleMod.BIGGER_CHEST, (container) -> new BiggerChestScreen(container, MinecraftClient.getInstance().player.inventory, new TranslatableText(ExampleMod.BIGGER_CHEST_TRANSLATION_KEY)));+        ScreenProviderRegistry.INSTANCE.<BiggerChestContainer>registerFactory(ExampleMod.BIGGER_CHEST, (container) -> new BiggerChestScreen(container, MinecraftClient.getInstance().player.inventory, Text.translatable(ExampleMod.BIGGER_CHEST_TRANSLATION_KEY)));
     }     }
 </code> </code>
Line 305: Line 308:
     @Override     @Override
     public void onInitializeClient() {     public void onInitializeClient() {
-        ScreenProviderRegistry.INSTANCE.<BiggerChestContainer>registerFactory(ExampleMod.BIGGER_CHEST, (container) -> new BiggerChestScreen(container, MinecraftClient.getInstance().player.inventory, new TranslatableText(ExampleMod.BIGGER_CHEST_TRANSLATION_KEY)));+        ScreenProviderRegistry.INSTANCE.<BiggerChestContainer>registerFactory(ExampleMod.BIGGER_CHEST, (container) -> new BiggerChestScreen(container, MinecraftClient.getInstance().player.inventory, Text.translatable(ExampleMod.BIGGER_CHEST_TRANSLATION_KEY)));
     }     }
 </code> </code>
tutorial/containers.1597388636.txt.gz · Last modified: 2020/08/14 07:03 by leocth2