Esempio n. 1
0
            protected override void OnTarget(Mobile from, object targ)
            {
                var done = false;

                if (!(targ is Item))
                {
                    from.SendMessage("You can only dupe items.");
                    return;
                }

                CommandLogging.WriteLine(
                    from,
                    "{0} {1} duping {2} (inBag={3}; amount={4})",
                    from.AccessLevel,
                    CommandLogging.Format(from),
                    CommandLogging.Format(targ),
                    m_InBag,
                    m_Amount
                    );

                var       copy = (Item)targ;
                Container pack = null;

                if (m_InBag)
                {
                    if (copy.Parent is Container cont)
                    {
                        pack = cont;
                    }
                    else if (copy.Parent is Mobile m)
                    {
                        pack = m.Backpack;
                    }
                }
                else
                {
                    pack = from.Backpack;
                }

                var c = ActivatorUtil.GetConstructor(copy.GetType());

                if (c != null)
                {
                    var paramList = c.GetParameters();
                    var args      = paramList.Length == 0 ? null : new object[paramList.Length];
                    if (args != null)
                    {
                        Array.Fill(args, Type.Missing);
                    }
                    try
                    {
                        from.SendMessage("Duping {0}...", m_Amount);
                        for (var i = 0; i < m_Amount; i++)
                        {
                            if (c.Invoke(args) is Item newItem)
                            {
                                CopyProperties(newItem, copy); // copy.Dupe( item, copy.Amount );
                                copy.OnAfterDuped(newItem);
                                newItem.Parent = null;

                                if (pack != null)
                                {
                                    pack.DropItem(newItem);
                                }
                                else
                                {
                                    newItem.MoveToWorld(from.Location, from.Map);
                                }

                                newItem.InvalidateProperties();

                                CommandLogging.WriteLine(
                                    from,
                                    "{0} {1} duped {2} creating {3}",
                                    from.AccessLevel,
                                    CommandLogging.Format(from),
                                    CommandLogging.Format(targ),
                                    CommandLogging.Format(newItem)
                                    );
                            }
                        }

                        from.SendMessage("Done");
                        done = true;
                    }
                    catch
                    {
                        from.SendMessage("Error!");
                        return;
                    }
                }

                if (!done)
                {
                    from.SendMessage("Unable to dupe.  Item must have a 0 parameter constructor.");
                }
            }