Код просаживает TPS

Имею код строителя площадок, вызывающийся на onItemRightClick
При множественном использования строителя, TPS сервера падает до 15 и ниже.
Код:
@Override
    public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player)
    {
        if (!world.isRemote)
        {
            MovingObjectPosition mop = this.getMovingObjectPositionFromPlayer(world, player, false);

            if (mop == null || mop.typeOfHit != MovingObjectPosition.MovingObjectType.BLOCK)
            {
                return stack;
            }

            ItemStack[] inventory = getInventory(stack);

            if (inventory[0] == null || inventory[1] == null)
            {
                return stack;
            }

            Block newBlock = Block.getBlockFromItem(inventory[1].getItem());

            if (newBlock == Blocks.air)
            {
                return stack;
            }

            int newMeta = inventory[1].getItemDamage();

            double kleinEmc = ItemPE.getEmc(inventory[0]);
            int reqEmc = EMCHelper.getEmcValue(inventory[1]);

            byte charge = getCharge(stack);
            byte mode = this.getMode(stack);

            int facing = MathHelper.floor_double((double) ((player.rotationYaw * 4F) / 360F) + 0.5D) & 3;
            ForgeDirection dir = ForgeDirection.getOrientation(mop.sideHit);
            Vec3 look = player.getLookVec();

            AxisAlignedBB box = AxisAlignedBB.getBoundingBox(
                    mop.blockX,
                    mop.blockY,
                    mop.blockZ,
                    mop.blockX,
                    mop.blockY,
                    mop.blockZ
            );

            int dX = 0, dY = 0, dZ = 0;

            boolean lookingDown = look.yCoord >= -1 && look.yCoord <= -WALL_MODE_ST;
            boolean lookingUp   = look.yCoord <=  1 && look.yCoord >=  WALL_MODE_ST;

            boolean lookingAlongZ = facing == 0 || facing == 2;

            switch (dir) {
                case UP:
                    if (lookingDown || mode == TRANSMUTATION_MODE)
                    {
                        box = box.expand(charge, 0, charge);
                        dY = 1;
                    }
                    else if (mode == WALL_MODE && lookingAlongZ)
                        box = box.expand(charge, charge * 2, 0).offset(0, charge, 0);
                    else if (mode == WALL_MODE)
                         box = box.expand(0, charge * 2, charge).offset(0, charge, 0);

                    break;

                case DOWN:
                    if (lookingUp || mode == TRANSMUTATION_MODE)
                    {
                        box = box.expand(charge, 0, charge);
                        dY = -1;
                    }
                    else if (mode == WALL_MODE && lookingAlongZ)
                        box = box.expand(charge, charge * 2, 0).offset(0, -charge, 0);
                    else if (mode == WALL_MODE)
                        box = box.expand(0, charge * 2, charge).offset(0, -charge, 0);
                    
                    break;

                case EAST:
                    if (mode == WALL_MODE)
                    {
                    box = box.expand(0, charge, charge);
                    dX = 1;
                    }
                    break;

                case WEST:
                    if (mode == WALL_MODE)
                    {
                    box = box.expand(0, charge, charge);
                    dX = -1;
                    }
                    break;

                case SOUTH:
                    if (mode == WALL_MODE)
                    {
                    box = box.expand(charge, charge, 0);
                    dZ = 1;
                    }
                    break;

                case NORTH:
                    if (mode == WALL_MODE)
                    {
                    box = box.expand(charge, charge, 0);
                    dZ = -1;
                    }
                    break;
            }

            if (box != null)
            {
                for (int x = (int) box.minX; x <= (int) box.maxX; x++)
                {
                    for (int y = (int) box.minY; y <= (int) box.maxY; y++)
                    {
                        for (int z = (int) box.minZ; z <= (int) box.maxZ; z++)
                        {
                            Block oldBlock = world.getBlock(x, y, z);
                            int oldMeta = oldBlock.getDamageValue(world, x, y, z);

                            if ((mode == EXTENTION_MODE || mode == WALL_MODE) && oldBlock == Blocks.air)
                                {
                                    if (kleinEmc < reqEmc)
                                        break;
                                    if (PlayerHelper.checkedPlaceBlock(((EntityPlayerMP) player), x, y, z, newBlock, newMeta))
                                    {
                                        removeKleinEMC(stack, reqEmc);
                                        kleinEmc -= reqEmc;
                                    }
                                }
                            else if (mode == TRANSMUTATION_MODE)
                            {
                                if ((oldBlock == newBlock && oldMeta == newMeta) || oldBlock == Blocks.air || world.getTileEntity(x, y, z) != null || !EMCHelper.doesItemHaveEmc(new ItemStack(oldBlock, 1, oldMeta)))
                                {
                                    continue;
                                }

                                int emc = EMCHelper.getEmcValue(new ItemStack(oldBlock, 1, oldMeta));

                                if (emc > reqEmc)
                                {
                                    if (PlayerHelper.checkedReplaceBlock(((EntityPlayerMP) player), x, y, z, newBlock, newMeta))
                                    {
                                        int difference = emc - reqEmc;
                                        kleinEmc += MathHelper.clamp_double(kleinEmc, 0, EMCHelper.getKleinStarMaxEmc(inventory[0]));
                                        addKleinEMC(stack, difference);
                                    }
                                }
                                else if (emc < reqEmc)
                                {
                                    int difference = reqEmc - emc;

                                    if (kleinEmc >= difference)
                                    {
                                        if (PlayerHelper.checkedReplaceBlock(((EntityPlayerMP) player), x, y, z, newBlock, newMeta))
                                        {
                                            kleinEmc -= difference;
                                            removeKleinEMC(stack, difference);
                                        }
                                    }
                                }
                                else
                                {
                                    PlayerHelper.checkedReplaceBlock(((EntityPlayerMP) player), x, y, z, newBlock, newMeta);
                                }
                            }
                        }
                    }
                }
                player.worldObj.playSoundAtEntity(player, "projecte:item.pepower", 1.0F, 0.80F + ((0.20F / (float)numCharges) * charge));
            }
        }

        return stack;
    }
хотел бы получить пару подсказок по оптимизаций этого кода.
 
Циклы сократить, че, велосипедирование нынче в моде.
 
ElectroDans написал(а):
wilah написал(а):
Flanagun написал(а):
Самый банальный ответ - убрать пустые строки и удалить лишнее.
Ну що тут сказати
Аххаахаха Я ПОД СТОЛОМ- ржу немагу
Вылезай и читай второе слово в моем посту
 
Назад
Сверху