3434import net .minecraft .registry .RegistryWrapper ;
3535import net .minecraft .util .Identifier ;
3636import net .fabricmc .api .ModInitializer ;
37- import net .fabricmc .fabric .api .client .event .lifecycle .v1 .ClientLifecycleEvents ;
38- import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationConnectionEvents ;
39- import net .fabricmc .fabric .api .client .networking .v1 .ClientConfigurationNetworking ;
40- import net .fabricmc .fabric .api .client .networking .v1 .ClientPlayConnectionEvents ;
4137import net .fabricmc .fabric .api .event .lifecycle .v1 .ServerLifecycleEvents ;
4238import net .fabricmc .fabric .api .networking .v1 .PayloadTypeRegistry ;
4339import net .fabricmc .fabric .api .networking .v1 .ServerConfigurationConnectionEvents ;
@@ -74,10 +70,9 @@ public class GrindEnchantmentsMod implements ModInitializer {
7470 public static final String MODID = "grindenchantments" ;
7571 public static final Logger LOGGER = LogManager .getLogger ("Grind Enchantments" );
7672
77- private static ClientConfig CLIENT_CONFIG = ClientConfig .DEFAULT ;
7873 @ Nullable
79- private static ServerConfig SERVER_CONFIG = null ;
80- private static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig .DEFAULT ;
74+ static ServerConfig SERVER_CONFIG = null ;
75+ static ServerConfig LOCAL_SERVER_CONFIG = ServerConfig .DEFAULT ;
8176
8277 @ Override
8378 public void onInitialize () {
@@ -90,43 +85,20 @@ public void onInitialize() {
9085 ServerLifecycleEvents .SERVER_STARTING .register (server -> {
9186 SERVER_CONFIG = initializeServerConfig (server .getRegistryManager ());
9287 SERVER_CONFIG .validateRegistryEntries (server .getRegistryManager ());
88+ LOCAL_SERVER_CONFIG = SERVER_CONFIG ;
9389 });
9490 ServerLifecycleEvents .SERVER_STOPPING .register (server -> SERVER_CONFIG = null );
9591
9692 // Multiplayer
9793 PayloadTypeRegistry .configurationS2C ().register (ServerConfigS2CPayload .ID ,
9894 ServerConfigS2CPayload .createPacketCodec (CostFunction .createPacketCodec ()));
9995
100- ClientLifecycleEvents .CLIENT_STARTED .register (client -> {
101- CLIENT_CONFIG = GrindEnchantmentsMod .initializeClientConfig ();
102- LOCAL_SERVER_CONFIG = GrindEnchantmentsMod .initializeServerConfig (RegistryWrapper .WrapperLookup .of (
103- Stream .of (GrindEnchantmentsRegistries .COST_FUNCTION )));
104-
105- ClientConfigurationNetworking .registerGlobalReceiver (ServerConfigS2CPayload .ID , (payload , context ) -> {
106- //noinspection resource
107- context .client ().execute (() -> {
108- // log(Level.DEBUG, payload.config());
109- SERVER_CONFIG = payload .config ();
110- });
111- });
112- ClientPlayConnectionEvents .INIT .register ((handler , client2 ) -> {
113- if (SERVER_CONFIG != null ) {
114- SERVER_CONFIG .validateRegistryEntries (handler .getRegistryManager ());
115- }
116- });
117- });
11896 ServerConfigurationConnectionEvents .CONFIGURE .register ((handler , server ) -> {
11997 if (ServerConfigurationNetworking .canSend (handler , ServerConfigS2CPayload .ID )) {
12098 ServerConfigurationNetworking .send (handler , new ServerConfigS2CPayload (SERVER_CONFIG != null ? SERVER_CONFIG : LOCAL_SERVER_CONFIG ));
12199 }
122100 });
123101
124- // Set server config to null when joining a world, so that it is known whether the server sent its config
125- ClientConfigurationConnectionEvents .INIT .register ((handler , client ) -> SERVER_CONFIG = null );
126- // Set server config to null when leaving the world too, for the same reason
127- ClientConfigurationConnectionEvents .DISCONNECT .register ((handler , client ) -> SERVER_CONFIG = null );
128- ClientPlayConnectionEvents .DISCONNECT .register ((handler , client ) -> SERVER_CONFIG = null );
129-
130102 DisenchantOperation disenchant = new DisenchantOperation ();
131103 MoveOperation move = new MoveOperation ();
132104 ResetRepairCostOperation resetRepairCost = new ResetRepairCostOperation ();
@@ -151,12 +123,12 @@ public void onInitialize() {
151123
152124 public static ServerConfig getServerConfig () {
153125 return SERVER_CONFIG == null ?
154- CLIENT_CONFIG .useLocalIfUnsynced () ? LOCAL_SERVER_CONFIG : ServerConfig .DISABLED
126+ GrindEnchantmentsClient . getClientConfig (). sync () .useLocalIfUnsynced () ? LOCAL_SERVER_CONFIG : ServerConfig .DISABLED
155127 : SERVER_CONFIG ;
156128 }
157129
158130 public static ClientConfig getClientConfig () {
159- return CLIENT_CONFIG ;
131+ return GrindEnchantmentsClient . getClientConfig () ;
160132 }
161133
162134 public static <C extends ModConfig <C >> ModConfig .Type <C , ? extends ModConfig <C >> getConfigType (ModConfig .Type <C , ? extends ModConfig <C >>[] versions , int version ) {
@@ -192,7 +164,7 @@ private static <C extends ModConfig<C>> C initializeGenericConfig(Path configNam
192164
193165 ConfigIo .encodeConfig (writer , codec , config .latest (), ops );
194166 } catch (IOException e ) {
195- log (Level .ERROR , "IO exception while trying to write updated config: " + e .getLocalizedMessage ());
167+ log (Level .ERROR , "IO exception while trying to write updated " + kind + " config: " + e .getLocalizedMessage ());
196168 } catch (ConfigException e ) {
197169 log (Level .ERROR , e .getLocalizedMessage ());
198170 }
@@ -203,14 +175,18 @@ private static <C extends ModConfig<C>> C initializeGenericConfig(Path configNam
203175 log (Level .ERROR , e .getLocalizedMessage ());
204176 }
205177 } else {
206- // Write default config if the file doesn't exist
207- try (OutputStream output = Files .newOutputStream (filePath , StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE );
208- OutputStreamWriter writer = new OutputStreamWriter (new BufferedOutputStream (output ))) {
209- log (Level .INFO , "Writing default " + kind + " config." );
178+ try {
179+ Files .createDirectories (filePath .getParent ());
180+
181+ // Write default config if the file doesn't exist
182+ try (OutputStream output = Files .newOutputStream (filePath , StandardOpenOption .CREATE_NEW , StandardOpenOption .WRITE );
183+ OutputStreamWriter writer = new OutputStreamWriter (new BufferedOutputStream (output ))) {
184+ log (Level .INFO , "Writing default " + kind + " config." );
210185
211- ConfigIo .encodeConfig (writer , codec , latestDefault , ops );
186+ ConfigIo .encodeConfig (writer , codec , latestDefault , ops );
187+ }
212188 } catch (IOException e ) {
213- log (Level .ERROR , "IO exception while trying to write config: " + e .getLocalizedMessage ());
189+ log (Level .ERROR , "IO exception while trying to write " + kind + " config: " + e .getLocalizedMessage ());
214190 } catch (ConfigException e ) {
215191 log (Level .ERROR , e .getLocalizedMessage ());
216192 }
@@ -224,7 +200,7 @@ public static ClientConfig initializeClientConfig() {
224200 JsonOps .INSTANCE , "client" );
225201 }
226202
227- private static ServerConfig initializeServerConfig (RegistryWrapper .WrapperLookup wrapperLookup ) {
203+ public static ServerConfig initializeServerConfig (RegistryWrapper .WrapperLookup wrapperLookup ) {
228204 return initializeGenericConfig (Path .of ("server.json" ), ServerConfig .DEFAULT , ServerConfig .CODEC ,
229205 RegistryOps .of (JsonOps .INSTANCE , wrapperLookup ), "server" );
230206 }
0 commit comments