Skip to content

Commit baaef5a

Browse files
committed
Fix changing the preview color of the MekaSuit actually changing the color (#8091)
1 parent ceaaf92 commit baaef5a

File tree

5 files changed

+23
-16
lines changed

5 files changed

+23
-16
lines changed

src/api/java/mekanism/api/gear/IModuleContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ default Set<ModuleData<?>> moduleTypes() {
5656
* @throws IllegalStateException If no module of the given type is installed, or there is no config with the same name is not found installed on the module of the
5757
* given type.
5858
*/
59-
<MODULE extends ICustomModule<MODULE>> IModuleContainer replaceModuleConfig(HolderLookup.Provider provider, ItemStack stack, ModuleData<MODULE> type,
59+
<MODULE extends ICustomModule<MODULE>> IModuleContainer replaceModuleConfig(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> type,
6060
ModuleConfig<?> config);
6161

6262
/**

src/main/java/mekanism/client/gui/element/GuiColorPickerSlot.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ public void renderForeground(GuiGraphics guiGraphics, int mouseX, int mouseY) {
5252

5353
@Override
5454
public void onClick(double mouseX, double mouseY, int button) {
55-
GuiColorWindow window = new GuiColorWindow(gui(), getGuiWidth() / 2 - 160 / 2, getGuiHeight() / 2 - 120 / 2, handlesAlpha, consumer);
56-
window.setColor(supplier.get());
57-
gui().addWindow(window);
55+
gui().addWindow(new GuiColorWindow(gui(), getGuiWidth() / 2 - 160 / 2, getGuiHeight() / 2 - 120 / 2, handlesAlpha, supplier.get(), consumer));
5856
}
5957
}

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.function.Consumer;
44
import mekanism.api.gear.IModule;
5+
import mekanism.api.gear.IModuleContainer;
6+
import mekanism.api.gear.IModuleHelper;
57
import mekanism.api.gear.config.ModuleColorConfig;
68
import mekanism.client.gui.GuiModuleTweaker;
79
import mekanism.client.gui.GuiUtils;
@@ -11,7 +13,9 @@
1113
import mekanism.common.MekanismLang;
1214
import mekanism.common.content.gear.shared.ModuleColorModulationUnit;
1315
import mekanism.common.lib.Color;
16+
import mekanism.common.registries.MekanismModules;
1417
import mekanism.common.util.text.TextUtils;
18+
import net.minecraft.client.Minecraft;
1519
import net.minecraft.client.gui.GuiGraphics;
1620
import net.minecraft.network.chat.Component;
1721
import net.minecraft.world.entity.EquipmentSlot;
@@ -76,21 +80,26 @@ protected void click(double mouseX, double mouseY) {
7680
Runnable previewReset = null;
7781
IModule<?> currentModule = parent.getCurrentModule();
7882
if (armorPreview != null && data.name().equals(ModuleColorModulationUnit.COLOR) && currentModule != null) {
79-
ItemStack stack = parent.getContainerStack();
83+
ItemStack stack = parent.getContainerStack().copy();
8084
if (stack.getItem() instanceof ArmorItem armorItem) {
8185
//Ensure the preview has been initialized
8286
armorPreview.get();
8387
EquipmentSlot slot = armorItem.getEquipmentSlot();
8488
//Replace the current preview with our copy
8589
armorPreview.updatePreview(slot, stack);
86-
updatePreviewColor = c -> setData(c.argb());
90+
updatePreviewColor = c -> {
91+
IModuleContainer moduleContainer = IModuleHelper.INSTANCE.getModuleContainer(stack);
92+
if (moduleContainer != null) {//Note: Should always be present
93+
//Note: We can use the source data to ensure we have the correct config option, as with does not mutate it
94+
moduleContainer.replaceModuleConfig(Minecraft.getInstance().level.registryAccess(), stack, MekanismModules.COLOR_MODULATION_UNIT,
95+
data.with(c.argb()));
96+
}
97+
};
8798
previewReset = () -> armorPreview.resetToDefault(slot);
8899
}
89100
}
90-
GuiColorWindow window = new GuiColorWindow(parent.gui(), parent.getGuiWidth() / 2 - 160 / 2, parent.getGuiHeight() / 2 - 120 / 2, supportsAlpha,
91-
color -> setData(color.argb()), armorPreview, updatePreviewColor, previewReset);
92-
window.setColor(getColor());
93-
parent.gui().addWindow(window);
101+
parent.gui().addWindow(new GuiColorWindow(parent.gui(), parent.getGuiWidth() / 2 - 160 / 2, parent.getGuiHeight() / 2 - 120 / 2, supportsAlpha,
102+
getColor(), color -> setData(color.argb()), armorPreview, updatePreviewColor, previewReset));
94103
}
95104
}
96105
}

src/main/java/mekanism/client/gui/element/window/GuiColorWindow.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public class GuiColorWindow extends GuiWindow {
5151
private float value = 0.5F;
5252
private float alpha = 1;
5353

54-
public GuiColorWindow(IGuiWrapper gui, int x, int y, boolean handlesAlpha, Consumer<Color> callback) {
55-
this(gui, x, y, handlesAlpha, callback, null, null, null);
54+
public GuiColorWindow(IGuiWrapper gui, int x, int y, boolean handlesAlpha, Color initialColor, Consumer<Color> callback) {
55+
this(gui, x, y, handlesAlpha, initialColor, callback, null, null, null);
5656
}
5757

58-
public GuiColorWindow(IGuiWrapper gui, int x, int y, boolean handlesAlpha, Consumer<Color> callback, @Nullable Supplier<LivingEntity> armorPreview,
58+
public GuiColorWindow(IGuiWrapper gui, int x, int y, boolean handlesAlpha, Color initialColor, Consumer<Color> callback, @Nullable Supplier<LivingEntity> armorPreview,
5959
@Nullable Consumer<Color> updatePreviewColor, @Nullable Runnable previewReset) {
6060
super(gui, x, y, (handlesAlpha ? 184 : 158) + (armorPreview == null ? 0 : 83), handlesAlpha ? 152 : 140, WindowType.COLOR);
6161
interactionStrategy = InteractionStrategy.NONE;
@@ -95,7 +95,7 @@ public GuiColorWindow(IGuiWrapper gui, int x, int y, boolean handlesAlpha, Consu
9595
addChild(new GuiEntityPreview(gui, relativeX + 155 + extraWidth, relativeY + 17, 80, height - 24, armorPreview));
9696
}
9797

98-
setColor(Color.rgbi(128, 70, 70));
98+
setColor(initialColor);
9999
}
100100

101101
@Override

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ public List<Component> getHUDStrings(Player player, ItemStack stack) {
108108
}
109109

110110
@Override
111-
public <MODULE extends ICustomModule<MODULE>> ModuleContainer replaceModuleConfig(HolderLookup.Provider provider, ItemStack stack, ModuleData<MODULE> type,
111+
public <MODULE extends ICustomModule<MODULE>> ModuleContainer replaceModuleConfig(HolderLookup.Provider provider, ItemStack stack, IModuleDataProvider<MODULE> type,
112112
ModuleConfig<?> config) {
113-
return replaceModuleConfig(provider, stack, type, config, false);
113+
return replaceModuleConfig(provider, stack, type.getModuleData(), config, false);
114114
}
115115

116116
/**

0 commit comments

Comments
 (0)