tutorial:propertydelegates
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:propertydelegates [2020/08/14 16:57] – manymoney2 | tutorial:propertydelegates [2022/05/27 16:00] (current) – solidblock | ||
---|---|---|---|
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: |
+ | |||
+ | 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: | ||
+ | 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 '' | ||
+ | |||
+ | <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 | ||
+ | // | ||
+ | |||
+ | //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() { | ||
+ | // For versions 1.18.2 and below, please use return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | ||
+ | return Text.translatable(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++; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Our new ScreenHandler ====== | ||
+ | <code java [enable_line_numbers=" | ||
+ | |||
+ | public class BoxScreenHandler extends ScreenHandler { | ||
+ | private final Inventory inventory; | ||
+ | PropertyDelegate propertyDelegate; | ||
+ | |||
+ | //This constructor gets called on the client when the server wants it to open the screenHandler, | ||
+ | //The client will call the super constructor with an empty Inventory and the screenHandler will automatically | ||
+ | //sync this empty inventory with the inventory on the server | ||
+ | |||
+ | //Similar to the inventory, the client will allocate an empty propertyDelegate which will be synced with the Server automatically | ||
+ | |||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory) { | ||
+ | this(syncId, | ||
+ | } | ||
+ | |||
+ | //This constructor gets called from the BlockEntity on the server, the server knows the inventory of the container | ||
+ | //and can therefore directly provide it as an argument. This inventory aswell as the propertyDelegate will then be synced to the Client | ||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
+ | super(Test.BOX_SCREEN_HANDLER, | ||
+ | checkSize(inventory, | ||
+ | this.inventory = inventory; | ||
+ | this.propertyDelegate = propertyDelegate; | ||
+ | //some inventories do custom logic when a player opens it. | ||
+ | inventory.onOpen(playerInventory.player); | ||
+ | |||
+ | //we need to tell the screenhandler about our propertyDelegate, | ||
+ | this.addProperties(propertyDelegate); | ||
+ | |||
+ | //This will place the slot in the correct locations for a 3x3 Grid. The slots exist on both server and client! | ||
+ | [...] | ||
+ | |||
+ | } | ||
+ | |||
+ | //we provide this getter for the synced integer so the Screen can access this to show it on screen | ||
+ | public int getSyncedNumber(){ | ||
+ | return propertyDelegate.get(0); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean canUse(PlayerEntity player) { | ||
+ | return this.inventory.canPlayerUse(player); | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public ItemStack transferSlot(PlayerEntity player, int invSlot) {[...]} | ||
+ | } | ||
+ | |||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Showing the Information with the Screen ====== | ||
+ | As the screen gets the '' | ||
+ | <code java [enable_line_numbers=" | ||
+ | |||
+ | public class BoxScreen extends HandledScreen< | ||
+ | private static final Identifier TEXTURE = new Identifier(" | ||
+ | BoxScreenHandler screenHandler; | ||
+ | |||
+ | public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) { | ||
+ | super(handler, | ||
+ | //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, | ||
+ | renderBackground(matrices); | ||
+ | super.render(matrices, | ||
+ | drawMouseoverTooltip(matrices, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | protected void init() { | ||
+ | super.init(); | ||
+ | // Center the title | ||
+ | titleX = (backgroundWidth - textRenderer.getWidth(title)) / 2; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Result ====== | ||
+ | As the registration of the '' | ||
+ | When the BlockEntity is placed it will increase the '' | ||
+ | automatically be synced to the client and rendered in the top left corner. | ||
+ | |||
+ | [[https:// | ||
+ | |||
+ | If you want a more realistic example, you might want to have a look at '' | ||
- | 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: | ||
- | Methods which have no code here were already shown in that tutorial |
tutorial/propertydelegates.1597424273.txt.gz · Last modified: 2020/08/14 16:57 by manymoney2