RedBeard

Member
I made a timer that worked fine in the OnDoubleClick method, but my timer needs to wait for the target confirmation before it sets DateTimeNow. Otherwise on a failed Target attempt the player still gets penalized by the timer.

Code:
using System;

using Server.Network;

using Server.Targeting;

namespace Server.Items

{

public class BearGrease : AddonComponent

{

/*

private static readonly TimeSpan UseDelay = TimeSpan.FromHours(8.0);

private DateTime m_LastClicked;

public DateTime LastClicked { get { return m_LastClicked; } set { m_LastClicked = value; } }

*/

[Constructable]

public BearGrease()

: base(2419)

{

Name = "Bear Grease";

Hue = 0;

}

public BearGrease(Serial serial)

: base(serial)

{

}



public override void Serialize(GenericWriter writer)

{

base.Serialize(writer);

writer.Write((int)0); // version

// writer.Write((int)1); // version

//Version 1 Addition

//writer.WriteDeltaTime((DateTime)m_LastClicked);

}

public override void Deserialize(GenericReader reader)

{

base.Deserialize(reader);

int version = reader.ReadInt();/*

switch (version)

{

case 1:

m_LastClicked = reader.ReadDeltaTime();

goto case 0;

case 0:

break;

*/

}

public override void OnDoubleClick(Mobile m)

{

if (!m.InRange(GetWorldLocation(), 1))

{

m.SendLocalizedMessage(500446); // That is too far away.

return;

}

/*

if (DateTime.Now < m_LastClicked + TimeSpan.FromHours(8.0))

{

m.SendMessage("You must wait awhile longer before you may apply more bear grease.");

return;

}

*/

m.SendMessage("Target an item to fortify.");

m.Target = new BearGreaseTarget();

//LastClicked = DateTime.Now;

return;

}

public class BearGreaseTarget : Target

{

public BearGreaseTarget()

: base(1, false, TargetFlags.None)

{

}

protected override void OnTarget(Mobile from, object targeted)

{

if (targeted is BaseWeapon || targeted is BaseJewel || targeted is BaseClothing || targeted is Item)

{

from.SendMessage("You may only fortify leather armor with Bear Grease.");

}

if ((targeted is BaseArmor && from.Backpack != null))

{

BaseArmor ba = targeted as BaseArmor;

Container pack = from.Backpack;

bool noGo = false;

int antique = 0;

if (!ba.IsChildOf(from.Backpack) && (!Core.ML || ba.Parent != from))

{

from.SendMessage("The item must be in your backpack to fortify it.");

return;

}

if (!(ba.Resource == CraftResource.RegularLeather || ba.Resource == CraftResource.SpinedLeather || ba.Resource == CraftResource.HornedLeather || ba.Resource == CraftResource.BarbedLeather))

{

from.SendMessage("You may only fortify leather armor with Bear Grease.");

return;

}

if (!Server.Engines.Craft.Repair.AllowsRepair(ba, null))

{

from.SendMessage("You cannot fortify that item.");

return;

}

#region SA

if (ba.Attributes.Brittle > 0 || (ba.NegativeAttributes.Brittle > 0))

noGo = true;

antique = ba.NegativeAttributes.Antique;

if (noGo)

{

from.SendLocalizedMessage(1149799); //That cannot be used on brittle items.

return;

}

#endregion

if (targeted is IDurability)

{

IDurability wearable = (IDurability)targeted;

if (!wearable.CanFortify)

{

from.SendMessage("You cannot fortify that item.");

return;

}

if ((ba.IsChildOf(from.Backpack) || (Core.ML && ba.Parent == from)))

{

int origMaxHP = wearable.MaxHitPoints;

int origCurHP = wearable.HitPoints;

if (origMaxHP > 0)

{

//int initMaxHP = Core.AOS ? 255 : wearable.InitMaxHits;

int initMaxHP = antique <= 0 ? 255 : antique == 1 ? 200 : 150;

wearable.UnscaleDurability();

if (wearable.MaxHitPoints < initMaxHP)

{

if (antique > 0)

{

ba.NegativeAttributes.Antique++;

}

int bonus = initMaxHP - wearable.MaxHitPoints;

if (bonus > 3)

bonus = 3;

wearable.MaxHitPoints += bonus;

wearable.HitPoints += bonus;

wearable.ScaleDurability();

if (wearable.MaxHitPoints > 255)

wearable.MaxHitPoints = 255;

if (wearable.HitPoints > 255)

wearable.HitPoints = 255;

if (wearable.MaxHitPoints > origMaxHP)

{

from.SendMessage("You successfully fortify the item.");

from.PlaySound(0x5AB);

}

else

{

wearable.MaxHitPoints = origMaxHP;

wearable.HitPoints = origCurHP;

from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

}

}

else

{

from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

wearable.ScaleDurability();

}

}

else

{

from.SendMessage("You cannot fortify that item.");

}

}

else

{

from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.

}

}

}

}

}

}

}
 
