tutorial:containers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:containers [2020/04/02 12:57] – [Block and BlockItem] Revert BlockWithEntity change earthcomputer | tutorial: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, | + | ItemScatterer.spawn(world, |
// update comparators | // update comparators | ||
world.updateHorizontalAdjacent(pos, | world.updateHorizontalAdjacent(pos, | ||
Line 81: | Line 81: | ||
public static final Identifier BIGGER_CHEST = new Identifier(MOD_ID, | public static final Identifier BIGGER_CHEST = new Identifier(MOD_ID, | ||
- | 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, | Registry.register(Registry.BLOCK, | ||
- | Registry.register(Registry.BLOCK, BIGGER_CHEST, | + | Registry.register(Registry.ITEM, BIGGER_CHEST, |
} | } | ||
} | } | ||
Line 109: | Line 109: | ||
@Override | @Override | ||
protected Text getContainerName() { | protected Text getContainerName() { | ||
+ | // versions 1.18.2 and below | ||
return new TranslatableText(" | return new TranslatableText(" | ||
+ | // versions since 1.19 | ||
+ | return Text.translatable(" | ||
} | } | ||
@Override | @Override | ||
- | protected | + | protected |
- | return new BiggerChestContainer(syncId, playerInventory, | + | return new BiggerChestScreenHandler(syncId, playerInventory, |
} | } | ||
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, | Inventories.fromTag(tag, | ||
Line 153: | Line 156: | ||
==== Container GUI and Screen ==== | ==== Container GUI and Screen ==== | ||
- | We need a Container | + | We need a ScreenHandler |
- | <code java [enable_line_numbers=" | + | <code java [enable_line_numbers=" |
- | public class BiggerChestContainer | + | public class BiggerChestScreenHandler |
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 | + | protected |
- | 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; | ||
- | | + | |
- | inventory.onInvOpen(playerInventory.player); | + | inventory.onOpen(playerInventory.player); |
- | // Creating Slots for GUI. A Slot is essentially a correspoding | + | // Creating Slots for GUI. A Slot is essentially a corresponding |
int i; | int i; | ||
int j; | int j; | ||
Line 193: | Line 196: | ||
@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 228: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class BiggerChestScreen extends | + | public class BiggerChestScreen extends |
// a path to gui texture, you may replace it with new Identifier(YourMod.MOD_ID, | // a path to gui texture, you may replace it with new Identifier(YourMod.MOD_ID, | ||
private static final Identifier TEXTURE = new Identifier(" | private static final Identifier TEXTURE = new Identifier(" | ||
- | public BiggerChestScreen(BiggerChestContainer container, PlayerInventory playerInventory, | + | public BiggerChestScreen(BiggerChestScreenHandler handler, PlayerInventory playerInventory, |
- | super(container, playerInventory, | + | super(handler, playerInventory, |
- | this.containerHeight | + | this.backgroundHeight |
} | } | ||
@Override | @Override | ||
- | protected void drawForeground(int mouseX, int mouseY) { | + | protected void drawForeground(MatrixStack matrices, |
- | this.font.draw(this.title.asFormattedString(), 8.0F, 6.0F, 4210752); | + | this.textRenderer.draw(matrices, |
- | this.font.draw(this.playerInventory.getDisplayName().asFormattedString(), 8.0F, (float)(this.containerHeight | + | this.textRenderer.draw(matrices, |
} | } | ||
Line 244: | Line 247: | ||
protected void drawBackground(float delta, int mouseX, int mouseY) { | protected void drawBackground(float delta, int mouseX, int mouseY) { | ||
RenderSystem.color4f(1.0F, | RenderSystem.color4f(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, | + | this.blit(i, |
- | this.blit(i, | + | this.blit(i, |
} | } | ||
} | } | ||
Line 274: | Line 277: | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
[...] | [...] | ||
- | ScreenProviderRegistry.INSTANCE.< | + | ScreenProviderRegistry.INSTANCE.< |
} | } | ||
</ | </ | ||
Line 305: | Line 308: | ||
@Override | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
- | ScreenProviderRegistry.INSTANCE.< | + | ScreenProviderRegistry.INSTANCE.< |
} | } | ||
</ | </ |
tutorial/containers.1585832241.txt.gz · Last modified: 2020/04/02 12:57 by earthcomputer