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

Commit

Permalink
Fixed #9 and #11, added yaw and pitch
Browse files Browse the repository at this point in the history
  • Loading branch information
RealRTTV committed Nov 12, 2023
1 parent a8e4c9b commit 0088208
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 55 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.14.23

# Mod Properties
mod_version = 3.0.17
mod_version = 3.0.18
maven_group = ca.rttv
archives_base_name = chatcalc
2 changes: 0 additions & 2 deletions src/main/java/ca/rttv/chatcalc/CallableFunction.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package ca.rttv.chatcalc;

import java.util.Arrays;
import java.util.Optional;
import java.util.stream.Collectors;

public record CallableFunction(String name, String rest, String[] params) {
public static Optional<CallableFunction> fromString(String str) {
Expand Down
44 changes: 21 additions & 23 deletions src/main/java/ca/rttv/chatcalc/ChatCalc.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package ca.rttv.chatcalc;

import com.mojang.logging.LogUtils;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.widget.TextFieldWidget;
import net.minecraft.text.Text;
import net.minecraft.util.Pair;
import org.jetbrains.annotations.Contract;
import org.slf4j.Logger;
import oshi.util.tuples.Triplet;

import java.util.Optional;
import java.util.regex.Pattern;
Expand All @@ -24,28 +20,30 @@ public static boolean tryParse(TextFieldWidget field) {
String originalText = field.getText();
int cursor = field.getCursor();
String text = ChatHelper.getWord(originalText, cursor);
String[] split = text.split("=");
if (split.length == 0) {
return false;
}
if (split.length == 2) {
if (Config.JSON.has(split[0])) {
Config.JSON.addProperty(split[0], split[1]);
Config.refreshJson();
return ChatHelper.replaceWord(field, "");
} else {
Optional<CallableFunction> func = CallableFunction.fromString(text);
if (func.isPresent()) {
Config.FUNCTIONS.put(func.get().name(), func.get());
{
String[] split = text.split("=");
if (split.length == 2) {
if (Config.JSON.has(split[0])) {
Config.JSON.addProperty(split[0], split[1]);
Config.refreshJson();
return ChatHelper.replaceWord(field, "");
};
} else {
Optional<CallableFunction> func = CallableFunction.fromString(text);
if (func.isPresent()) {
Config.FUNCTIONS.put(func.get().name(), func.get());
Config.refreshJson();
return ChatHelper.replaceWord(field, "");
}
;
}
} else if (split.length == 1) {
if (Config.JSON.has(split[0])) {
return ChatHelper.replaceWord(field, Config.JSON.get(split[0]).getAsString());
} 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;
}
}
} else if (Config.JSON.has(split[0])) {
return ChatHelper.replaceWord(field, Config.JSON.get(split[0]).getAsString());
} 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;
}

if ((text.equals("config?") || text.equals("cfg?") || text.equals("?")) && client.player != null) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/ca/rttv/chatcalc/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import com.google.common.collect.ImmutableMap;
import com.google.gson.*;
import net.minecraft.client.MinecraftClient;
import net.minecraft.util.Pair;
import oshi.util.tuples.Triplet;

import java.io.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

public class Config {
Expand Down Expand Up @@ -60,8 +57,12 @@ public static DecimalFormat getDecimalFormat() {
return new DecimalFormat(JSON.get("decimal_format").getAsString());
}

public static double convertIfRadians(double value) {
return Boolean.parseBoolean(JSON.get("radians").getAsString()) ? value : Math.toRadians(value); // sine takes in radians, so we have to do inverse, if we have radians, it'll convert, if we don't, we need to cancel out
public static double convertFromDegrees(double value) {
return Boolean.parseBoolean(JSON.get("radians").getAsString()) ? value : Math.toRadians(value);
}

public static double convertFromRadians(double value) {
return Boolean.parseBoolean(JSON.get("radians").getAsString()) ? value : Math.toDegrees(value);
}

public static void refreshJson() {
Expand Down
38 changes: 26 additions & 12 deletions src/main/java/ca/rttv/chatcalc/MathematicalFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,32 @@ public final class MathematicalFunction {
functions = new HashMap<>();
functions.put("sqrt", Math::sqrt);
functions.put("cbrt", Math::cbrt);
functions.put("sin", val -> Math.sin(Config.convertIfRadians(val)));
functions.put("asin", val -> Math.asin(Config.convertIfRadians(val)));
functions.put("arcsin", val -> Math.asin(Config.convertIfRadians(val)));
functions.put("cos", val -> Math.cos(Config.convertIfRadians(val)));
functions.put("acos", val -> Math.acos(Config.convertIfRadians(val)));
functions.put("arccos", val -> Math.acos(Config.convertIfRadians(val)));
functions.put("tan", val -> Math.tan(Config.convertIfRadians(val)));
functions.put("atan", val -> Math.atan(Config.convertIfRadians(val)));
functions.put("arctan", val -> Math.atan(Config.convertIfRadians(val)));
functions.put("sec", val -> 1 / Math.cos(Config.convertIfRadians(val)));
functions.put("csc", val -> 1 / Math.sin(Config.convertIfRadians(val)));
functions.put("cot", val -> 1 / Math.tan(Config.convertIfRadians(val)));

functions.put("sin", val -> Math.sin(Config.convertFromDegrees(val)));
functions.put("cos", val -> Math.cos(Config.convertFromDegrees(val)));
functions.put("tan", val -> Math.tan(Config.convertFromDegrees(val)));
functions.put("csc", val -> 1 / Math.sin(Config.convertFromDegrees(val)));
functions.put("sec", val -> 1 / Math.cos(Config.convertFromDegrees(val)));
functions.put("cot", val -> 1 / Math.tan(Config.convertFromDegrees(val)));

functions.put("arcsin", val -> Config.convertFromRadians(Math.asin(val)));
functions.put("asin", val -> Config.convertFromRadians(Math.asin(val)));

functions.put("acos", val -> Config.convertFromRadians(Math.acos(val)));
functions.put("arccos", val -> Config.convertFromRadians(Math.acos(val)));

functions.put("atan", val -> Config.convertFromRadians(Math.atan(val)));
functions.put("arctan", val -> Config.convertFromRadians(Math.atan(val)));

functions.put("arccsc", val -> Config.convertFromRadians(Math.asin(1 / val)));
functions.put("acsc", val -> Config.convertFromRadians(Math.asin(1/ val)));

functions.put("arcsec", val -> Config.convertFromRadians(Math.acos(1 / val)));
functions.put("asec", val -> Config.convertFromRadians(Math.acos(1/ val)));

functions.put("arccot", val -> Config.convertFromRadians(Math.atan(1 / val)));
functions.put("acot", val -> Config.convertFromRadians(Math.atan(1/ val)));

functions.put("floor", Math::floor);
functions.put("ceil", Math::ceil);
functions.put("round", x -> Math.floor(x + 0.5d));
Expand Down
27 changes: 20 additions & 7 deletions src/main/java/ca/rttv/chatcalc/NibbleMathEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;

import java.nio.charset.StandardCharsets;
import java.util.Optional;

public class NibbleMathEngine implements MathEngine {
byte[] bytes;
int idx;
FunctionParameter[] params;
double x, y, z;
double x, y, z, yaw, pitch;

@Override
public double eval(String input, FunctionParameter[] paramaters) {
final MinecraftClient client = MinecraftClient.getInstance();
bytes = fixParenthesis(input).concat("\0").getBytes(StandardCharsets.US_ASCII); // we shouldn't encounter unicode in our math
idx = 0;
params = paramaters;
//noinspection DataFlowIssue -- player != null when the chat != null
Vec3d pos = MinecraftClient.getInstance().player.getPos();
Vec3d pos = client.player.getPos();
x = pos.x;
y = pos.y;
z = pos.z;
yaw = Config.convertFromRadians(client.player.getYaw());
pitch = Config.convertFromRadians(client.player.getPitch());
double result = expression(false);
if (idx + 1 != bytes.length) {
throw new IllegalArgumentException("Evaluation had unexpected remaining characters");
Expand Down Expand Up @@ -123,6 +124,18 @@ private double part(boolean abs) {
double u = 1.0;
b:
while (true) {
if (func.startsWith("yaw")) {
x *= u;
u = this.yaw;
func = func.substring(4);
continue;
}
if (func.startsWith("pitch")) {
x *= u;
u = this.pitch;
func = func.substring(4);
continue;
}
if (func.startsWith("pi")) {
x *= u;
u = Math.PI;
Expand Down Expand Up @@ -162,7 +175,7 @@ private double part(boolean abs) {
}
}
if (func.isEmpty()) {
if (bite('^')) u = Math.pow(u, part(false));
if (bite('^')) u = Math.pow(u, term(false));
x *= u;
break a;
}
Expand All @@ -179,7 +192,7 @@ private double part(boolean abs) {
}
int param_count = 1;
double exponent = 1.0;
if (bite('^')) exponent = part(false);
if (bite('^')) exponent = term(false);
if (!bite('(')) throw new IllegalArgumentException("Expected parenthesis for function");
int depth = 0;
int before = idx;
Expand All @@ -204,7 +217,7 @@ else if (bytes[idx] == ')') {
throw new IllegalArgumentException("Expected a valid character for equation, not " + (char) bytes[idx]);
}

if (bite('^')) x = Math.pow(x, part(false));
if (bite('^')) x = Math.pow(x, term(false));

return Double.longBitsToDouble(Double.doubleToLongBits(x) | sign);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,19 @@
abstract class TextFieldWidgetMixin {
@Shadow @Final private TextRenderer textRenderer;

@Shadow private String text;

@Shadow public native int getCursor();

@Shadow public native String getText();

@Unique
@Nullable
private Pair<String, Double> evaluationCache;

// 1.20.2
@Inject(method = "renderButton", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Ljava/lang/String;isEmpty()Z", ordinal = 1), locals = LocalCapture.CAPTURE_FAILSOFT)
private void renderButton1202(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci, int i, int j, String string, boolean bl, boolean bl2, int k, int l, int m, int n, boolean bl3, int o) {
chatcalc$displayAbove(context, o, l);
}

// // 1.20.1
// @Inject(method = "renderButton", at = @At(value = "INVOKE", shift = At.Shift.AFTER, target = "Ljava/lang/String;isEmpty()Z", ordinal = 1), locals = LocalCapture.CAPTURE_FAILSOFT)
// private void renderButton1201(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci, int i, int j, int k, String string, boolean bl, boolean bl2, int l, int m, int n, boolean bl3, int o) {
// chatcalc$displayAbove(context, o, m);
Expand All @@ -49,7 +47,7 @@ private void renderButton1202(DrawContext context, int mouseX, int mouseY, float
return;
}

String word = ChatHelper.getWord(text, getCursor());
String word = ChatHelper.getWord(getText(), getCursor());

if (ChatCalc.NUMBER.matcher(word).matches()) {
return;
Expand Down

0 comments on commit 0088208

Please sign in to comment.