User Tools

Site Tools


tutorial:propertydelegates

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
Next revisionBoth sides next revision
tutorial:propertydelegates [2020/08/14 16:57] – created manymoney2tutorial:propertydelegates [2020/08/14 17:06] manymoney2
Line 1: Line 1:
 ====== Syncing Integers with PropertyDelegates ====== ====== Syncing Integers with PropertyDelegates ======
-In this Tutorial we will sync Integer values between the client and the Server.   +**PropertyDelegate:** A PropertyDelegate is a kind of Container which contains a specific amounts of integer values which can be read or changed. 
-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.  
 +Methods which have no code here were already shown in that tutorial. 
 + 
 +We will not use the [[tutorial:extendedscreenhandler|ExtendedScreenHandler]] in this tutorial any more to save us some complexity. 
 + 
 +====== BlockEntity ====== 
 +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 
 + 
 +<code java [enable_line_numbers="true"] BoxBlockEntity.java> 
 + 
 +public class BoxBlockEntity extends BlockEntity implements NamedScreenHandlerFactory, ImplementedInventory, Tickable { 
 +    private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(9, ItemStack.EMPTY); 
 +    //this is the int we want to sync, it gets increased by one each tick  
 +    private int syncedInt; 
 +     
 +    //PropertyDelegate is an interface which we will implement inline here. 
 +    //It can normally contain multiple integers as data identified by the index, but in this example we only have one. 
 +    private final PropertyDelegate propertyDelegate = new PropertyDelegate() { 
 +        @Override 
 +        public int get(int index) { 
 +            return syncedInt; 
 +        } 
 + 
 +        @Override 
 +        public void set(int index, int value) { 
 +            syncedInt = value; 
 +        } 
 + 
 +        //this is supposed to return the amount of integers you have in your delegate, in our example only one 
 +        @Override 
 +        public int size() { 
 +            return 1; 
 +        } 
 +    }; 
 + 
 +    public BoxBlockEntity() { 
 +        super(Test.BOX_BLOCK_ENTITY); 
 +    } 
 + 
 + 
 +    //From the ImplementedInventory Interface 
 + 
 +    @Override 
 +    public DefaultedList<ItemStack> getItems() { 
 +        return inventory; 
 + 
 +    } 
 + 
 +    //These Methods are from the NamedScreenHandlerFactory Interface 
 + 
 +    @Override 
 +    public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, PlayerEntity player) { 
 +        //We provide this to the screenHandler as our class Implements Inventory 
 +        //Only the Server has the Inventory at the start, this will be synced to the client in the ScreenHandler 
 + 
 +        //Similar to the inventory: The server has the PropertyDelegate and gives it to the server instance of the screen handler directly 
 +        return new BoxScreenHandler(syncId, playerInventory, this,propertyDelegate); 
 +    } 
 + 
 +    @Override 
 +    public Text getDisplayName() { 
 +        return new TranslatableText(getCachedState().getBlock().getTranslationKey()); 
 +    } 
 + 
 +    //increase the synced Integer by one each tick, we only do this on the server for demonstration purposes. 
 +    @Override 
 +    public void tick() { 
 +    if(!world.isClient) 
 +        syncedInt++; 
 +    } 
 +
 + 
 +</code> 
 + 
tutorial/propertydelegates.txt · Last modified: 2022/05/27 16:00 by solidblock