tutorial:containers
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
tutorial:containers [2020/04/02 11:04] – [Block and BlockItem] Replace BlockWithEntity with BlockEntityProvider and other cleanup earthcomputer | tutorial:containers [2020/08/14 07:03] – screen handlers part 2, needs extra testing leocth2 | ||
---|---|---|---|
Line 6: | Line 6: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
- | public class BiggerChestBlock extends | + | public class BiggerChestBlock extends |
public BiggerChestBlock(Settings settings) { | public BiggerChestBlock(Settings settings) { | ||
super(settings); | super(settings); | ||
+ | } | ||
+ | | ||
+ | // A side effect of extending BlockWithEntity is it changes the render type to INVISIBLE, so we have to revert this | ||
+ | @Override | ||
+ | public BlockRenderType getRenderType(BlockState state) { | ||
+ | return BlockRenderType.MODEL; | ||
} | } | ||
Line 107: | Line 113: | ||
@Override | @Override | ||
- | protected Container | + | protected Container |
return new BiggerChestContainer(syncId, | return new BiggerChestContainer(syncId, | ||
} | } | ||
Line 147: | Line 153: | ||
==== 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 187: | Line 193: | ||
@Override | @Override | ||
public boolean canUse(PlayerEntity player) { | public boolean canUse(PlayerEntity player) { | ||
- | return this.inventory.canPlayerUseInv(player); | + | return this.inventory.canPlayerUse(player); |
} | } | ||
// Shift + Player Inv Slot | // Shift + Player Inv Slot | ||
+ | @Override | ||
public ItemStack transferSlot(PlayerEntity player, int invSlot) { | public ItemStack transferSlot(PlayerEntity player, int invSlot) { | ||
ItemStack newStack = ItemStack.EMPTY; | ItemStack newStack = ItemStack.EMPTY; | ||
Line 218: | Line 225: | ||
<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 237: | Line 244: | ||
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 252: | Line 259: | ||
public static final String BIGGER_CHEST_TRANSLATION_KEY = Util.createTranslationKey(" | public static final String BIGGER_CHEST_TRANSLATION_KEY = Util.createTranslationKey(" | ||
+ | @Override | ||
public void onInitialize() { | public void onInitialize() { | ||
[...] | [...] | ||
Line 263: | Line 271: | ||
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
+ | @Override | ||
public void onInitializeClient() { | public void onInitializeClient() { | ||
[...] | [...] |
tutorial/containers.txt · Last modified: 2022/05/27 15:57 by solidblock