tutorial:propertydelegates
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:propertydelegates [2020/08/14 16:57] – manymoney2 | tutorial: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 | + | **PropertyDelegate: |
- | An Example | + | In this Tutorial we will sync Integer values between the client and the Server, an example |
To understand this tutorial you need to read the first [[tutorial: | To understand this tutorial you need to read the first [[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. |
+ | |||
+ | We will not use the [[tutorial: | ||
+ | |||
+ | ====== 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=" | ||
+ | |||
+ | public class BoxBlockEntity extends BlockEntity implements NamedScreenHandlerFactory, | ||
+ | private final DefaultedList< | ||
+ | //this is the int we want to sync, it gets increased by one each tick | ||
+ | private int syncedInt; | ||
+ | |||
+ | // | ||
+ | //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< | ||
+ | return inventory; | ||
+ | |||
+ | } | ||
+ | |||
+ | //These Methods are from the NamedScreenHandlerFactory Interface | ||
+ | |||
+ | @Override | ||
+ | public @Nullable ScreenHandler createMenu(int syncId, PlayerInventory playerInventory, | ||
+ | //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, | ||
+ | } | ||
+ | |||
+ | @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++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ |
tutorial/propertydelegates.txt · Last modified: 2022/05/27 16:00 by solidblock