It is currently July 16th, 2018, 12:20 pm


All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: How to respawn a player
PostPosted: October 2nd, 2012, 1:08 pm 

Joined: October 2nd, 2012, 1:01 pm
Posts: 6
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:
        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:
   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.


Top
Offline Profile  
Reply with quote  
 Post subject: Re: How to respawn a player
PostPosted: October 2nd, 2012, 6:49 pm 

Joined: August 21st, 2011, 6:31 am
Posts: 280
Location: United Kingdom
This topic may help you: viewtopic.php?f=40&t=1112

_________________
You cannot use certain BBCodes: [img].


Top
Offline Profile  
Reply with quote  
 Post subject: Re: How to respawn a player
PostPosted: October 2nd, 2012, 6:58 pm 

Joined: August 21st, 2011, 6:31 am
Posts: 280
Location: United Kingdom
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:
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:
 //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:
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].


Top
Offline Profile  
Reply with quote  
 Post subject: Re: How to respawn a player
PostPosted: October 2nd, 2012, 10:13 pm 

Joined: October 2nd, 2012, 1:01 pm
Posts: 6
Works perfectly! Thanks heaps :)


Top
Offline Profile  
Reply with quote  
 Post subject: Re: How to respawn a player
PostPosted: October 3rd, 2012, 2:18 am 

Joined: August 21st, 2011, 6:31 am
Posts: 280
Location: United Kingdom
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].


Top
Offline Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Theme created StylerBB.net