User Tools

Site Tools


tutorial:propertydelegates

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:propertydelegates [2020/08/14 17:11] – [Our new ScreenHandler] manymoney2tutorial:propertydelegates [2020/08/14 17:23] – [Result] manymoney2
Line 132: Line 132:
 </code> </code>
  
 +====== Showing the Information with the Screen ======
 +As the screen gets the ScreenHandler in its constructor, we have access to the property delegate from above and can render the integer on screen
 +<code java [enable_line_numbers="true"] BoxScreen.java>
 +
 +public class BoxScreen extends HandledScreen<ScreenHandler> {
 +    private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/container/dispenser.png");
 +    BoxScreenHandler screenHandler;
 +
 +    public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) {
 +        super(handler, inventory, title);
 +        //we save a reference to the screenhandler so we can render the number from our propertyDelegate on screen
 +        screenHandler = (BoxScreenHandler) handler;
 +
 +    }
 +
 +    @Override
 +    protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) {[...]}
 +
 +    @Override
 +    public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) {
 +        //We just render our synced number somewhere in the container, this is a demonstration after all
 +        //the last argument is a color code, making the font bright green
 +        textRenderer.draw(matrices, Integer.toString(screenHandler.getSyncedNumber()), 0, 0, 65280);
 +        renderBackground(matrices);
 +        super.render(matrices, mouseX, mouseY, delta);
 +        drawMouseoverTooltip(matrices, mouseX, mouseY);
 +    }
 +
 +    @Override
 +    protected void init() {
 +        super.init();
 +        // Center the title
 +        titleX = (backgroundWidth - textRenderer.getWidth(title)) / 2;
 +    }
 +}
 +
 +</code>
 +
 +====== Result ======
 +As the registration of the ScreenHandler is identical to the first tutorial we can see the result already!
 +When the BlockEntity is placed it will increase the syncedInt by one each tick, when we look inside the container the integer will
 +automatically be synced to the client and rendered in the top left corner.
 +
 +[[https://streamable.com/7aic8q|Example Video]]
 +
 +If you want a more realistic example, you might want to have a look at AbstractFurnaceEntity and AbstractFurnaceScreenHandler in the minecraft code.
  
  
tutorial/propertydelegates.txt · Last modified: 2022/05/27 16:00 by solidblock