Skip to content

Commit

Permalink
Fix lib events
Browse files Browse the repository at this point in the history
  • Loading branch information
ZXMushroom63 committed Sep 12, 2024
1 parent 7615fb2 commit 2e60921
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 36 deletions.
3 changes: 3 additions & 0 deletions examplemods/blocklook.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ ModAPI.dedicatedServer.appendCode(function () {
var blockPos = blockPosConstructor(Math.round(hitResult.$hitVec.$xCoord), Math.round(hitResult.$hitVec.$yCoord), Math.round(hitResult.$hitVec.$zCoord));
var blockType = blockTypesList[Math.floor(Math.random() * blockTypesList.length)];
blockType = ModAPI.blocks[blockType];
console.log(blockType);
var block = blockType.getDefaultState().getRef();
console.log(block, blockPos);
pair.world.setBlockState(blockPos, block, 3);
}
});
Expand Down
38 changes: 20 additions & 18 deletions postinit.injector.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ globalThis.modapi_postinit = `(() => {
}
ModAPI.reflect.getClassByName = function (className) {
var classKeys = Object.keys(ModAPI.hooks._classMap);
var key = classKeys.filter(k => {return ModAPI.hooks._classMap[k].name === className})[0];
var key = classKeys.filter(k => { return ModAPI.hooks._classMap[k].name === className })[0];
return key ? ModAPI.hooks._classMap[key] : null;
}
var reloadDeprecationWarnings = 0;
Expand Down Expand Up @@ -364,7 +364,7 @@ globalThis.modapi_postinit = `(() => {
if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!");
}
if (ModAPI.events.types.includes(name) || name.startsWith("custom:") || name.startsWith("lib:")) {
if (ModAPI.events.types.includes(name) || name.startsWith("custom:")) {
if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = [];
}
Expand Down Expand Up @@ -406,6 +406,15 @@ globalThis.modapi_postinit = `(() => {
};
ModAPI.events.callEvent = function callEvent(name, data) {
if (ModAPI.events.types.includes(name) && name.startsWith("lib:")) {
if (Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name].forEach((func) => {
func({});
});
}
ModAPI.events.lib_map[name] = true;
return;
}
if (
!ModAPI.events.types.includes(name) ||
!Array.isArray(ModAPI.events.listeners[name])
Expand All @@ -425,19 +434,12 @@ globalThis.modapi_postinit = `(() => {
console.error("Please report this bug to the repo.");
return;
}
if (name.startsWith("lib:")) {
ModAPI.events.listeners[name].forEach((func) => {
func({});
});
ModAPI.events.lib_map[name] = true;
} else {
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
}
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
};
ModAPI.events.newEvent("update", "client");
Expand Down Expand Up @@ -497,11 +499,11 @@ globalThis.modapi_postinit = `(() => {
return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents);
}
ModAPI.util.hashCode = function hashCode(string){
ModAPI.util.hashCode = function hashCode(string) {
var hash = 0;
for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = ((hash << 5) - hash) + code;
hash = hash & hash;
}
return Math.floor(Math.abs(hash)) + "";
Expand Down Expand Up @@ -654,7 +656,7 @@ globalThis.modapi_postinit = `(() => {
var msg = Math.random() < 0.05 ? "Plugins" : "Mods";
// Find the right constructor. (int id, int x, int y, int width, int height, String buttonText);
var btnConstructor = ModAPI.hooks._classMap['nmcg_GuiButton'].constructors.filter(c => {return c.length === 6})[0];
var btnConstructor = ModAPI.hooks._classMap['nmcg_GuiButton'].constructors.filter(c => { return c.length === 6 })[0];
var btn = btnConstructor(9635329, 0, args[0].$height8 - 21, 100, 20, ModAPI.util.str(msg));
args[0].$buttonList.$add(btn);
Expand Down
38 changes: 20 additions & 18 deletions postinit.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@
}
ModAPI.reflect.getClassByName = function (className) {
var classKeys = Object.keys(ModAPI.hooks._classMap);
var key = classKeys.filter(k => {return ModAPI.hooks._classMap[k].name === className})[0];
var key = classKeys.filter(k => { return ModAPI.hooks._classMap[k].name === className })[0];
return key ? ModAPI.hooks._classMap[key] : null;
}
var reloadDeprecationWarnings = 0;
Expand Down Expand Up @@ -364,7 +364,7 @@
if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!");
}
if (ModAPI.events.types.includes(name) || name.startsWith("custom:") || name.startsWith("lib:")) {
if (ModAPI.events.types.includes(name) || name.startsWith("custom:")) {
if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = [];
}
Expand Down Expand Up @@ -406,6 +406,15 @@
};

ModAPI.events.callEvent = function callEvent(name, data) {
if (ModAPI.events.types.includes(name) && name.startsWith("lib:")) {
if (Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name].forEach((func) => {
func({});
});
}
ModAPI.events.lib_map[name] = true;
return;
}
if (
!ModAPI.events.types.includes(name) ||
!Array.isArray(ModAPI.events.listeners[name])
Expand All @@ -425,19 +434,12 @@
console.error("Please report this bug to the repo.");
return;
}
if (name.startsWith("lib:")) {
ModAPI.events.listeners[name].forEach((func) => {
func({});
});
ModAPI.events.lib_map[name] = true;
} else {
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
}
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
};
ModAPI.events.newEvent("update", "client");

Expand Down Expand Up @@ -497,11 +499,11 @@
return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents);
}

ModAPI.util.hashCode = function hashCode(string){
ModAPI.util.hashCode = function hashCode(string) {
var hash = 0;
for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code;
hash = ((hash << 5) - hash) + code;
hash = hash & hash;
}
return Math.floor(Math.abs(hash)) + "";
Expand Down Expand Up @@ -654,7 +656,7 @@
var msg = Math.random() < 0.05 ? "Plugins" : "Mods";

// Find the right constructor. (int id, int x, int y, int width, int height, String buttonText);
var btnConstructor = ModAPI.hooks._classMap['nmcg_GuiButton'].constructors.filter(c => {return c.length === 6})[0];
var btnConstructor = ModAPI.hooks._classMap['nmcg_GuiButton'].constructors.filter(c => { return c.length === 6 })[0];
var btn = btnConstructor(9635329, 0, args[0].$height8 - 21, 100, 20, ModAPI.util.str(msg));
args[0].$buttonList.$add(btn);

Expand Down

0 comments on commit 2e60921

Please sign in to comment.