tutorial:extendedscreenhandler
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
tutorial:extendedscreenhandler [2020/08/14 16:14] – [BlockEntity] manymoney2 | tutorial:extendedscreenhandler [2022/12/17 15:38] (current) – [Registering our ScreenHandler] registry (f***) miir | ||
---|---|---|---|
Line 1: | Line 1: | ||
====== Syncing Custom Data with Extended ScreenHandlers ====== | ====== Syncing Custom Data with Extended ScreenHandlers ====== | ||
- | In this tutorial we will use the ExtendedScreenHandler to transfer arbitary data from the Server | + | In this tutorial we will use the ExtendedScreenHandler to transfer arbitary data from the server |
- | In our example we will send the position of the block and render it as the containers | + | In our example we will send the position of the block and render it as the container' |
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. |
====== 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 | + | Our block entity |
<code java [enable_line_numbers=" | <code java [enable_line_numbers=" | ||
Line 41: | Line 41: | ||
@Override | @Override | ||
public Text getDisplayName() { | public Text getDisplayName() { | ||
+ | // versions 1.18 and below | ||
return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | return new TranslatableText(getCachedState().getBlock().getTranslationKey()); | ||
+ | | ||
+ | // versions 1.19 and later | ||
+ | return Text.translatable(getCachedState().getBlock().getTranslationKey()); | ||
} | } | ||
Line 59: | Line 63: | ||
</ | </ | ||
+ | |||
+ | ====== Our new ExtendedScreenHandler ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public class BoxScreenHandler extends ScreenHandler { | ||
+ | //We save the blockPos we got from the Server and provide a getter for it so the BoxScreen can read that information | ||
+ | private BlockPos pos; | ||
+ | private final Inventory inventory; | ||
+ | |||
+ | //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 | ||
+ | |||
+ | //NEW: The constructor of the client now gets the PacketByteBuf we filled in the BlockEntity | ||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
+ | this(syncId, | ||
+ | pos = buf.readBlockPos(); | ||
+ | } | ||
+ | |||
+ | //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 will then be synced to the Client | ||
+ | public BoxScreenHandler(int syncId, PlayerInventory playerInventory, | ||
+ | //[...] | ||
+ | // See first Screenhandler Tutorial for the rest of the code | ||
+ | | ||
+ | //Why do we use BlockPos.ORIGIN here? | ||
+ | //This is because the packetByteBuf with our blockPosition is only availible on the Client, so we need a placeholder | ||
+ | //value here. This is not a problem however, as the Server version of the ScreenHandler does not really need this | ||
+ | // | ||
+ | pos = BlockPos.ORIGIN; | ||
+ | |||
+ | [...] | ||
+ | } | ||
+ | |||
+ | //this getter will be used by our Screen class | ||
+ | public BlockPos getPos() { | ||
+ | return pos; | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public boolean canUse(PlayerEntity player) { | ||
+ | return this.inventory.canPlayerUse(player); | ||
+ | } | ||
+ | |||
+ | // See Screenhandler Tutorial | ||
+ | // Shift + Player Inv Slot | ||
+ | @Override | ||
+ | public ItemStack transferSlot(PlayerEntity player, int invSlot); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Using the Information of the ExtendedScreenHandler in our Screen ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | |||
+ | public class BoxScreen extends HandledScreen< | ||
+ | private static final Identifier TEXTURE = new Identifier(" | ||
+ | |||
+ | public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) { | ||
+ | super(handler, | ||
+ | //We try to get the block position to use it as our title, if that fails for some reason we will use the default title | ||
+ | } | ||
+ | |||
+ | //This method will try to get the Position from the ScreenHandler, | ||
+ | //get the ScreenHandler instance here which has the correct BlockPos in it! | ||
+ | private static Optional< | ||
+ | if (handler instanceof BoxScreenHandler) { | ||
+ | BlockPos pos = ((BoxScreenHandler) handler).getPos(); | ||
+ | // for versions 1.18.2 and below, use `new LiteralText` | ||
+ | return pos != null ? Optional.of(Text.literal(" | ||
+ | } else { | ||
+ | return Optional.empty(); | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | @Override | ||
+ | protected void drawBackground(MatrixStack matrices, float delta, int mouseX, int mouseY) { [...] } | ||
+ | |||
+ | @Override | ||
+ | public void render(MatrixStack matrices, int mouseX, int mouseY, float delta) { [...] } | ||
+ | |||
+ | @Override | ||
+ | protected void init() { [...] } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | ====== Registering our ScreenHandler ====== | ||
+ | |||
+ | <code java [enable_line_numbers=" | ||
+ | public class ExampleMod implements ModInitializer { | ||
+ | |||
+ | [...] | ||
+ | public static final ScreenHandlerType< | ||
+ | |||
+ | static { | ||
+ | [...] | ||
+ | |||
+ | //we now use registerExtended as our screenHandler now accepts a packetByteBuf in its Constructor | ||
+ | BOX_SCREEN_HANDLER = Registry.register(Registries.SCREEN_HANDLER, | ||
+ | } | ||
+ | |||
+ | @Override | ||
+ | public void onInitialize() { | ||
+ | |||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ====== Result ====== | ||
+ | You have now seen how to transfer data when the ScreenHandler is opened. In the image you can see the result: The Block' | ||
+ | there are easier ways of setting the position as the title. | ||
+ | |||
+ | You might be wondering: //Can I transfer this data again even after the Screen was opened?// | ||
+ | This is possible by sending custom Packets (see: [[tutorial: | ||
+ | You might also want to have a look at the '' | ||
+ | |||
+ | If you only want to sync integer values you can use '' | ||
+ | |||
+ | {{: | ||
+ | |||
+ |
tutorial/extendedscreenhandler.txt · Last modified: 2022/12/17 15:38 by miir