Skip to content
This repository has been archived by the owner on Jun 30, 2024. It is now read-only.

Commit

Permalink
fixed numerous tiny issues
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Jun 30, 2022
1 parent a1bc970 commit b5d354d
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.14.6

# Mod Properties
mod_version = 3.0.3
mod_version = 3.0.4
maven_group = ca.rttv
archives_base_name = chatcalc

Expand Down
13 changes: 8 additions & 5 deletions src/main/java/ca/rttv/chatcalc/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,27 +61,31 @@ public static boolean runExpression(TextFieldWidget field) {
}
} else if (json.has(split[0])) {
return ChatHelper.replaceWord(field, json.get(split[0]).getAsString());
} else if (json.has(split[0].substring(0, split[0].length() - 1)) && split[0].endsWith("?") && client.player != null) {
} else if (split[0].length() > 0 && json.has(split[0].substring(0, split[0].length() - 1)) && split[0].endsWith("?") && client.player != null) {
client.player.sendMessage(Text.translatable("chatcalc." + split[0].substring(0, split[0].length() - 1) + ".description"));
return false;
}

return EventHandler.runExprReplace(field) || EventHandler.runExprAdd(field);
try {
Double.parseDouble(ChatHelper.getWord(field.getText(), field.getCursor()));
return false;
} catch (NumberFormatException e) {
return EventHandler.runExprReplace(field) || EventHandler.runExprAdd(field);
}
}

private static boolean runExprReplace(TextFieldWidget field) {
String originalText = field.getText();
int cursor = field.getCursor();
try {
String word = ChatHelper.getWord(originalText, cursor);
if (word.endsWith("=")) {
if (word.endsWith("=") || word.length() == 0) {
return false;
}
double solution = MathEngine.eval(word);
String solStr = EventHandler.getDecimalFormat().format(solution);
return ChatHelper.replaceWord(field, solStr);
} catch (Exception e) {
LOGGER.warn("Error parsing equation; " + e.getClass().getSimpleName() + ": " + e.getMessage());
return false;
}
}
Expand All @@ -99,7 +103,6 @@ private static boolean runExprAdd(TextFieldWidget field) {
String solStr = EventHandler.getDecimalFormat().format(solution);
return ChatHelper.addWordAfterIndex(field, ChatHelper.getEndOfWord(originalText, cursor), solStr);
} catch (Exception e) {
LOGGER.warn("Error parsing equation; " + e.getClass().getSimpleName() + ": " + e.getMessage());
return false;
}
}
Expand Down

0 comments on commit b5d354d

Please sign in to comment.