You can create your own widget by extending the Widget class
publicclassMyWidgetextendsWidget{ /** * Constructs a Widget object. * * @param client The Minecraft client instance */publicMyWidget(MinecraftClient client, <OtherParameters>) { super(client); } @OverridepublicWidgetBoxgetWidgetBox() {// x1,y1,x2,y2 are used to define the box around the widget// scale variable to use the value from user or set your own scalereturnnewWidgetBox(x1,y1,x2,y2,scale); } @Overridepublicvoidrender(DrawContext drawContext) { }}
You can render whatever you want within the render() method
If you want to save and load this widget then you can add the following:
To save and load this Widget, you can implement the WidgetLoading class in your main class:
Using this code, we get the className from the load file of the widgets and using that we determine if the widget is of MyWidget class. If it is then we create a new widget whose parameter values are changed by reading the widget save data ( i.e, xpercent, yPercent, enabled, etc.)
Now set the MyMainClass as widgetLoading so that it is registered during loading or saving: