3333import java .util .IdentityHashMap ;
3434import java .util .List ;
3535import java .util .Map ;
36- import net .fabricmc .api .ClientModInitializer ;
3736import net .fabricmc .fabric .api .client .event .lifecycle .v1 .ClientChunkEvents ;
3837import net .fabricmc .fabric .api .client .event .lifecycle .v1 .ClientTickEvents ;
3938import net .fabricmc .fabric .api .client .keybinding .v1 .KeyBindingHelper ;
4443import net .minecraft .world .level .ChunkPos ;
4544import org .apache .logging .log4j .LogManager ;
4645import org .apache .logging .log4j .Logger ;
46+ import org .jetbrains .annotations .ApiStatus ;
4747import org .jetbrains .annotations .NotNull ;
4848import org .lwjgl .glfw .GLFW ;
4949
50- public final class MapSyncMod implements ClientModInitializer {
51- private static final Minecraft mc = Minecraft .getInstance ();
52-
50+ public final class MapSyncMod {
5351 public static final Logger logger = LogManager .getLogger (MapSyncMod .class );
5452
55- private static MapSyncMod INSTANCE ;
56-
57- public static ModConfig modConfig ;
58-
59- public static MapSyncMod getMod () {
60- return INSTANCE ;
61- }
62-
63- public static final String MOD_ID = "mapsync" ;
64-
65- public static final Identifier CATEGORY_ID = Identifier .fromNamespaceAndPath (MOD_ID , "general" );
66- public static final KeyMapping .Category CATEGORY = KeyMapping .Category .register (CATEGORY_ID );
67-
68- private static final KeyMapping openGuiKey = new KeyMapping (
69- "key.map-sync.openGui" ,
70- InputConstants .Type .KEYSYM ,
71- GLFW .GLFW_KEY_COMMA ,
72- CATEGORY
73- //"category.map-sync"
53+ private static final KeyMapping .Category KEY_MAPPING_CATEGORY = KeyMapping .Category .register (Identifier .fromNamespaceAndPath ("mapsync" , "general" ));
54+ private static final KeyMapping OPEN_GUI_KEY = new KeyMapping (
55+ "key.map-sync.openGui" ,
56+ InputConstants .Type .KEYSYM ,
57+ GLFW .GLFW_KEY_COMMA ,
58+ KEY_MAPPING_CATEGORY
7459 );
7560
76- public MapSyncMod () {
77- if (INSTANCE != null ) throw new IllegalStateException ("Constructor called twice" );
78- INSTANCE = this ;
79- }
61+ public static ModConfig modConfig ;
8062
81- @ Override
82- public void onInitializeClient () {
83- KeyBindingHelper .registerKeyBinding (openGuiKey );
63+ @ ApiStatus . Internal
64+ public static void bootstrap () {
65+ KeyBindingHelper .registerKeyBinding (OPEN_GUI_KEY );
8466
8567 modConfig = ModConfig .load ();
8668 modConfig .saveNow (); // creates the default file if it doesn't exist yet
8769
8870 ClientTickEvents .START_CLIENT_TICK .register ((minecraft ) -> {
89- try {
90- handleTick (minecraft );
71+ final GameContext gameContext = GameContext .get ().orElse (null );
72+ if (gameContext == null ) { // This *shouldn't* ever happen, but just case
73+ return ;
9174 }
92- catch ( Throwable e ) {
93- e . printStackTrace ( );
75+ while ( OPEN_GUI_KEY . consumeClick () ) {
76+ minecraft . setScreen ( new SyncConnectionsGui ( minecraft . screen , gameContext ) );
9477 }
78+ gameContext .getDimensionState ().ifPresent (DimensionState ::onTick );
9579 });
9680 GameContext .initEvents ();
9781 ClientChunkEvents .CHUNK_LOAD .register ((level , chunk ) -> {
@@ -121,37 +105,22 @@ public void onInitializeClient() {
121105 });
122106 }
123107
124- public void handleTick (
125- final @ NotNull Minecraft minecraft
126- ) {
127- final GameContext gameContext = GameContext .get ().orElse (null );
128- if (gameContext == null ) { // This *shouldn't* ever happen, but just case
129- return ;
130- }
131-
132- while (openGuiKey .consumeClick ()) {
133- minecraft .setScreen (new SyncConnectionsGui (minecraft .screen , gameContext ));
134- }
135-
136- gameContext .getDimensionState ().ifPresent (DimensionState ::onTick );
137- }
138-
139- public void handleSyncConnection (
108+ public static void handleSyncConnection (
140109 final @ NotNull SyncClient client
141110 ) throws Exception {
142111 client .authState .set (null );
143112 AuthProcess .sendHandshake (client );
144113 }
145114
146- public void handleSyncDisconnection (
115+ public static void handleSyncDisconnection (
147116 final @ NotNull SyncClient client ,
148117 final @ NotNull CloseContext context
149118 ) {
150119 client .authState .set (null );
151120 }
152121
153122 /// BEWARE: This is called from whatever thread the given SyncClient websocket is using for reads.
154- public void handleSyncPacket (
123+ public static void handleSyncPacket (
155124 final @ NotNull SyncClient client ,
156125 final @ NotNull Packet received
157126 ) throws Exception {
@@ -166,7 +135,7 @@ public void handleSyncPacket(
166135 }
167136
168137 /// @param clientLevel This is the *new* dimension.
169- public void handleDimensionChange (
138+ public static void handleDimensionChange (
170139 final @ NotNull Minecraft minecraft ,
171140 final @ NotNull ClientLevel clientLevel ,
172141 final @ NotNull GameContext gameContext
@@ -179,16 +148,11 @@ public void handleDimensionChange(
179148 * part of a chunk changed, and the chunk is likely to change again soon,
180149 * so a ChunkTile update is queued, instead of updating instantly.
181150 */
182- public void handleMcChunkPartialChange (int cx , int cz ) {
151+ public static void handleMcChunkPartialChange (int cx , int cz ) {
183152 // TODO update ChunkTile in a second or so; remember dimension in case it changes til then
184153 }
185154
186- public void handleSyncServerEncryptionSuccess () {
187- debugLog ("tcp encrypted" );
188- // TODO tell server our current dimension
189- }
190-
191- public void handleRegionTimestamps (SyncClient client , ClientboundRegionTimestampsPacket packet ) {
155+ public static void handleRegionTimestamps (SyncClient client , ClientboundRegionTimestampsPacket packet ) {
192156 client .authState .requireWelcomed ();
193157 DimensionState dimension = client .gameContext .getDimensionState ().orElse (null );
194158 if (dimension == null ) return ;
@@ -212,7 +176,7 @@ public void handleRegionTimestamps(SyncClient client, ClientboundRegionTimestamp
212176 }
213177 }
214178
215- public void handleSharedChunk (SyncClient client , ChunkTile chunkTile ) {
179+ public static void handleSharedChunk (SyncClient client , ChunkTile chunkTile ) {
216180 client .authState .requireWelcomed ();
217181 debugLog ("received shared chunk: " + chunkTile .chunkPos ());
218182 for (SyncClient syncClient : client .gameContext .getSyncConnections ()) {
@@ -222,7 +186,7 @@ public void handleSharedChunk(SyncClient client, ChunkTile chunkTile) {
222186 client .gameContext .getDimensionState ().ifPresent ((dimensionState ) -> dimensionState .processSharedChunk (chunkTile ));
223187 }
224188
225- public void handleCatchupData (SyncClient client , ClientboundChunkTimestampsResponsePacket packet ) {
189+ public static void handleCatchupData (SyncClient client , ClientboundChunkTimestampsResponsePacket packet ) {
226190 client .authState .requireWelcomed ();
227191 for (CatchupChunk chunk : packet .chunks ()) {
228192 chunk .syncClient = client ;
@@ -233,7 +197,7 @@ public void handleCatchupData(SyncClient client, ClientboundChunkTimestampsRespo
233197 });
234198 }
235199
236- public void requestCatchupData (
200+ public static void requestCatchupData (
237201 final @ NotNull DimensionState dimensionState ,
238202 final List <@ NotNull CatchupChunk > chunks
239203 ) {
0 commit comments