0

This is my simple script:

public void cast(Player caster) {
    Location loc = caster.getTargetBlock(null, 512).getLocation();

    for (int c = 0; c < 2; c++) {
        for (int b = 0; b < 2; b++) {
            for (int a = 0; a < 1; a++) {
                caster.sendMessage("" + loc);
                Block ice = caster.getWorld().getBlockAt(loc.add(a, b, c));
                ice.setTypeId(79);
            }
        }
    }
}

I'm trying to make it so loc remains static and unchanging. It has been changing throughout the for loop and I wish to prevent this.

Makoto
  • 104,088
  • 27
  • 192
  • 230
tincopper2
  • 79
  • 10
  • 3
    You mean that when you call `loc.add(a, b, c)`, it should have no effect? What's wrong with just eliminating that call from the loop? – Ted Hopp Dec 11 '12 at 05:45
  • So you're sure that `loc` is changing? What specifically is changing about it - the x, y, z coordinates? – Makoto Dec 11 '12 at 05:47
  • @Ted Hopp if i remove the call add for loc it wont shift the coordinates from the reliative position using abc as the for loops, should be making a cube but its not... – tincopper2 Dec 11 '12 at 06:03
  • This is confusing. You don't want `loc` to change, but you need it to change. Perhaps you just want to clone `loc` at the start of the method. – Ted Hopp Dec 11 '12 at 06:33
  • One reason that methods that change in place should return `void`. – quantum Feb 01 '13 at 21:37

3 Answers3

0

Loc most likely has an internal state that is incrementing and not resetting every time

public void cast(Player caster){
Location loc = caster.getTargetBlock(null, 512).getLocation();

int initalC = 0;
int initalB = 0;
int initalA = 0;
Location staticLoc;
for (int c = initalC; c < 2; c++)
{
    for (int b = initalB; b < 2; b++)
    {
        for (int a = initalA; a < 1; a++)
        {
            if (a == initalA && b == initalB && c == initalC) {
                 staticLoc = caster.getTargetBlock(null, 512).getLocation().add(a, b, c);
            }

            loc = staticLoc;
            caster.sendMessage("" + loc);
            Block ice = caster.getWorld().getBlockAt(staticLoc.add(a, b, c));
            ice.setTypeId(79);
        }
    }
}

}

0

Found answer:

public void cast(Player caster){
    Location loc = caster.getTargetBlock(null, 512).getLocation();
    for (int c = -3; c < 3; c++)
        for (int b = -1; b < 5; b++)
            for (int a = -3; a < 3; a++) {
                Block ice = caster.getWorld().getBlockAt(loc.add(a, b, c));
                ice.setTypeId(79);
                loc = loc.subtract(a, b, c);
            }
}
quantum
  • 3,672
  • 29
  • 51
tincopper2
  • 79
  • 10
0

I know that this question has already been answered, but I wish to present a more efficient method. Adding and subtracting from a Location is quite inefficient, especially when doing this in nested loops.

The Location object has a clone() method which returns an identical Location but which is not a reference to the original location. So really, all you need to do is:

public void cast(Player caster) {
    Location loc = caster.getTargetBlock(null, 512).getLocation();

    for (int c = 0; c < 2; c++) {
        for (int b = 0; b < 2; b++) {
            for (int a = 0; a < 1; a++) {
                caster.sendMessage("" + loc);
                caster.getWorld().getBlockAt(loc.clone().add(a, b, c)).setTypeId(79);
           }
        }
    }
}

If performance is an issue, I would even consider caching getWorld() in a local variable outside the for loops.

Xyene
  • 2,304
  • 20
  • 36