1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
|
/*
* Copyright (C) 2022 NotEnoughUpdates contributors
*
* This file is part of NotEnoughUpdates.
*
* NotEnoughUpdates is free software: you can redistribute it
* and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either
* version 3 of the License, or (at your option) any later version.
*
* NotEnoughUpdates is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with NotEnoughUpdates. If not, see <https://www.gnu.org/licenses/>.
*/
package io.github.moulberry.notenoughupdates.miscgui;
import io.github.moulberry.notenoughupdates.NotEnoughUpdates;
import io.github.moulberry.notenoughupdates.autosubscribe.NEUAutoSubscribe;
import io.github.moulberry.notenoughupdates.events.SignSubmitEvent;
import io.github.moulberry.notenoughupdates.mixins.AccessorGuiEditSign;
import io.github.moulberry.notenoughupdates.util.Calculator;
import io.github.moulberry.notenoughupdates.util.Utils;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.inventory.GuiEditSign;
import net.minecraft.tileentity.TileEntitySign;
import net.minecraft.util.EnumChatFormatting;
import net.minecraftforge.client.event.GuiScreenEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.Objects;
@NEUAutoSubscribe
public class SignCalculator {
String lastSource = null;
BigDecimal lastResult = null;
Calculator.CalculatorException lastException = null;
private boolean isEnabled() {
return NotEnoughUpdates.INSTANCE.config.misc.calculationMode != 0;
}
@SubscribeEvent
public void onSignDrawn(GuiScreenEvent.DrawScreenEvent.Post event) {
if (!(event.gui instanceof GuiEditSign))
return;
if (!isEnabled()) return;
GuiEditSign guiEditSign = (GuiEditSign) event.gui;
TileEntitySign tileSign = ((AccessorGuiEditSign) guiEditSign).getTileSign();
if (!tileSign.signText[1].getUnformattedText().equals("^^^^^^^^^^^^^^^") &&
!tileSign.signText[1].getUnformattedText().equals("^^^^^^")) return;
String source = tileSign.signText[0].getUnformattedText();
refresh(source);
int calculationMode = NotEnoughUpdates.INSTANCE.config.misc.calculationMode;
if ((calculationMode == 1 && !source.startsWith("!"))) return;
Utils.drawStringCentered(getRenderedString(), guiEditSign.width / 2F, 58, false, 0x808080FF);
}
@SubscribeEvent
public void onSignSubmitted(SignSubmitEvent event) {
if (!isEnabled()) return;
if (Objects.equals(event.lines[1], "^^^^^^^^^^^^^^^") || Objects.equals(event.lines[1], "^^^^^^")) {
refresh(event.lines[0]);
if (lastResult != null) {
event.lines[0] = lastResult.toPlainString();
}
}
}
public String getRenderedString() {
if (lastResult != null) {
DecimalFormat formatter = new DecimalFormat("#,##0.##");
String lr = formatter.format(lastResult);
if (Minecraft.getMinecraft().fontRendererObj.getStringWidth(lr) > 90) {
return EnumChatFormatting.WHITE + lastSource + " " + EnumChatFormatting.YELLOW + "= " + EnumChatFormatting.RED +
"Result too long";
}
return EnumChatFormatting.WHITE + lastSource + " " + EnumChatFormatting.YELLOW + "= " + EnumChatFormatting.GREEN +
lr;
} else if (lastException != null) {
return EnumChatFormatting.RED + lastException.getMessage();
}
return EnumChatFormatting.RED + "No calculation has been done.";
}
private void refresh(String source) {
if (Objects.equals(source, lastSource)) return;
lastSource = source;
int calculationMode = NotEnoughUpdates.INSTANCE.config.misc.calculationMode;
if (source.isEmpty() || calculationMode == 0 || (calculationMode == 1 && !source.startsWith("!"))) {
lastResult = null;
lastException = null;
return;
}
try {
lastResult = Calculator.calculate(calculationMode == 1 ? source.substring(1) : source);
lastException = null;
} catch (Calculator.CalculatorException ex) {
lastException = ex;
lastResult = null;
}
}
}
|