• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / IntegratedDynamics / 15366205963

31 May 2025 06:03PM UTC coverage: 45.019% (-0.5%) from 45.524%
15366205963

push

github

rubensworks
Merge remote-tracking branch 'origin/master-1.21-lts' into master-1.21

2555 of 8509 branches covered (30.03%)

Branch coverage included in aggregate %.

11885 of 23566 relevant lines covered (50.43%)

2.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/org/cyclops/integrateddynamics/core/evaluate/variable/gui/GuiElementValueTypeString.java
1
package org.cyclops.integrateddynamics.core.evaluate.variable.gui;
2

3
import com.google.common.base.Predicates;
4
import lombok.Data;
5
import net.minecraft.ChatFormatting;
6
import net.minecraft.client.gui.Font;
7
import net.minecraft.client.gui.GuiGraphics;
8
import net.minecraft.client.gui.screens.Screen;
9
import net.minecraft.client.renderer.texture.TextureManager;
10
import net.minecraft.network.chat.Component;
11
import net.minecraft.resources.ResourceLocation;
12
import net.minecraft.world.inventory.AbstractContainerMenu;
13
import net.neoforged.api.distmarker.Dist;
14
import net.neoforged.api.distmarker.OnlyIn;
15
import org.cyclops.cyclopscore.client.gui.container.ContainerScreenExtended;
16
import org.cyclops.cyclopscore.client.gui.image.Images;
17
import org.cyclops.cyclopscore.helper.IModHelpers;
18
import org.cyclops.cyclopscore.helper.StringHelpers;
19
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElement;
20
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElementValueType;
21
import org.cyclops.integrateddynamics.api.client.gui.subgui.ISubGuiBox;
22
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
23
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
24
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
25
import org.cyclops.integrateddynamics.api.logicprogrammer.IConfigRenderPattern;
26
import org.cyclops.integrateddynamics.core.client.gui.subgui.SubGuiBox;
27
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
28
import org.cyclops.integrateddynamics.core.helper.L10NValues;
29

30
import java.util.List;
31
import java.util.function.Predicate;
32
import java.util.stream.Collectors;
33

34
/**
35
 * GUI element for value type that can be read from and written to strings.
36
 * @author rubensworks
37
 */
