So im using this global chat and today imade some changes, and it crashed im not sure wheres the null reference:


Error:


Code:
Exception:
System.NullReferenceException: Object reference not set to an instance of an object.
   at Felladrin.Engines.GlobalChat.Broadcast(Mobile sender, String message) in c:\Users\1\2\Scripts\GlobalChat.cs:line 147
   at Felladrin.Engines.GlobalChat.OnCommandChat(CommandEventArgs e) in c:\Users\1\2\Scripts\GlobalChat.cs:line 113
   at Server.Commands.CommandSystem.Handle(Mobile from, String text, MessageType type)
   at Server.Mobiles.PlayerMobile.DoSpeech(String text, Int32[] keywords, MessageType type, Int32 hue) in c:\Users\1\2\Scripts\Mobiles\PlayerMobile.cs:line 5062
   at Server.Engines.PartySystem.Chat3Guild.UnicodeSpeechChat3(NetState state, PacketReader pvSrc) in c:\Users\1\2\Scripts\Government System\Knives Chat 3.0 Beta 9\General\Chat3Guild.cs:line 122
   at Server.Network.MessagePump.HandleReceive(NetState ns)
   at Server.Network.MessagePump.Slice()
   at Server.Core.Main(String[] args)



The line number from crash log may not be correct, so if it says 146 maybe its 147 or 148


Code:
// Global Chat v1.4.0
// Author: Felladrin
// Started: 2013-07-03
// Updated: 2016-01-06

using System.Collections.Generic;
using Server;
using Server.Accounting;
using Server.Commands;
using Server.Gumps;
using Server.Mobiles;
using Server.Factions;
using Server.Misc;
using Server.Network;
using Server.Items;
using Server.Guilds;

namespace Felladrin.Engines
{
    public static class GlobalChat
    {
        public static class Config
        {
            public static bool Enabled = true;               // Is this system enabled?
            public static bool OpenHistoryOnLogin = true;    // Should we display the history when player logs in?
            public static bool AutoColoredNames = true;      // Should we auto color the players names?
            public static int HistorySize = 50;              // How many messages should we keep in the history?
            public static int MessageHue = /*1154*/2548;             // What is the hue of the chat messages?
			public static int MessageHuePK = /*1154*/1955;
			public static int MessageHuecrim = /*1154*/1154;
			public static int MessageHuestaff = /*1154*/1965;
        }

        public static void Initialize()
        {
            if (Config.Enabled)
            {
                CommandSystem.Register("Chaton", AccessLevel.Player, new CommandEventHandler(OnCommandToggle));
                CommandSystem.Register("Chathistoria", AccessLevel.Player, new CommandEventHandler(OnCommandHistory));
                CommandSystem.Register("C", AccessLevel.Player, new CommandEventHandler(OnCommandChat));
                EventSink.Login += OnLogin;
            }
        }

        static readonly List<string> History = new List<string>();

        static HashSet<int> DisabledPlayers = new HashSet<int>();

        [Usage("ChatToggle")]
        [Description("Enables or Disables the Chat.")]
        static void OnCommandToggle(CommandEventArgs e)
        {
            var pm = e.Mobile as PlayerMobile;
            var acc = pm.Account as Account;

            if (acc.GetTag("Chat") == null || acc.GetTag("Chat") == "Enabled")
            {
                DisabledPlayers.Add(pm.Serial.Value);
                acc.SetTag("Chat", "Disabled");
                pm.SendMessage(38, "Has desactivado el chat para tu cuenta.");

                if (pm.HasGump(typeof(ChatHistoryGump)))
                    pm.CloseGump(typeof(ChatHistoryGump));
            }
            else
            {
                DisabledPlayers.Remove(pm.Serial.Value);
                acc.SetTag("Chat", "Enabled");
                pm.SendMessage(68, "Has activado el chat para tu cuenta.");
                pm.SendGump(new ChatHistoryGump());
            }
        }

        [Usage("ChatHistory")]
        [Description("Opens the Chat History.")]
        static void OnCommandHistory(CommandEventArgs e)
        {
            var pm = e.Mobile as PlayerMobile;

            if (DisabledPlayers.Contains(pm.Serial.Value))
            {
                pm.SendMessage(38, "El chat esta desactivado para tu cuenta escribe .chaton para activarlo.");
                return;
            }

            if (pm.HasGump(typeof(ChatHistoryGump)))
                pm.CloseGump(typeof(ChatHistoryGump));

            pm.SendGump(new ChatHistoryGump());
        }

