Skip to content
This repository has been archived by the owner on Aug 24, 2024. It is now read-only.

Commit

Permalink
Project upload
Browse files Browse the repository at this point in the history
  • Loading branch information
KocTu4eK committed Dec 6, 2022
1 parent 76c605b commit 7dae2ae
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 0 deletions.
61 changes: 61 additions & 0 deletions command.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { isInspector, setInspector, lookupPage, clearUserPos } = require("./database.js");

mc.listen("onServerStarted", () => {
let command = mc.newCommand("coreprotect", "CoreProtect by KocTu4eK.", PermType.Any);
command.setAlias("co");
command.setEnum("Inspect", ["inspect", "i"]);
command.setEnum("Lookup", ["lookup", "l"]);
command.mandatory("inspect", ParamType.Enum, "Inspect", 1);
command.mandatory("lookup", ParamType.Enum, "Lookup", 1);
command.mandatory("page", ParamType.Int);
command.overload(["inspect"]);
command.overload(["lookup", "page"]);

command.setCallback((_cmd, ori, out, res) => {
if (ori.player === undefined) return out.error("You are not a player!");

if (ori.player.hasTag("coreprotect")) {
if (res.inspect !== undefined) {
if (!isInspector(ori.player.realName)) {
setInspector(ori.player.realName, 1);
return out.success("§3CoreProtect §r- Inspector now enabled.");
}

setInspector(ori.player.realName, 0);
clearUserPos(ori.player.realName);
return out.success("§3CoreProtect §r- Inspector now disabled.");
}

if (res.lookup !== undefined) {
if (res.page < 1) return out.error("Invalid integer specified!");
return lookupPage(ori.player, res.page, out);
}
}

return out.error("You don't have permission for this!");
});

command.setup();

let coperms = mc.newCommand("coperms", "Grant or revoke CoreProtect permissions.", PermType.GameMasters);
coperms.mandatory("player", ParamType.Player);
coperms.overload(["player"]);

coperms.setCallback((_cmd, ori, out, res) => {
if (res["player"].length !== 1) return out.error("Choose one player!");
let pl = res["player"][0];

if (pl.hasTag("coreprotect")) {
pl.removeTag("coreprotect");
setInspector(pl.realName, 0);
return out.success(`§3CoreProtect §r- Permissions have been successfully removed from ${pl.realName}.`);
}

pl.addTag("coreprotect");
return out.success(`§3CoreProtect §r- Permissions have been successfully added to ${pl.realName}.`);
});

coperms.setup();
});

module.exports = null;
121 changes: 121 additions & 0 deletions database.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
let session = null;

function initDatabase() {
if (session === null) session = new DBSession("sqlite", { path: "./plugins/nodejs/CoreProtectMini/database.db" });

session.exec("CREATE TABLE IF NOT EXISTS co_art_map (id INTEGER PRIMARY KEY AUTOINCREMENT, block TEXT UNIQUE)");
session.exec("CREATE TABLE IF NOT EXISTS co_user (id INTEGER PRIMARY KEY AUTOINCREMENT, nickname TEXT UNIQUE, inspector INT DEFAULT 0, wid INT DEFAULT -1, x INT DEFAULT 0, y INT DEFAULT -256, z INT DEFAULT 0)");
session.exec("CREATE TABLE IF NOT EXISTS co_item (id INTEGER PRIMARY KEY AUTOINCREMENT, item TEXT UNIQUE)");
session.exec("CREATE TABLE IF NOT EXISTS co_block (time TIMESTAMP DEFAULT (CAST (((julianday('now') - 2440587.5) * 86400.0 * 1000) AS INT)), block INT, user INT, action INT, wid INT, x INT, y INT, z INT)");
}

function getUserId(playerName) {
let userId = session.query(`SELECT id FROM co_user WHERE nickname = "${playerName}"`);

if (userId === undefined) {
session.exec(`INSERT INTO co_user (nickname) VALUES ("${playerName}")`);
return session.query(`SELECT id FROM co_user WHERE nickname = "${playerName}"`)[1][0];
}

return userId[1][0];
}

