Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
9.2 kB
2
Indexable
Never
import { world, Player, system } from "@minecraft/server";
import { ActionFormData, ModalFormData } from "@minecraft/server-ui";
import { locationsDB } from "./scripts/Database/dbsave.js";

const MONEY_SCORE = "money";
const db = new locationsDB("land-claims");

world.afterEvents.itemUse.subscribe(({ itemStack: item, source: player }) => {
    if (item.typeId === 'minecraft:clock') {
        landclaimMenu(player);
    }
});

// Main Menu
async function landclaimMenu(player) {
    const form = new ActionFormData()
        .title("Land Claim System")
        .button("Buy a Plot")
        .button("List of Plot")
        .button("Settings")
        .button("Player Request");

    const formResult = await form.show(player);
    if (formResult.canceled) return;

    switch (formResult.selection) {
        case 0:
            buyPlotMenu(player);
            break;
        case 1:
            listPlotsMenu(player);
            break;
        case 2:
            settingsMenu(player);
            break;
        case 3:
            playerRequestMenu(player);
            break;
    }
}

// Buy Plot Menu
async function buyPlotMenu(player) {
    const form = new ActionFormData()
        .title("Buy a Plot")
        .body(`Money: ${getPlayerMoney(player)}\nPrice: 20 per block`)
        .button("Start Point")
        .button("End Point");

    const formResult = await form.show(player);
    if (formResult.canceled) return landclaimMenu(player);

    switch (formResult.selection) {
        case 0:
            setStartPoint(player);
            break;
        case 1:
            setEndPoint(player);
            break;
    }
}

function setPoint(player, type) {
    player.sendMessage(`Sneak to set the ${type} point.`);
    world.afterEvents.playerSneak.subscribe(({ player: sneakingPlayer }) => {
        if (player.id === sneakingPlayer.id) {
            const point = player.location;
            db.save([{ type, point, player: player.id }]);
            player.sendMessage(`${type} Point set at ${JSON.stringify(point)}`);
            if (type === "start") {
                setEndPoint(player);
            } else {
                confirmPurchase(player);
            }
        }
    });
}

function setStartPoint(player) {
    setPoint(player, "start");
}

function setEndPoint(player) {
    setPoint(player, "end");
}

async function confirmPurchase(player) {
    const form = new ModalFormData()
        .title("Confirm Purchase")
        .textField("Name This Plot", "Enter name...")
        .toggle("Public Plot", false);

    const formResult = await form.show(player);
    if (formResult.canceled) return;

    const name = formResult.formValues[0];
    const isPublic = formResult.formValues[1];

    const startPoint = db.load().find(entry => entry.player === player.id && entry.type === "start").point;
    const endPoint = db.load().find(entry => entry.player === player.id && entry.type === "end").point;

    // Calculate Plot Price based on volume
    const blocks = calculatePlotBlocks(startPoint, endPoint);
    const price = blocks * 20;

    if (getPlayerMoney(player) >= price) {
        deductPlayerMoney(player, price);
        db.save([{ player: player.id, name, startPoint, endPoint, isPublic }]);
        player.sendMessage(`Plot "${name}" successfully purchased for ${price}.`);
    } else {
        player.sendMessage(`Not enough money. You need ${price}.`);
    }
}

// List Plots Menu
async function listPlotsMenu(player) {
    const playerPlots = db.load().filter(entry => entry.player === player.id);
    const form = new ActionFormData().title("Your Plots");

    playerPlots.forEach(plot => form.button(plot.name));

    const formResult = await form.show(player);
    if (formResult.canceled) return landclaimMenu(player);

    const selectedPlot = playerPlots[formResult.selection];
    plotDetailsMenu(player, selectedPlot);
}

async function plotDetailsMenu(player, plot) {
    const form = new ActionFormData()
        .title(plot.name)
        .body(`Price: ${calculatePlotBlocks(plot.startPoint, plot.endPoint) * 20}`)
        .button("Add or Remove Access")
        .button("Delete Plot")
        .button("Back");

    const formResult = await form.show(player);
    if (formResult.canceled) return listPlotsMenu(player);

    switch (formResult.selection) {
        case 0:
            manageAccessMenu(player, plot);
            break;
        case 1:
            deletePlot(player, plot);
            break;
        case 2:
            listPlotsMenu(player);
            break;
    }
}