just to be a little easier to read

Code:
using System;

using Server.Network;

using Server.Targeting;

namespace Server.Items
{

    public class BearGrease : AddonComponent
    {

        /*

        private static readonly TimeSpan UseDelay = TimeSpan.FromHours(8.0);

        private DateTime m_LastClicked;

        public DateTime LastClicked { get { return m_LastClicked; } set { m_LastClicked = value; } }

        */

        [Constructable]

        public BearGrease()

            : base(2419)
        {

            Name = "Bear Grease";

            Hue = 0;

        }

        public BearGrease(Serial serial)

            : base(serial)
        {

        }



        public override void Serialize(GenericWriter writer)
        {

            base.Serialize(writer);

            writer.Write((int)0); // version

            // writer.Write((int)1); // version

            //Version 1 Addition

            //writer.WriteDeltaTime((DateTime)m_LastClicked);

        }

        public override void Deserialize(GenericReader reader)
        {

            base.Deserialize(reader);

            int version = reader.ReadInt();/*

switch (version)

{

case 1:

m_LastClicked = reader.ReadDeltaTime();

goto case 0;

case 0:

break;

*/

        }

        public override void OnDoubleClick(Mobile m)
        {

            if (!m.InRange(GetWorldLocation(), 1))
            {

                m.SendLocalizedMessage(500446); // That is too far away.

                return;

            }

            /*

            if (DateTime.Now < m_LastClicked + TimeSpan.FromHours(8.0))

            {

            m.SendMessage("You must wait awhile longer before you may apply more bear grease.");

            return;

            }

            */

            m.SendMessage("Target an item to fortify.");

            m.Target = new BearGreaseTarget();

            //LastClicked = DateTime.Now;

            return;

        }

        public class BearGreaseTarget : Target
        {

            public BearGreaseTarget()

                : base(1, false, TargetFlags.None)
            {

            }

