0

これは私の簡単なスクリプトです:

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);
            }
        }
    }
}

私はそれをloc静的で不変のままにしようとしています。for ループ全体で変更されているため、これを防止したいと考えています。

4

3 に答える 3

0

この質問はすでに回答されていることは知っていますが、より効率的な方法を提示したいと思います。特にネストされたループでこれを行う場合は、場所の追加と削除は非常に非効率的です。

Location オブジェクトにはclone()、同一の Location を返すメソッドがありますが、元の場所への参照ではありません。本当に、あなたがする必要があるのは次のことだけです:

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);
           }
        }
    }
}

パフォーマンスが問題になる場合は、for ループの外側のローカル変数に getWorld() をキャッシュすることも検討します。

于 2013-01-03T19:14:56.067 に答える
0

見つかった答え:

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);
            }
}
于 2012-12-11T06:19:17.553 に答える
0

Loc には、毎回インクリメントされ、リセットされるわけではない内部状態がある可能性が高い

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);
        }
    }
}

}

于 2012-12-11T05:49:42.150 に答える