    001package net.minecraft.command;
    002
    003import java.util.List;
    004import net.minecraft.enchantment.Enchantment;
    005import net.minecraft.entity.player.EntityPlayerMP;
    006import net.minecraft.item.ItemStack;
    007import net.minecraft.nbt.NBTTagCompound;
    008import net.minecraft.nbt.NBTTagList;
    009import net.minecraft.server.MinecraftServer;
    010
    011public class CommandEnchant extends CommandBase
    012{
    013    public String getCommandName()
    014    {
    015        return "enchant";
    016    }
    017
    018    /**
    019     * Return the required permission level for this command.
    020     */
    021    public int getRequiredPermissionLevel()
    022    {
    023        return 2;
    024    }
    025
    026    public String getCommandUsage(ICommandSender par1ICommandSender)
    027    {
    028        return par1ICommandSender.translateString("commands.enchant.usage", new Object[0]);
    029    }
    030
    031    public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    032    {
    033        if (par2ArrayOfStr.length < 2)
    034        {
    035            throw new WrongUsageException("commands.enchant.usage", new Object[0]);
    036        }
    037        else
    038        {
    039            EntityPlayerMP entityplayermp = func_82359_c(par1ICommandSender, par2ArrayOfStr[0]);
    040            int i = parseIntBounded(par1ICommandSender, par2ArrayOfStr[1], 0, Enchantment.enchantmentsList.length - 1);
    041            int j = 1;
    042            ItemStack itemstack = entityplayermp.getCurrentEquippedItem();
    043
    044            if (itemstack == null)
    045            {
    046                notifyAdmins(par1ICommandSender, "commands.enchant.noItem", new Object[0]);
    047            }
    048            else
    049            {
    050                Enchantment enchantment = Enchantment.enchantmentsList[i];
    051
    052                if (enchantment == null)
    053                {
    054                    throw new NumberInvalidException("commands.enchant.notFound", new Object[] {Integer.valueOf(i)});
    055                }
    056                else if (!enchantment.func_92089_a(itemstack))
    057                {
    058                    notifyAdmins(par1ICommandSender, "commands.enchant.cantEnchant", new Object[0]);
    059                }
    060                else
    061                {
    062                    if (par2ArrayOfStr.length >= 3)
    063                    {
    064                        j = parseIntBounded(par1ICommandSender, par2ArrayOfStr[2], enchantment.getMinLevel(), enchantment.getMaxLevel());
    065                    }
    066
    067                    if (itemstack.hasTagCompound())
    068                    {
    069                        NBTTagList nbttaglist = itemstack.getEnchantmentTagList();
    070
    071                        if (nbttaglist != null)
    072                        {
    073                            for (int k = 0; k < nbttaglist.tagCount(); ++k)
    074                            {
    075                                short short1 = ((NBTTagCompound)nbttaglist.tagAt(k)).getShort("id");
    076
    077                                if (Enchantment.enchantmentsList[short1] != null)
    078                                {
    079                                    Enchantment enchantment1 = Enchantment.enchantmentsList[short1];
    080
    081                                    if (!enchantment1.canApplyTogether(enchantment))
    082                                    {
    083                                        notifyAdmins(par1ICommandSender, "commands.enchant.cantCombine", new Object[] {enchantment.getTranslatedName(j), enchantment1.getTranslatedName(((NBTTagCompound)nbttaglist.tagAt(k)).getShort("lvl"))});
    084                                        return;
    085                                    }
    086                                }
    087                            }
    088                        }
    089                    }
    090
    091                    itemstack.addEnchantment(enchantment, j);
    092                    notifyAdmins(par1ICommandSender, "commands.enchant.success", new Object[0]);
    093                }
    094            }
    095        }
    096    }
    097
    098    /**
    099     * Adds the strings available in this command to the given list of tab completion options.
    100     */
    101    public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    102    {
    103        return par2ArrayOfStr.length == 1 ? getListOfStringsMatchingLastWord(par2ArrayOfStr, this.getListOfPlayers()) : null;
    104    }
    105
    106    protected String[] getListOfPlayers()
    107    {
    108        return MinecraftServer.getServer().getAllUsernames();
    109    }
    110
    111    /**
    112     * Return whether the specified command parameter index is a username parameter.
    113     */
    114    public boolean isUsernameIndex(String[] par1ArrayOfStr, int par2)
    115    {
    116        return par2 == 0;
    117    }
    118}