            protected override void OnTarget(Mobile from, object targeted)
            {

                if (targeted is BaseWeapon || targeted is BaseJewel || targeted is BaseClothing || targeted is Item)
                {

                    from.SendMessage("You may only fortify leather armor with Bear Grease.");

                }

                if ((targeted is BaseArmor && from.Backpack != null))
                {

                    BaseArmor ba = targeted as BaseArmor;

                    Container pack = from.Backpack;

                    bool noGo = false;

                    int antique = 0;

                    if (!ba.IsChildOf(from.Backpack) && (!Core.ML || ba.Parent != from))
                    {

                        from.SendMessage("The item must be in your backpack to fortify it.");

                        return;

                    }

                    if (!(ba.Resource == CraftResource.RegularLeather || ba.Resource == CraftResource.SpinedLeather || ba.Resource == CraftResource.HornedLeather || ba.Resource == CraftResource.BarbedLeather))
                    {

                        from.SendMessage("You may only fortify leather armor with Bear Grease.");

                        return;

                    }

                    if (!Server.Engines.Craft.Repair.AllowsRepair(ba, null))
                    {

                        from.SendMessage("You cannot fortify that item.");

                        return;

                    }

                    #region SA

                    if (ba.Attributes.Brittle > 0 || (ba.NegativeAttributes.Brittle > 0))

                        noGo = true;

                    antique = ba.NegativeAttributes.Antique;

                    if (noGo)
                    {

                        from.SendLocalizedMessage(1149799); //That cannot be used on brittle items.

                        return;

                    }

                    #endregion

                    if (targeted is IDurability)
                    {

                        IDurability wearable = (IDurability)targeted;

                        if (!wearable.CanFortify)
                        {

                            from.SendMessage("You cannot fortify that item.");

                            return;

                        }

                        if ((ba.IsChildOf(from.Backpack) || (Core.ML && ba.Parent == from)))
                        {

                            int origMaxHP = wearable.MaxHitPoints;

                            int origCurHP = wearable.HitPoints;

                            if (origMaxHP > 0)
                            {

                                //int initMaxHP = Core.AOS ? 255 : wearable.InitMaxHits;

                                int initMaxHP = antique <= 0 ? 255 : antique == 1 ? 200 : 150;

                                wearable.UnscaleDurability();

                                if (wearable.MaxHitPoints < initMaxHP)
                                {

                                    if (antique > 0)
                                    {

                                        ba.NegativeAttributes.Antique++;

                                    }

                                    int bonus = initMaxHP - wearable.MaxHitPoints;

                                    if (bonus > 3)

                                        bonus = 3;

                                    wearable.MaxHitPoints += bonus;

                                    wearable.HitPoints += bonus;

                                    wearable.ScaleDurability();

                                    if (wearable.MaxHitPoints > 255)

                                        wearable.MaxHitPoints = 255;

                                    if (wearable.HitPoints > 255)

                                        wearable.HitPoints = 255;

                                    if (wearable.MaxHitPoints > origMaxHP)
                                    {

                                        from.SendMessage("You successfully fortify the item.");

                                        from.PlaySound(0x5AB);

                                    }

                                    else
                                    {

                                        wearable.MaxHitPoints = origMaxHP;

                                        wearable.HitPoints = origCurHP;

                                        from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

                                    }

                                }

                                else
                                {

                                    from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

                                    wearable.ScaleDurability();

                                }

                            }

                            else
                            {

                                from.SendMessage("You cannot fortify that item.");

                            }

                        }

                        else
                        {

                            from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.

                        }

                    }

                }

            }

        }

    }

}
 
try this, i commented // edit for delay where i made changes

Code:
using System;

using Server.Network;

using Server.Targeting;

namespace Server.Items
{

    public class BearGrease : AddonComponent
    {

        /*

        private static readonly TimeSpan UseDelay = TimeSpan.FromHours(8.0);

        private DateTime m_LastClicked;

        public DateTime LastClicked { get { return m_LastClicked; } set { m_LastClicked = value; } }

        */

        [Constructable]

        public BearGrease()

            : base(2419)
        {

            Name = "Bear Grease";

            Hue = 0;

        }

        public BearGrease(Serial serial)

            : base(serial)
        {

        }



        public override void Serialize(GenericWriter writer)
        {

            base.Serialize(writer);

            writer.Write((int)0); // version

            // writer.Write((int)1); // version

            //Version 1 Addition

            //writer.WriteDeltaTime((DateTime)m_LastClicked);

        }

        public override void Deserialize(GenericReader reader)
        {

            base.Deserialize(reader);

            int version = reader.ReadInt();/*

switch (version)

{

case 1:

m_LastClicked = reader.ReadDeltaTime();

goto case 0;

case 0:

break;

*/

        }

        public override void OnDoubleClick(Mobile m)
        {

            if (!m.InRange(GetWorldLocation(), 1))
            {

                m.SendLocalizedMessage(500446); // That is too far away.

                return;

            }

            /*

            if (DateTime.Now < m_LastClicked + TimeSpan.FromHours(8.0))

            {

            m.SendMessage("You must wait awhile longer before you may apply more bear grease.");

            return;

            }

            */

            m.SendMessage("Target an item to fortify.");

            m.Target = new BearGreaseTarget(this);

            //LastClicked = DateTime.Now;

            return;

        }