38
@Data
×
39
public class GuiElementValueTypeString<G extends Screen, C extends AbstractContainerMenu> implements IGuiInputElementValueType<GuiElementValueTypeStringRenderPattern, G, C> {
40

41
    private final IValueType valueType;
×
42
    private Predicate<IValue> validator;
×
43
    private final IConfigRenderPattern renderPattern;
44
    private String defaultInputString;
×
45
    private String inputString;
×
46

47
    public GuiElementValueTypeString(IValueType valueType, IConfigRenderPattern renderPattern) {
×
48
        this.valueType = valueType;
×
49
        this.validator = Predicates.alwaysTrue();
×
50
        this.renderPattern = renderPattern;
×
51
        defaultInputString = ValueHelpers.toString(getValueType().getDefault());
×
52
    }
×
53

54
    @Override
55
    public void setValue(IValue value) {
56
        setInputString(ValueHelpers.toString(value));
×
57
    }
×
58

59
    @Override
60
    public void setValueInGui(GuiElementValueTypeStringRenderPattern subGui, boolean sendToServer) {
61
        if(subGui != null) {
×
62
            subGui.getTextField().setValue(inputString);
×
63
            if (sendToServer) {
×
64
                subGui.sendValueToServer();
×
65
            }
66
        }
67
    }
×
68

69
    public void setInputString(String inputString) {
70
        this.inputString = inputString;
×
71
    }
×
72

73
    @Override
74
    public void setValidator(Predicate<IValue> validator) {
75
        this.validator = validator;
×
76
    }
×
77

78
    @Override
79
    public IValue getValue() {
80
        try {
81
            return ValueHelpers.parseString(getValueType(), getInputString());
×
82
        } catch (EvaluationException e) {
×
83
            // Should not occur, as validation must've happened before.
84
            return getValueType().getDefault();
×
85
        }
86
    }
87

88
    @Override
89
    public Component getName() {
90
        return Component.translatable(getValueType().getTranslationKey());
×
91
    }
92

93
    @Override
94
    public void loadTooltip(List<Component> lines) {
95
        getValueType().loadTooltip(lines, true, null);
×
96
    }
×
97

98
    @Override
99
    public IConfigRenderPattern getRenderPattern() {
100
        return renderPattern;
×
101
    }
102

103
    @Override
104
    public void activate() {
105
        this.inputString = defaultInputString;
×
106
    }
×
107

108
    @Override
109
    public void deactivate() {
110
        this.inputString = null;
×
111
    }
×
112

113
    @Override
114
    public Component validate() {
115
        try {
116
            IValue value = getValueType().parseString(inputString);
×
117
            if (!this.validator.test(value)) {
×
118
                return Component.translatable(L10NValues.VALUE_ERROR);
×
119
            }
120
        } catch (EvaluationException e) {
×
121
            return e.getErrorMessage();
×
122
        }
×
123
        return null;
×
124
    }
125

126
    @Override
127
    public int getColor() {
128
        return getValueType().getDisplayColor();
×
129
    }
130

131
    @Override
132
    public String getSymbol() {
133
        return IModHelpers.get().getL10NHelpers().localize(getValueType().getTranslationKey());
×
134
    }
135

136
    @Override
137
    @OnlyIn(Dist.CLIENT)
138
    public GuiElementValueTypeStringRenderPattern<?, G, C> createSubGui(int baseX, int baseY,
139
                                                                        int maxWidth, int maxHeight, G gui, C container) {
140
        return new GuiElementValueTypeStringRenderPattern<>(this, baseX, baseY, maxWidth, maxHeight, gui, container);
×
141
    }
142

143
    @OnlyIn(Dist.CLIENT)
144
    public abstract static class SubGuiValueTypeInfo<S extends ISubGuiBox, G extends ContainerScreenExtended<?>, C extends AbstractContainerMenu> extends SubGuiBox.Base {
145

146
        private final IGuiInputElement element;
147
        protected final G gui;
148
        protected final C container;
149

150
        public SubGuiValueTypeInfo(G gui, C container, IGuiInputElement<S, G, C> element, int x, int y, int width, int height) {
151
            super(Box.DARK, x, y, width, height);
×
152
            this.gui = gui;
×
153
            this.container = container;
×
154
            this.element = element;
×
155
        }
×
156

157
        protected abstract boolean showError();
158
        protected abstract Component getLastError();
159
        protected abstract ResourceLocation getTexture();
160

161
        protected int getSignalX() {
162
            return getWidth() - 22;
×
163
        }
164

165
        protected int getSignalY() {
166
            return (getHeight() - 12) / 2;
×
167
        }
168

169
        @Override
170
        public void renderBg(GuiGraphics guiGraphics, int guiLeft, int guiTop, TextureManager textureManager, Font fontRenderer, float partialTicks, int mouseX, int mouseY) {
171
            super.renderBg(guiGraphics, guiLeft, guiTop, textureManager, fontRenderer, partialTicks, mouseX, mouseY);
×
172

173
            int x = guiLeft + getX();
×
174
            int y = guiTop + getY();
×
175

176
            if (this.shouldRenderElementName()) {
×
177
                guiGraphics.drawString(fontRenderer, element.getName(), x + 2, y + 6, IModHelpers.get().getBaseHelpers().RGBToInt(240, 240, 240), true);
×
178
            }
179

180
            if(showError()) {
×
181
                Component lastError = getLastError();
×
182
                if (lastError != null) {
×
183
                    Images.ERROR.draw(guiGraphics, x + getSignalX(), y + getSignalY() - 1);
×
184
                } else {
185
                    Images.OK.draw(guiGraphics, x + getSignalX(), y + getSignalY() + 1);
×
186
                }
187
            }
188
        }
×
189

190
        public boolean shouldRenderElementName() {
191
            return true;
×
192
        }
193

194
        @Override
195
        public void drawGuiContainerForegroundLayer(GuiGraphics guiGraphics, int guiLeft, int guiTop, TextureManager textureManager, Font fontRenderer, int mouseX, int mouseY) {
196
            super.drawGuiContainerForegroundLayer(guiGraphics, guiLeft, guiTop, textureManager, fontRenderer, mouseX, mouseY);
×
197

198
            int x = getX();
×
199
            int y = getY();
×
200

201
            if(showError()) {
×
202
                Component lastError = getLastError();
×
203
                if (lastError != null && gui.isHovering(x + getSignalX(), y + getSignalY() - 1, Images.ERROR.getSheetWidth(), Images.ERROR.getSheetHeight(), mouseX, mouseY)) {
×
204
                    List<Component> lines = StringHelpers.splitLines(lastError.getString(), IModHelpers.get().getL10NHelpers().getMaxTooltipLineLength(),
×
205
                            ChatFormatting.RED.toString())
×
206
                            .stream()
×
207
                            .map(Component::literal)
×
208
                            .collect(Collectors.toList());
×
209
                    gui.drawTooltip(lines, guiGraphics.pose(), mouseX - guiLeft, mouseY - guiTop);
×
210
                }
211
            }
212
        }
×
213

214
    }
215

216
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc