#The best way to stop players from breaking blocks in a area?

1 messages · Page 1 of 1 (latest)

tame pivot
#

How can i stop players from breaking blocks dynamically using script API. I know i can use blockBreak, but it makes problems with chests and other containers.

twilit sparrow
#

You could just every tick see whether a person was within a region and give them mining fatigue for 5 ticks, giving it to them every tick if they are within the region and 5 ticks later the effect would wear off if the player wasn't in the area. You would have to make the area around 10 blocks larger on all sides though to account for how far inside the area the player can reach outside of the area.

tame pivot
twilit sparrow
#

too laggy? and what problems would happen with the crosshairs? I believe the mining fatigue effect does nothing to the players screen (other than the small icon). You could use deny blocks

regal gorge
#

Would on interact with chest give mining fatigue? Would this stop anything?

#

Lol

twilit sparrow
#

I just tested it, deny blocks work fine. They don't interfere with the players ability to interact with blocks that have a container, and you can't build or place blocks as long as you aren't in creative

regal gorge
#

For a split sec

coarse musk
#

loop every player
get block from view
check to see if its in the range
somehow, disable player breaking block

tame pivot
#
world.events.blockBreak.subscribe(
  ({ block, brokenBlockPermutation, player, dimension }) => {
    dimension.getBlock(block.location).setPermutation(brokenBlockPermutation);
    if (BLOCK_CONTAINERS.includes(block.typeId))
      system.run(() => {
        const container = block.getComponent("inventory").container;
        dimension
          .getEntitiesAtBlockLocation(block.location)
          .filter((e) => e.typeId == "minecraft:item")
          .forEach((e) => {
            container.addItem(e.getComponent("item").itemStack);
            e.kill();
          });
      });
  }
);```
#

this is what i have rn

#

it works pretty well however, some times if the item spawns outside the block it will ignore it, (which makes sense)

twilit sparrow
#

you could fix the item spawning outside the block and not being killed by using an entity query and .getEntities().

#

Use the location and maxDistance options and it should be fine. 1.5 would probably be a good value for maxDistance. Also the type option being set to "minecraft:item".

rich path
#

what's the issue with gamemode switching?

rich path
#

why not adventure mode

tame pivot