        public class BearGreaseTarget : Target
        {
            private BearGrease _bearGrease; // edit for delay

            public BearGreaseTarget(BearGrease beargrease) // edit for delay

                : base(1, false, TargetFlags.None)
            {
                _bearGrease = beargrease; // edit for delay
            }

            protected override void OnTarget(Mobile from, object targeted)
            {

                if (targeted is BaseWeapon || targeted is BaseJewel || targeted is BaseClothing || targeted is Item)
                {

                    from.SendMessage("You may only fortify leather armor with Bear Grease.");

                }

                if ((targeted is BaseArmor && from.Backpack != null))
                {

                    BaseArmor ba = targeted as BaseArmor;

                    Container pack = from.Backpack;

                    bool noGo = false;

                    int antique = 0;

                    if (!ba.IsChildOf(from.Backpack) && (!Core.ML || ba.Parent != from))
                    {

                        from.SendMessage("The item must be in your backpack to fortify it.");

                        return;

                    }

                    if (!(ba.Resource == CraftResource.RegularLeather || ba.Resource == CraftResource.SpinedLeather || ba.Resource == CraftResource.HornedLeather || ba.Resource == CraftResource.BarbedLeather))
                    {

                        from.SendMessage("You may only fortify leather armor with Bear Grease.");

                        return;

                    }

                    if (!Server.Engines.Craft.Repair.AllowsRepair(ba, null))
                    {

                        from.SendMessage("You cannot fortify that item.");

                        return;

                    }

                    #region SA

                    if (ba.Attributes.Brittle > 0 || (ba.NegativeAttributes.Brittle > 0))

                        noGo = true;

                    antique = ba.NegativeAttributes.Antique;

                    if (noGo)
                    {

                        from.SendLocalizedMessage(1149799); //That cannot be used on brittle items.

                        return;

                    }

                    #endregion

                    if (targeted is IDurability)
                    {

                        IDurability wearable = (IDurability)targeted;

                        if (!wearable.CanFortify)
                        {

                            from.SendMessage("You cannot fortify that item.");

                            return;

                        }

                        if ((ba.IsChildOf(from.Backpack) || (Core.ML && ba.Parent == from)))
                        {

                            int origMaxHP = wearable.MaxHitPoints;

                            int origCurHP = wearable.HitPoints;

                            if (origMaxHP > 0)
                            {

                                //int initMaxHP = Core.AOS ? 255 : wearable.InitMaxHits;

                                int initMaxHP = antique <= 0 ? 255 : antique == 1 ? 200 : 150;

                                wearable.UnscaleDurability();

                                if (wearable.MaxHitPoints < initMaxHP)
                                {

                                    if (antique > 0)
                                    {

                                        ba.NegativeAttributes.Antique++;

                                    }

                                    int bonus = initMaxHP - wearable.MaxHitPoints;

                                    if (bonus > 3)

                                        bonus = 3;

                                    wearable.MaxHitPoints += bonus;

                                    wearable.HitPoints += bonus;

                                    wearable.ScaleDurability();

                                    if (wearable.MaxHitPoints > 255)

                                        wearable.MaxHitPoints = 255;

                                    if (wearable.HitPoints > 255)

                                        wearable.HitPoints = 255;

                                    if (wearable.MaxHitPoints > origMaxHP)
                                    {

                                        from.SendMessage("You successfully fortify the item.");
                                       
                                        _bearGrease.LastClicked = DateTime.Now; // edit for delay

                                        from.PlaySound(0x5AB);

                                    }

                                    else
                                    {

                                        wearable.MaxHitPoints = origMaxHP;

                                        wearable.HitPoints = origCurHP;

                                        from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

                                    }

                                }

                                else
                                {

                                    from.SendLocalizedMessage(1049085); // The item cannot be improved any further.

                                    wearable.ScaleDurability();

                                }

                            }

                            else
                            {

                                from.SendMessage("You cannot fortify that item.");

                            }

                        }

                        else
                        {

                            from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.

                        }

                    }

                }

            }

        }

    }

}
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back