User Tools

Site Tools


tutorial:extendedscreenhandler

Differences

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

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
tutorial:extendedscreenhandler [2020/08/14 16:20] – [Our new ExtendedScreenHandler] manymoney2tutorial: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 to the Client ScreenHandler when the ScreenHandler is opened.+In this tutorial we will use the ExtendedScreenHandler to transfer arbitary data from the server to the client ScreenHandler when the ScreenHandler is opened.
  
-In our example we will send the position of the block and render it as the containers title+In our example we will send the position of the block and render it as the container'title.
  
 To understand this tutorial you need to read the first [[tutorial:screenhandler|Screenhandler]] tutorial.  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+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 now implements ExtendedScreenHandlerFactory, this interfaces provides us the writeScreenOpeningData method, which will be called on the server when it requests the client to open a screenHandler. The data you write into the PacketByteBuf will be transfered to the client over the network.+Our block entity now implements ''ExtendedScreenHandlerFactory'', this interfaces provides us the ''writeScreenOpeningData'' method, which will be called on the server when it requests the client to open a ''ScreenHandler''. The data you write into the ''PacketByteBuf'' will be transferred to the client over the network.
  
 <code java [enable_line_numbers="true"] BoxBlockEntity.java> <code java [enable_line_numbers="true"] BoxBlockEntity.java>
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 62: Line 66:
 ====== Our new ExtendedScreenHandler ====== ====== Our new ExtendedScreenHandler ======
  
-<code java [enable_line_numbers="true"BoxScrenHandler.java>+<code java [enable_line_numbers="true"BoxScreenHandler.java>
 public class BoxScreenHandler extends ScreenHandler { 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     //We save the blockPos we got from the Server and provide a getter for it so the BoxScreen can read that information
Line 109: Line 113:
 } }
 </code> </code>
 +
 +====== Using the Information of the ExtendedScreenHandler in our Screen ======
 +
 +<code java [enable_line_numbers="true"] BoxScreen.java>
 +
 +public class BoxScreen extends HandledScreen<ScreenHandler> {
 +    private static final Identifier TEXTURE = new Identifier("minecraft", "textures/gui/container/dispenser.png");
 +
 +    public BoxScreen(ScreenHandler handler, PlayerInventory inventory, Text title) {
 +        super(handler, inventory, getPositionText(handler).orElse(title));
 +        //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, as ScreenRendering only happens on the client we
 +    //get the ScreenHandler instance here which has the correct BlockPos in it!
 +    private static Optional<Text> getPositionText(ScreenHandler handler) {
 +        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("(" + pos.toShortString() + ")")) : Optional.empty();
 +        } 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() { [...] }
 +}
 +
 +</code>
 +
 +====== Registering our ScreenHandler ======
 +
 +<code java [enable_line_numbers="true"] ExampleMod.java>
 +public class ExampleMod implements ModInitializer {
 +
 +    [...]
 +    public static final ScreenHandlerType<BoxScreenHandler> BOX_SCREEN_HANDLER = new ExtendedScreenHandlerType<>(BoxScreenHandler::new);
 +
 +    static {
 +        [...]
 +       
 +        //we now use registerExtended as our screenHandler now accepts a packetByteBuf in its Constructor
 +        BOX_SCREEN_HANDLER = Registry.register(Registries.SCREEN_HANDLER, new Identifier("mymod", "box"), BOX);
 +    }
 +
 +    @Override
 +    public void onInitialize() {
 +
 +    }
 +}
 +</code>
 +
 +====== Result ======
 +You have now seen how to transfer data when the ScreenHandler is opened. In the image you can see the result: The Block's title is now the block position. Do note that this is just a demonstration,
 +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:networking|Networking Tutorial]]) after the Screen has been opened. \\
 +You might also want to have a look at the ''BlockEntityClientSerializable'' interface from the Fabric API.
 +
 +If you only want to sync integer values you can use ''PropertyDelegate''s: [[tutorial:propertydelegates]].
 +
 +{{:tutorial:bildschirmfoto_vom_2020-08-14_18-37-51.png?nolink&400|}}
 +
  
tutorial/extendedscreenhandler.1597422025.txt.gz · Last modified: 2020/08/14 16:20 by manymoney2