        [Usage("C <message>")]
        [Description("Broadcasts a message to all players online. If no message is provided, it opens the Chat History.")]
        static void OnCommandChat(CommandEventArgs e)
        {
            var pm = e.Mobile as PlayerMobile;

            if (DisabledPlayers.Contains(pm.Serial.Value))
            {
                pm.SendMessage(38, "El chat esta desactivado para tu cuenta escribe .chaton para activarlo.");
                return;
            }

            if (e.ArgString.Length == 0)
            {
                if (pm.HasGump(typeof(ChatHistoryGump)))
                    pm.CloseGump(typeof(ChatHistoryGump));

                pm.SendGump(new ChatHistoryGump());
            }
            else
            {
                Broadcast(e.Mobile, e.ArgString);
            }
        }

        static void OnLogin(LoginEventArgs e)
        {
            var pm = e.Mobile as PlayerMobile;
            var acc = pm.Account as Account;

            if (acc.GetTag("Chat") == "Disabled")
            {
                DisabledPlayers.Add(pm.Serial.Value);
                pm.SendMessage("Chat desactivado para tu cuenta.");
            }
            else
            {
                pm.SendMessage("Chat activado para tu cuenta.");

                if (Config.OpenHistoryOnLogin)
                    pm.SendGump(new ChatHistoryGump());
            }
        }

