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

CyclopsMC / IntegratedDynamics / 15366166327

31 May 2025 05:58PM UTC coverage: 44.766% (-0.5%) from 45.303%
15366166327

push

github

rubensworks
Bump mod version

2565 of 8515 branches covered (30.12%)

Branch coverage included in aggregate %.

11734 of 23427 relevant lines covered (50.09%)

2.38 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.Helpers;
18
import org.cyclops.cyclopscore.helper.L10NHelpers;
19
import org.cyclops.cyclopscore.helper.StringHelpers;
20
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElement;
21
import org.cyclops.integrateddynamics.api.client.gui.subgui.IGuiInputElementValueType;
22
import org.cyclops.integrateddynamics.api.client.gui.subgui.ISubGuiBox;
23
import org.cyclops.integrateddynamics.api.evaluate.EvaluationException;
24
import org.cyclops.integrateddynamics.api.evaluate.variable.IValue;
25
import org.cyclops.integrateddynamics.api.evaluate.variable.IValueType;
26
import org.cyclops.integrateddynamics.api.logicprogrammer.IConfigRenderPattern;
27
import org.cyclops.integrateddynamics.core.client.gui.subgui.SubGuiBox;
28
import org.cyclops.integrateddynamics.core.evaluate.variable.ValueHelpers;
29
import org.cyclops.integrateddynamics.core.helper.L10NValues;
30

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

132
    @Override
133
    public String getSymbol() {
134
        return L10NHelpers.localize(getValueType().getTranslationKey());
×
135
    }
136

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

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

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

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

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

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

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

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

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

177
            if (this.shouldRenderElementName()) {
×
178
                fontRenderer.drawInBatch(element.getName(), x + 2, y + 6, Helpers.RGBToInt(240, 240, 240), true, guiGraphics.pose().last().pose(), guiGraphics.bufferSource(), Font.DisplayMode.NORMAL, 0, 15728880);
×
179
            }
180

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

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

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

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

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

215
    }
216

217
}
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