Skip to content

Commit

Permalink
Allow equals sign
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed May 12, 2024
1 parent 34a804c commit 7edaa41
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ public class SignCalculator {
private static double output;

public static void renderCalculator(DrawContext context, String message, int renderX, int renderY) {
if (SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.requiresEquals) {
if (message.startsWith("=")) {
message = message.substring(1);
}
else {
output = -1;
lastInput = message;
return;
}
if (SkyblockerConfigManager.get().uiAndVisuals.inputCalculator.requiresEquals && !message.startsWith("=")) {
output = -1;
lastInput = message;
return;
}
if (message.startsWith("=")) {
message = message.substring(1);
}
//only update output if new input
if (!message.equals(lastInput)) { //
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/de/hysky/skyblocker/utils/Calculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ public static class Token {
int tokenLength;
}

private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+\\.?\\d*)([kmbse]?)");
private static final Map<String, Integer> magnitudeValues = Map.of(
"s", 64,
"e", 160,
"k", 1000,
"m", 1000000,
"b", 1000000000
private static final Pattern NUMBER_PATTERN = Pattern.compile("(\\d+\\.?\\d*)([sekmbt]?)");
private static final Map<String, Long> MAGNITUDE_VALUES = Map.of(
"s", 64L,
"e", 160L,
"k", 1_000L,
"m", 1_000_000L,
"b", 1_000_000_000L,
"t", 1_000_000_000_000L
);

private static List<Token> lex(String input) {
Expand Down Expand Up @@ -88,7 +89,7 @@ private static List<Token> shunt(List<Token> tokens) {
Deque<Token> operatorStack = new ArrayDeque<>();
List<Token> outputQueue = new ArrayList<>();

for (Token shuntingToken : tokens)
for (Token shuntingToken : tokens) {
switch (shuntingToken.type) {
case NUMBER -> outputQueue.add(shuntingToken);
case OPERATOR -> {
Expand Down Expand Up @@ -122,6 +123,7 @@ private static List<Token> shunt(List<Token> tokens) {
}
}
}
}
//empty the operator stack
while (!operatorStack.isEmpty()) {
Token leftToken = operatorStack.pop();
Expand Down Expand Up @@ -189,18 +191,18 @@ private static double calculateValue(String value) {
String magnitude = numberMatcher.group(2);

if (!magnitude.isEmpty()) {
if (!magnitudeValues.containsKey(magnitude)) {//its invalid if its another letter
if (!MAGNITUDE_VALUES.containsKey(magnitude)) {//its invalid if its another letter
throw new UnsupportedOperationException("Invalid magnitude");
}
number *= magnitudeValues.get(magnitude);
number *= MAGNITUDE_VALUES.get(magnitude);
}

return number;
}

public static double calculate(String equation) {
//custom bit for replacing purse with its value
equation = equation.toLowerCase().replaceAll("p(urse)?",String.valueOf((int)Utils.getPurse()));
equation = equation.toLowerCase().replaceAll("p(urse)?", String.valueOf(Utils.getPurse()));
return evaluate(shunt(lex(equation)));
}
}

0 comments on commit 7edaa41

Please sign in to comment.