What is the best way to make a PVP free zone, whether it be using commands or by using settings, for a Minecraft realm.
Put them on a team
Scoreboard teams have a friendly fire option, which, when it is set to false, makes it impossible for players on the same team to hurt each other.
First set up the team itself
/scoreboard teams add noPvP /scoreboard teams option noPvP friendlyfire false
Place two command blocks on a clock (or use repeat/chain command blocks from 1.9) and have them run
/scoreboard teams join noPvP @a[team=!noPvP,x=X,y=Y,z=Z,r=R1] /scoreboard teams leave noPvP @a[team=noPvP,x=X,y=Y,z=Z,rm=R2]
Z with the coordinates in the center of your PvP-free zone.
R2 are the radii of the zone. They are different since you don't want the literal edge-case where a player constantly joins and leaves the team. By making
R2 a few blocks larger than
R1, the border is a bit more "fuzzy".
Note that players can't be on two teams at the same time, meaning that if you already have teams set up, things are a bit more complicated. You could, however, "remember" the originally assigned team by assigning a scoreboard tag corresponding to the team.
This is how you can do this:
You simply have a clock (I used a hopper one) connected to a command block with this command
/effect @a[r=how far you want them to not be able to attack in the form of a number] weakness 1 255 This will make them too weak to hit any entity including players while in the radius.