async function manageAccessMenu(player, plot) {
    const onlinePlayers = Array.from(world.getPlayers()).map(p => p.name);
    const form = new ModalFormData()
        .title("Manage Access")
        .dropdown("Select Player", onlinePlayers)
        .toggle("Grant Access", true);

    const formResult = await form.show(player);
    if (formResult.canceled) return plotDetailsMenu(player, plot);

    const selectedPlayer = onlinePlayers[formResult.formValues[0]];
    const grantAccess = formResult.formValues[1];

    if (grantAccess) {
        plot.accessList.push(selectedPlayer);
    } else {
        plot.accessList = plot.accessList.filter(p => p !== selectedPlayer);
    }

    db.save([{ ...plot, accessList: plot.accessList }]);
    player.sendMessage(`Access updated for ${selectedPlayer}.`);
}

function deletePlot(player, plot) {
    db.save(db.load().filter(entry => entry.name !== plot.name));
    player.sendMessage(`Plot "${plot.name}" has been deleted.`);
    listPlotsMenu(player);
}

// Settings Menu
async function settingsMenu(player) {
    const form = new ActionFormData()
        .title("Settings")
        .button("Delete Plot")
        .button("Toggle Notification")
        .button("Access");

    const formResult = await form.show(player);
    if (formResult.canceled) return landclaimMenu(player);

    switch (formResult.selection) {
        case 0:
            deletePlotMenu(player);
            break;
        case 1:
            toggleNotificationMenu(player);
            break;
        case 2:
            accessMenu(player);
            break;
    }
}

async function deletePlotMenu(player) {
    const playerPlots = db.load().filter(entry => entry.player === player.id);
    const form = new ActionFormData().title("Delete Plot");

    playerPlots.forEach(plot => form.button(plot.name));

    const formResult = await form.show(player);
    if (formResult.canceled) return settingsMenu(player);

    const selectedPlot = playerPlots[formResult.selection];
    const confirmForm = new ModalFormData()
        .title("Confirm Deletion")
        .body(`Do you really want to delete plot "${selectedPlot.name}"?`)
        .button("Yes")
        .button("No");

    const confirmResult = await confirmForm.show(player);
    if (confirmResult.selection === 0) {
        db.save(db.load().filter(entry => entry.name !== selectedPlot.name));
        player.sendMessage(`Plot "${selectedPlot.name}" has been deleted.`);
    }

    settingsMenu(player);
}

function toggleNotificationMenu(player) {
    // Implementation based on individual requirements
}

function accessMenu(player) {
    // Implementation based on individual requirements
}

// Player Request Menu
async function playerRequestMenu(player) {
    // Implementation based on individual requirements
}

// Helper Functions
function calculatePlotBlocks(start, end) {
    const xLength = Math.abs(end.x - start.x) + 1;
    const yLength = Math.abs(end.y - start.y) + 1;
    const zLength = Math.abs(end.z - start.z) + 1;
    return xLength * yLength * zLength;
}

function getPlayerMoney(player) {
    const moneyObj = getScoreboardObjective(MONEY_SCORE);
    return moneyObj.getScore(player);
}

function deductPlayerMoney(player, amount) {
    const moneyObj = getScoreboardObjective(MONEY_SCORE);
    const currentMoney = moneyObj.getScore(player);
    moneyObj.setScore(player, currentMoney - amount);
}

function getScoreboardObjective(name) {
    let objective = world.scoreboard.getObjective(name);
    if (!objective) {
        world.scoreboard.addObjective(name, "dummy", name);
        objective = world.scoreboard.getObjective(name);
    }
    return objective;
}

// Block Edit Restrictions
world.afterEvents.playerPlaceBlock.subscribe((event) => {
    const { player, block } = event;
    if (!canEditPlot(player, block.location)) {
        player.sendMessage("You cannot place this block here.");
        event.cancel = true;
    }
});

world.afterEvents.playerBreakBlock.subscribe((event) => {
    const { player, block } = event;
    if (!canEditPlot(player, block.location)) {
        player.sendMessage("You cannot break this block here.");
        event.cancel = true;
    }
});

function canEditPlot(player, location) {
    const playerPlots = db.load().filter(entry => entry.player === player.id);
    return playerPlots.some(plot => isInsidePlot(location, plot.startPoint, plot.endPoint));
}

function isInsidePlot(location, start, end) {
    return location.x >= start.x && location.x <= end.x &&
           location.y >= start.y && location.y <= end.y &&
           location.z >= start.z && location.z <= end.z;
}
Leave a Comment