Fix lib events

This commit is contained in:
ZXMushroom63 2024-09-12 16:53:39 +08:00
parent 7615fb213f
commit 2e60921d5f
3 changed files with 43 additions and 36 deletions

View File

@ -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 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)]; var blockType = blockTypesList[Math.floor(Math.random() * blockTypesList.length)];
blockType = ModAPI.blocks[blockType]; blockType = ModAPI.blocks[blockType];
console.log(blockType);
var block = blockType.getDefaultState().getRef();
console.log(block, blockPos);
pair.world.setBlockState(blockPos, block, 3); pair.world.setBlockState(blockPos, block, 3);
} }
}); });

View File

@ -221,7 +221,7 @@ globalThis.modapi_postinit = `(() => {
} }
ModAPI.reflect.getClassByName = function (className) { ModAPI.reflect.getClassByName = function (className) {
var classKeys = Object.keys(ModAPI.hooks._classMap); 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; return key ? ModAPI.hooks._classMap[key] : null;
} }
var reloadDeprecationWarnings = 0; var reloadDeprecationWarnings = 0;
@ -364,7 +364,7 @@ globalThis.modapi_postinit = `(() => {
if (!callback || typeof callback !== "function") { if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!"); 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])) { if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = []; ModAPI.events.listeners[name] = [];
} }
@ -406,6 +406,15 @@ globalThis.modapi_postinit = `(() => {
}; };
ModAPI.events.callEvent = function callEvent(name, data) { 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 ( if (
!ModAPI.events.types.includes(name) || !ModAPI.events.types.includes(name) ||
!Array.isArray(ModAPI.events.listeners[name]) !Array.isArray(ModAPI.events.listeners[name])
@ -425,19 +434,12 @@ globalThis.modapi_postinit = `(() => {
console.error("Please report this bug to the repo."); console.error("Please report this bug to the repo.");
return; return;
} }
if (name.startsWith("lib:")) { ModAPI.events.listeners[name].forEach((func) => {
ModAPI.events.listeners[name].forEach((func) => { func(data);
func({}); });
}); ModAPI.events.listeners.event.forEach((func) => {
ModAPI.events.lib_map[name] = true; func({ event: name, data: data });
} else { });
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
}
}; };
ModAPI.events.newEvent("update", "client"); ModAPI.events.newEvent("update", "client");
@ -497,11 +499,11 @@ globalThis.modapi_postinit = `(() => {
return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents); return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents);
} }
ModAPI.util.hashCode = function hashCode(string){ ModAPI.util.hashCode = function hashCode(string) {
var hash = 0; var hash = 0;
for (var i = 0; i < string.length; i++) { for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i); var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code; hash = ((hash << 5) - hash) + code;
hash = hash & hash; hash = hash & hash;
} }
return Math.floor(Math.abs(hash)) + ""; return Math.floor(Math.abs(hash)) + "";
@ -654,7 +656,7 @@ globalThis.modapi_postinit = `(() => {
var msg = Math.random() < 0.05 ? "Plugins" : "Mods"; var msg = Math.random() < 0.05 ? "Plugins" : "Mods";
// Find the right constructor. (int id, int x, int y, int width, int height, String buttonText); // 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)); var btn = btnConstructor(9635329, 0, args[0].$height8 - 21, 100, 20, ModAPI.util.str(msg));
args[0].$buttonList.$add(btn); args[0].$buttonList.$add(btn);

View File

@ -221,7 +221,7 @@
} }
ModAPI.reflect.getClassByName = function (className) { ModAPI.reflect.getClassByName = function (className) {
var classKeys = Object.keys(ModAPI.hooks._classMap); 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; return key ? ModAPI.hooks._classMap[key] : null;
} }
var reloadDeprecationWarnings = 0; var reloadDeprecationWarnings = 0;
@ -364,7 +364,7 @@
if (!callback || typeof callback !== "function") { if (!callback || typeof callback !== "function") {
throw new Error("[ModAPI] Invalid callback!"); 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])) { if (!Array.isArray(ModAPI.events.listeners[name])) {
ModAPI.events.listeners[name] = []; ModAPI.events.listeners[name] = [];
} }
@ -406,6 +406,15 @@
}; };
ModAPI.events.callEvent = function callEvent(name, data) { 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 ( if (
!ModAPI.events.types.includes(name) || !ModAPI.events.types.includes(name) ||
!Array.isArray(ModAPI.events.listeners[name]) !Array.isArray(ModAPI.events.listeners[name])
@ -425,19 +434,12 @@
console.error("Please report this bug to the repo."); console.error("Please report this bug to the repo.");
return; return;
} }
if (name.startsWith("lib:")) { ModAPI.events.listeners[name].forEach((func) => {
ModAPI.events.listeners[name].forEach((func) => { func(data);
func({}); });
}); ModAPI.events.listeners.event.forEach((func) => {
ModAPI.events.lib_map[name] = true; func({ event: name, data: data });
} else { });
ModAPI.events.listeners[name].forEach((func) => {
func(data);
});
ModAPI.events.listeners.event.forEach((func) => {
func({ event: name, data: data });
});
}
}; };
ModAPI.events.newEvent("update", "client"); ModAPI.events.newEvent("update", "client");
@ -497,11 +499,11 @@
return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents); return ModAPI.hooks._teavm.$rt_createArrayFromData(arrayClass, arrayContents);
} }
ModAPI.util.hashCode = function hashCode(string){ ModAPI.util.hashCode = function hashCode(string) {
var hash = 0; var hash = 0;
for (var i = 0; i < string.length; i++) { for (var i = 0; i < string.length; i++) {
var code = string.charCodeAt(i); var code = string.charCodeAt(i);
hash = ((hash<<5)-hash)+code; hash = ((hash << 5) - hash) + code;
hash = hash & hash; hash = hash & hash;
} }
return Math.floor(Math.abs(hash)) + ""; return Math.floor(Math.abs(hash)) + "";
@ -654,7 +656,7 @@
var msg = Math.random() < 0.05 ? "Plugins" : "Mods"; var msg = Math.random() < 0.05 ? "Plugins" : "Mods";
// Find the right constructor. (int id, int x, int y, int width, int height, String buttonText); // 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)); var btn = btnConstructor(9635329, 0, args[0].$height8 - 21, 100, 20, ModAPI.util.str(msg));
args[0].$buttonList.$add(btn); args[0].$buttonList.$add(btn);