|
19 | 19 |
|
20 | 20 | package de.mschae23.grindenchantments; |
21 | 21 |
|
| 22 | +import java.io.IOException; |
| 23 | +import java.io.InputStream; |
| 24 | +import java.nio.file.Files; |
22 | 25 | import java.nio.file.Path; |
23 | 26 | import java.nio.file.Paths; |
24 | | -import net.minecraft.registry.RegistryOps; |
| 27 | +import java.util.Optional; |
25 | 28 | import net.minecraft.util.Identifier; |
26 | 29 | import net.fabricmc.api.ModInitializer; |
27 | | -import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents; |
28 | 30 | import net.fabricmc.loader.api.FabricLoader; |
29 | 31 | import com.mojang.serialization.Codec; |
30 | 32 | import com.mojang.serialization.JsonOps; |
31 | 33 | import com.mojang.serialization.MapCodec; |
32 | | -import de.mschae23.config.api.ConfigIo; |
33 | 34 | import de.mschae23.config.api.ModConfig; |
| 35 | +import de.mschae23.config.api.exception.ConfigException; |
| 36 | +import de.mschae23.config.impl.ConfigUtil; |
34 | 37 | import de.mschae23.grindenchantments.config.legacy.v1.GrindEnchantmentsConfigV1; |
35 | 38 | import de.mschae23.grindenchantments.config.legacy.v2.GrindEnchantmentsConfigV2; |
36 | | -import de.mschae23.grindenchantments.config.legacy.GrindEnchantmentsConfigV3; |
| 39 | +import de.mschae23.grindenchantments.config.legacy.v3.GrindEnchantmentsConfigV3; |
37 | 40 | import de.mschae23.grindenchantments.cost.CostFunctionType; |
38 | 41 | import de.mschae23.grindenchantments.event.ApplyLevelCostEvent; |
39 | 42 | import de.mschae23.grindenchantments.event.GrindstoneEvents; |
|
45 | 48 | import org.apache.logging.log4j.Level; |
46 | 49 | import org.apache.logging.log4j.LogManager; |
47 | 50 | import org.apache.logging.log4j.Logger; |
| 51 | +import org.jetbrains.annotations.Nullable; |
48 | 52 |
|
49 | 53 | public class GrindEnchantmentsMod implements ModInitializer { |
50 | 54 | public static final String MODID = "grindenchantments"; |
51 | 55 | public static final Logger LOGGER = LogManager.getLogger("Grind Enchantments"); |
52 | 56 |
|
53 | | - public static final Path CONFIG_PATH = Paths.get(MODID + ".json"); |
54 | | - |
55 | 57 | private static GrindEnchantmentsConfigV3 LEGACY_CONFIG = GrindEnchantmentsConfigV3.DEFAULT; |
56 | 58 |
|
57 | 59 | @Override |
58 | 60 | public void onInitialize() { |
59 | | - final GrindEnchantmentsConfigV3 legacyLatestConfigDefault = GrindEnchantmentsConfigV3.DEFAULT; |
60 | | - final int legacyLatestConfigVersion = legacyLatestConfigDefault.version(); |
61 | | - @SuppressWarnings({"unchecked", "deprecation"}) |
62 | | - final MapCodec<? extends ModConfig<GrindEnchantmentsConfigV3>>[] legacyConfigCodecs = new MapCodec[] { |
63 | | - GrindEnchantmentsConfigV1.TYPE_CODEC, GrindEnchantmentsConfigV2.TYPE_CODEC, GrindEnchantmentsConfigV3.TYPE_CODEC |
64 | | - }; |
65 | | - |
66 | | - final Codec<ModConfig<GrindEnchantmentsConfigV3>> legacyConfigCodec = ModConfig.createCodec(legacyLatestConfigVersion, version -> |
67 | | - getConfigType(0, legacyConfigCodecs, version)); |
68 | | - |
69 | | - // TODO Proper solution to client-side configs |
70 | | - // ClientLifecycleEvents.CLIENT_STARTED.register(client -> |
71 | | - // CONFIG = ConfigIo.initializeConfig(Paths.get(MODID + ".json"), LATEST_CONFIG_VERSION, LATEST_CONFIG_DEFAULT, CONFIG_CODEC, |
72 | | - // JsonOps.INSTANCE, LOGGER::info, LOGGER::error) |
73 | | - // ); |
74 | | - ServerLifecycleEvents.SERVER_STARTING.register(server -> |
75 | | - LEGACY_CONFIG = ConfigIo.initializeConfig(Paths.get(MODID + ".json"), legacyLatestConfigVersion, legacyLatestConfigDefault, legacyConfigCodec, |
76 | | - RegistryOps.of(JsonOps.INSTANCE, server.getRegistryManager()), LOGGER::info, LOGGER::error) |
77 | | - ); |
| 61 | + LEGACY_CONFIG = readLegacyConfig().orElse(GrindEnchantmentsConfigV3.DEFAULT); |
78 | 62 |
|
79 | 63 | GrindEnchantmentsRegistries.init(); |
80 | 64 | CostFunctionType.init(); |
@@ -111,6 +95,41 @@ public void onInitialize() { |
111 | 95 | return new ModConfig.Type<>(codecs.length + versionOffset, codecs[codecs.length - 1]); |
112 | 96 | } |
113 | 97 |
|
| 98 | + @SuppressWarnings("deprecation") |
| 99 | + private static Optional<GrindEnchantmentsConfigV3> readLegacyConfig() { |
| 100 | + final GrindEnchantmentsConfigV3 legacyLatestConfigDefault = GrindEnchantmentsConfigV3.DEFAULT; |
| 101 | + final int legacyLatestConfigVersion = legacyLatestConfigDefault.version(); |
| 102 | + @SuppressWarnings({"unchecked", "deprecation"}) |
| 103 | + final MapCodec<? extends ModConfig<GrindEnchantmentsConfigV3>>[] legacyConfigCodecs = new MapCodec[] { |
| 104 | + GrindEnchantmentsConfigV1.TYPE_CODEC, GrindEnchantmentsConfigV2.TYPE_CODEC, GrindEnchantmentsConfigV3.TYPE_CODEC |
| 105 | + }; |
| 106 | + |
| 107 | + final Codec<ModConfig<GrindEnchantmentsConfigV3>> legacyConfigCodec = ModConfig.createCodec(legacyLatestConfigVersion, version -> |
| 108 | + getConfigType(0, legacyConfigCodecs, version)); |
| 109 | + |
| 110 | + // Unfortunately, this requires some manual work and usage of codec config API's internals |
| 111 | + |
| 112 | + Path configPath = FabricLoader.getInstance().getConfigDir().resolve(Paths.get(MODID + ".json")); |
| 113 | + @Nullable |
| 114 | + GrindEnchantmentsConfigV3 config = null; |
| 115 | + |
| 116 | + if (Files.exists(configPath) && Files.isRegularFile(configPath)) { |
| 117 | + try (InputStream input = Files.newInputStream(configPath)) { |
| 118 | + log(Level.INFO, "Reading legacy config."); |
| 119 | + |
| 120 | + @SuppressWarnings("UnstableApiUsage") |
| 121 | + ModConfig<GrindEnchantmentsConfigV3> readConfig = ConfigUtil.decodeConfig(input, legacyConfigCodec, JsonOps.INSTANCE); |
| 122 | + config = readConfig.latest(); |
| 123 | + } catch (IOException e) { |
| 124 | + log(Level.ERROR, "IO exception while trying to read config: " + e.getLocalizedMessage()); |
| 125 | + } catch (ConfigException e) { |
| 126 | + log(Level.ERROR, e.getLocalizedMessage()); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + return Optional.ofNullable(config); |
| 131 | + } |
| 132 | + |
114 | 133 | public static GrindEnchantmentsConfigV3 getConfig() { |
115 | 134 | return LEGACY_CONFIG; |
116 | 135 | } |
|
0 commit comments