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

Commit

Permalink
removed the previous engine since it sucked, then made it use gema in…
Browse files Browse the repository at this point in the history
…stead of bedmas. also added hints above so you don't have to press tab, even easier, eh?
  • Loading branch information
RealRTTV committed Oct 13, 2023
1 parent 8911efd commit d75a32f
Show file tree
Hide file tree
Showing 17 changed files with 99 additions and 656 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ dependencies {
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
}

processResources {
Expand Down
11 changes: 4 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.19.2
yarn_mappings=1.19.2+build.1
loader_version=0.14.9
minecraft_version=1.20.2
yarn_mappings=1.20.2+build.4
loader_version=0.14.23

# Mod Properties
mod_version = 3.0.15
mod_version = 3.0.16
maven_group = ca.rttv
archives_base_name = chatcalc

# Dependencies
fabric_version=0.59.0+1.19.2
4 changes: 1 addition & 3 deletions src/main/java/ca/rttv/chatcalc/ChatCalc.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ca.rttv.chatcalc;

import ca.rttv.chatcalc.tokens.Token;
import com.mojang.logging.LogUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
Expand All @@ -11,7 +10,6 @@
import org.slf4j.Logger;
import oshi.util.tuples.Triplet;

import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;

Expand Down Expand Up @@ -43,7 +41,7 @@ public static boolean tryParse(TextFieldWidget field) {
}
} else if (Config.JSON.has(split[0])) {
return ChatHelper.replaceWord(field, Config.JSON.get(split[0]).getAsString());
} else if (split[0].length() > 0 && Config.JSON.has(split[0].substring(0, split[0].length() - 1)) && split[0].endsWith("?") && client.player != null) {
} else if (!split[0].isEmpty() && Config.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;
}
Expand Down
12 changes: 3 additions & 9 deletions src/main/java/ca/rttv/chatcalc/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class Config {
.put("log_exceptions", "false")
.put("copy_type", "none")
.put("calculate_last", "true")
.put("engine", "nibble")
.put("display_above", "true")
.build();
CONFIG_FILE = new File(".", "config/chatcalc.json");
GSON = new GsonBuilder().setPrettyPrinting().create();
Expand Down Expand Up @@ -119,9 +119,7 @@ public static Optional<Triplet<String, String, String[]>> parseFunction(String f
return Optional.empty();
}

public static boolean debugTokens() {
return Boolean.parseBoolean(JSON.get("debug_tokens").getAsString());
}
public static boolean displayAbove() { return Boolean.parseBoolean(JSON.get("display_above").getAsString()); }

public static void saveToChatHud(String input) {
if (JSON.get("copy_type").getAsString().equalsIgnoreCase("chat_history")) {
Expand Down Expand Up @@ -157,10 +155,6 @@ public static void saveToClipboard(String input) {
}

public static MathEngine makeEngine() {
if (JSON.get("engine").getAsString().equals("token")) {
return new TokenizedMathEngine();
} else {
return new NibbleMathEngine();
}
return new NibbleMathEngine();
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
package ca.rttv.chatcalc.tokens;

import ca.rttv.chatcalc.Config;
import ca.rttv.chatcalc.TokenizedMathEngine;
import net.minecraft.text.LiteralTextContent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
package ca.rttv.chatcalc;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.function.DoubleUnaryOperator;

public final class FunctionToken implements Token {
public final class MathematicalFunction {
private static final Map<String, DoubleUnaryOperator> functions;

static {
Expand Down Expand Up @@ -45,17 +38,11 @@ public String toString() {
return func;
}

public FunctionToken(String value) {
public MathematicalFunction(String value) {
func = value;
}

public double apply(double... values) {
if (func.startsWith("log_")) {
if (values.length != 1) {
throw new IllegalArgumentException();
}
return log(Config.makeEngine().eval(func.substring(4), Optional.empty()), values[0]);
}
if (functions.containsKey(func)) {
if (values.length != 1) {
throw new IllegalArgumentException();
Expand All @@ -69,9 +56,4 @@ public double apply(double... values) {
public static double log(double base, double value) {
return Math.log(value) / Math.log(base);
}

@Override
public Text toText() {
return MutableText.of(new LiteralTextContent("§e" + func));
}
}
49 changes: 29 additions & 20 deletions src/main/java/ca/rttv/chatcalc/NibbleMathEngine.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ca.rttv.chatcalc;

import ca.rttv.chatcalc.tokens.FunctionToken;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -38,7 +37,7 @@ private boolean bite(char bite) { // pun intended

private double expression(boolean abs) {
double x = modulo(abs);
while (true) { // prolly unnecessarily
while (true) {
if (bite('+')) x += modulo(abs);
else if (bite('-')) x -= modulo(abs);
else return x;
Expand All @@ -47,27 +46,38 @@ private double expression(boolean abs) {

private double modulo(boolean abs) {
double x = term(abs);
while (true) { // prolly unnecessarily
while (true) {
if (bite('%')) x %= term(abs);
else return x;
}
}

private double term(boolean abs) {
double x = grouping(abs);
while (true) {
if (bite('*')) x *= grouping(abs);
else if (bite('/')) x /= grouping(abs);
else if (bytes[idx] <= '9' & bytes[idx] >= '0') x *= expression(abs);
else if (!abs & bytes[idx] == '|') x *= Math.abs(expression(false)); // simplify to false
else return x;
}
}

private double grouping(boolean abs) {
double x = part(abs);
while (true) {
if (bite('*')) x *= part(abs);
else if (bite('/')) x /= part(abs);
else if (bytes[idx] == '(') x *= expression(abs);
else if (bytes[idx] <= '9' && bytes[idx] >= '0') x *= expression(abs);
else if (!abs && bytes[idx] == '|') x *= Math.abs(expression(false)); // simplify to false
if (bytes[idx] == '(') x *= expression(abs);
else return x;
}
}

private double part(boolean abs) {
long sign = bite('-') ? 0x8000_0000_0000_0000L : 0L;
if (sign == 0) bite('+');
long sign = 0L;
while (bytes[idx] == '+' | bytes[idx] == '-') {
if (bytes[idx++] == '-') {
sign ^= 0x8000_0000_0000_0000L;
}
}

double x = 1.0;

Expand All @@ -79,10 +89,10 @@ private double part(boolean abs) {
} else if (!abs && bite('|')) {
x = Math.abs(expression(true));
if (!bite('|')) throw new IllegalArgumentException("Expected closing absolute value character");
} else if ((bytes[idx] <= '9' & bytes[idx] >= '0') | bytes[idx] == '.') {
} else if ((bytes[idx] <= '9' & bytes[idx] >= '0') | bytes[idx] == '.' | bytes[idx] == ',') {
int start = idx;
while ((bytes[idx] <= '9' & bytes[idx] >= '0') | bytes[idx] == '.') idx++;
x = Double.parseDouble(new String(bytes, start, idx - start, StandardCharsets.US_ASCII));
while ((bytes[idx] <= '9' & bytes[idx] >= '0') | bytes[idx] == '.' | bytes[idx] == ',') idx++;
x = Double.parseDouble(new String(bytes, start, idx - start, StandardCharsets.US_ASCII).replace(",", ""));
} else if (bytes[idx] <= 'z' & bytes[idx] >= 'a') {
int start = idx;
while (bytes[idx] <= 'z' & bytes[idx] >= 'a' | bytes[idx] == '_') idx++;
Expand Down Expand Up @@ -129,7 +139,7 @@ private double part(boolean abs) {
}
}
}
if (func.length() == 0) {
if (func.isEmpty()) {
if (bite('^')) u = Math.pow(u, part(false));
x *= u;
break a;
Expand All @@ -138,10 +148,10 @@ private double part(boolean abs) {
}
if (func.equals("log_")) {
double base = part(false);
if (!bite('(')) throw new IllegalArgumentException("Expected parenthesis for function");
if (!bite('(')) throw new IllegalArgumentException("Expected parenthesis for logarithmic function");
double value = expression(false);
if (!bite(')')) throw new IllegalArgumentException("Expected closing parenthesis for function");
x *= FunctionToken.log(base, value);
if (!bite(')')) throw new IllegalArgumentException("Expected closing parenthesis for logarithmic function");
x *= MathematicalFunction.log(base, value);
break a;
}
int param_count = 1;
Expand All @@ -166,9 +176,8 @@ else if (bytes[idx] == ')') {
if (bite(')')) break;
if (!bite(';')) throw new AssertionError("Expected that a semicolon exists between the parameters");
}
x *= Math.pow(new FunctionToken(func).apply(values), exponent);
} else
throw new IllegalArgumentException("Expected a valid character for equation, not " + (char) bytes[idx]);
x *= Math.pow(new MathematicalFunction(func).apply(values), exponent);
} else throw new IllegalArgumentException("Expected a valid character for equation, not " + (char) bytes[idx]);
}

if (bite('^')) x = Math.pow(x, part(false));
Expand Down
Loading

0 comments on commit d75a32f

Please sign in to comment.