|
28 | 28 | import mekanism.common.util.MekanismUtils; |
29 | 29 | import mekanism.common.util.StorageUtils; |
30 | 30 | import net.minecraft.MethodsReturnNonnullByDefault; |
31 | | -import net.minecraft.core.HolderLookup; |
32 | | -import net.minecraft.nbt.CompoundTag; |
33 | | -import net.minecraft.nbt.NbtOps; |
34 | | -import net.minecraft.nbt.Tag; |
35 | 31 | import net.minecraft.network.RegistryFriendlyByteBuf; |
36 | 32 | import net.minecraft.network.chat.Component; |
37 | 33 | import net.minecraft.network.codec.ByteBufCodecs; |
38 | 34 | import net.minecraft.network.codec.StreamCodec; |
39 | | -import net.minecraft.resources.RegistryOps; |
40 | 35 | import net.minecraft.resources.ResourceLocation; |
41 | 36 | import net.minecraft.util.ExtraCodecs; |
42 | 37 | import net.minecraft.world.entity.LivingEntity; |
@@ -217,12 +212,31 @@ public boolean isEnabled() { |
217 | 212 | return enabled; |
218 | 213 | } |
219 | 214 |
|
220 | | - Module<MODULE> withReplacedInstallCount(HolderLookup.Provider provider, int installed) { |
221 | | - RegistryOps<Tag> registryOps = provider.createSerializationContext(NbtOps.INSTANCE); |
222 | | - //TODO: See if we can come up with a cleaner way to do this |
223 | | - CompoundTag tag = (CompoundTag) Module.CODEC.encodeStart(registryOps, this).getOrThrow(); |
224 | | - tag.putInt(SerializationConstants.AMOUNT, installed); |
225 | | - return (Module<MODULE>) Module.CODEC.decode(registryOps, tag).getOrThrow().getFirst(); |
| 215 | + Module<MODULE> withReplacedInstallCount(int installed) { |
| 216 | + List<ModuleConfig<?>> moduleConfigs = data.defaultConfigs(installed); |
| 217 | + List<ModuleConfig<?>> copiedConfigs = new ArrayList<>(moduleConfigs.size()); |
| 218 | + for (ModuleConfig<?> moduleConfig : moduleConfigs) { |
| 219 | + ResourceLocation name = moduleConfig.name(); |
| 220 | + ModuleConfig<?> existingConfig = configItemsByName.get(name); |
| 221 | + if (existingConfig == null) { |
| 222 | + copiedConfigs.add(moduleConfig); |
| 223 | + } else if (moduleConfig.getClass() == existingConfig.getClass()) { |
| 224 | + copiedConfigs.add(configWithValue(moduleConfig, existingConfig)); |
| 225 | + } else { |
| 226 | + throw new IllegalStateException("Expected module config " + name + " to have the same class regardless of installed count."); |
| 227 | + } |
| 228 | + } |
| 229 | + return new Module<>(data, installed, List.copyOf(copiedConfigs)); |
| 230 | + } |
| 231 | + |
| 232 | + @SuppressWarnings("unchecked") |
| 233 | + private <CONFIG> ModuleConfig<CONFIG> configWithValue(ModuleConfig<CONFIG> defaultConfig, ModuleConfig<?> existing) { |
| 234 | + try { |
| 235 | + return defaultConfig.with(((ModuleConfig<CONFIG>) existing).get()); |
| 236 | + } catch (IllegalArgumentException e) { |
| 237 | + //If the existing value isn't valid for the new config, fallback to the default value |
| 238 | + return defaultConfig; |
| 239 | + } |
226 | 240 | } |
227 | 241 |
|
228 | 242 | Module<MODULE> withReplacedConfig(ModuleConfig<?> config) { |
|
0 commit comments