Ignore command (need to finish)

Ignore command (need to finish)

Postby ismellike » 01 Jun 2013, 21:43

Ok so I'm not entirely sure what's going on here.

I get this error
Code: Select all
Type: NullReferenceException
Source: Cmdignore
Message: Object reference not set to an instance of an object.
Target: Use
Trace:    at MCDzienny.CmdIgnore.Use(Player p, String message)
   at MCDzienny.Player.<>c__DisplayClass26.<HandleCommand>b__21()


I've tried many things but I just keep ending up with that error.
It happens when I do /ignore remove.

Here is the code to the ignore command (not finished)

Code: Select all
using System;
namespace MCDzienny
{
    public class CmdIgnore : Command
    {
        ChatType current;
        public override string name { get { return "ignore"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        public override bool ConsoleAccess { get { return false; } }
        public override void Init()
        {
            Player.ChatOther += new EventHandler<ChatOtherEventArgs>(Player_ChatOther);
        }
        public void Player_ChatOther(object sender, ChatOtherEventArgs e)
        {
            switch (current)
            {
                case ChatType.CrossMaps:
                    if (e.To.ExtraData.ContainsKey("ignore"))
                    {
                        if (!e.To.ExtraData["ignore"].ToString().Contains(e.From.PublicName))
                        {
                            Player.SendMessage(e.To, e.Message);
                        }
                    }
                    return;
                case ChatType.Map:
                    if (e.To.level == e.From.level)
                    {
                        if (e.To.ExtraData.ContainsKey("ignore"))
                        {
                            if (!e.To.ExtraData["ignore"].ToString().Contains(e.From.PublicName))
                            {
                                Player.SendMessage(e.To, e.Message);
                            }
                        }
                    }
                    return;
            }
        }
        public override void Use(Player p, string message)
        {
            if (!p.ExtraData.ContainsKey("ignore"))
            {
                p.ExtraData.Add("ignore", "");
            }
            if (message == "list")
            {
                Player.SendMessage(p, "Ignore List %a-----");
                Player.SendMessage(p, (string)p.ExtraData["ignore"]);
                Player.SendMessage(p, "Ignore List %a-----");
            }
            else if (message == "remove")
            {
                Player who;
                try
                {
                    who = Player.Find(message.Split(' ')[1]);
                }
                catch
                {
                    Help(p);
                    return;
                }
                if (p.ExtraData["ignore"].ToString().Contains(who.PublicName))
                {
                    string edit = (string)p.ExtraData["ignore"];
                    Player.SendMessage(p, "string");
                    p.ExtraData["ignore"] = edit.Replace(who.PublicName + ",", "");
                    Player.SendMessage(p, "second");
                }

                else
                {
                    Player.SendMessage(p, who.PublicName + " is not on your ignore list.");
                }
            }
            else
            {
                Player who;
                try
                {
                    who = Player.Find(message);
                }
                catch
                {
                    Help(p);
                    return;
                }
                if (!p.ExtraData["ignore"].ToString().Contains(who.PublicName))
                {
                    p.ExtraData["ignore"] = who.PublicName + ", " + p.ExtraData["ignore"];
                    Player.SendMessage(p, who.PublicName + " is now on your ignore list.");
                }
                else
                {
                    Player.SendMessage(p, who.PublicName + " is already on your ignore list.");
                }
            }
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/ignore list -- returns all of the people you have ignored");
        }
    }
}
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ignore command (need to finish)

Postby lucasds12 » 01 Jun 2013, 21:49

Maybe the method you used for "/ignore remove" doesn't seem to exist in MCDzienny.
-Lucas
There is only one thing I do in life, that's contributing here.
lucasds12
 
Posts: 334
Joined: 17 Apr 2013, 16:17
Location: In the deep caves.

Re: Ignore command (need to finish)

Postby Conor » 01 Jun 2013, 21:50

If you typed '/ignore remove asdakjnsku324' then it would proceed with the Player object 'who' with a name of 'asdakjnsku324'.

However, if this player doesn't exist, and you're trying to call its name and other variables, then this could be where your error stems from. I'm not sure if this is the case, but you can put some more code in there to prevent this and see if it helps.

So just slap in some code like:
Code: Select all
if (who == null || who.hidden)
{
   Player.SendMessage(p, "Player could not be found.");
   return;
}


Put it in the relevant position and hopefully this may help.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Ignore command (need to finish)

Postby ismellike » 01 Jun 2013, 22:00

It gives me the error even when the person is both online and on the ignore list.
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ignore command (need to finish)

Postby Conor » 01 Jun 2013, 22:54

ismellike wrote:It gives me the error even when the person is both online and on the ignore list.


Does the error not give you a line number?
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Ignore command (need to finish)

Postby ismellike » 01 Jun 2013, 22:58

No the error happens when I use /ignore remove [name]
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ignore command (need to finish)

Postby Warren1001 » 01 Jun 2013, 22:59

Conor wrote:
ismellike wrote:It gives me the error even when the person is both online and on the ignore list.


Does the error not give you a line number?

This in the "error" section on the console, the error log thing is.
It's not in the little pop-up compiler error thing.
Warren1001
 
Posts: 197
Joined: 08 Aug 2012, 03:50

Re: Ignore command (need to finish)

Postby ismellike » 01 Jun 2013, 23:07

/facepalm wow I just figured it out,

the (message=="remove") is supposed to be (message.Split(' ')[0]=="remove")
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Re: Ignore command (need to finish)

Postby Conor » 01 Jun 2013, 23:09

ismellike wrote:/facepalm wow I just figured it out,

the (message=="remove") is supposed to be (message.Split(' ')[0]=="remove")


Haha, bugs can be the simplest of things yet the most annoying to find eh.
Conor (Conanza121)
User avatar
Conor
Coder
 
Posts: 390
Joined: 10 Oct 2012, 21:36
Location: @21Conor

Re: Ignore command (need to finish)

Postby ismellike » 01 Jun 2013, 23:26

Anyone want to finish :roll:

I think it's just the message part.

When you ignore and then unignore it sends messages 2x.

Code: Select all
using System;
namespace MCDzienny
{
    public class CmdIgnore : Command
    {
        ChatType current;
        public override string name { get { return "ignore"; } }
        public override string shortcut { get { return ""; } }
        public override string type { get { return "other"; } }
        public override bool museumUsable { get { return false; } }
        public override LevelPermission defaultRank { get { return LevelPermission.Guest; } }
        public override bool ConsoleAccess { get { return false; } }
        public override void Init()
        {
            Player.ChatOther += new EventHandler<ChatOtherEventArgs>(Player_ChatOther);
        }
        public void Player_ChatOther(object sender, ChatOtherEventArgs e)
        {
            switch (current)
            {
                case ChatType.CrossMaps:
                    if (e.To.ExtraData.ContainsKey("ignore"))
                    {
                        if (!e.To.ExtraData["ignore"].ToString().Contains(e.From.PublicName))
                        {
                            Player.SendMessage(e.To, e.Message);
                        }
                    }
                    return;
                case ChatType.Map:
                    if (e.To.level == e.From.level)
                    {
                        if (e.To.ExtraData.ContainsKey("ignore"))
                        {
                            if (!e.To.ExtraData["ignore"].ToString().Contains(e.From.PublicName))
                            {
                                Player.SendMessage(e.To, e.Message);
                            }
                        }
                    }
                    return;
                case ChatType.Whisper:
                    if (e.To.ExtraData.ContainsKey("ignore"))
                    {
                        if (!e.To.ExtraData["ignore"].ToString().Contains(e.From.PublicName))
                        {
                            Player.SendMessage(e.To, e.Message);
                        }
                    }
                    return;
            }
        }
        public override void Use(Player p, string message)
        {
            string split0 = message.Split(' ')[0];
            if (!p.ExtraData.ContainsKey("ignore"))
            {
                p.ExtraData.Add("ignore", "");
            }
            if (split0 == "list")
            {
                Player.SendMessage(p, "Ignore List %a-----");
                Player.SendMessage(p, (string)p.ExtraData["ignore"]);
                Player.SendMessage(p, "Ignore List %a-----");
            }
            else if (split0 == "remove")
            {
                Player who;
                try
                {
                    who = Player.Find(message.Split(' ')[1]);
                }
                catch
                {
                    Help(p);
                    return;
                }
                if (who == null)
                {
                    Player.SendMessage(p, message.Split(' ')[1] + " is offline.");
                }
                else if (p.ExtraData["ignore"].ToString().Contains(who.PublicName))
                {
                    string edit = (string)p.ExtraData["ignore"];
                    p.ExtraData["ignore"] = edit.Replace(who.PublicName + ",", "");
                    Player.SendMessage(p, who.PublicName + " was taken off your ignore list.");
                }

                else
                {
                    Player.SendMessage(p, who.PublicName + " is not on your ignore list.");
                }
            }
            else
            {
                Player who;
                try
                {
                    who = Player.Find(message.Split(' ')[0]);
                }
                catch
                {
                    Help(p);
                    return;
                }
                if (who == null)
                {
                    Player.SendMessage(p, message.Split(' ')[0] + " is offline.");
                }
                else if (!p.ExtraData["ignore"].ToString().Contains(who.PublicName))
                {
                    p.ExtraData["ignore"] = who.PublicName + ", " + p.ExtraData["ignore"];
                    Player.SendMessage(p, who.PublicName + " is now on your ignore list.");
                }
                else
                {
                    Player.SendMessage(p, who.PublicName + " is already on your ignore list.");
                }
            }
        }
        public override void Help(Player p)
        {
            Player.SendMessage(p, "/ignore list -- returns all of the people you have ignored");
        }
    }
}
What a beast...
User avatar
ismellike
Coder
 
Posts: 731
Joined: 31 Oct 2012, 04:04
Location: Kansas

Next

Return to Help in Coding

Who is online

Users browsing this forum: No registered users and 6 guests