This repository has been archived by the owner on Jun 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- setting functions to not exist now works correctly
- copy to clipboard on defined functions - added sgn function - added a terrible factorial - fixed a bug with negative signs not working - reworked the math engine logic - changed `random()` and `rand()` to be `rand` and `random` - added constants
- Loading branch information
Showing
13 changed files
with
379 additions
and
349 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ca.rttv.chatcalc; | ||
|
||
import java.util.Optional; | ||
|
||
public record CustomConstant(String name, String eval) { | ||
|
||
public static Optional<CustomConstant> fromString(String text) { | ||
int equalsIdx = text.indexOf('='); | ||
if (equalsIdx == -1) { | ||
return Optional.empty(); | ||
} | ||
|
||
String lhs = text.substring(0, equalsIdx); | ||
String rhs = text.substring(equalsIdx + 1); | ||
|
||
if (!ChatCalc.CONSTANT.matcher(lhs).matches()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(new CustomConstant(lhs, rhs)); | ||
} | ||
|
||
public double value() { | ||
return Config.makeEngine().eval(eval, new FunctionParameter[0]); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + "=" + eval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ca.rttv.chatcalc; | ||
|
||
import java.util.Optional; | ||
|
||
public record CustomFunction(String name, String eval, String[] params) { | ||
public static Optional<CustomFunction> fromString(String text) { | ||
int equalsIdx = text.indexOf('='); | ||
if (equalsIdx == -1) { | ||
return Optional.empty(); | ||
} | ||
String lhs = text.substring(0, equalsIdx); | ||
String rhs = text.substring(equalsIdx + 1); | ||
if (!ChatCalc.FUNCTION.matcher(lhs).matches()) { | ||
return Optional.empty(); | ||
} | ||
int functionNameEnd = lhs.indexOf('('); | ||
String functionName = lhs.substring(0, functionNameEnd); | ||
int paramsEnd = lhs.substring(functionNameEnd).indexOf(')') + functionNameEnd; | ||
String[] params = lhs.substring(functionNameEnd + 1, paramsEnd).split(ChatCalc.SEPARATOR); | ||
return Optional.of(new CustomFunction(functionName, rhs, params)); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name + '(' + String.join(ChatCalc.SEPARATOR, params) + ")=" + eval; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package ca.rttv.chatcalc; | ||
|
||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.util.math.MathHelper; | ||
|
||
import java.util.LinkedHashSet; | ||
import java.util.function.DoubleSupplier; | ||
|
||
public class MathematicalConstant { | ||
public static final LinkedHashSet<MathematicalConstant> CONSTANTS = new LinkedHashSet<>(); | ||
|
||
static { | ||
CONSTANTS.add(new MathematicalConstant("random", Math::random)); | ||
CONSTANTS.add(new MathematicalConstant("rand", Math::random)); | ||
CONSTANTS.add(new MathematicalConstant("rad", () -> Config.radians() ? 1.0 : 57.29577951308232)); | ||
CONSTANTS.add(new MathematicalConstant("deg", () -> Config.radians() ? 0.017453292519943295 : 1.0)); | ||
CONSTANTS.add(new MathematicalConstant("yaw", () -> Config.convertFromDegrees(MathHelper.wrapDegrees(MinecraftClient.getInstance().player.getYaw())))); | ||
CONSTANTS.add(new MathematicalConstant("pitch", () -> Config.convertFromDegrees(MathHelper.wrapDegrees(MinecraftClient.getInstance().player.getPitch())))); | ||
CONSTANTS.add(new MathematicalConstant("pi", () -> Math.PI)); | ||
CONSTANTS.add(new MathematicalConstant("tau", () -> 2.0d * Math.PI)); | ||
CONSTANTS.add(new MathematicalConstant("e", () -> Math.E)); | ||
CONSTANTS.add(new MathematicalConstant("phi", () -> 1.6180339887498948482)); | ||
CONSTANTS.add(new MathematicalConstant("x", () -> MinecraftClient.getInstance().player.getPos().x)); | ||
CONSTANTS.add(new MathematicalConstant("y", () -> MinecraftClient.getInstance().player.getPos().y)); | ||
CONSTANTS.add(new MathematicalConstant("z", () -> MinecraftClient.getInstance().player.getPos().z)); | ||
} | ||
|
||
private final String name; | ||
private final DoubleSupplier value; | ||
|
||
public MathematicalConstant(String name, DoubleSupplier value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String name() { | ||
return name; | ||
} | ||
|
||
public double value() { | ||
return value.getAsDouble(); | ||
} | ||
} |
Oops, something went wrong.