1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

# Creating a square of blocks

Comments in 'Plugin Development' started by BalAnce, Jun 16, 2015.

1. Offline

### BalAnceActive Member

Joined:
Mar 10, 2015
Posts:
125
Minecraft User:
Easiest way to take two position and make a square. I looked at resources of frequitely used algorithms but i couldnt get the cuboid algoithm to work.

PHP:
```         \$xa = \$this->arena1pos1[0];        \$ya = \$this->pos1[1];        \$za = \$this->pos1[2];        \$xb = \$this->pos2[0];        \$yb = \$this->pos2[1];        \$zb = \$this->pos2[2];        \$level = \$this->getServer()->getLevelByName("world");        \$block = Block::GOLD_BLOCK;        for(\$x = \$xa; \$x <= \$xb; \$x++){            for(\$y = \$ya; \$y <= \$yb; \$y++){                for(\$z = \$za; \$z <= \$zb; \$z++){                    \$level->setBlock(new Vector3(\$x, \$y, \$z), \$block, false, false);                }            }        } ```
I know the arrays are right because i tried making blocks at each point to make sure it wasnt a cordinate issue. But when i use the algorithm above it didnt work. So im asking if anyone knows another way to do it or a fix. Thanks
2. Offline

### Guillaume351Active MemberPlugin Developer

Joined:
Oct 11, 2013
Posts:
150
Plugins:
1
Minecraft User:
Try
->setBlock(new Vector3(\$x,\$y,\$z),Block::get(1,0));

I have issues when I do Block::"block type"
3. Offline

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,230
Plugins:
11
Minecraft User:
The second argument passed to \$Level->setBlock() must be an instance of Block. you are passing an integer (Block::GOLD_BLOCK). You should change it into Block::get(Block::GOLD_BLOCK)
Guillaume351 likes this.
4. Offline

### BalAnceActive Member

Joined:
Mar 10, 2015
Posts:
125
Minecraft User:
Ok so i changed that but it still does not do anything no errors.

EDIT: Ok i got but it seems the points must be in started in a certain direction if that makes sense.
5. Offline

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,230
Plugins:
11
Minecraft User:
\$xa must be smaller than \$xb. In your for loop, you are incrementing \$x one by one from \$xa until it exceeds \$xb. If \$xa is larger than \$xb, the first loop is already exceeding, so it won't run at all.
Tips: http://php.net/min http://php.net/max
EvolSoft and BalAnce like this.
6. Offline

### xiaoqActive Member

Joined:
Dec 23, 2014
Posts:
231
Minecraft User:
Like that?
for(\$x = min(\$xa,\$xb); \$x <= max(\$xa,\$xb); \$x++){
PEMapModder likes this.
7. Offline

### PEMapModderNotable MemberPlugin Developer

Joined:
Oct 9, 2013
Posts:
7,230
Plugins:
11
Minecraft User:
Cache the max to make better performance. Calculate it before the for loop, or the function overhead will happen every time.

This is when @PEMapModder is stubborn and tries to do premature optimization. Therefore, ignore me.
xiaoq likes this.