#Detect chest on a chunk

1 messages · Page 1 of 1 (latest)

rugged osprey
#

Hello ! I've created a script to detect whether there's a block in a chunk (in this case, a chest), so I've got the logic part, only it doesn't really work because it requires too many resources as it checks every tick, for every block, i.e. around 65,000. I've already tried to put a cooldown on it, but even running the check every second takes up far too much resource. So I was wondering if anyone could give me a way of doing it differently that wouldn't crash my server, thanks in advance!

#
import { system, world } from "@minecraft/server";

function fonctionScriptGyroscope() {

    system.afterEvents.scriptEventReceive.subscribe((event) => {
        const { id, message, sourceEntity, sourceType } = event;
        const { x, z } = sourceEntity.location;
        const chunkX1 = Math.floor(x / 16);
        const chunkZ1 = Math.floor(z / 16);
        const bordureMinX = (chunkX1 * 16);
        const bordureMinZ = (chunkZ1 * 16);
        const bordureMaxX = (bordureMinX + 15);
        const bordureMaxZ = (bordureMinZ + 15);
        const minY = 0;
        const maxY = 256;

        for (let xOffset = bordureMinX; xOffset <= bordureMaxX; xOffset++) {
            for (let zOffset = bordureMinZ; zOffset <= bordureMaxZ; zOffset++) {
                for (let yOffset = minY; yOffset <= maxY; yOffset++) {
                    sourceEntity.runCommand(`execute as @s if block ${xOffset} ${yOffset} ${zOffset} minecraft:chest run say there is a chest`);
                }
            }
        }
    });


    world.getPlayers().forEach((player) => {
        player.runCommand("scriptevent gyroscope:event");
    });


}
export { fonctionScriptGyroscope };```
broken bough
#

also I don't even see how you're making this have a cooldown.

rugged osprey
rugged osprey
#

as it didn't work anyway, even with cooldown, I removed it

broken bough
# rugged osprey okay thanks I see, I'll try, thanks

try

class Database {
    constructor(name){
        this.name = name;
    }

    scoreboardName(name){
        return `${this.name}:${name}`;
    }

    get(name){
        let obj_name = this.scoreboardName(name);
        let obj = world.scoreboard.getObjective(obj_name);
        if(!obj) {
            return null;
        }
        return obj.displayName;
    }

    has(name){
        return Boolean(this.get(name));
    }

    set(name, value){
        let obj_name = this.scoreboardName(name);
        if (this.get(name)) {
            this.del(name);
            this.set(name, objValue);
        } else {
            world.scoreboard.addObjective(obj_name, objValue);
        }
    }

    del(name){
        if (this.has(name)) {
            world.scoreboard.removeObjective(this.scoreboardName(name))
        }
        return this;
    }
}