        static void Broadcast(Mobile sender, string message)
        {
            if (History.Count > Config.HistorySize)
                History.RemoveAt(0);

            History.Add(string.Format("[{0}] <basefont color=#{1}>{2}<basefont color=white> {3}", System.DateTime.UtcNow.ToString("HH:mm"), (Config.AutoColoredNames ? (sender.Name.GetHashCode()>>8).ToString() : "FFF"), sender.Name, Utility.FixHtml(message)));
#region chat
            foreach (NetState ns in NetState.Instances)
            {
                var player = ns.Mobile as PlayerMobile;
				var guild = sender.Guild as Guild;
                if (player.Squelched == true || player == null || DisabledPlayers.Contains(player.Serial.Value))
                    continue;

					 if (sender != null)
				{
					 Container pack = sender.Backpack;	
                   if (pack == null)
                    return;
					 Item keyyp = sender.Backpack.FindItemByType(typeof(Keypuente));
					 Item keyyew = sender.Backpack.FindItemByType(typeof(Keyyew));*/
				
				  if (sender.AccessLevel >= AccessLevel.Seer)   
			  {
				   player.SendMessage(Config.MessageHuestaff, string.Format("<{0}> (Stf) {1}", sender.Name, message));
			  }
				 else if (sender.Female && keyyew != null && sender.Kills >= 5)
			   {
                 player.SendMessage(Config.MessageHuePK, string.Format("<{0}> [test] {1}", sender.Name, message));
			   }
			     else if (sender.Female && keyyew != null)
			   {
                 player.SendMessage(Config.MessageHue, string.Format("<{0}> [test] {1}", sender.Name, message));
			   }
			
			   else if (keyyew != null)
			   {
                 player.SendMessage(Config.MessageHue, string.Format("<{0}> [test] {1}", sender.Name, message));
			   }
			
			   else  if (sender.Female && keyyp != null && sender.Kills >= 5)
			   {
                 player.SendMessage(Config.MessageHuePK, string.Format ("<{0}> [test] {1}", sender.Name, message));
			   }
			     else  if (sender.Female && keyyp != null)
			   {
                 player.SendMessage(Config.MessageHue, string.Format ("<{0}> [test] {1}", sender.Name, message));
			   }

			   else if (keyyp != null && sender.Kills >= 5)
			   {
                 player.SendMessage(Config.MessageHuePK, string.Format ("<{0}> [test] {1}", sender.Name, message));
			   }
			    else if (keyyp != null)
			   {
                 player.SendMessage(Config.MessageHue, string.Format ("<{0}> [test] {1}", sender.Name, message));
			   }
			   else if (sender.Female && sender.Kills >= 5 && sender.Guild != null && sender.Guild.Type == GuildType.Chaos)    //CHAOS PK
			  {
				   player.SendMessage(Config.MessageHuePK, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			   else if (sender.Kills >= 5 && sender.Guild != null && sender.Guild.Type == GuildType.Chaos)    //CHAOS PK
			  {
				   player.SendMessage(Config.MessageHuePK, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			
			     else if (sender.Guild != null && sender.Guild != null && sender.Guild.Type == GuildType.Chaos)    //CHAOS
			  {
			  player.SendMessage(Config.MessageHue, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			
			   else if (sender.Guild != null && sender.Guild != null && sender.Guild.Type == GuildType.Order)    //Order
			  {
			  player.SendMessage(Config.MessageHue, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			  else if (sender.Female && sender.Guild != null && sender.Guild != null && sender.Guild.Type == GuildType.Order)    //Order
			  {
			  player.SendMessage(Config.MessageHuePK, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			  else if (sender.Guild != null && sender.Guild != null && sender.Guild.Type == GuildType.Order)    //Order
			  {
			  player.SendMessage(Config.MessageHue, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			   else if (sender.Female && sender.Kills >= 5)    //PK
			  {
				   player.SendMessage(Config.MessageHuePK, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			   else if (sender.Kills >= 5)    //PK
			  {
				   player.SendMessage(Config.MessageHuePK, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			   else if(sender.Criminal == true)
			  {
				   player.SendMessage(Config.MessageHuecrim, string.Format("<{0}> (test) {1}", sender.Name, message));
			  }
			  else
			  {
				  player.SendMessage(Config.MessageHue, string.Format("<{0}> {1}", sender.Name, message));
			  }
				}
				
			 if (player != null && player.guild != null && player.guild != null && player.guild.Type == Guilds.GuildType.Chaos)
				{
           Item keyy = player.Backpack.FindItemByType(typeof(Keyyew));
    	if (keyy != null)
    	{
                player.SendMessage(Config.MessageHue, string.Format("<{0}> {test} {1}", sender.Name, message));
		}
				}
				else if (player != null && player.Guild != null && player.Guild != null && player.Guild.Type == Guilds.GuildType.Order)
				{
           Item keyy = player.Backpack.FindItemByType(typeof(Keyyew));
    	if (keyy != null)
    	{
                player.SendMessage(Config.MessageHue, string.Format("<{0}> {test} {1}", sender.Name, message));
		}
				}
				else if (player != null && player.Guild != null && player.Guild != null && player.Guild.Type == Guild.GuildType.Chaos)
				{
           Item keyy = player.Backpack.FindItemByType(typeof(Keyyew));
    	if (keyy != null)
    	{
                player.SendMessage(Config.MessageHue, string.Format("<{0}> {test} {1}", sender.Name, message));
		}
				}
				else if (player != null && player.Guild != null && player.Guild != null && player.Guild.Type == Guilds.GuildType.Order)
				{
           Item keyy = player.Backpack.FindItemByType(typeof(Keyyew));
    	if (keyy != null)
    	{
                player.SendMessage(Config.MessageHue, string.Format("<{0}> {Order} {1}", sender.Name, message));
		}
				}
				else
				player.SendMessage(Config.MessageHue, string.Format("<{0}> {1}", sender.Name, message));
				}
				
                if (player.HasGump(typeof(ChatHistoryGump)))
                {
                    player.CloseGump(typeof(ChatHistoryGump));
                    player.SendGump(new ChatHistoryGump());
                }
            }
			#endregion
        }

        static string GenerateHistoryHTML()
        {
            if (History.Count == 0)
                return "No hay mensajes.";

            string HTML = "";

            foreach (string msg in History)
                HTML = msg + " <br/>" + HTML;

            return HTML;
        }

        public class ChatHistoryGump : Gump
        {
            public ChatHistoryGump() : base(110, 100)
            {
                Closable = true;
                Dragable = true;
                Disposable = true;
                Resizable = false;

                AddPage(0);
                AddBackground(0, 0, 420, 250, 5054);
                AddImageTiled(10, 10, 400, 20, 2624);
                AddAlphaRegion(10, 10, 400, 20);
                AddLabel(15, 10, 73, "Ultimos Mensajes " + Config.HistorySize + " mensajes");
                AddImageTiled(10, 40, 400, 200, 2624);
                AddAlphaRegion(10, 40, 400, 200);
                AddHtml(15, 40, 395, 200, GenerateHistoryHTML(), false, true);
            }
        }
    }
}
 
Last edited:
if (player.Squelched == true || player == null

Try reversing these maybe, putting the null check ahead of the Squelched check. If player IS null, then trying to access Squelched field would probably cause that crash, so likely better to check for null first.
 
Try reversing these maybe, putting the null check ahead of the Squelched check. If player IS null, then trying to access Squelched field would probably cause that crash, so likely better to check for null first.
thank you, will do
 
I would use the following because if both conditions, on either side of the operator '&&' are true then it will execute the block of code within the brackets; this is what you are looking for because you want to check both sides of that '&&' operator:

Code:
if (player != null && player.Squelched == true)
{
}

One last thing using the operator '!=' means: "not equal to".



I wouldn't use the following code because it is saying if the player is squelched, OR if the player is equal to null (meaning the player is not in the game world) then execute the block of code within the brackets. The problem is that if the left side of the operator '||' is true, then the right side is never checked; You want both sides of whatever operator you use to get checked.

Code:
if(player.Squelched == true || player == null)
{
}

One last thing using the operator '==' means: "is equal to".

 
Last edited:
Back