Skip to content

Commit 759edae

Browse files
committed
Breaking API change to module configs and lookup to make names be resource locations so that things like enabled can still be properly translated (#8121)
Note: This change may also cause module configs to fail to load for non mekanism modules, so we recommend removing them. There are no configs for the modules in mekanism generators so those should be fine. The legacy loading will be removed in a few Mek builds
1 parent a6e8176 commit 759edae

27 files changed

+121
-83
lines changed

src/api/java/mekanism/api/gear/IModule.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import mekanism.api.math.FloatingLongSupplier;
88
import mekanism.api.text.IHasTextComponent;
99
import net.minecraft.network.chat.Component;
10+
import net.minecraft.resources.ResourceLocation;
1011
import net.minecraft.world.entity.LivingEntity;
1112
import net.minecraft.world.entity.player.Player;
1213
import net.minecraft.world.item.ItemStack;
@@ -35,7 +36,7 @@ public interface IModule<MODULE extends ICustomModule<MODULE>> {
3536
* @since 10.6.0
3637
*/
3738
@Nullable
38-
<TYPE> ModuleConfig<TYPE> getConfig(String name);
39+
<TYPE> ModuleConfig<TYPE> getConfig(ResourceLocation name);
3940

4041
/**
4142
* Gets the value of a boolean config with the given name.
@@ -46,7 +47,7 @@ public interface IModule<MODULE extends ICustomModule<MODULE>> {
4647
*
4748
* @since 10.6.0
4849
*/
49-
default boolean getBooleanConfigOrFalse(String name) {
50+
default boolean getBooleanConfigOrFalse(ResourceLocation name) {
5051
ModuleConfig<Boolean> config = getConfig(name);
5152
return config != null && config.get();
5253
}
@@ -59,7 +60,7 @@ default boolean getBooleanConfigOrFalse(String name) {
5960
* @throws IllegalArgumentException If this module does not contain a config with the given name.
6061
* @since 10.6.0
6162
*/
62-
default <TYPE> ModuleConfig<TYPE> getConfigOrThrow(String name) {
63+
default <TYPE> ModuleConfig<TYPE> getConfigOrThrow(ResourceLocation name) {
6364
ModuleConfig<TYPE> config = getConfig(name);
6465
if (config == null) {
6566
throw new IllegalArgumentException("Expected module to contain a config with name " + name);

src/api/java/mekanism/api/gear/ModuleData.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public final List<ModuleConfig<?>> defaultConfigs(int installed) {
176176
* @since 10.6.0
177177
*/
178178
@Nullable
179-
public final ModuleConfig<?> getNamedConfig(int installed, String name) {
179+
public final ModuleConfig<?> getNamedConfig(int installed, ResourceLocation name) {
180180
for (ModuleConfig<?> config : getConfigData(installed).configs()) {
181181
if (config.name().equals(name)) {
182182
return config;
@@ -265,7 +265,7 @@ private record ConfigData(List<ModuleConfig<?>> configs, List<Codec<? extends Mo
265265
List<StreamCodec<? super RegistryFriendlyByteBuf, ? extends ModuleConfig<?>>> streamCodecs) {
266266

267267
private ConstructedConfigData construct() {
268-
Set<String> uniqueNames = new HashSet<>(configs.size());
268+
Set<ResourceLocation> uniqueNames = new HashSet<>(configs.size());
269269
for (ModuleConfig<?> config : configs) {
270270
if (!uniqueNames.add(config.name())) {
271271
throw new IllegalStateException("Duplicate module config name " + config.name());

src/api/java/mekanism/api/gear/config/ModuleBooleanConfig.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import mekanism.api.annotations.NothingNullByDefault;
99
import net.minecraft.network.codec.ByteBufCodecs;
1010
import net.minecraft.network.codec.StreamCodec;
11+
import net.minecraft.resources.ResourceLocation;
1112

1213
/**
1314
* Immutable class representing a boolean module config (name and boolean value).
@@ -27,7 +28,7 @@ public class ModuleBooleanConfig extends ModuleConfig<Boolean> {
2728
* Stream codec for encoding and decoding boolean module configs over the network.
2829
*/
2930
public static final StreamCodec<ByteBuf, ModuleBooleanConfig> STREAM_CODEC = StreamCodec.composite(
30-
ByteBufCodecs.STRING_UTF8, ModuleConfig::name,
31+
ResourceLocation.STREAM_CODEC, ModuleConfig::name,
3132
ByteBufCodecs.BOOL, ModuleConfig::get,
3233
ModuleBooleanConfig::new
3334
);
@@ -38,19 +39,19 @@ public class ModuleBooleanConfig extends ModuleConfig<Boolean> {
3839
* @param name Name of the config option.
3940
* @param value Value of the config option.
4041
*/
41-
public static ModuleBooleanConfig create(String name, boolean value) {
42+
public static ModuleBooleanConfig create(ResourceLocation name, boolean value) {
4243
return new ModuleBooleanConfig(name, value);
4344
}
4445

4546
private final boolean value;
4647

47-
protected ModuleBooleanConfig(String name, boolean value) {
48+
protected ModuleBooleanConfig(ResourceLocation name, boolean value) {
4849
super(name);
4950
this.value = value;
5051
}
5152

5253
@Override
53-
public StreamCodec<ByteBuf, ModuleConfig<Boolean>> namedStreamCodec(String name) {
54+
public StreamCodec<ByteBuf, ModuleConfig<Boolean>> namedStreamCodec(ResourceLocation name) {
5455
return ByteBufCodecs.BOOL.map(val -> new ModuleBooleanConfig(name, val), ModuleConfig::get);
5556
}
5657

src/api/java/mekanism/api/gear/config/ModuleColorConfig.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import mekanism.api.annotations.NothingNullByDefault;
99
import net.minecraft.network.codec.ByteBufCodecs;
1010
import net.minecraft.network.codec.StreamCodec;
11+
import net.minecraft.resources.ResourceLocation;
1112

1213
/**
1314
* Immutable class representing a color based module config (name and int value).
@@ -27,7 +28,7 @@ public class ModuleColorConfig extends ModuleConfig<Integer> {
2728
* Stream codec for encoding and decoding ARGB based color module configs over the network.
2829
*/
2930
public static final StreamCodec<ByteBuf, ModuleColorConfig> ARGB_STREAM_CODEC = StreamCodec.composite(
30-
ByteBufCodecs.STRING_UTF8, ModuleConfig::name,
31+
ResourceLocation.STREAM_CODEC, ModuleConfig::name,
3132
//Note: We don't do var-int as we include alpha data
3233
ByteBufCodecs.INT, ModuleConfig::get,
3334
ModuleColorConfig::argb
@@ -43,7 +44,7 @@ public class ModuleColorConfig extends ModuleConfig<Integer> {
4344
* Stream codec for encoding and decoding RGB based color module configs over the network.
4445
*/
4546
public static final StreamCodec<ByteBuf, ModuleColorConfig> RGB_STREAM_CODEC = StreamCodec.composite(
46-
ByteBufCodecs.STRING_UTF8, ModuleConfig::name,
47+
ResourceLocation.STREAM_CODEC, ModuleConfig::name,
4748
//Note: We can use var int here and just not send the alpha data over the network
4849
ByteBufCodecs.VAR_INT, module -> module.get() & 0x00FFFFFF,
4950
ModuleColorConfig::rgb
@@ -54,7 +55,7 @@ public class ModuleColorConfig extends ModuleConfig<Integer> {
5455
*
5556
* @implNote Color format is ARGB.
5657
*/
57-
public static ModuleColorConfig argb(String name) {
58+
public static ModuleColorConfig argb(ResourceLocation name) {
5859
return argb(name, 0xFFFFFFFF);
5960
}
6061

@@ -65,7 +66,7 @@ public static ModuleColorConfig argb(String name) {
6566
*
6667
* @implNote Color format is ARGB.
6768
*/
68-
public static ModuleColorConfig argb(String name, int defaultColor) {
69+
public static ModuleColorConfig argb(ResourceLocation name, int defaultColor) {
6970
return new ModuleColorConfig(name, true, defaultColor);
7071
}
7172

@@ -74,7 +75,7 @@ public static ModuleColorConfig argb(String name, int defaultColor) {
7475
*
7576
* @implNote Color format is ARGB with the alpha component being locked to {@code 0xFF}.
7677
*/
77-
public static ModuleColorConfig rgb(String name) {
78+
public static ModuleColorConfig rgb(ResourceLocation name) {
7879
return rgb(name, 0xFFFFFFFF);
7980
}
8081

@@ -85,15 +86,15 @@ public static ModuleColorConfig rgb(String name) {
8586
*
8687
* @implNote Color format is ARGB with the alpha component being locked to {@code 0xFF}.
8788
*/
88-
public static ModuleColorConfig rgb(String name, int defaultColor) {
89+
public static ModuleColorConfig rgb(ResourceLocation name, int defaultColor) {
8990
//If we don't handle alpha make sure we do have the alpha component present
9091
return new ModuleColorConfig(name, false, defaultColor);
9192
}
9293

9394
private final boolean supportsAlpha;
9495
private final int value;
9596

96-
private ModuleColorConfig(String name, boolean supportsAlpha, int value) {
97+
private ModuleColorConfig(ResourceLocation name, boolean supportsAlpha, int value) {
9798
super(name);
9899
this.supportsAlpha = supportsAlpha;
99100
//If we don't handle alpha make sure we do have the alpha component present though
@@ -110,7 +111,7 @@ public boolean supportsAlpha() {
110111
}
111112

112113
@Override
113-
public StreamCodec<ByteBuf, ModuleConfig<Integer>> namedStreamCodec(String name) {
114+
public StreamCodec<ByteBuf, ModuleConfig<Integer>> namedStreamCodec(ResourceLocation name) {
114115
if (supportsAlpha) {
115116
//Note: We don't do varint as we include alpha data
116117
return ByteBufCodecs.INT.map(val -> ModuleColorConfig.argb(name, val), ModuleConfig::get);

src/api/java/mekanism/api/gear/config/ModuleConfig.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44
import com.mojang.serialization.codecs.RecordCodecBuilder.Instance;
55
import com.mojang.serialization.codecs.RecordCodecBuilder.Mu;
66
import java.util.Objects;
7+
import java.util.function.Function;
8+
import mekanism.api.MekanismAPI;
79
import mekanism.api.SerializationConstants;
810
import mekanism.api.annotations.NothingNullByDefault;
911
import net.minecraft.network.RegistryFriendlyByteBuf;
1012
import net.minecraft.network.codec.StreamCodec;
11-
import net.minecraft.util.ExtraCodecs;
13+
import net.minecraft.resources.ResourceLocation;
1214

1315
/**
1416
* Immutable class representing a module config (name and value).
@@ -22,39 +24,47 @@
2224
@NothingNullByDefault
2325
public abstract class ModuleConfig<DATA> {
2426

27+
private static ResourceLocation rl(String path) {
28+
return ResourceLocation.fromNamespaceAndPath(MekanismAPI.MEKANISM_MODID, path);
29+
}
30+
2531
/**
2632
* Constant representing the module config for enabled state.
2733
*/
28-
public static final String ENABLED_KEY = "enabled";
34+
public static final ResourceLocation ENABLED_KEY = rl("enabled");
2935
/**
3036
* Constant representing the module config for if a handling of mode change state.
3137
*/
32-
public static final String HANDLES_MODE_CHANGE_KEY = "handle_mode_change";
38+
public static final ResourceLocation HANDLES_MODE_CHANGE_KEY = rl("handle_mode_change");
3339
/**
3440
* Constant representing the module config for whether the module should render on the HUD.
3541
*/
36-
public static final String RENDER_HUD_KEY = "render_hud";
42+
public static final ResourceLocation RENDER_HUD_KEY = rl("render_hud");
3743

3844
/**
3945
* Helper method to get the base part of a codec that contains all the parts necessary by this parent class.
4046
*/
41-
protected static <DATA, CONFIG extends ModuleConfig<DATA>> P1<Mu<CONFIG>, String> baseCodec(Instance<CONFIG> instance) {
42-
return instance.group(ExtraCodecs.NON_EMPTY_STRING.fieldOf(SerializationConstants.NAME).forGetter(ModuleConfig::name));
47+
protected static <DATA, CONFIG extends ModuleConfig<DATA>> P1<Mu<CONFIG>, ResourceLocation> baseCodec(Instance<CONFIG> instance) {
48+
//TODO - 1.21: In a couple builds remove this hacky way to make old modules load
49+
// Note: This legacy method only supports mekanism configs and not modded configs, as there is no clean way to know what namespace it should actually be in
50+
return instance.group(ResourceLocation.CODEC.xmap(rl -> rl.getNamespace().equals("minecraft") ? rl(rl.getPath()) : rl, Function.identity())
51+
.fieldOf(SerializationConstants.NAME).forGetter(ModuleConfig::name));
52+
//return instance.group(ResourceLocation.CODEC.fieldOf(SerializationConstants.NAME).forGetter(ModuleConfig::name));
4353
}
4454

4555
//TODO: Do we want to make module configs be a registry or something rather than being named?
4656
// It probably won't make that much difference as it still would need to keep track of the
4757
// "config type" which would basically just be a named registry object
48-
private final String name;
58+
private final ResourceLocation name;
4959

50-
protected ModuleConfig(String name) {
60+
protected ModuleConfig(ResourceLocation name) {
5161
this.name = Objects.requireNonNull(name, "Name cannot be null.");
5262
}
5363

5464
/**
5565
* {@return the name of this config option, should be unique}
5666
*/
57-
public final String name() {
67+
public final ResourceLocation name() {
5868
return name;
5969
}
6070

@@ -63,7 +73,7 @@ public final String name() {
6373
*
6474
* @param name Name to use during config decoding.
6575
*/
66-
public abstract StreamCodec<? super RegistryFriendlyByteBuf, ModuleConfig<DATA>> namedStreamCodec(String name);
76+
public abstract StreamCodec<? super RegistryFriendlyByteBuf, ModuleConfig<DATA>> namedStreamCodec(ResourceLocation name);
6777

6878
/**
6979
* {@return the value of this config option}
@@ -87,7 +97,7 @@ public final String name() {
8797
*
8898
* @return {@code false}, unless overridden to conditionally disable.
8999
*
90-
* @apiNote When overriding, make sure to also override {@link #get()}, {@link #with(Object)}, {@link #namedStreamCodec(String)}, and use a custom codec and stream
100+
* @apiNote When overriding, make sure to also override {@link #get()}, {@link #with(Object)}, {@link #namedStreamCodec(ResourceLocation)}, and use a custom codec and stream
91101
* codec when adding the config to the module data.
92102
*/
93103
public boolean isConfigDisabled() {

src/api/java/mekanism/api/gear/config/ModuleEnumConfig.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
import mekanism.api.annotations.NothingNullByDefault;
1111
import mekanism.api.text.IHasTextComponent;
1212
import net.minecraft.network.FriendlyByteBuf;
13-
import net.minecraft.network.codec.ByteBufCodecs;
1413
import net.minecraft.network.codec.StreamCodec;
14+
import net.minecraft.resources.ResourceLocation;
1515
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
1616
import org.jetbrains.annotations.Nullable;
1717

@@ -73,7 +73,7 @@ public static <TYPE extends Enum<TYPE> & IHasTextComponent> Codec<ModuleEnumConf
7373
*/
7474
public static <BUF extends ByteBuf, TYPE extends Enum<TYPE> & IHasTextComponent> StreamCodec<BUF, ModuleEnumConfig<TYPE>> streamCodec(StreamCodec<BUF, TYPE> enumCodec) {
7575
return StreamCodec.composite(
76-
ByteBufCodecs.STRING_UTF8, ModuleConfig::name,
76+
ResourceLocation.STREAM_CODEC, ModuleConfig::name,
7777
enumCodec, ModuleConfig::get,
7878
ModuleEnumConfig::new
7979
);
@@ -102,7 +102,7 @@ public static <BUF extends ByteBuf, TYPE extends Enum<TYPE> & IHasTextComponent>
102102
return streamCodec(enumCodec);
103103
}
104104
return StreamCodec.composite(
105-
ByteBufCodecs.STRING_UTF8, ModuleConfig::name,
105+
ResourceLocation.STREAM_CODEC, ModuleConfig::name,
106106
enumCodec, ModuleConfig::get,
107107
(name, value) -> new ModuleEnumConfig<>(name, value, enumConstants)
108108
);
@@ -115,7 +115,7 @@ public static <BUF extends ByteBuf, TYPE extends Enum<TYPE> & IHasTextComponent>
115115
* @param value Value of the config option.
116116
* @param <TYPE> Type of the data stored by this config.
117117
*/
118-
public static <TYPE extends Enum<TYPE> & IHasTextComponent> ModuleEnumConfig<TYPE> create(String name, TYPE value) {
118+
public static <TYPE extends Enum<TYPE> & IHasTextComponent> ModuleEnumConfig<TYPE> create(ResourceLocation name, TYPE value) {
119119
return new ModuleEnumConfig<>(name, value);
120120
}
121121

@@ -127,9 +127,9 @@ public static <TYPE extends Enum<TYPE> & IHasTextComponent> ModuleEnumConfig<TYP
127127
* @param selectableCount The number of selectable elements.
128128
* @param <TYPE> Type of the data stored by this config.
129129
*
130-
* @implNote If selectedCount is equal to the number of elements in the enum, this acts as if {@link #create(String, Enum)} was called instead.
130+
* @implNote If selectedCount is equal to the number of elements in the enum, this acts as if {@link #create(ResourceLocation, Enum)} was called instead.
131131
*/
132-
public static <TYPE extends Enum<TYPE> & IHasTextComponent> ModuleEnumConfig<TYPE> createBounded(String name, TYPE value, int selectableCount) {
132+
public static <TYPE extends Enum<TYPE> & IHasTextComponent> ModuleEnumConfig<TYPE> createBounded(ResourceLocation name, TYPE value, int selectableCount) {
133133
if (selectableCount <= 0) {
134134
throw new IllegalArgumentException("Invalid selectableCount, there must be at least one element that is selectable.");
135135
} else if (value.ordinal() >= selectableCount) {
@@ -157,18 +157,18 @@ private static <TYPE extends Enum<TYPE> & IHasTextComponent> List<TYPE> getEnumC
157157
private final List<TYPE> enumConstants;
158158
private final TYPE value;
159159

160-
private ModuleEnumConfig(String name, TYPE value) {
160+
private ModuleEnumConfig(ResourceLocation name, TYPE value) {
161161
this(name, value, List.of(value.getDeclaringClass().getEnumConstants()));
162162
}
163163

164-
private ModuleEnumConfig(String name, TYPE value, List<TYPE> enumConstants) {
164+
private ModuleEnumConfig(ResourceLocation name, TYPE value, List<TYPE> enumConstants) {
165165
super(name);
166166
this.value = value;
167167
this.enumConstants = enumConstants;
168168
}
169169

170170
@Override
171-
public StreamCodec<FriendlyByteBuf, ModuleConfig<TYPE>> namedStreamCodec(String name) {
171+
public StreamCodec<FriendlyByteBuf, ModuleConfig<TYPE>> namedStreamCodec(ResourceLocation name) {
172172
return NeoForgeStreamCodecs.enumCodec(value.getDeclaringClass()).map(
173173
value -> new ModuleEnumConfig<>(name, value, enumConstants),
174174
ModuleConfig::get

src/datagen/main/java/mekanism/client/lang/BaseLanguageProvider.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ protected void addEntity(Holder<EntityType<?>> key, String value) {
6969
add(key.value().getDescriptionId(), value);
7070
}
7171

72+
protected void addModuleConfig(ResourceLocation configKey, String value) {
73+
add("module." + configKey.getNamespace() + "." + configKey.getPath(), value);
74+
}
75+
7276
protected void addModuleConfig(String configKey, String value) {
7377
add("module." + modid + "." + configKey, value);
7478
}

src/main/java/mekanism/client/gui/element/custom/module/GuiModuleScreen.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import net.minecraft.Util;
2424
import net.minecraft.client.gui.GuiGraphics;
2525
import net.minecraft.network.chat.Component;
26+
import net.minecraft.resources.ResourceLocation;
2627
import net.minecraft.world.item.ItemStack;
2728
import org.jetbrains.annotations.NotNull;
2829
import org.jetbrains.annotations.Nullable;
@@ -38,7 +39,7 @@ public class GuiModuleScreen extends GuiScrollableElement {
3839

3940
@Nullable
4041
private Module<?> currentModule;
41-
private Map<String, MiniElement<?>> miniElements = new LinkedHashMap<>();
42+
private Map<ResourceLocation, MiniElement<?>> miniElements = new LinkedHashMap<>();
4243
private int maxElements;
4344

4445
public GuiModuleScreen(IGuiWrapper gui, int x, int y, Supplier<ItemStack> itemSupplier, Consumer<ModuleConfig<?>> saveCallback, ArmorPreview armorPreview) {
@@ -53,7 +54,7 @@ private GuiModuleScreen(IGuiWrapper gui, int x, int y, int width, int height, Su
5354
}
5455

5556
public void setModule(@Nullable Module<?> module) {
56-
Map<String, MiniElement<?>> newElements = new LinkedHashMap<>();
57+
Map<ResourceLocation, MiniElement<?>> newElements = new LinkedHashMap<>();
5758

5859
if (module != null) {
5960
int startY = getStartY(module);
@@ -62,8 +63,8 @@ public void setModule(@Nullable Module<?> module) {
6263
//Skip options that are force disabled by the config
6364
continue;
6465
}
65-
Component description = TextComponentUtil.translate(Util.makeDescriptionId("module", module.getData().getRegistryName().withPath(configItem.name())));
66-
String name = configItem.name();
66+
Component description = TextComponentUtil.translate(Util.makeDescriptionId("module", configItem.name()));
67+
ResourceLocation name = configItem.name();
6768
MiniElement<?> element = switch (configItem) {
6869
// Don't show the enabled option if this is enabled by default
6970
case ModuleBooleanConfig config when !name.equals(ModuleConfig.ENABLED_KEY) || !module.getData().isNoDisable() ->

0 commit comments

Comments
 (0)