1. Hi!

    I would like to get the name of a player in OnPlayerInit(Baseplayer player) and pass it to {player.name} in a message in the lang file of my plugin, which is then loaded when the message is displayed with the player's name.

    I am unsure of how to do this even after referencing a few plugins out there. Would some one mind suggesting how I could do this or point me in the right direction?


    Cheers
     
  2. There are multiple way's to do it, I use the first method because I am lazy and can't be bothered always writing replace :p

    Code:
                string testmessage = "{0} has joined the server";
                PrintToChat(string.Format(testmessage, player.displayName)); // string.Format replaces {0}, {1}, {2} etc with the supplied arguments. In this case I am supplying 1 arg which is the players name            string furthertestmessage = "{0} {1} {2}";
                PrintToChat(string.Format(furthertestmessage, arg1, arg2, arg3));            string othermessage = "{playername} has joined the server";
                PrintToChat(othermessage.Replace("{playername}", player.displayName)); // Using the replace function finds and replaces the 1st arg with the second.            string furtherexample = "{attacker} has killed {victim}";
                PrintToChat(furtherexample.Replace("{attacker}", attacker.displayName).Replace("{victim}", victim.displayName)); // The replace function can be used multiple times on 1 string
     
  3. Ah so simple! I have seen that being used before but didn't think to use it for this. Thank's @k1lly0u

    I just found the string displayName in Assembly-CSharp.dll too.
     
    Last edited by a moderator: May 20, 2016
  4. Wulf

    Wulf Community Admin

    Don't forgot string interpolation:
    Code:
    PrintToChat($"{player.displayName} has joined the server");
     
  5. That is the preferred way but I didn't mention it because he was talking about strings via Lang API:p
     
  6. Wulf

    Wulf Community Admin

    In that case for multiple messages, this would help:
    Code:
    string Lang(string key, string id = null, params object[] args) => string.Format(lang.GetMessage(key, this, id), args);
    ...
    PrintToChat(Lang("MyMessage", player.UserIDString, player.displayName));
     
  7. I basically just did:
    Code:
    CreatePrivateMsgGUI(welcomeMessageText.Replace("{playername}", player.displayName), bannerTintColor, textColor, player);
    Cheers guys.

    I now just need to figure out how to wait until the player's sleep ends after joining before I send the GUI to them, any ideas?
     
  8. Wulf

    Wulf Community Admin

    OnPlayerInit maybe? I don't think we have a hook for OnPlayerSleep yet.
     
  9. OnPlayerInit is what I am using at the moment, and was wandering if there would be a way to wait until the player wakes up rather than delaying the GUI creation 10 seconds (If it is created instantly it is too early and you won't see it). I know there is void OnPlayerSleepEnded but that will end up sending a welcome GUI message every time they wake up rather than after joining the server (obviously).

    Maybe I could do something with OnPlayerInit setting bool joined = true and OnPlayerSleepEnded checking if joined = true then proceeding with creating the welcome GUI message after which setting joined = false?
     
  10. Wulf

    Wulf Community Admin

    Adding a timer would usually cover most cases.
     
  11. While it does I know some players will join then leave themselves sleeping for a while at which point the timer for destroying the GUI message will have already ran. I might see what I can do with what I mentioned. :p
    [DOUBLEPOST=1463772382][/DOUBLEPOST]Hmm with the bool true until the player wakes up, it will create issues when multiple players are joining. I don't think it is possible to sort of "append" a variable to a player.
    [DOUBLEPOST=1463951871,1463769810][/DOUBLEPOST]
    Code:
    if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
                    {
                        timer.Once(2, () => OnPlayerInit(player));
                        return;
                    }
    This works like a charm, I have debugging "Puts" messages to tell me at what part in the code it is on, and it circles on the "Player Connected" message just before the above code, after I finished receiving data it wen't through all the other "Puts" messages through the whole GUI creation process and I saw the GUI as soon as I woke up.


    EDIT:
    This does everything I wanted to have done.


    Code:
    void OnPlayerInit(BasePlayer player)
            {
                if(welcomeAnnouncement)
                {
                    WelcomeAnnouncement(player);
                }
            }
         
    void WelcomeAnnouncement(BasePlayer player)
            {
                if (player.HasPlayerFlag(BasePlayer.PlayerFlags.ReceivingSnapshot))
                {
                    timer.Once(2, () => WelcomeAnnouncement(player));
                    return;
                }
                if (player.HasPlayerFlag(BasePlayer.PlayerFlags.Sleeping))
                {
                    timer.Once(2, () => WelcomeAnnouncement(player));
                    return;
                }
                destroyPrivateGUI(player);
                string bannerTintColor = bannerTintGrey;
                string textColor = textWhite;
                CreatePrivateMsgGUI(welcomeAnnouncementText.Replace("{playername}", player.displayName), bannerTintColor, textColor, player);
            }
    Checks if the player is still at receiving data before checking if they are sleeping. Then as soon as they wake up it is created. Perfect.
     
    Last edited by a moderator: May 22, 2016
  12. Just a suggestion for what I would have done: I would have made a list, and when a player joins he gets added to that list. Using the hook OnPlayerSleepEnded it will check if he is the list. If so then do stuff, otherwise do nothing.
     
  13. Wulf

    Wulf Community Admin

    I can't believe I missed that... I was thinking he wanted to detect when they were sleeping. :p
     
  14. That would probably result in the plugin processing a little less. Thanks :p
     
  15. Yeah lol. I was not gonna comment but when I saw the code he posted(last portion) I realized it was what I used to do and that always used way too much processing power. Plus that code is much more effective!

    No problem bud :)
     
  16. I got your suggestion set up and working wonderfully :) Cheers.