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
Last revisionBoth sides next revision
tutorial:containers [2020/04/02 12:57] – [Block and BlockItem] Revert BlockWithEntity change earthcomputertutorial:containers [2020/08/14 07:49] – evil cast my smol brain no understand leocth2
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 113: Line 113:
  
     @Override     @Override
-    protected Container createContainer(int syncId, PlayerInventory playerInventory) { +    protected ScreenHandler createScreenHandler(int syncId, PlayerInventory playerInventory) { 
-        return new BiggerChestContainer(syncId, playerInventory, (Inventory) this);+        return new BiggerChestScreenHandler(syncId, playerInventory, (Inventory) this);
     }     }
  
Line 128: Line 128:
  
     @Override     @Override
-    public int getInvSize() {+    public int size() {
         return INVENTORY_SIZE;         return INVENTORY_SIZE;
     }     }
Line 135: Line 135:
     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 153: Line 153:
  
 ==== Container GUI and Screen ==== ==== Container GUI and Screen ====
-We need a Container Class and a ContainerScreen Class to display and sync the GUI. Container Classes are used to synchronize GUI state between the server and the client. ContainerScreen Classes are fully client-sided and are responsible for drawing GUI elements.+We need a ScreenHandler Class and a HandledScreen Class to display and sync the GUI. ScreenHandler classes are used to synchronize GUI state between the server and the client. HandledScreen classes are fully client-sided and are responsible for drawing GUI elements.
  
-<code java [enable_line_numbers="true"BiggerChestContainer.java> +<code java [enable_line_numbers="true"BiggerChestScreenHandler.java> 
-public class BiggerChestContainer extends Container {+public class BiggerChestScreenHandler extends ScreenHandler {
     private final Inventory inventory; // Chest inventory     private final Inventory inventory; // Chest inventory
     private static final int INVENTORY_SIZE = 54; // 6 rows * 9 cols     private static final int INVENTORY_SIZE = 54; // 6 rows * 9 cols
  
-    protected BiggerChestContainer(int syncId, PlayerInventory playerInventory, Inventory inventory) { +    protected BiggerChestScreenHandler(int syncId, PlayerInventory playerInventory, Inventory inventory) { 
-        super(null, syncId); // Since we didn't create a ContainerType, we will place null here.+        super(null, syncId); // Since we didn't create a ScreenHandlerType, we will place null here.
         this.inventory = inventory;         this.inventory = inventory;
-        checkContainerSize(inventory, INVENTORY_SIZE); +        checkSize(inventory, INVENTORY_SIZE); 
-        inventory.onInvOpen(playerInventory.player);+        inventory.onOpen(playerInventory.player);
  
-        // Creating Slots for GUI. A Slot is essentially a correspoding from inventory itemstacks to the GUI position.+        // Creating Slots for GUI. A Slot is essentially a corresponding from inventory ItemStacks to the GUI position.
         int i;         int i;
         int j;         int j;
Line 193: Line 193:
     @Override     @Override
     public boolean canUse(PlayerEntity player) {     public boolean canUse(PlayerEntity player) {
-        return this.inventory.canPlayerUseInv(player);+        return this.inventory.canPlayerUse(player);
     }     }
  
Line 225: Line 225:
  
 <code java [enable_line_numbers="true"] BiggerChestScreen.java> <code java [enable_line_numbers="true"] BiggerChestScreen.java>
-public class BiggerChestScreen extends ContainerScreen<BiggerChestContainer> {+public class BiggerChestScreen extends HandledScreen<BiggerChestScreenHandler> {
  
     // a path to gui texture, you may replace it with new Identifier(YourMod.MOD_ID, "textures/gui/container/your_container.png");     // a path to gui texture, you may replace it with new Identifier(YourMod.MOD_ID, "textures/gui/container/your_container.png");
     private static final Identifier TEXTURE = new Identifier("textures/gui/container/generic_54.png");     private static final Identifier TEXTURE = new Identifier("textures/gui/container/generic_54.png");
  
-    public BiggerChestScreen(BiggerChestContainer container, PlayerInventory playerInventory, Text title) { +    public BiggerChestScreen(BiggerChestScreenHandler handler, PlayerInventory playerInventory, Text title) { 
-        super(container, playerInventory, title); +        super(handler, playerInventory, title); 
-        this.containerHeight = 114 + 6 * 18;+        this.backgroundHeight = 114 + 6 * 18;
     }     }
  
     @Override     @Override
-    protected void drawForeground(int mouseX, int mouseY) { +    protected void drawForeground(MatrixStack matrices, int mouseX, int mouseY) { 
-        this.font.draw(this.title.asFormattedString(), 8.0F, 6.0F, 4210752); +        this.textRenderer.draw(matrices, this.title.asString(), 8.0F, 6.0F, 4210752); 
-        this.font.draw(this.playerInventory.getDisplayName().asFormattedString(), 8.0F, (float)(this.containerHeight - 96 + 2), 4210752);+        this.textRenderer.draw(matrices, this.playerInventory.getDisplayName().asString(), 8.0F, (float)(this.backgroundHeight - 96 + 2), 4210752);
     }     }
  
Line 244: Line 244:
     protected void drawBackground(float delta, int mouseX, int mouseY) {     protected void drawBackground(float delta, int mouseX, int mouseY) {
         RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);         RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F);
-        this.minecraft.getTextureManager().bindTexture(TEXTURE); +        this.client.getTextureManager().bindTexture(TEXTURE); 
-        int i = (this.width - this.containerWidth) / 2; +        int i = (this.width - this.backgroundWidth) / 2; 
-        int j = (this.height - this.containerHeight) / 2; +        int j = (this.height - this.backgroundHeight) / 2; 
-        this.blit(i, j, 0, 0, this.containerWidth, 6 * 18 + 17); +        this.blit(i, j, 0, 0, this.backgroundWidth, 6 * 18 + 17); 
-        this.blit(i, j + 6 * 18 + 17, 0, 126, this.containerWidth, 96);+        this.blit(i, j + 6 * 18 + 17, 0, 126, this.backgroundWidth, 96);
     }     }
 } }
tutorial/containers.txt · Last modified: 2022/05/27 15:57 by solidblock