From e33bd95b6ae4eb764bfeed15ef4a1d52d97ebfcc Mon Sep 17 00:00:00 2001 From: ZXMushroom63 Date: Mon, 16 Dec 2024 15:40:53 +0800 Subject: [PATCH] push waypoints mod + increase meta char limit --- examplemods/waypoints.js | 134 +++++++++++++++++++++++++++++++++++++++ postinit.js | 10 +-- 2 files changed, 139 insertions(+), 5 deletions(-) create mode 100644 examplemods/waypoints.js diff --git a/examplemods/waypoints.js b/examplemods/waypoints.js new file mode 100644 index 0000000..e3794c3 --- /dev/null +++ b/examplemods/waypoints.js @@ -0,0 +1,134 @@ +(function Waypoints() { + ModAPI.meta.title("Waypoints Mod"); + ModAPI.meta.description("Use /setwp to make a waypoint, and /wp to go to it. /remwp to delete a waypoint. /listwp to list all waypoints."); + ModAPI.meta.credits("By blizz828, Block_2222 & ZXMushroom63"); + + ModAPI.dedicatedServer.appendCode(async ()=>{ //The mods should probably be running on the server + function initDB(dbName, storeName) { + return new Promise((resolve, reject) => { + const request = indexedDB.open(dbName, 2); + + request.onupgradeneeded = (event) => { + const db = event.target.result; + if (!db.objectStoreNames.contains(storeName)) { + db.createObjectStore(storeName); + } + resolve(db); + }; + + request.onsuccess = (event) => { + const db = event.target.result; + resolve(db); + }; + + request.onerror = (event) => { + reject('Error opening database: ' + event.target.errorCode); + }; + }); + } + function storeString(dbName, storeName, key, value) { + return initDB(dbName, storeName).then((db) => { + return new Promise((resolve, reject) => { + const transaction = db.transaction(storeName, 'readwrite'); + const store = transaction.objectStore(storeName); + const putRequest = store.put(value, key); + + putRequest.onsuccess = () => { + resolve('String stored successfully.'); + }; + + putRequest.onerror = (event) => { + reject('Error storing string: ' + event.target.errorCode); + }; + }); + }); + } + function retrieveString(dbName, storeName, key) { + return initDB(dbName, storeName).then((db) => { + return new Promise((resolve, reject) => { + const transaction = db.transaction(storeName, 'readonly'); + const store = transaction.objectStore(storeName); + const getRequest = store.get(key); + + getRequest.onsuccess = () => { + if (getRequest.result !== undefined) { + resolve(getRequest.result); + } else { + resolve(''); + } + }; + + getRequest.onerror = (event) => { + resolve(''); + }; + }); + }); + } + + + var data = {}; + try { + data = JSON.parse(await retrieveString("waypoints_db", "waypoints", "waypoints")); + } catch(e) { + //didn't ask + } + + async function saveData() { + await storeString("waypoints_db", "waypoints", "waypoints", JSON.stringify(data)); + } + + + ModAPI.addEventListener("processcommand", (e)=>{ + if (!ModAPI.reflect.getClassById("net.minecraft.entity.player.EntityPlayerMP").instanceOf(e.sender.getRef())) { + return; + } + + if (e.command.toLowerCase().startsWith("/setwp ") && e.sender.canCommandSenderUseCommand(2, ModAPI.util.str("setwp"))) { + e.preventDefault = true; + var pos = e.sender.getPosition(); + var name = ModAPI.util.unstring(e.sender.getName().getRef()); + var waypointId = e.command.split(" ")[1] || "waypoint"; + waypointId = waypointId.replace(/[^a-zA-Z0-9_]/gm, "_"); + if (!data[name]) { + data[name] = {}; + } + data[name][waypointId] = [pos.x,pos.y,pos.z]; + saveData(); + e.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("Set waypoint "+waypointId+"."))); + } + if (e.command.toLowerCase().startsWith("/wp ") && e.sender.canCommandSenderUseCommand(2, ModAPI.util.str("wp"))) { + e.preventDefault = true; + var name = ModAPI.util.unstring(e.sender.getName().getRef()); + var waypointId = e.command.split(" ")[1]; + if (waypointId && Array.isArray(data?.[name]?.[waypointId])) { + + // Wildly important! regular setPosition triggers minecraft's built in anti-cheat and teleports you back in the same tick. + e.sender.setPositionAndUpdate(...data?.[name]?.[waypointId]); + + e.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("Teleported to waypoint " + waypointId + "."))); + } else { + e.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("No such waypoint."))); + } + } + if (e.command.toLowerCase().startsWith("/remwp ") && e.sender.canCommandSenderUseCommand(2, ModAPI.util.str("remwp"))) { + e.preventDefault = true; + var name = ModAPI.util.unstring(e.sender.getName().getRef()); + var waypointId = e.command.split(" ")[1] || "waypoint"; + if (!data[name]) { + data[name] = {}; + } + delete data[name][waypointId]; + saveData(); + e.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("Removed waypoint "+waypointId+"."))); + } + if ((e.command.toLowerCase() === "/listwp") && e.sender.canCommandSenderUseCommand(2, ModAPI.util.str("listwp"))) { + e.preventDefault = true; + var name = ModAPI.util.unstring(e.sender.getName().getRef()); + if (!data[name]) { + data[name] = {}; + } + e.sender.addChatMessage(ModAPI.reflect.getClassById("net.minecraft.util.ChatComponentText").constructors[0](ModAPI.util.str("Your waypoints: " + Object.keys(data[name]).join(", ")))); + } + }); + }); +})(); \ No newline at end of file diff --git a/postinit.js b/postinit.js index f7b290c..14e9698 100644 --- a/postinit.js +++ b/postinit.js @@ -42,7 +42,7 @@ globalThis.modapi_postinit = "(" + (() => { if (!document.currentScript.hasAttribute("data-hash")) { return console.log("[ModAPIMeta] Script does not have a hashcode."); } - ModAPI.meta._titleMap[document.currentScript.getAttribute("data-hash")] = limitSize(title, 16); + ModAPI.meta._titleMap[document.currentScript.getAttribute("data-hash")] = limitSize(title, 36); } ModAPI.meta.icon = function (iconSrc) { if (!document.currentScript || document.currentScript.getAttribute("data-isMod") !== "true") { @@ -60,7 +60,7 @@ globalThis.modapi_postinit = "(" + (() => { if (!document.currentScript.hasAttribute("data-hash")) { return console.log("[ModAPIMeta] Script does not have a hashcode."); } - ModAPI.meta._developerMap[document.currentScript.getAttribute("data-hash")] = limitSize(cd, 36); + ModAPI.meta._developerMap[document.currentScript.getAttribute("data-hash")] = limitSize(cd, 128); } ModAPI.meta.description = function (desc) { if (!document.currentScript || document.currentScript.getAttribute("data-isMod") !== "true") { @@ -78,7 +78,7 @@ globalThis.modapi_postinit = "(" + (() => { if (!document.currentScript.hasAttribute("data-hash")) { return console.log("[ModAPIMeta] Script does not have a hashcode."); } - ModAPI.meta._versionMap[document.currentScript.getAttribute("data-hash")] = limitSize(ver, 6); + ModAPI.meta._versionMap[document.currentScript.getAttribute("data-hash")] = limitSize(ver, 7); } ModAPI.reflect ||= {}; ModAPI.server = ModAPI.serverInstance = null; @@ -471,7 +471,7 @@ globalThis.modapi_postinit = "(" + (() => { } ModAPI.events.listeners[name].push(callback); } - console.log("[ModAPI] Added new library listener."); + console.log("[ModAPI] Added new library listener: " + name); return; } if (!callback || typeof callback !== "function") { @@ -482,7 +482,7 @@ globalThis.modapi_postinit = "(" + (() => { ModAPI.events.listeners[name] = []; } ModAPI.events.listeners[name].push(callback); - console.log("[ModAPI] Added new event listener."); + console.log("[ModAPI] Added new event listener: " + name); } else { throw new Error("[ModAPI] This event does not exist!"); }