    001package net.minecraft.inventory;
    002
    003import cpw.mods.fml.relauncher.Side;
    004import cpw.mods.fml.relauncher.SideOnly;
    005import java.util.List;
    006import java.util.Random;
    007import net.minecraft.block.Block;
    008import net.minecraft.enchantment.EnchantmentData;
    009import net.minecraft.enchantment.EnchantmentHelper;
    010import net.minecraft.entity.player.EntityPlayer;
    011import net.minecraft.entity.player.InventoryPlayer;
    012import net.minecraft.item.Item;
    013import net.minecraft.item.ItemStack;
    014import net.minecraft.world.World;
    015import net.minecraftforge.common.ForgeHooks;
    016
    017public class ContainerEnchantment extends Container
    018{
    019    /** SlotEnchantmentTable object with ItemStack to be enchanted */
    020    public IInventory tableInventory = new SlotEnchantmentTable(this, "Enchant", true, 1);
    021
    022    /** current world (for bookshelf counting) */
    023    private World worldPointer;
    024    private int posX;
    025    private int posY;
    026    private int posZ;
    027    private Random rand = new Random();
    028
    029    /** used as seed for EnchantmentNameParts (see GuiEnchantment) */
    030    public long nameSeed;
    031
    032    /** 3-member array storing the enchantment levels of each slot */
    033    public int[] enchantLevels = new int[3];
    034
    035    public ContainerEnchantment(InventoryPlayer par1InventoryPlayer, World par2World, int par3, int par4, int par5)
    036    {
    037        this.worldPointer = par2World;
    038        this.posX = par3;
    039        this.posY = par4;
    040        this.posZ = par5;
    041        this.addSlotToContainer(new SlotEnchantment(this, this.tableInventory, 0, 25, 47));
    042        int l;
    043
    044        for (l = 0; l < 3; ++l)
    045        {
    046            for (int i1 = 0; i1 < 9; ++i1)
    047            {
    048                this.addSlotToContainer(new Slot(par1InventoryPlayer, i1 + l * 9 + 9, 8 + i1 * 18, 84 + l * 18));
    049            }
    050        }
    051
    052        for (l = 0; l < 9; ++l)
    053        {
    054            this.addSlotToContainer(new Slot(par1InventoryPlayer, l, 8 + l * 18, 142));
    055        }
    056    }
    057
    058    public void addCraftingToCrafters(ICrafting par1ICrafting)
    059    {
    060        super.addCraftingToCrafters(par1ICrafting);
    061        par1ICrafting.sendProgressBarUpdate(this, 0, this.enchantLevels[0]);
    062        par1ICrafting.sendProgressBarUpdate(this, 1, this.enchantLevels[1]);
    063        par1ICrafting.sendProgressBarUpdate(this, 2, this.enchantLevels[2]);
    064    }
    065
    066    /**
    067     * Looks for changes made in the container, sends them to every listener.
    068     */
    069    public void detectAndSendChanges()
    070    {
    071        super.detectAndSendChanges();
    072
    073        for (int i = 0; i < this.crafters.size(); ++i)
    074        {
    075            ICrafting icrafting = (ICrafting)this.crafters.get(i);
    076            icrafting.sendProgressBarUpdate(this, 0, this.enchantLevels[0]);
    077            icrafting.sendProgressBarUpdate(this, 1, this.enchantLevels[1]);
    078            icrafting.sendProgressBarUpdate(this, 2, this.enchantLevels[2]);
    079        }
    080    }
    081
    082    @SideOnly(Side.CLIENT)
    083    public void updateProgressBar(int par1, int par2)
    084    {
    085        if (par1 >= 0 && par1 <= 2)
    086        {
    087            this.enchantLevels[par1] = par2;
    088        }
    089        else
    090        {
    091            super.updateProgressBar(par1, par2);
    092        }
    093    }
    094
    095    /**
    096     * Callback for when the crafting matrix is changed.
    097     */
    098    public void onCraftMatrixChanged(IInventory par1IInventory)
    099    {
    100        if (par1IInventory == this.tableInventory)
    101        {
    102            ItemStack itemstack = par1IInventory.getStackInSlot(0);
    103            int i;
    104
    105            if (itemstack != null && itemstack.isItemEnchantable())
    106            {
    107                this.nameSeed = this.rand.nextLong();
    108
    109                if (!this.worldPointer.isRemote)
    110                {
    111                    i = 0;
    112                    int j;
    113                    float power = 0;
    114
    115                    for (j = -1; j <= 1; ++j)
    116                    {
    117                        for (int k = -1; k <= 1; ++k)
    118                        {
    119                            if ((j != 0 || k != 0) && this.worldPointer.isAirBlock(this.posX + k, this.posY, this.posZ + j) && this.worldPointer.isAirBlock(this.posX + k, this.posY + 1, this.posZ + j))
    120                            {
    121                                power += ForgeHooks.getEnchantPower(worldPointer, posX + k * 2, posY,     posZ + j * 2);
    122                                power += ForgeHooks.getEnchantPower(worldPointer, posX + k * 2, posY + 1, posZ + j * 2);
    123
    124                                if (k != 0 && j != 0)
    125                                {
    126                                    power += ForgeHooks.getEnchantPower(worldPointer, posX + k * 2, posY,     posZ + j    );
    127                                    power += ForgeHooks.getEnchantPower(worldPointer, posX + k * 2, posY + 1, posZ + j    );
    128                                    power += ForgeHooks.getEnchantPower(worldPointer, posX + k,     posY,     posZ + j * 2);
    129                                    power += ForgeHooks.getEnchantPower(worldPointer, posX + k,     posY + 1, posZ + j * 2);
    130                                }
    131                            }
    132                        }
    133                    }
    134
    135                    for (j = 0; j < 3; ++j)
    136                    {
    137                        this.enchantLevels[j] = EnchantmentHelper.calcItemStackEnchantability(this.rand, j, (int)power, itemstack);
    138                    }
    139
    140                    this.detectAndSendChanges();
    141                }
    142            }
    143            else
    144            {
    145                for (i = 0; i < 3; ++i)
    146                {
    147                    this.enchantLevels[i] = 0;
    148                }
    149            }
    150        }
    151    }
    152
    153    /**
    154     * enchants the item on the table using the specified slot; also deducts XP from player
    155     */
    156    public boolean enchantItem(EntityPlayer par1EntityPlayer, int par2)
    157    {
    158        ItemStack itemstack = this.tableInventory.getStackInSlot(0);
    159
    160        if (this.enchantLevels[par2] > 0 && itemstack != null && (par1EntityPlayer.experienceLevel >= this.enchantLevels[par2] || par1EntityPlayer.capabilities.isCreativeMode))
    161        {
    162            if (!this.worldPointer.isRemote)
    163            {
    164                List list = EnchantmentHelper.buildEnchantmentList(this.rand, itemstack, this.enchantLevels[par2]);
    165                boolean flag = itemstack.itemID == Item.book.itemID;
    166
    167                if (list != null)
    168                {
    169                    par1EntityPlayer.addExperienceLevel(-this.enchantLevels[par2]);
    170
    171                    if (flag)
    172                    {
    173                        itemstack.itemID = Item.enchantedBook.itemID;
    174                    }
    175
    176                    int j = flag ? this.rand.nextInt(list.size()) : -1;
    177
    178                    for (int k = 0; k < list.size(); ++k)
    179                    {
    180                        EnchantmentData enchantmentdata = (EnchantmentData)list.get(k);
    181
    182                        if (!flag || k == j)
    183                        {
    184                            if (flag)
    185                            {
    186                                Item.enchantedBook.func_92115_a(itemstack, enchantmentdata);
    187                            }
    188                            else
    189                            {
    190                                itemstack.addEnchantment(enchantmentdata.enchantmentobj, enchantmentdata.enchantmentLevel);
    191                            }
    192                        }
    193                    }
    194
    195                    this.onCraftMatrixChanged(this.tableInventory);
    196                }
    197            }
    198
    199            return true;
    200        }
    201        else
    202        {
    203            return false;
    204        }
    205    }
    206
    207    /**
    208     * Callback for when the crafting gui is closed.
    209     */
    210    public void onCraftGuiClosed(EntityPlayer par1EntityPlayer)
    211    {
    212        super.onCraftGuiClosed(par1EntityPlayer);
    213
    214        if (!this.worldPointer.isRemote)
    215        {
    216            ItemStack itemstack = this.tableInventory.getStackInSlotOnClosing(0);
    217
    218            if (itemstack != null)
    219            {
    220                par1EntityPlayer.dropPlayerItem(itemstack);
    221            }
    222        }
    223    }
    224
    225    public boolean canInteractWith(EntityPlayer par1EntityPlayer)
    226    {
    227        return this.worldPointer.getBlockId(this.posX, this.posY, this.posZ) != Block.enchantmentTable.blockID ? false : par1EntityPlayer.getDistanceSq((double)this.posX + 0.5D, (double)this.posY + 0.5D, (double)this.posZ + 0.5D) <= 64.0D;
    228    }
    229
    230    /**
    231     * Called when a player shift-clicks on a slot. You must override this or you will crash when someone does that.
    232     */
    233    public ItemStack transferStackInSlot(EntityPlayer par1EntityPlayer, int par2)
    234    {
    235        ItemStack itemstack = null;
    236        Slot slot = (Slot)this.inventorySlots.get(par2);
    237
    238        if (slot != null && slot.getHasStack())
    239        {
    240            ItemStack itemstack1 = slot.getStack();
    241            itemstack = itemstack1.copy();
    242
    243            if (par2 == 0)
    244            {
    245                if (!this.mergeItemStack(itemstack1, 1, 37, true))
    246                {
    247                    return null;
    248                }
    249            }
    250            else
    251            {
    252                if (((Slot)this.inventorySlots.get(0)).getHasStack() || !((Slot)this.inventorySlots.get(0)).isItemValid(itemstack1))
    253                {
    254                    return null;
    255                }
    256
    257                if (itemstack1.hasTagCompound() && itemstack1.stackSize == 1)
    258                {
    259                    ((Slot)this.inventorySlots.get(0)).putStack(itemstack1.copy());
    260                    itemstack1.stackSize = 0;
    261                }
    262                else if (itemstack1.stackSize >= 1)
    263                {
    264                    ((Slot)this.inventorySlots.get(0)).putStack(new ItemStack(itemstack1.itemID, 1, itemstack1.getItemDamage()));
    265                    --itemstack1.stackSize;
    266                }
    267            }
    268
    269            if (itemstack1.stackSize == 0)
    270            {
    271                slot.putStack((ItemStack)null);
    272            }
    273            else
    274            {
    275                slot.onSlotChanged();
    276            }
    277
    278            if (itemstack1.stackSize == itemstack.stackSize)
    279            {
    280                return null;
    281            }
    282
    283            slot.onPickupFromSlot(par1EntityPlayer, itemstack1);
    284        }
    285
    286        return itemstack;
    287    }
    288}