Last active
February 25, 2023 08:15
-
-
Save LowByteFox/85d2d110ae0b29a0fe5f6cee02c3edc6 to your computer and use it in GitHub Desktop.
Dynmap player notifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dynmap player notifier | |
// Execute the javascript in the console of running dynmap website and let yourself be notified | |
// when someone enters/leaves your base so you would know when they may be stealing your stuff | |
// Copyright (C) Ján Gajdoš 2023 | |
// change these | |
const discordWebhook = ""; // discord webhook url | |
const pos = [StartX, StartZ, EndX, EndZ]; // positions of your base | |
const ignore = []; // player names to ignore | |
const alarmMode = false; // if set to true, the webhook won't stop notifing | |
// After you are done with configuring, paste the code into console and to run, type `monitor()` and hit enter | |
globalThis.monitor = () => { | |
const namesInside = []; | |
function sendMessage(msg) { | |
const request = new XMLHttpRequest(); | |
request.open("POST", discordWebhook); | |
request.setRequestHeader('Content-type', 'application/json'); | |
const params = { | |
username: "Dynmap", | |
avatar_url: "", | |
content: msg | |
} | |
request.send(JSON.stringify(params)); | |
} | |
function checkInBox(x, y, startX, startY, endX, endY, callback, name) { | |
const left = Math.min(startX, endX); | |
const right = Math.max(startX, endX); | |
const top = Math.min(startY, endY); | |
const bottom = Math.max(startY, endY); | |
if (x >= left && x <= right && y >= top && y <= bottom) { | |
if ((!namesInside.includes(name) || alarmMode) && !ignore.includes(name)) { | |
callback(`Player ${name} has entered safe area!`); | |
if (!namesInside.includes(name)) namesInside.push(name); | |
} | |
} else { | |
if (namesInside.includes(name)) { | |
namesInside.splice(namesInside.indexOf(name), 1); | |
callback(`Player ${name} has left the safe area!`); | |
} | |
} | |
} | |
(function (open) { | |
XMLHttpRequest.prototype.open = function (method, url, async, user, pass) { | |
this.addEventListener("load", e => { | |
try { | |
const json = JSON.parse(e.target.responseText); | |
const hraci = json.players; | |
hraci.forEach(h => checkInBox(h.x, h.z, pos[0], pos[1], pos[2], pos[3], sendMessage, h.name)); | |
} catch (e) { } | |
}); | |
open.call(this, method, url, async, user, pass); | |
}; | |
})(XMLHttpRequest.prototype.open); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment