Skip to content

Commit

Permalink
Add displayToChat, js string to jcl string
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Aug 23, 2024
1 parent d33afde commit 4aebb8b
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 11 deletions.
2 changes: 1 addition & 1 deletion examplemods/grapplehook.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ var GrappleHookPlugin = {
};
PluginAPI.addEventListener("update", () => { //Every client tick
if (!PluginAPI.player.fishEntity) { //If the fish hook does not exist.
if (GrappleHookPlugin.prev === "GROUND" && (!GrappleHookPlugin.crouchToCancel || !PluginAPI.player.isSneaking.apply(PluginAPI.javaClient.$thePlayer, []))) { //If the old state was ground
if (GrappleHookPlugin.prev === "GROUND" && (!GrappleHookPlugin.crouchToCancel || !PluginAPI.player.isSneaking())) { //If the old state was ground
GrappleHookPlugin.prev = "NONE"; //Update the state
var mx = GrappleHookPlugin.oldXYZ[0] - PluginAPI.player.posX; //Get delta X
var my = GrappleHookPlugin.oldXYZ[1] - PluginAPI.player.posY; //Get delta Y
Expand Down
53 changes: 46 additions & 7 deletions injector.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<title>EaglerForge Injector</title>
<style>
* {
font-family: sans-serif;
font-family: monospace;
}
button {
border: 2px solid black;
Expand Down Expand Up @@ -249,9 +249,9 @@ <h4>
);

const extractConstructorRegex =
/^\s*function (\S*?)__init_\d+?\((?!\$)/gm;
/^\s*function (\S*?)__init_\d*?\((?!\$)/gm;
const extractConstructorFullNameRegex =
/function (\S*?)__init_[0-9]+/gm;
/function (\S*?)__init_[0-9]*/gm;
patchedFile = patchedFile.replaceAll(
extractConstructorRegex,
(match) => {
Expand Down Expand Up @@ -285,7 +285,7 @@ <h4>
}
ModAPI.hooks._rippedMethodTypeMap[\`${fullName}\`] = \`${
match.includes("function " + fullName + "($this")
? "instance"
? "instance" //Todo: fix static/instance detection
: "static"
}\`;
ModAPI.hooks.methods[\`${fullName}\`]=` +
Expand Down Expand Up @@ -381,8 +381,18 @@ <h4>
}
var outProp = "$" + prop;
console.log(outProp);
return Reflect.get(target, outProp, receiver);
var outputValue = Reflect.get(target, outProp, receiver);
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
return outputValue;
},
set(object, prop, value) {
var outProp = "$" + prop;
Expand All @@ -403,9 +413,14 @@ <h4>
var outProp = "$" + prop;
var outputValue = Reflect.get(target, outProp, receiver);
if (outputValue && typeof outputValue === "object") {
if (outputValue && typeof outputValue === "object" && !Array.isArray(outputValue)) {
return new Proxy(outputValue, TeaVM_to_Recursive_BaseData_ProxyConf);
}
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
return outputValue;
},
set(object, prop, value) {
Expand Down Expand Up @@ -519,6 +534,30 @@ <h4>
console.error(error);
}
}
ModAPI.util.stringToUint16Array = function stringToUint16Array(str) {
const buffer = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const uint16Array = new Uint16Array(buffer);
for (let i = 0; i < str.length; i++) {
uint16Array[i] = str.charCodeAt(i);
}
return uint16Array;
}
var stringDefaultConstructor = ModAPI.hooks._classMap["java.lang.String"].constructors.filter(x => {return x.length === 0})[0];
ModAPI.util.string = ModAPI.util.str = function (string) {
var jclString = stringDefaultConstructor();
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
return jclString;
}
ModAPI.displayToChat = function (param) {
var v = typeof param === "object" ? param.msg : (param + "");
v ||= "";
var jclString = ModAPI.util.string(v);
ModAPI.hooks.methods["nmcg_GuiNewChat_printChatMessage"](ModAPI.javaClient.$ingameGUI.$persistantChatGUI, ModAPI.hooks._classMap["net.minecraft.util.ChatComponentText"].constructors[0](jclString));
}
const updateMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.entity.EntityPlayerSP", "onUpdate");
const originalUpdate = ModAPI.hooks.methods[updateMethodName];
ModAPI.hooks.methods[updateMethodName] = function (...args) {
Expand Down
45 changes: 42 additions & 3 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,18 @@
}

var outProp = "$" + prop;
console.log(outProp);
return Reflect.get(target, outProp, receiver);
var outputValue = Reflect.get(target, outProp, receiver);
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
return outputValue;
},
set(object, prop, value) {
var outProp = "$" + prop;
Expand All @@ -97,9 +107,14 @@

var outProp = "$" + prop;
var outputValue = Reflect.get(target, outProp, receiver);
if (outputValue && typeof outputValue === "object") {
if (outputValue && typeof outputValue === "object" && !Array.isArray(outputValue)) {
return new Proxy(outputValue, TeaVM_to_Recursive_BaseData_ProxyConf);
}
if (outputValue && typeof outputValue === "function") {
return function (...args) {
return outputValue.apply(target, args);
}
}
return outputValue;
},
set(object, prop, value) {
Expand Down Expand Up @@ -213,6 +228,30 @@
console.error(error);
}
}

ModAPI.util.stringToUint16Array = function stringToUint16Array(str) {
const buffer = new ArrayBuffer(str.length * 2); // 2 bytes for each char
const uint16Array = new Uint16Array(buffer);
for (let i = 0; i < str.length; i++) {
uint16Array[i] = str.charCodeAt(i);
}
return uint16Array;
}

var stringDefaultConstructor = ModAPI.hooks._classMap["java.lang.String"].constructors.filter(x => {return x.length === 0})[0];
ModAPI.util.string = ModAPI.util.str = function (string) {
var jclString = stringDefaultConstructor();
jclString.$characters.data = ModAPI.util.stringToUint16Array(string);
return jclString;
}

ModAPI.displayToChat = function (param) {
var v = typeof param === "object" ? param.msg : (param + "");
v ||= "";
var jclString = ModAPI.util.string(v);
ModAPI.hooks.methods["nmcg_GuiNewChat_printChatMessage"](ModAPI.javaClient.$ingameGUI.$persistantChatGUI, ModAPI.hooks._classMap["net.minecraft.util.ChatComponentText"].constructors[0](jclString));
}

const updateMethodName = ModAPI.util.getMethodFromPackage("net.minecraft.client.entity.EntityPlayerSP", "onUpdate");
const originalUpdate = ModAPI.hooks.methods[updateMethodName];
ModAPI.hooks.methods[updateMethodName] = function (...args) {
Expand Down

0 comments on commit 4aebb8b

Please sign in to comment.