    001    package net.minecraft.command;
    002
    003    import java.util.List;
    004    import net.minecraft.enchantment.Enchantment;
    005    import net.minecraft.entity.player.EntityPlayerMP;
    006    import net.minecraft.item.ItemStack;
    007    import net.minecraft.nbt.NBTTagCompound;
    008    import net.minecraft.nbt.NBTTagList;
    009    import net.minecraft.server.MinecraftServer;
    010
    011    public 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 var3 = func_82359_c(par1ICommandSender, par2ArrayOfStr[0]);
    040                int var4 = parseIntBounded(par1ICommandSender, par2ArrayOfStr[1], 0, Enchantment.enchantmentsList.length - 1);
    041                int var5 = 1;
    042                ItemStack var6 = var3.getCurrentEquippedItem();
    043
    044                if (var6 == null)
    045                {
    046                    notifyAdmins(par1ICommandSender, "commands.enchant.noItem", new Object[0]);
    047                }
    048                else
    049                {
    050                    Enchantment var7 = Enchantment.enchantmentsList[var4];
    051
    052                    if (var7 == null)
    053                    {
    054                        throw new NumberInvalidException("commands.enchant.notFound", new Object[] {Integer.valueOf(var4)});
    055                    }
    056                    else if (!var7.func_92037_a(var6))
    057                    {
    058                        notifyAdmins(par1ICommandSender, "commands.enchant.cantEnchant", new Object[0]);
    059                    }
    060                    else
    061                    {
    062                        if (par2ArrayOfStr.length >= 3)
    063                        {
    064                            var5 = parseIntBounded(par1ICommandSender, par2ArrayOfStr[2], var7.getMinLevel(), var7.getMaxLevel());
    065                        }
    066
    067                        if (var6.hasTagCompound())
    068                        {
    069                            NBTTagList var8 = var6.getEnchantmentTagList();
    070
    071                            if (var8 != null)
    072                            {
    073                                for (int var9 = 0; var9 < var8.tagCount(); ++var9)
    074                                {
    075                                    short var10 = ((NBTTagCompound)var8.tagAt(var9)).getShort("id");
    076
    077                                    if (Enchantment.enchantmentsList[var10] != null)
    078                                    {
    079                                        Enchantment var11 = Enchantment.enchantmentsList[var10];
    080
    081                                        if (!var11.canApplyTogether(var7))
    082                                        {
    083                                            notifyAdmins(par1ICommandSender, "commands.enchant.cantCombine", new Object[] {var7.getTranslatedName(var5), var11.getTranslatedName(((NBTTagCompound)var8.tagAt(var9)).getShort("lvl"))});
    084                                            return;
    085                                        }
    086                                    }
    087                                }
    088                            }
    089                        }
    090
    091                        var6.addEnchantment(var7, var5);
    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(int par1)
    115        {
    116            return par1 == 0;
    117        }
    118    }