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
tutorial:propertydelegates [2020/08/14 17:10] – [BlockEntity] manymoney2tutorial:propertydelegates [2022/05/27 16:00] (current) solidblock
Line 1: Line 1:
 ====== Syncing Integers with PropertyDelegates ====== ====== Syncing Integers with PropertyDelegates ======
-**PropertyDelegate:** A PropertyDelegate is a kind of Container which contains a specific amounts of integer values which can be read or changed.+**PropertyDelegate:** A ''PropertyDelegate'' is a kind of Container which contains a specific amounts of integer values which can be read or changed.
  
-In this Tutorial we will sync Integer values between the client and the Server, an example for this in vanilla would be the smelting progress of a furnace.+In this Tutorial we will sync Integer values between the client and the server, an example for this in Vanilla would be the smelting progress of a furnace.
  
-To understand this tutorial you need to read the first [[tutorial:screenhandler|Screenhandler]] tutorial. +To understand this tutorial you need to read the first [[tutorial:screenhandler|ScreenHandler]] tutorial. 
 Methods which have no code here were already shown in that tutorial. Methods which have no code here were already shown in that tutorial.
  
Line 12: Line 12:
 As the Block class does not need to be changed at all we leave it out here. As the Block class does not need to be changed at all we leave it out here.
  
-Our BlockEntity now implements Tickable, this will provide the tick() method which gets called every tick. We use it to increase our Integer we want to sync+Our ''BlockEntity'' now implements ''Tickable'', this will provide the ''tick()'' method which gets called every tick. We use it to increase our Integer we want to sync.
  
 <code java [enable_line_numbers="true"] BoxBlockEntity.java> <code java [enable_line_numbers="true"] BoxBlockEntity.java>
Line 67: Line 67:
     @Override     @Override
     public Text getDisplayName() {     public Text getDisplayName() {
-        return new TranslatableText(getCachedState().getBlock().getTranslationKey());+        // For versions 1.18.2 and below, please use return new TranslatableText(getCachedState().getBlock().getTranslationKey()); 
 +        return Text.translatable(getCachedState().getBlock().getTranslationKey());
     }     }
  
Line 114: Line 115:
  
     }     }
 +     
 +    //we provide this getter for the synced integer so the Screen can access this to show it on screen
     public int getSyncedNumber(){     public int getSyncedNumber(){
         return propertyDelegate.get(0);         return propertyDelegate.get(0);
Line 131: Line 133:
 </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 that of 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.1597425033.txt.gz · Last modified: 2020/08/14 17:10 by manymoney2