#get all chests inside an area

1 messages · Page 1 of 1 (latest)

hard kettle
#

i have an area from x1 z1 to x2 z2 i wanna get all the chests inside this area and check if it empty, i know how to do the last steeps the only thing thats i want now is how to get chests inside an area (:

covert elbow
#

What you want to do?
I'm asking you so I can optimize the code.

humble rain
covert elbow
#

I understand that, but what is the purpose? If he only wants to run it at a certain time, there won't be much of a problem, but if he wants to do it at an interval, it will increase the tps.

humble rain
covert elbow
#

import { world, BlockInventoryComponent } from "@minecraft/server";
const dimension = world.getDimension('overworld');
function getEmptyChestsOnArea(inicio, final) {
    const chestsEncontrados = [];
    for (let x = inicio.x; x <= final.x; x++) {
        for (let y = inicio.y; y <= final.y; y++) {
            for (let z = inicio.z; z <= final.z; z++) {
                const block = dimension.getBlock({ x, y, z });
                if (block.typeId === 'minecraft:chest') {
                    const chestInv = block.getComponent(BlockInventoryComponent.componentId).container;
                    if (chestInv.emptySlotsCount === 27 || 56) {
                        chestsEncontrados.push(block);
                    }
                }
            }
        }
    }
    return chestsEncontrados;
}

const inicio = { x: -1, y: -61, z: 0 };
const final = { x: -1, y: -61, z: 0 };

const chests = getEmptyChestsOnArea(inicio, final);
for (const chest of chests) {
    world.sendMessage(chest.typeId);
}```
#

If the area is very large it will reduce the tps of the world.

humble rain
#

@covert elbow woudl this even work?

covert elbow
#

Work

humble rain
#

i didnt knowed that, nice

covert elbow
#

Although there could be problems if a large chest has 27 free spaces

humble rain
#

can you check if its a large chest?

#

like

if (emptySlots === (chest.isLarge ? 57 || 27)) { ... }
stone halo
humble rain
#

true

teal pebble
#

If the area is too big this might lag the game and trigger the watchdog. You can use system.runJob in that case.

glacial girder
#

Because I need it to be a system to detect if they are in the vault generating path area and not be laggy

#

I made the other two types of anti-vault raiding but I need this one

alpine creek
# glacial girder Do you think you could make this work for me if they are outside of spawn and pv...

You have very limited options. In order for it to detect and check the vault generating path it will need to check every player in the world. Otherwise, how would it conditionally verify? So if you have 40 players in the world, then that code, which is very expensive, is going to create a lot of lag.

You choosing a specific location to run the code don't prevent that lag. It will only minimize the chances of it happening. Let a bunch of players simultaneously go to that location and you will surely experience it, as will they.

covert elbow
#

I'm not sure what a Vault is, can you explain?

glacial girder
glacial girder
teal pebble
glacial girder
#

I am looking to see if they are in the path because they could .tp and make it there

#

Wait is it possible to check if they .tp?

teal pebble
# glacial girder Wait is it possible to check if they .tp?

You should probably make a different thread if your question isn't about finding chests.

But yeah, you should be able to make it so they can't teleport into a certain area. Or you can make it so they can't access chests in vaults that aren't their own. I'm not sure why you would let players teleport anywhere they want to in a PVP server anyway, but maybe I'm not understanding something.