    001    package net.minecraft.src;
    002
    003    import cpw.mods.fml.common.Side;
    004    import cpw.mods.fml.common.asm.SideOnly;
    005
    006    public class ItemMap extends ItemMapBase
    007    {
    008        protected ItemMap(int par1)
    009        {
    010            super(par1);
    011            this.setMaxStackSize(1);
    012            this.setCreativeTab(CreativeTabs.tabMisc);
    013        }
    014
    015        @SideOnly(Side.CLIENT)
    016        public static MapData getMPMapData(short par0, World par1World)
    017        {
    018            MapData var3 = (MapData)par1World.loadItemData(MapData.class, "map_" + par0);
    019
    020            if (var3 == null)
    021            {
    022                int var4 = par1World.getUniqueDataId("map");
    023                String var2 = "map_" + var4;
    024                var3 = new MapData(var2);
    025                par1World.setItemData(var2, var3);
    026            }
    027
    028            return var3;
    029        }
    030
    031        public MapData getMapData(ItemStack par1ItemStack, World par2World)
    032        {
    033            MapData var4 = (MapData)par2World.loadItemData(MapData.class, "map_" + par1ItemStack.getItemDamage());
    034
    035            if (var4 == null)
    036            {
    037                par1ItemStack.setItemDamage(par2World.getUniqueDataId("map"));
    038                String var3 = "map_" + par1ItemStack.getItemDamage();
    039                var4 = new MapData(var3);
    040                var4.xCenter = par2World.getWorldInfo().getSpawnX();
    041                var4.zCenter = par2World.getWorldInfo().getSpawnZ();
    042                var4.scale = 3;
    043                var4.dimension = par2World.provider.dimensionId;
    044                var4.markDirty();
    045                par2World.setItemData(var3, var4);
    046            }
    047
    048            return var4;
    049        }
    050
    051        public void updateMapData(World par1World, Entity par2Entity, MapData par3MapData)
    052        {
    053            if (par1World.provider.dimensionId == par3MapData.dimension)
    054            {
    055                short var4 = 128;
    056                short var5 = 128;
    057                int var6 = 1 << par3MapData.scale;
    058                int var7 = par3MapData.xCenter;
    059                int var8 = par3MapData.zCenter;
    060                int var9 = MathHelper.floor_double(par2Entity.posX - (double)var7) / var6 + var4 / 2;
    061                int var10 = MathHelper.floor_double(par2Entity.posZ - (double)var8) / var6 + var5 / 2;
    062                int var11 = 128 / var6;
    063
    064                if (par1World.provider.hasNoSky)
    065                {
    066                    var11 /= 2;
    067                }
    068
    069                ++par3MapData.randomEffect;
    070
    071                for (int var12 = var9 - var11 + 1; var12 < var9 + var11; ++var12)
    072                {
    073                    if ((var12 & 15) == (par3MapData.randomEffect & 15))
    074                    {
    075                        int var13 = 255;
    076                        int var14 = 0;
    077                        double var15 = 0.0D;
    078
    079                        for (int var17 = var10 - var11 - 1; var17 < var10 + var11; ++var17)
    080                        {
    081                            if (var12 >= 0 && var17 >= -1 && var12 < var4 && var17 < var5)
    082                            {
    083                                int var18 = var12 - var9;
    084                                int var19 = var17 - var10;
    085                                boolean var20 = var18 * var18 + var19 * var19 > (var11 - 2) * (var11 - 2);
    086                                int var21 = (var7 / var6 + var12 - var4 / 2) * var6;
    087                                int var22 = (var8 / var6 + var17 - var5 / 2) * var6;
    088                                byte var23 = 0;
    089                                byte var24 = 0;
    090                                byte var25 = 0;
    091                                int[] var26 = new int[Block.blocksList.length];
    092                                Chunk var27 = par1World.getChunkFromBlockCoords(var21, var22);
    093
    094                                if (!var27.isEmpty())
    095                                {
    096                                    int var28 = var21 & 15;
    097                                    int var29 = var22 & 15;
    098                                    int var30 = 0;
    099                                    double var31 = 0.0D;
    100                                    int var34;
    101                                    int var35;
    102                                    int var33;
    103                                    int var38;
    104
    105                                    if (par1World.provider.hasNoSky)
    106                                    {
    107                                        var33 = var21 + var22 * 231871;
    108                                        var33 = var33 * var33 * 31287121 + var33 * 11;
    109
    110                                        if ((var33 >> 20 & 1) == 0)
    111                                        {
    112                                            var26[Block.dirt.blockID] += 10;
    113                                        }
    114                                        else
    115                                        {
    116                                            var26[Block.stone.blockID] += 10;
    117                                        }
    118
    119                                        var31 = 100.0D;
    120                                    }
    121                                    else
    122                                    {
    123                                        for (var33 = 0; var33 < var6; ++var33)
    124                                        {
    125                                            for (var34 = 0; var34 < var6; ++var34)
    126                                            {
    127                                                var35 = var27.getHeightValue(var33 + var28, var34 + var29) + 1;
    128                                                int var36 = 0;
    129
    130                                                if (var35 > 1)
    131                                                {
    132                                                    boolean var37 = false;
    133
    134                                                    do
    135                                                    {
    136                                                        var37 = true;
    137                                                        var36 = var27.getBlockID(var33 + var28, var35 - 1, var34 + var29);
    138
    139                                                        if (var36 == 0)
    140                                                        {
    141                                                            var37 = false;
    142                                                        }
    143                                                        else if (var35 > 0 && var36 > 0 && Block.blocksList[var36].blockMaterial.materialMapColor == MapColor.airColor)
    144                                                        {
    145                                                            var37 = false;
    146                                                        }
    147
    148                                                        if (!var37)
    149                                                        {
    150                                                            --var35;
    151
    152                                                            if (var35 <= 0)
    153                                                            {
    154                                                                break;
    155                                                            }
    156
    157                                                            var36 = var27.getBlockID(var33 + var28, var35 - 1, var34 + var29);
    158                                                        }
    159                                                    }
    160                                                    while (var35 > 0 && !var37);
    161
    162                                                    if (var35 > 0 && var36 != 0 && Block.blocksList[var36].blockMaterial.isLiquid())
    163                                                    {
    164                                                        var38 = var35 - 1;
    165                                                        boolean var39 = false;
    166                                                        int var41;
    167
    168                                                        do
    169                                                        {
    170                                                            var41 = var27.getBlockID(var33 + var28, var38--, var34 + var29);
    171                                                            ++var30;
    172                                                        }
    173                                                        while (var38 > 0 && var41 != 0 && Block.blocksList[var41].blockMaterial.isLiquid());
    174                                                    }
    175                                                }
    176
    177                                                var31 += (double)var35 / (double)(var6 * var6);
    178                                                ++var26[var36];
    179                                            }
    180                                        }
    181                                    }
    182
    183                                    var30 /= var6 * var6;
    184                                    int var10000 = var23 / (var6 * var6);
    185                                    var10000 = var24 / (var6 * var6);
    186                                    var10000 = var25 / (var6 * var6);
    187                                    var33 = 0;
    188                                    var34 = 0;
    189
    190                                    for (var35 = 0; var35 < Block.blocksList.length; ++var35)
    191                                    {
    192                                        if (var26[var35] > var33)
    193                                        {
    194                                            var34 = var35;
    195                                            var33 = var26[var35];
    196                                        }
    197                                    }
    198
    199                                    double var43 = (var31 - var15) * 4.0D / (double)(var6 + 4) + ((double)(var12 + var17 & 1) - 0.5D) * 0.4D;
    200                                    byte var42 = 1;
    201
    202                                    if (var43 > 0.6D)
    203                                    {
    204                                        var42 = 2;
    205                                    }
    206
    207                                    if (var43 < -0.6D)
    208                                    {
    209                                        var42 = 0;
    210                                    }
    211
    212                                    var38 = 0;
    213
    214                                    if (var34 > 0)
    215                                    {
    216                                        MapColor var45 = Block.blocksList[var34].blockMaterial.materialMapColor;
    217
    218                                        if (var45 == MapColor.waterColor)
    219                                        {
    220                                            var43 = (double)var30 * 0.1D + (double)(var12 + var17 & 1) * 0.2D;
    221                                            var42 = 1;
    222
    223                                            if (var43 < 0.5D)
    224                                            {
    225                                                var42 = 2;
    226                                            }
    227
    228                                            if (var43 > 0.9D)
    229                                            {
    230                                                var42 = 0;
    231                                            }
    232                                        }
    233
    234                                        var38 = var45.colorIndex;
    235                                    }
    236
    237                                    var15 = var31;
    238
    239                                    if (var17 >= 0 && var18 * var18 + var19 * var19 < var11 * var11 && (!var20 || (var12 + var17 & 1) != 0))
    240                                    {
    241                                        byte var44 = par3MapData.colors[var12 + var17 * var4];
    242                                        byte var40 = (byte)(var38 * 4 + var42);
    243
    244                                        if (var44 != var40)
    245                                        {
    246                                            if (var13 > var17)
    247                                            {
    248                                                var13 = var17;
    249                                            }
    250
    251                                            if (var14 < var17)
    252                                            {
    253                                                var14 = var17;
    254                                            }
    255
    256                                            par3MapData.colors[var12 + var17 * var4] = var40;
    257                                        }
    258                                    }
    259                                }
    260                            }
    261                        }
    262
    263                        if (var13 <= var14)
    264                        {
    265                            par3MapData.func_76194_a(var12, var13, var14);
    266                        }
    267                    }
    268                }
    269            }
    270        }
    271
    272        /**
    273         * Called each tick as long the item is on a player inventory. Uses by maps to check if is on a player hand and
    274         * update it's contents.
    275         */
    276        public void onUpdate(ItemStack par1ItemStack, World par2World, Entity par3Entity, int par4, boolean par5)
    277        {
    278            if (!par2World.isRemote)
    279            {
    280                MapData var6 = this.getMapData(par1ItemStack, par2World);
    281
    282                if (par3Entity instanceof EntityPlayer)
    283                {
    284                    EntityPlayer var7 = (EntityPlayer)par3Entity;
    285                    var6.updateVisiblePlayers(var7, par1ItemStack);
    286                }
    287
    288                if (par5)
    289                {
    290                    this.updateMapData(par2World, par3Entity, var6);
    291                }
    292            }
    293        }
    294
    295        /**
    296         * Called when item is crafted/smelted. Used only by maps so far.
    297         */
    298        public void onCreated(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    299        {
    300            par1ItemStack.setItemDamage(par2World.getUniqueDataId("map"));
    301            String var4 = "map_" + par1ItemStack.getItemDamage();
    302            MapData var5 = new MapData(var4);
    303            par2World.setItemData(var4, var5);
    304            var5.xCenter = MathHelper.floor_double(par3EntityPlayer.posX);
    305            var5.zCenter = MathHelper.floor_double(par3EntityPlayer.posZ);
    306            var5.scale = 3;
    307            var5.dimension = par2World.provider.dimensionId;
    308            var5.markDirty();
    309        }
    310
    311        /**
    312         * returns null if no update is to be sent
    313         */
    314        public Packet createMapDataPacket(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    315        {
    316            byte[] var4 = this.getMapData(par1ItemStack, par2World).func_76193_a(par1ItemStack, par2World, par3EntityPlayer);
    317            return var4 == null ? null : new Packet131MapData((short)Item.map.shiftedIndex, (short)par1ItemStack.getItemDamage(), var4);
    318        }
    319    }