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 17:06] – manymoney2 | tutorial:propertydelegates [2020/08/14 17:15] – [Showing the Information with the Screen] manymoney2 | ||
---|---|---|---|
Line 11: | Line 11: | ||
====== BlockEntity ====== | ====== BlockEntity ====== | ||
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 | ||
Line 74: | Line 75: | ||
if(!world.isClient) | if(!world.isClient) | ||
syncedInt++; | 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 ScreenHandler in its constructor, | ||
+ | <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; | ||
} | } | ||
} | } |
tutorial/propertydelegates.txt · Last modified: 2022/05/27 16:00 by solidblock