function getUserName(userId) {
return session.query(`SELECT nickname FROM co_user WHERE id = ${userId}`)[1][0];
}

function getBlockId(blockName) {
blockName = blockName.replace("minecraft:", "");
let blockId = session.query(`SELECT id FROM co_art_map WHERE block = "${blockName}"`);

if (blockId === undefined) {
session.exec(`INSERT INTO co_art_map (block) VALUES ("${blockName}")`);
return session.query(`SELECT id FROM co_art_map WHERE block = "${blockName}"`)[1][0];
}

return blockId[1][0];
}

function getBlockName(blockId) {
return session.query(`SELECT block FROM co_art_map WHERE id = ${blockId}`)[1][0];
}

function logBlock(pl, blockName, wid, x, y, z, action) {
if (!isInspector(pl.realName)) session.exec(`INSERT INTO co_block (block, user, action, wid, x, y, z) VALUES (${getBlockId(blockName)}, ${getUserId(pl.realName)}, ${action}, ${wid}, ${x}, ${y}, ${z})`);
else {
if (action === 1) mc.setBlock(x, y, z, wid, "minecraft:air", 0);
let pageCount = getPageCount(wid, x, y, z);

if (pageCount > 0) {
let output = `---- §3CoreProtect §r---- §7(x${x}/y${y}/z${z})§r\n`;

let blockPage = getBlockPage(wid, x, y, z, 0);
for (i = 1; i < blockPage.length; i++) {
output += `§7${timeFormat(blockPage[i][0])} ago§r - §3${getUserName(blockPage[i][1])} §r${blockPage[i][3] === 0 ? "broke" : blockPage[i][3] === 1 ? "placed" : blockPage[i][3] === 2 ? "clicked" : blockPage[i][3] === 3 ? "add" : blockPage[i][3] === 4 ? "remove" : "?"} §3${getBlockName(blockPage[i][2])}§r.\n`;
}

if (pageCount > 1) output += `-----\nPage 1/${pageCount}. View older data by typing "§3/co l <page>§r".`;
pl.tell(output);
}
else pl.tell(action ? `§3CoreProtect §r- No data found at this location.` : `§3CoreProtect §r- No data found at §o${blockName.replace("minecraft:", "")}§r.`);

session.exec(`UPDATE co_user SET x = ${x}, y = ${y}, z = ${z}, wid = ${wid} WHERE id = ${getUserId(pl.realName)}`);

return false;
}

return true;
}

function lookupPage(pl, page, out) {
if (!isInspector(pl.realName)) return out.error("First enter inspector mode!");

let userId = getUserId(pl.realName);
let userData = session.query(`SELECT wid, x, y, z FROM co_user WHERE id = ${userId}`)[1];
let pageCount = getPageCount(userData[0], userData[1], userData[2], userData[3]);

if (pageCount < page) return out.success("§3CoreProtect §r- No data found for that page.");

let output = `---- §3CoreProtect §r---- §7(x${userData[1]}/y${userData[2]}/z${userData[3]})§r\n`;

let blockPage = getBlockPage(userData[0], userData[1], userData[2], userData[3], (page - 1) * 7);
for (i = 1; i < blockPage.length; i++) {
output += `§7${timeFormat(blockPage[i][0])} ago§r - §3${getUserName(blockPage[i][1])} §r${blockPage[i][3] === 0 ? "broke" : blockPage[i][3] === 1 ? "placed" : blockPage[i][3] === 2 ? "clicked" : blockPage[i][3] === 3 ? "add" : blockPage[i][3] === 4 ? "remove" : "?"} §3${getBlockName(blockPage[i][2])}§r.\n`;
}

if (pageCount > 1) output += `-----\nPage ${page}/${pageCount}. View older data by typing "§3/co l <page>§r".`;
return out.success(output);
}

function isInspector(playerName) {
return session.query(`SELECT inspector FROM co_user WHERE id = ${getUserId(playerName)}`)[1][0];
}

