Skip to content

Commit 3d54df9

Browse files
committed
Fix installing modules after the first or removing modules one at a time not working. If the value set in the module tweaker for a config option is no longer valid after changing the number of installed modules, the value will be reset to the default
1 parent 7d19539 commit 3d54df9

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

src/main/java/mekanism/common/content/gear/Module.java

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,10 @@
2828
import mekanism.common.util.MekanismUtils;
2929
import mekanism.common.util.StorageUtils;
3030
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;
3531
import net.minecraft.network.RegistryFriendlyByteBuf;
3632
import net.minecraft.network.chat.Component;
3733
import net.minecraft.network.codec.ByteBufCodecs;
3834
import net.minecraft.network.codec.StreamCodec;
39-
import net.minecraft.resources.RegistryOps;
4035
import net.minecraft.resources.ResourceLocation;
4136
import net.minecraft.util.ExtraCodecs;
4237
import net.minecraft.world.entity.LivingEntity;
@@ -217,12 +212,31 @@ public boolean isEnabled() {
217212
return enabled;
218213
}
219214

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+
}
226240
}
227241

228242
Module<MODULE> withReplacedConfig(ModuleConfig<?> config) {

src/main/java/mekanism/common/content/gear/ModuleContainer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ public <MODULE extends ICustomModule<MODULE>> int addModule(HolderLookup.Provide
292292
//Nothing to actually install because we are already at the max stack size
293293
return 0;
294294
}
295-
module = module.withReplacedInstallCount(provider, module.getInstalledCount() + toInstall);
295+
module = module.withReplacedInstallCount(module.getInstalledCount() + toInstall);
296296
}
297297
//Add the module to the list of tracked and known modules if necessary or replace the existing value
298298
SequencedMap<ModuleData<?>, Module<?>> copiedModules = new LinkedHashMap<>(typedModules);
@@ -332,7 +332,7 @@ public <MODULE extends ICustomModule<MODULE>> void removeModule(HolderLookup.Pro
332332
}
333333
}
334334
} else {//update the module with the new installed count
335-
module = module.withReplacedInstallCount(provider, installed);
335+
module = module.withReplacedInstallCount(installed);
336336
copiedModules.put(type, module);
337337
//Update the level of any corresponding enchantment
338338
adjustedEnchantments = updateEnchantment(provider, module, null);

0 commit comments

Comments
 (0)