Jump to content

Simple server-time code + fix GetTime (C#)


jix

Recommended Posts

Once I faced a problem with NAPI.World.GetTime and didn't find on it the adequate answer. Can anyone help my code. Bonus will make a simple server time system

class server_side : Script
    {
        // The global variables that will be replace our GetTime
        int serverHour = 6;
        int serverMin = 0;
        int serverSec = 0;

		// A method that, when accessed, will change the time on the server
        private void server_Time(int hour, int minute, int second)
        {
            NAPI.World.SetTime(hour, minute, second);
            serverHour = hour; // ----------------------------------
            serverMin = minute; // save in our variable time
            serverSec = second; // ---------------------------------
        }

		// Bonus code that creates server time (1 server minute = 20 seconds)
        async void time()
        {
                while (true)
                {
                    serverMin++;
                    NAPI.World.SetTime(serverHour, serverMin, 0);
                    if (serverMin == 60)
                    {
                        if (serverHour >= 23)
                        {
                            serverHour = 0;
                            serverMin = 0;
                            NAPI.World.SetTime(serverHour, serverMin, 0);
                        }
                        else
                        {
                            serverHour++;
                            serverMin = 0;
                            NAPI.World.SetTime(serverHour, serverMin, 0);
                        }
                    }
                    await Task.Delay(20000);
                }
        }

        [Command("time")] // command to know the current time on the server
        public void CMD_time(Client client)
        {
            if (serverMin < 10)
            {
                NAPI.Chat.SendChatMessageToPlayer(client, $"Current time {serverHour.ToString()}:0{serverMin.ToString()}");
            }
            else
            {
                NAPI.Chat.SendChatMessageToPlayer(client, $"Current time {serverHour.ToString()}:{serverMin.ToString()}");
            }
        }
                               
        // Change the time on the server
        [Command("settime")]
        public void CMD_settime(Client client, int hrs, int min)
        {
            server_Time(hrs, min, 0);
            NAPI.Chat.SendChatMessageToPlayer(client, $"Time was change to {hrs}:{min}");
        }
        // Event that the time worked
		[ServerEvent(Event.ResourceStart)]
        public void OnResourceStart()
        {
            NAPI.World.SetTime(serverHour, serverMin, serverSec);
            time(); // Cycle start time at the start of the server
        }

 

sorry for my russian english xd i hope this will help u ❤️

Edited by jix
Link to comment
Share on other sites

  • 7 months later...

ok here when you change the ResourceStart event to:

// Event that the time worked
        [ServerEvent(Event.ResourceStart)]
        public void OnResourceStart()
        {
            int seconds = DateTime.Now.Second;
            int minute = DateTime.Now.Minute;
            int hour = DateTime.Now.Hour;
            server_Time(hour, minute, seconds);

            NAPI.World.SetTime(serverHour, serverMin, serverSec);
            time(); // Cycle start time at the start of the server
        }

then you have Realtime system, i mean its save on start the Time from Windows(not testet on linux)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...