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 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);
}
});

View File

@ -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] = [];
}
@ -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])
@ -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.newEvent("update", "client");

View File

@ -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] = [];
}
@ -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])
@ -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.newEvent("update", "client");