Skip to content
This repository was archived by the owner on Mar 15, 2023. It is now read-only.

Commit b9435d7

Browse files
committed
Support for more than one calculation in one command
1 parent 1efecba commit b9435d7

File tree

4 files changed

+31
-30
lines changed

4 files changed

+31
-30
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
### Installation
55
1. Press `Win` + `R`
66
2. Paste this into the window that popped up: `%appdata%/.minecraft/LabyMod/addons-1.12` and press enter
7-
3. It should open your Labymod addon directory; Paste the [Calculator.jar](https://github.com/RappyLabyAddons/Calculator/releases/download/1.0.0/Calculator.jar) in there.
7+
3. It should open your Labymod addon directory; Paste the [Calculator.jar](https://github.com/RappyLabyAddons/Calculator/releases/download/1.0.1/Calculator.jar) in there.
88
4. Launch your Labymod client.
99

1010
### Config

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
1111
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.
1212

1313

14-
version = "1"
14+
version = "2"
1515
group = "com.rappytv.calc" // http://maven.apache.org/guides/mini/guide-naming-conventions.html
1616
archivesBaseName = "calc"
1717

src/main/java/com/rappytv/calc/event/ChatEvent.java

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import com.rappytv.calc.Calculator;
44
import net.labymod.api.events.MessageSendEvent;
55
import net.labymod.utils.ModColor;
6+
import scala.actors.Eval;
7+
8+
import javax.script.ScriptEngine;
9+
import javax.script.ScriptEngineManager;
10+
import javax.script.ScriptException;
611

712
public class ChatEvent implements MessageSendEvent {
813

@@ -11,42 +16,38 @@ public boolean onSend(String s) {
1116
String[] args = s.split(" ");
1217

1318
if(args[0].equalsIgnoreCase("/" + Calculator.cmd)) {
14-
if(args.length != 4) {
15-
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "Usage: /" + Calculator.cmd + " <Number> <Operator> <Number>");
19+
if(args.length < 2) {
20+
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.RED + "Usage: /" + Calculator.cmd + " <Operation>\n\u00A78\u00bb");
1621
return true;
1722
}
18-
String operation = args[2];
19-
float number1;
20-
float number2;
23+
StringBuilder operation = new StringBuilder();
24+
for(int i = 1; i < args.length; i++) {
25+
operation.append(args[i]);
26+
}
2127

2228
try {
23-
number1 = Float.parseFloat(args[1]);
24-
number2 = Float.parseFloat(args[3]);
25-
} catch (NumberFormatException e) {
26-
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "On of your provided numbers is invalid!");
27-
return true;
29+
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.GREEN + operation + " = " + formatNumber(calculation(operation.toString())) + "\n\u00A78\u00bb");
30+
} catch (ScriptException e) {
31+
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.RED + "Invalid operation!" + "\n\u00A78\u00bb");
2832
}
29-
if(!operation.equalsIgnoreCase("+") && !operation.equalsIgnoreCase("-") && !operation.equalsIgnoreCase("*") && !operation.equalsIgnoreCase("/")) {
30-
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "Invalid Operator! Valid operators are +, -, *, /");
31-
return true;
32-
}
33-
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.GREEN + formatNumber(number1) + " " + operation + " " + formatNumber(number2) + " = " + formatNumber(calculation(operation, number1, number2)));
3433
return true;
3534
} else return false;
3635
}
3736

38-
public static float calculation(String operation, float num1, float num2) {
39-
switch (operation) {
40-
case "+":
41-
return num1 + num2;
42-
case "-":
43-
return num1 - num2;
44-
case "*":
45-
return num1 * num2;
46-
case "/":
47-
return num1 / num2;
48-
default:
49-
return 0;
37+
public static float calculation(String operation) throws ScriptException {
38+
ScriptEngineManager manager = new ScriptEngineManager();
39+
ScriptEngine engine = manager.getEngineByName("js");
40+
Object num = engine.eval(operation);
41+
42+
if(num instanceof Float) {
43+
return (float) num;
44+
} else if(num instanceof Integer) {
45+
return (int) num;
46+
} else if(num instanceof Double) {
47+
double doubleNumber = (double) num;
48+
return (float) doubleNumber;
49+
} else {
50+
return 0;
5051
}
5152
}
5253

src/main/resources/addon.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"name": "Calculator",
44
"mainClass": "com.rappytv.calc.Calculator",
55
"description": "This addon lets you calculate simple things through your chat.",
6-
"version": 1,
6+
"version": 2,
77
"author": "RappyTV#6969",
88
"category": 2,
99
"icon": "https://cdn.discordapp.com/attachments/938944740920016916/1009237495256453261/icon.png"

0 commit comments

Comments
 (0)