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

Commit

Permalink
Support for more than one calculation in one command
Browse files Browse the repository at this point in the history
  • Loading branch information
RappyTV committed Aug 17, 2022
1 parent 1efecba commit b9435d7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 30 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
### Installation
1. Press `Win` + `R`
2. Paste this into the window that popped up: `%appdata%/.minecraft/LabyMod/addons-1.12` and press enter
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.
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.
4. Launch your Labymod client.

### Config
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ apply plugin: 'net.minecraftforge.gradle.forge'
//Only edit below this line, the above code adds and enables the necessary things for Forge to be setup.


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

Expand Down
55 changes: 28 additions & 27 deletions src/main/java/com/rappytv/calc/event/ChatEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import com.rappytv.calc.Calculator;
import net.labymod.api.events.MessageSendEvent;
import net.labymod.utils.ModColor;
import scala.actors.Eval;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

public class ChatEvent implements MessageSendEvent {

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

if(args[0].equalsIgnoreCase("/" + Calculator.cmd)) {
if(args.length != 4) {
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "Usage: /" + Calculator.cmd + " <Number> <Operator> <Number>");
if(args.length < 2) {
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.RED + "Usage: /" + Calculator.cmd + " <Operation>\n\u00A78\u00bb");
return true;
}
String operation = args[2];
float number1;
float number2;
StringBuilder operation = new StringBuilder();
for(int i = 1; i < args.length; i++) {
operation.append(args[i]);
}

try {
number1 = Float.parseFloat(args[1]);
number2 = Float.parseFloat(args[3]);
} catch (NumberFormatException e) {
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "On of your provided numbers is invalid!");
return true;
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.GREEN + operation + " = " + formatNumber(calculation(operation.toString())) + "\n\u00A78\u00bb");
} catch (ScriptException e) {
Calculator.get().getApi().displayMessageInChat("\u00A78\u00bb\n" + Calculator.prefix + ModColor.RED + "Invalid operation!" + "\n\u00A78\u00bb");
}
if(!operation.equalsIgnoreCase("+") && !operation.equalsIgnoreCase("-") && !operation.equalsIgnoreCase("*") && !operation.equalsIgnoreCase("/")) {
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.RED + "Invalid Operator! Valid operators are +, -, *, /");
return true;
}
Calculator.get().getApi().displayMessageInChat(Calculator.prefix + ModColor.GREEN + formatNumber(number1) + " " + operation + " " + formatNumber(number2) + " = " + formatNumber(calculation(operation, number1, number2)));
return true;
} else return false;
}

public static float calculation(String operation, float num1, float num2) {
switch (operation) {
case "+":
return num1 + num2;
case "-":
return num1 - num2;
case "*":
return num1 * num2;
case "/":
return num1 / num2;
default:
return 0;
public static float calculation(String operation) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("js");
Object num = engine.eval(operation);

if(num instanceof Float) {
return (float) num;
} else if(num instanceof Integer) {
return (int) num;
} else if(num instanceof Double) {
double doubleNumber = (double) num;
return (float) doubleNumber;
} else {
return 0;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/addon.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"name": "Calculator",
"mainClass": "com.rappytv.calc.Calculator",
"description": "This addon lets you calculate simple things through your chat.",
"version": 1,
"version": 2,
"author": "RappyTV#6969",
"category": 2,
"icon": "https://cdn.discordapp.com/attachments/938944740920016916/1009237495256453261/icon.png"
Expand Down

0 comments on commit b9435d7

Please sign in to comment.