In my server, anyone is free to enter and visit server members' buildings in default adventure mode.
However, there are some areas that we don't want non-member players to enter.
So I want to create an entry prohibited area with the command:
When a non-member player approaches, a particle wall will appear to indicate that the area is inaccessible, and the non-member player will be automatically teleported to the outside when he or she steps into the area.
And for member players, there will be nothing changed.
I have ideas to implementing this feature, but I don't know how to use command blocks or mcfunction
s to complete it.
The main problem is "how to make loops, Basic addition / subtraction, and logical judgments, in a commend block or mcfunction". In my sentence, 'loop' means repeatedly running a command with different position parameters each time. For example, the first execution uses coordinates 1 2 3
, the second execution uses 1 2 4
, and then 1 2 5
, and so on, until it met the boundary conditions.
I think this idea might be a little difficult to say it clear, so I have written some TypeScript codes.
////////////////
// Data
////////////////
const prohibited_area = {
x: {
from: 0,
to: 10,
},
z: {
from: 0,
to: 10,
}
};
const door_position = [1, 2, 3];
const display_distance = 10;
////////////////
// Existing Feature
////////////////
/**
* keep unauthorized players out of entry prohibited areas
*
* NOTE: May be teleport them to nearest position outside
* will be better then a fixed position infront of door
*/
a_command_block(
`tp ${positionition_range(
[prohibited_area.x.from, prohibited_area.x.to],
[prohibited_area.z.from, prohibited_area.z.to]
)} ` +
`${door_position.join(" ")}`,
{
blockType: "Repeat",
condition: false,
redstone: false,
}
)
////////////////
// New Feature: Make the edge visible with particles
////////////////
// Detect Players
const player_list = positionition_range(
[prohibited_area.x.from - display_distance, prohibited_area.x.to + display_distance],
[prohibited_area.z.from - display_distance, prohibited_area.z.to + display_distance]
) as Array<playerposition>
// Generate Particles for each nearby player
player_list.forEach(player => {
// Check the player's position
const left = player.x < prohibited_area.x.from;
const right = player.x > prohibited_area.x.to;
const front = player.z < prohibited_area.z.from;
const back = player.z > prohibited_area.z.to;
// Generate particles based on player's position
if (left) generate_particles("z", prohibited_area.x.from, player.y, player.z);
else if (right) generate_particles("z", prohibited_area.x.to, player.y, player.z);
if (front) generate_particles("x", player.x, player.y, prohibited_area.z.from);
if (back) generate_particles("x", player.x, player.y, prohibited_area.z.to);
// How to "generate_particles" has been written below
})
/**
* `generate_particles` Display a 5x5x1 vertical square plane, made of barrier particles
* BTW, how to delete particles or generate them in different sizes?
*/
function generate_particles(side: "x" | "z", centerX: number, centerY: number, centerZ: number) {
const centerHori = side == "x" ? centerX : centerZ;
for (let hori = centerHori - 2; hori < centerHori + 2; hori++) {
if (hori < prohibited_area[side].from || hori > prohibited_area[side].to) continue;
for (let vert = centerY - 2; vert < centerY + 2; vert++) {
executeCommand(
`/particle minecraft:block_marker{block_state:"barrier"}` +
`${(side == "x" ? hori : centerX) + 0.5} ${vert + 0.5} ${(side == "z" ? hori : centerZ) + 0.5}`
)
}
}
}
////////////////
// Other Definition that can be understood by name without content
////////////////
type TypeOfCommandBlock = "Impulse" | "Chain" | "Repeat";
type playerposition = { x: number, y: number, z: number };
function a_command_block(
_command: string,
_option: { blockType: TypeOfCommandBlock, condition: boolean, redstone: boolean } = {
blockType: "Impulse",
condition: false,
redstone: true,
}) {
// ...
};
function executeCommand(_command: string) {
// ...
};
function positionition_range(x: [number, number], z: [number, number]): unknown {
return `@a[tag=!visitor,` +
`x=${x[0]},y=-100,z=${z[0]},` +
`dx=${x[1] - x[0]},dy=450,dz=${z[1] - z[0]}]`
}