1
1
package com .tanishisherewith .dynamichud ;
2
2
3
3
import com .tanishisherewith .dynamichud .config .GlobalConfig ;
4
- import com .tanishisherewith .dynamichud .screens .AbstractMoveableScreen ;
5
- import com .tanishisherewith .dynamichud .utils .BooleanPool ;
6
- import com .tanishisherewith .dynamichud .widget .Widget ;
7
- import com .tanishisherewith .dynamichud .widget .WidgetManager ;
8
- import com .tanishisherewith .dynamichud .widget .WidgetRenderer ;
9
- import com .tanishisherewith .dynamichud .widgets .TextWidget ;
4
+ import com .tanishisherewith .dynamichud .integration .IntegrationManager ;
10
5
import net .fabricmc .api .ClientModInitializer ;
11
- import net .fabricmc .fabric .api .client .event .lifecycle .v1 .ClientLifecycleEvents ;
12
- import net .fabricmc .fabric .api .client .event .lifecycle .v1 .ClientTickEvents ;
6
+ import net .fabricmc .api .EnvType ;
7
+ import net .fabricmc .api .Environment ;
8
+ import net .fabricmc .fabric .api .client .command .v2 .FabricClientCommandSource ;
13
9
import net .fabricmc .fabric .api .client .rendering .v1 .HudRenderCallback ;
14
- import net .fabricmc .fabric .api .event .lifecycle .v1 .ServerLifecycleEvents ;
15
- import net .fabricmc .fabric .api .networking .v1 .ServerPlayConnectionEvents ;
16
- import net .fabricmc .loader .api .FabricLoader ;
17
- import net .fabricmc .loader .api .metadata .ModMetadata ;
18
10
import net .minecraft .client .MinecraftClient ;
19
- import net .minecraft .client .option .KeyBinding ;
20
11
import org .slf4j .Logger ;
21
12
import org .slf4j .LoggerFactory ;
22
13
23
- import java .io .File ;
24
- import java .io .IOException ;
25
- import java .util .ArrayList ;
26
- import java .util .HashMap ;
27
- import java .util .List ;
28
- import java .util .Objects ;
29
-
14
+ @ Environment (EnvType .CLIENT )
30
15
public class DynamicHUD implements ClientModInitializer {
31
- /**
32
- * This is a map to store the list of widgets for each widget file to be saved.
33
- * <p>
34
- * Allows saving widgets across different mods with same save file name.
35
- */
36
- public static final HashMap <String , List <Widget >> FILE_MAP = new HashMap <>();
37
- public static final Logger logger = LoggerFactory .getLogger ("DynamicHud" );
38
- private static final List <WidgetRenderer > widgetRenderers = new ArrayList <>();
39
16
public static MinecraftClient MC = MinecraftClient .getInstance ();
17
+ public static final Logger logger = LoggerFactory .getLogger ("DynamicHud" );
40
18
public static String MOD_ID = "dynamichud" ;
41
19
42
- public static void addWidgetRenderer (WidgetRenderer widgetRenderer ) {
43
- widgetRenderers .add (widgetRenderer );
44
- }
45
-
46
- public static List <WidgetRenderer > getWidgetRenderers () {
47
- return widgetRenderers ;
48
- }
49
-
50
20
public static void printInfo (String msg ) {
51
21
logger .info (msg );
52
22
}
@@ -55,133 +25,16 @@ public static void printWarn(String msg) {
55
25
logger .warn (msg );
56
26
}
57
27
58
- /**
59
- * Opens the MovableScreen when the specified key is pressed.
60
- *
61
- * @param key The key to listen for
62
- * @param screen The AbstractMoveableScreen instance to use to set the screen
63
- */
64
- public static void openDynamicScreen (KeyBinding key , AbstractMoveableScreen screen ) {
65
- if (key .wasPressed ()) {
66
- MC .setScreen (screen );
67
- }
68
- }
69
-
70
28
@ Override
71
29
public void onInitializeClient () {
72
- printInfo ("Initialising DynamicHud" );
73
-
74
- // Add WidgetData of included widgets
75
- WidgetManager .registerCustomWidgets (
76
- TextWidget .DATA
77
- );
30
+ printInfo ("Initialising DynamicHUD" );
78
31
79
32
//YACL load
80
33
GlobalConfig .HANDLER .load ();
81
34
82
- printInfo ("Integrating mods..." );
83
- FabricLoader .getInstance ()
84
- .getEntrypointContainers ("dynamicHud" , DynamicHudIntegration .class )
85
- .forEach (entrypoint -> {
86
- ModMetadata metadata = entrypoint .getProvider ().getMetadata ();
87
- String modId = metadata .getId ();
88
-
89
- printInfo (String .format ("Supported mod with id %s was found!" , modId ));
90
-
91
- AbstractMoveableScreen screen ;
92
- KeyBinding binding ;
93
- WidgetRenderer widgetRenderer ;
94
- File widgetsFile ;
95
- try {
96
- DynamicHudIntegration DHIntegration = entrypoint .getEntrypoint ();
97
-
98
- //Calls the init method
99
- DHIntegration .init ();
100
-
101
- //Gets the widget file to save and load the widgets from
102
- widgetsFile = DHIntegration .getWidgetsFile ();
103
-
104
- // Adds / loads widgets from file
105
- if (widgetsFile .exists ()) {
106
- WidgetManager .loadWidgets (widgetsFile );
107
- } else {
108
- DHIntegration .addWidgets ();
109
- }
110
-
111
- //Calls the second init method
112
- DHIntegration .initAfter ();
113
-
114
- // Get the instance of AbstractMoveableScreen
115
- screen = Objects .requireNonNull ( DHIntegration .getMovableScreen ());
116
-
117
- // Get the keybind to open the screen instance
118
- binding = DHIntegration .getKeyBind ();
119
-
120
- //Register custom widget datas by WidgetManager.registerCustomWidgets();
121
- DHIntegration .registerCustomWidgets ();
122
-
123
- //WidgetRenderer with widgets instance
124
- widgetRenderer = DHIntegration .getWidgetRenderer ();
125
- addWidgetRenderer (widgetRenderer );
126
-
127
- List <Widget > widgets = FILE_MAP .get (widgetsFile .getName ());
128
-
129
- if (widgets == null || widgets .isEmpty ()) {
130
- FILE_MAP .put (widgetsFile .getName (), widgetRenderer .getWidgets ());
131
- } else {
132
- widgets .addAll (widgetRenderer .getWidgets ());
133
- FILE_MAP .put (widgetsFile .getName (), widgets );
134
- }
135
-
136
- //Register events for rendering, saving, loading, and opening the hudEditor
137
- ClientTickEvents .START_CLIENT_TICK .register ((client ) -> openDynamicScreen (binding , screen ));
138
-
139
- /* === Saving === */
140
-
141
- //When a player exits a world (SinglePlayer worlds) or a server stops
142
- ServerLifecycleEvents .SERVER_STOPPING .register (server -> saveWidgetsSafely (widgetsFile , FILE_MAP .get (widgetsFile .getName ())));
143
-
144
- // When a resource pack is reloaded.
145
- ServerLifecycleEvents .END_DATA_PACK_RELOAD .register ((server , resourceManager , s ) -> saveWidgetsSafely (widgetsFile , FILE_MAP .get (widgetsFile .getName ())));
146
-
147
- //When player disconnects
148
- ServerPlayConnectionEvents .DISCONNECT .register ((handler , packetSender ) -> saveWidgetsSafely (widgetsFile , FILE_MAP .get (widgetsFile .getName ())));
149
-
150
- //When minecraft closes
151
- ClientLifecycleEvents .CLIENT_STOPPING .register ((minecraftClient ) -> saveWidgetsSafely (widgetsFile , FILE_MAP .get (widgetsFile .getName ())));
152
-
153
- printInfo (String .format ("Integration of mod %s was successful" , modId ));
154
- } catch (Throwable e ) {
155
- if (e instanceof IOException ) {
156
- logger .warn ("An error has occurred while loading widgets of mod {}" , modId , e );
157
- } else {
158
- logger .warn ("Mod {} has improper implementation of DynamicHUD" , modId , e );
159
- }
160
- }
161
- });
162
- printInfo ("(DynamicHUD) Integration of mods found was successful" );
163
-
164
-
165
- //Global config saving (YACL)
166
- ServerLifecycleEvents .SERVER_STOPPING .register (server -> GlobalConfig .HANDLER .save ());
167
- ServerLifecycleEvents .END_DATA_PACK_RELOAD .register ((server , resourceManager , s ) -> GlobalConfig .HANDLER .save ());
168
- ServerPlayConnectionEvents .DISCONNECT .register ((handler , packetSender ) -> GlobalConfig .HANDLER .save ());
169
- ClientLifecycleEvents .CLIENT_STOPPING .register ((minecraftClient ) -> {
170
- GlobalConfig .HANDLER .save ();
171
- });
172
-
35
+ IntegrationManager .integrate ();
173
36
174
37
//In game screen render.
175
38
HudRenderCallback .EVENT .register (new HudRender ());
176
39
}
177
-
178
- private void saveWidgetsSafely (File widgetsFile , List <Widget > widgets ) {
179
- try {
180
- WidgetManager .saveWidgets (widgetsFile , widgets );
181
- } catch (IOException e ) {
182
- logger .error ("Failed to save widgets. Widgets passed: {}" , widgets );
183
- throw new RuntimeException (e );
184
- }
185
- }
186
-
187
40
}
0 commit comments