How to respawn a player

For all mod-related questions and custom code.
Post Reply
00pol
Offline
Posts: 6
Joined: October 2nd, 2012, 1:01 pm

How to respawn a player

Post by 00pol »

Hello! I'm creating a mod for my own personal server in fCraft, and I want people to have changeable names (in the player list). However, this means I need to respawn players. I'm used to doing it in MCForge, but respawning players in fCraft seems to be more of a challenge.

Here is my code to change the players name and respawn them

Code: Select all

        private string _DisplayName;

        /// <summary> When changed spawns the player with a new name </summary>
        public string DisplayName
        {
            get { 
                if (!String.IsNullOrEmpty(_DisplayName))
                    return _DisplayName;
                else
                    return ListName;
            }
            set
            {
                _DisplayName = value;
                if (HasFullyConnected)
                {
                    RemoveEntity(this);
                    AddEntity(this);
                }
            }
        }
Note that I have also made the changes when spawning to use DisplayName from ListName. However, I get this when I change it:

On line 1369 of Player.Networking.cs: KeyNotFoundException "The given key was not present in the dictionary."

Code: Select all

   at System.ThrowHelper.ThrowKeyNotFoundException()
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at fCraft.Player.RemoveEntity(Player player) in C:\Users\Me\FCraft\fCraft\Network\Player.Networking.cs:line 1369
   at fCraft.Player.set_DisplayName(String value) in C:\Users\Me\FCraft\fCraft\Player\Player.cs:line 159
   at fCraft.Mod.Main.ChangeName(Player e) in C:\Users\Me\FCraft\fCraft\Mod\Main.cs:line 208
   at fCraft.Scheduler.MainLoop() in C:\Users\Me\FCraft\fCraft\System\Scheduler.cs:line 72
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
May I please get some assistance?

Thanks for any help.

Jonty800
Offline
Posts: 280
Joined: August 21st, 2011, 6:31 am
Location: United Kingdom
Contact:

Re: How to respawn a player

Post by Jonty800 »

You cannot use certain BBCodes: [img].

Jonty800
Offline
Posts: 280
Joined: August 21st, 2011, 6:31 am
Location: United Kingdom
Contact:

Re: How to respawn a player

Post by Jonty800 »

Here is my command for /Impersonate - a command which does this. Sometimes the skin doesn't update, so I need to rewrite it one day when I get time, but hopefully this will help

Command:

Code: Select all

static readonly CommandDescriptor CdImpersonate = new CommandDescriptor
        {
            Name = "Impersonate",
            Category = CommandCategory.Moderation | CommandCategory.Fun,
            IsConsoleSafe = true,
            Permissions = new[] { Permission.EditPlayerDB },
            Help = "&HChanges to players skin to a desired name. " + 
            "If no playername is given, all changes are reverted. " + 
            "Note: The name above your head changes too",
            Usage = "/Impersonate PlayerName",
            Handler = ImpersonateHandler
        };

        static void ImpersonateHandler(Player player, Command cmd)
        {
            //entityChanged should be set to true for the skin update to happen in real time
            string iName = cmd.Next();
            if (iName == null && player.iName == null){
                CdImpersonate.PrintUsage(player);
                return;
            }
            if (iName == null){
                player.iName = null;
                player.entityChanged = true;
                player.Message("&SAll changes have been removed and your skin has been updated");
                return;
            }
            //ignore isvalidname for percent codes to work
            if (player.iName == null){
                player.Message("&SYour name has changed from '" + player.Info.Rank.Color + player.Name + "&S' to '" + iName);
            }
            if (player.iName != null){
                player.Message("&SYour name has changed from '" + player.iName + "&S' to '" + iName);
            }
            player.iName = iName;
            player.entityChanged = true;
        }
Player.cs additions and modifications:

Code: Select all

 //used for impersonation (skin changing)
        //if null, default skin is used
        public string iName = null;
        public bool entityChanged = false;

[NotNull]
        public string ListName {
            get {
                string displayedName = Name;
                if (iName != null) displayedName = Color.ReplacePercentCodes(iName); //impersonate
                if( ConfigKey.RankPrefixesInList.Enabled() ) {
                    displayedName = Info.Rank.Prefix + displayedName;
                }
                if( ConfigKey.RankColorsInChat.Enabled() && Info.Rank.Color != Color.White && iName == null ) {
                    displayedName = Info.Rank.Color + displayedName;
                }
                return displayedName;
            }
        }

Player.Networking addition:

Code: Select all

VisibleEntity entity;
                // if Player has a corresponding VisibleEntity
                if( entities.TryGetValue( otherPlayer, out entity ) ) {
                    entity.MarkedForRetention = true;

                    if( entity.LastKnownRank != otherPlayer.Info.Rank) {
                        ReAddEntity( entity, otherPlayer, otherPos );
                        entity.LastKnownRank = otherPlayer.Info.Rank;
                    }
					if( otherPlayer.entityChanged ){
						ReAddEntity( entity, otherPlayer, otherPos );
                            otherPlayer.entityChanged = false;

					}
You cannot use certain BBCodes: [img].

00pol
Offline
Posts: 6
Joined: October 2nd, 2012, 1:01 pm

Re: How to respawn a player

Post by 00pol »

Works perfectly! Thanks heaps :)

Jonty800
Offline
Posts: 280
Joined: August 21st, 2011, 6:31 am
Location: United Kingdom
Contact:

Re: How to respawn a player

Post by Jonty800 »

It may seem perfect, but it seems to work 9 times out of 10.

I'll post a more reliable solution when I write one :)
You cannot use certain BBCodes: [img].

Post Reply