    001package net.minecraft.block;
    002
    003import java.util.ArrayList;
    004import java.util.List;
    005import net.minecraft.world.ChunkPosition;
    006import net.minecraft.world.World;
    007
    008public class BlockBaseRailLogic
    009{
    010    private World field_94516_b;
    011    private int field_94517_c;
    012    private int field_94514_d;
    013    private int field_94515_e;
    014    private final boolean field_94512_f;
    015    private List field_94513_g;
    016
    017    private final boolean canMakeSlopes;
    018
    019    final BlockRailBase field_94518_a;
    020
    021    public BlockBaseRailLogic(BlockRailBase par1, World par2, int par3, int par4, int par5)
    022    {
    023        this.field_94518_a = par1;
    024        this.field_94513_g = new ArrayList();
    025        this.field_94516_b = par2;
    026        this.field_94517_c = par3;
    027        this.field_94514_d = par4;
    028        this.field_94515_e = par5;
    029        int l = par2.getBlockId(par3, par4, par5);
    030
    031        BlockRailBase target = (BlockRailBase)Block.blocksList[l];
    032        int i1 = target.getBasicRailMetadata(par2, null, par3, par4, par5);
    033        field_94512_f = !target.isFlexibleRail(par2, par3, par4, par5);
    034        canMakeSlopes = target.canMakeSlopes(par2, par3, par4, par5);
    035
    036        this.func_94504_a(i1);
    037    }
    038
    039    private void func_94504_a(int par1)
    040    {
    041        this.field_94513_g.clear();
    042
    043        if (par1 == 0)
    044        {
    045            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1));
    046            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1));
    047        }
    048        else if (par1 == 1)
    049        {
    050            this.field_94513_g.add(new ChunkPosition(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e));
    051            this.field_94513_g.add(new ChunkPosition(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e));
    052        }
    053        else if (par1 == 2)
    054        {
    055            this.field_94513_g.add(new ChunkPosition(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e));
    056            this.field_94513_g.add(new ChunkPosition(this.field_94517_c + 1, this.field_94514_d + 1, this.field_94515_e));
    057        }
    058        else if (par1 == 3)
    059        {
    060            this.field_94513_g.add(new ChunkPosition(this.field_94517_c - 1, this.field_94514_d + 1, this.field_94515_e));
    061            this.field_94513_g.add(new ChunkPosition(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e));
    062        }
    063        else if (par1 == 4)
    064        {
    065            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d + 1, this.field_94515_e - 1));
    066            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1));
    067        }
    068        else if (par1 == 5)
    069        {
    070            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1));
    071            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d + 1, this.field_94515_e + 1));
    072        }
    073        else if (par1 == 6)
    074        {
    075            this.field_94513_g.add(new ChunkPosition(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e));
    076            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1));
    077        }
    078        else if (par1 == 7)
    079        {
    080            this.field_94513_g.add(new ChunkPosition(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e));
    081            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1));
    082        }
    083        else if (par1 == 8)
    084        {
    085            this.field_94513_g.add(new ChunkPosition(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e));
    086            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1));
    087        }
    088        else if (par1 == 9)
    089        {
    090            this.field_94513_g.add(new ChunkPosition(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e));
    091            this.field_94513_g.add(new ChunkPosition(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1));
    092        }
    093    }
    094
    095    private void func_94509_b()
    096    {
    097        for (int i = 0; i < this.field_94513_g.size(); ++i)
    098        {
    099            BlockBaseRailLogic blockbaseraillogic = this.func_94501_a((ChunkPosition)this.field_94513_g.get(i));
    100
    101            if (blockbaseraillogic != null && blockbaseraillogic.func_94508_a(this))
    102            {
    103                this.field_94513_g.set(i, new ChunkPosition(blockbaseraillogic.field_94517_c, blockbaseraillogic.field_94514_d, blockbaseraillogic.field_94515_e));
    104            }
    105            else
    106            {
    107                this.field_94513_g.remove(i--);
    108            }
    109        }
    110    }
    111
    112    private boolean func_94502_a(int par1, int par2, int par3)
    113    {
    114        return BlockRailBase.isRailBlockAt(this.field_94516_b, par1, par2, par3) ? true : (BlockRailBase.isRailBlockAt(this.field_94516_b, par1, par2 + 1, par3) ? true : BlockRailBase.isRailBlockAt(this.field_94516_b, par1, par2 - 1, par3));
    115    }
    116
    117    private BlockBaseRailLogic func_94501_a(ChunkPosition par1ChunkPosition)
    118    {
    119        return BlockRailBase.isRailBlockAt(this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y, par1ChunkPosition.z) ? new BlockBaseRailLogic(this.field_94518_a, this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y, par1ChunkPosition.z) : (BlockRailBase.isRailBlockAt(this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y + 1, par1ChunkPosition.z) ? new BlockBaseRailLogic(this.field_94518_a, this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y + 1, par1ChunkPosition.z) : (BlockRailBase.isRailBlockAt(this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y - 1, par1ChunkPosition.z) ? new BlockBaseRailLogic(this.field_94518_a, this.field_94516_b, par1ChunkPosition.x, par1ChunkPosition.y - 1, par1ChunkPosition.z) : null));
    120    }
    121
    122    private boolean func_94508_a(BlockBaseRailLogic par1BlockBaseRailLogic)
    123    {
    124        for (int i = 0; i < this.field_94513_g.size(); ++i)
    125        {
    126            ChunkPosition chunkposition = (ChunkPosition)this.field_94513_g.get(i);
    127
    128            if (chunkposition.x == par1BlockBaseRailLogic.field_94517_c && chunkposition.z == par1BlockBaseRailLogic.field_94515_e)
    129            {
    130                return true;
    131            }
    132        }
    133
    134        return false;
    135    }
    136
    137    private boolean func_94510_b(int par1, int par2, int par3)
    138    {
    139        for (int l = 0; l < this.field_94513_g.size(); ++l)
    140        {
    141            ChunkPosition chunkposition = (ChunkPosition)this.field_94513_g.get(l);
    142
    143            if (chunkposition.x == par1 && chunkposition.z == par3)
    144            {
    145                return true;
    146            }
    147        }
    148
    149        return false;
    150    }
    151
    152    public int func_94505_a()
    153    {
    154        int i = 0;
    155
    156        if (this.func_94502_a(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1))
    157        {
    158            ++i;
    159        }
    160
    161        if (this.func_94502_a(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1))
    162        {
    163            ++i;
    164        }
    165
    166        if (this.func_94502_a(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e))
    167        {
    168            ++i;
    169        }
    170
    171        if (this.func_94502_a(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e))
    172        {
    173            ++i;
    174        }
    175
    176        return i;
    177    }
    178
    179    private boolean func_94507_b(BlockBaseRailLogic par1BlockBaseRailLogic)
    180    {
    181        return this.func_94508_a(par1BlockBaseRailLogic) ? true : (this.field_94513_g.size() == 2 ? false : (this.field_94513_g.isEmpty() ? true : true));
    182    }
    183
    184    private void func_94506_c(BlockBaseRailLogic par1BlockBaseRailLogic)
    185    {
    186        this.field_94513_g.add(new ChunkPosition(par1BlockBaseRailLogic.field_94517_c, par1BlockBaseRailLogic.field_94514_d, par1BlockBaseRailLogic.field_94515_e));
    187        boolean flag = this.func_94510_b(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1);
    188        boolean flag1 = this.func_94510_b(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1);
    189        boolean flag2 = this.func_94510_b(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e);
    190        boolean flag3 = this.func_94510_b(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e);
    191        byte b0 = -1;
    192
    193        if (flag || flag1)
    194        {
    195            b0 = 0;
    196        }
    197
    198        if (flag2 || flag3)
    199        {
    200            b0 = 1;
    201        }
    202
    203        if (!this.field_94512_f)
    204        {
    205            if (flag1 && flag3 && !flag && !flag2)
    206            {
    207                b0 = 6;
    208            }
    209
    210            if (flag1 && flag2 && !flag && !flag3)
    211            {
    212                b0 = 7;
    213            }
    214
    215            if (flag && flag2 && !flag1 && !flag3)
    216            {
    217                b0 = 8;
    218            }
    219
    220            if (flag && flag3 && !flag1 && !flag2)
    221            {
    222                b0 = 9;
    223            }
    224        }
    225
    226        if (b0 == 0 && canMakeSlopes)
    227        {
    228            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c, this.field_94514_d + 1, this.field_94515_e - 1))
    229            {
    230                b0 = 4;
    231            }
    232
    233            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c, this.field_94514_d + 1, this.field_94515_e + 1))
    234            {
    235                b0 = 5;
    236            }
    237        }
    238
    239        if (b0 == 1 && canMakeSlopes)
    240        {
    241            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c + 1, this.field_94514_d + 1, this.field_94515_e))
    242            {
    243                b0 = 2;
    244            }
    245
    246            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c - 1, this.field_94514_d + 1, this.field_94515_e))
    247            {
    248                b0 = 3;
    249            }
    250        }
    251
    252        if (b0 < 0)
    253        {
    254            b0 = 0;
    255        }
    256
    257        int i = b0;
    258
    259        if (this.field_94512_f)
    260        {
    261            i = this.field_94516_b.getBlockMetadata(this.field_94517_c, this.field_94514_d, this.field_94515_e) & 8 | b0;
    262        }
    263
    264        this.field_94516_b.setBlockMetadataWithNotify(this.field_94517_c, this.field_94514_d, this.field_94515_e, i, 3);
    265    }
    266
    267    private boolean func_94503_c(int par1, int par2, int par3)
    268    {
    269        BlockBaseRailLogic blockbaseraillogic = this.func_94501_a(new ChunkPosition(par1, par2, par3));
    270
    271        if (blockbaseraillogic == null)
    272        {
    273            return false;
    274        }
    275        else
    276        {
    277            blockbaseraillogic.func_94509_b();
    278            return blockbaseraillogic.func_94507_b(this);
    279        }
    280    }
    281
    282    public void func_94511_a(boolean par1, boolean par2)
    283    {
    284        boolean flag2 = this.func_94503_c(this.field_94517_c, this.field_94514_d, this.field_94515_e - 1);
    285        boolean flag3 = this.func_94503_c(this.field_94517_c, this.field_94514_d, this.field_94515_e + 1);
    286        boolean flag4 = this.func_94503_c(this.field_94517_c - 1, this.field_94514_d, this.field_94515_e);
    287        boolean flag5 = this.func_94503_c(this.field_94517_c + 1, this.field_94514_d, this.field_94515_e);
    288        byte b0 = -1;
    289
    290        if ((flag2 || flag3) && !flag4 && !flag5)
    291        {
    292            b0 = 0;
    293        }
    294
    295        if ((flag4 || flag5) && !flag2 && !flag3)
    296        {
    297            b0 = 1;
    298        }
    299
    300        if (!this.field_94512_f)
    301        {
    302            if (flag3 && flag5 && !flag2 && !flag4)
    303            {
    304                b0 = 6;
    305            }
    306
    307            if (flag3 && flag4 && !flag2 && !flag5)
    308            {
    309                b0 = 7;
    310            }
    311
    312            if (flag2 && flag4 && !flag3 && !flag5)
    313            {
    314                b0 = 8;
    315            }
    316
    317            if (flag2 && flag5 && !flag3 && !flag4)
    318            {
    319                b0 = 9;
    320            }
    321        }
    322
    323        if (b0 == -1)
    324        {
    325            if (flag2 || flag3)
    326            {
    327                b0 = 0;
    328            }
    329
    330            if (flag4 || flag5)
    331            {
    332                b0 = 1;
    333            }
    334
    335            if (!this.field_94512_f)
    336            {
    337                if (par1)
    338                {
    339                    if (flag3 && flag5)
    340                    {
    341                        b0 = 6;
    342                    }
    343
    344                    if (flag4 && flag3)
    345                    {
    346                        b0 = 7;
    347                    }
    348
    349                    if (flag5 && flag2)
    350                    {
    351                        b0 = 9;
    352                    }
    353
    354                    if (flag2 && flag4)
    355                    {
    356                        b0 = 8;
    357                    }
    358                }
    359                else
    360                {
    361                    if (flag2 && flag4)
    362                    {
    363                        b0 = 8;
    364                    }
    365
    366                    if (flag5 && flag2)
    367                    {
    368                        b0 = 9;
    369                    }
    370
    371                    if (flag4 && flag3)
    372                    {
    373                        b0 = 7;
    374                    }
    375
    376                    if (flag3 && flag5)
    377                    {
    378                        b0 = 6;
    379                    }
    380                }
    381            }
    382        }
    383
    384        if (b0 == 0 && canMakeSlopes)
    385        {
    386            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c, this.field_94514_d + 1, this.field_94515_e - 1))
    387            {
    388                b0 = 4;
    389            }
    390
    391            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c, this.field_94514_d + 1, this.field_94515_e + 1))
    392            {
    393                b0 = 5;
    394            }
    395        }
    396
    397        if (b0 == 1 && canMakeSlopes)
    398        {
    399            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c + 1, this.field_94514_d + 1, this.field_94515_e))
    400            {
    401                b0 = 2;
    402            }
    403
    404            if (BlockRailBase.isRailBlockAt(this.field_94516_b, this.field_94517_c - 1, this.field_94514_d + 1, this.field_94515_e))
    405            {
    406                b0 = 3;
    407            }
    408        }
    409
    410        if (b0 < 0)
    411        {
    412            b0 = 0;
    413        }
    414
    415        this.func_94504_a(b0);
    416        int i = b0;
    417
    418        if (this.field_94512_f)
    419        {
    420            i = this.field_94516_b.getBlockMetadata(this.field_94517_c, this.field_94514_d, this.field_94515_e) & 8 | b0;
    421        }
    422
    423        if (par2 || this.field_94516_b.getBlockMetadata(this.field_94517_c, this.field_94514_d, this.field_94515_e) != i)
    424        {
    425            this.field_94516_b.setBlockMetadataWithNotify(this.field_94517_c, this.field_94514_d, this.field_94515_e, i, 3);
    426
    427            for (int j = 0; j < this.field_94513_g.size(); ++j)
    428            {
    429                BlockBaseRailLogic blockbaseraillogic = this.func_94501_a((ChunkPosition)this.field_94513_g.get(j));
    430
    431                if (blockbaseraillogic != null)
    432                {
    433                    blockbaseraillogic.func_94509_b();
    434
    435                    if (blockbaseraillogic.func_94507_b(this))
    436                    {
    437                        blockbaseraillogic.func_94506_c(this);
    438                    }
    439                }
    440            }
    441        }
    442    }
    443}