function setInspector(playerName, inspector) {
session.exec(`UPDATE co_user SET inspector = ${inspector} WHERE id = ${getUserId(playerName)}`);
}

function clearUserPos(playerName) {
session.exec(`UPDATE co_user SET y = -256 WHERE id = ${getUserId(playerName)}`);
}

function getBlockPage(wid, x, y, z, offset) {
return session.query(`SELECT time, user, block, action FROM co_block WHERE wid = ${wid} AND x = ${x} AND y = ${y} AND z = ${z} ORDER BY time DESC LIMIT ${offset}, 7`);
}

function getPageCount(wid, x, y, z) {
return Math.ceil(session.query(`SELECT COUNT(block) FROM co_block WHERE wid = ${wid} AND x = ${x} AND y = ${y} AND z = ${z}`)[1][0] / 7);
}

function timeFormat(time) {
return `${((Date.now() - time) / 3600000).toFixed(2)}/h`.replace(".", ",");
}

module.exports = {
initDatabase,
logBlock,
isInspector,
setInspector,
lookupPage,
clearUserPos
};
55 changes: 55 additions & 0 deletions listener.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { logBlock } = require("./database.js");

mc.listen("onDestroyBlock", (pl, bl) => logBlock(pl, bl.name, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 0));
mc.listen("afterPlaceBlock", (pl, bl) => logBlock(pl, bl.name, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 1));
mc.listen("onBlockInteracted", (pl, bl) => logBlock(pl, bl.name, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 2));

mc.listen("onUseItemOn", (pl, it, bl, side, pos) => {
if (it.type === "minecraft:bucket" && (bl.type === "minecraft:powder_snow" || bl.type === "minecraft:water" || bl.type === "minecraft:flowing_water" || bl.type === "minecraft:lava" || bl.type === "minecraft:flowing_lava")) {
return logBlock(pl, bl.type.replace("flowing_", ""), bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 0);
}

let x = bl.pos.x, y = bl.pos.y, z = bl.pos.z;
switch (side) {
case 0:
y -= 1;
break;
case 1:
y += 1;
break;
case 2:
z -= 1;
break;
case 3:
z += 1;
break;
case 4:
x -= 1;
break;
case 5:
x += 1;
}
bl = mc.getBlock(x, y, z, bl.pos.dimid);

if (it.type !== "minecraft:bucket" && it.type.includes("_bucket") && (bl.type === "minecraft:air" || bl.type === "minecraft:powder_snow" || bl.type === "minecraft:water" || bl.type === "minecraft:flowing_water" || bl.type === "minecraft:lava" || bl.type === "minecraft:flowing_lava")) {
if (!logBlock(pl, it.type.replace("_bucket", ""), bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 1)) {
mc.setBlock(bl.pos, bl.type, bl.tileData);
return false;
}
}

return true;
});

mc.listen("onContainerChange", (pl, bl, slotNum, oldIt, newIt) => {
if (!oldIt.isNull() && newIt.isNull()) logBlock(pl, oldIt.type, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 3); // take out

if (oldIt.isNull() && !newIt.isNull()) logBlock(pl, newIt.type, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 4); // put

if (!oldIt.isNull() && !newIt.isNull()) { // replace
logBlock(pl, oldIt.type, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 3);
logBlock(pl, newIt.type, bl.pos.dimid, bl.pos.x, bl.pos.y, bl.pos.z, 4);
}
});

module.exports = null;
7 changes: 7 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
const { initDatabase } = require("./database.js");
const listener = require("./listener.js");
const command = require("./command.js");

ll.registerPlugin("CoreProtectMini", "Stripped down version of CoreProtect", [1, 0, 0]);

initDatabase();
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "coreprotectmini",
"version": "1.0.0",
"description": "Stripped down version of CoreProtect",
"main": "main.js",
"author": "KocTu4eK",
"license": "ISC"
}

0 comments on commit 7dae2ae

Please sign in to comment.