#Area protection
1 messages · Page 1 of 1 (latest)
I assume you already have some bounding box of this area, where those blocks should be unbreakable. Set up a subscription to the PlayerBreakBlock before-event; here we will see if the block is within those bounds, and if so, prevent the block from breaking. Here's a place to get started:
import {world} from '@minecraft/server';
const start = {x: 0, y: -64, z: 0};
const end = {x: 64, y: 0, z: 64};
function inRange(val, min, max) {
return min <= val && val <= max;
}
world.beforeEvents.playerBreakBlock.subscribe(
function areaProtection(e) {
if(e.player.getTags().includes('admin')) return;
if(inRange(e.block.location.x, start.x, end.x) &&
inRange(e.block.location.y, start.y, end.y) &&
inRange(e.block.location.z, start.z, end.z)) {
e.cancel = true;
}
}
)
The block's location must be between the bounds described in start and end. And if your player has the 'admin' tag they should be allowed to break blocks freely.
Relevant documentation: PlayerBreakBlockBeforeEvent
Thanks
I can replace break Block with placeBlock??
If there's a before-event for that too, then sure. I'll check real quick
OK thanks
Looks like it. playerPlaceBlock
Though it's not in version 1.11.0, rather the 1.15.0 beta
I'm using the beta version
I created a safe tag or they can't be typed if they have it, is it possible to be a tag for me in a certain area?
Are you wanting to apply a tag when a player enters a certain area?
I would like to put a "safe" tag when a player is in the zone and his removes the tag if he is not in the zone
Every tick, you could grab all players and see if their location is in the zone, in a similar manner to the block stuff we did earlier.
system.runInterval(function tagPlayersInArea() {
world.getAllPlayers().forEach(function inArea(player) {
if(inRange(player.location.x, start.x, end.x) &&
inRange(player.location.y, start.y, end.y) &&
inRange(player.location.z, start.z, end.z)) {
player.addTag("safe");
}
else player.removeTag("safe");
});
}, 1);