MCCTeam / Minecraft-Console-Client

Lightweight console for Minecraft chat and automated scripts
https://mccteam.github.io
Other
1.67k stars 402 forks source link

[Idea] Script Alarm Bot #2800

Open farfromsubtlee opened 1 month ago

farfromsubtlee commented 1 month ago

Prerequisites

Console Client Version

Latest

Describe your problem

Hello, I looked a little bit including closed topics, but I couldn't see a script that will detect in-game time. Is this possible? What I want to do is “send me a private message when the in-game time is 12:00”. It would be correct to call it an alarm.

Suggest a possible solution

Since I don't know any command or coding to detect an in-game clock like timeValue setting, maybe it can be handled with .cs.

Attach screenshot here (If applicable)

No response

Minecraft Version

1.16.5 / 1.20.4

Device

Desktop

Operating System

Windows

milutinke commented 3 weeks ago

Here is an example that I made for someone else on Discord a couple of months ago, you can modify it for your needs.

// MCCScript 1.0
MCC.LoadBot(new CheckForSleep());

// MCCScript Extensions
public class CheckForSleep : ChatBot
{
    private DateTime nextTaskRun = DateTime.Now;
    private long timeOfDay;

    public override void OnTimeUpdate(long worldAge, long timeOfDay)
    {
        this.timeOfDay = timeOfDay;
    }

    public override void Update()
    {
        var dateNow = DateTime.Now;

        if (nextTaskRun < dateNow)
        {
            var (hours, minutes) = GetCurrentInGameTime();

            if (hours == 22)
            {
                PerformInternalCommand("sleep");

                // Schedule next check to run in 15 minutes
                nextTaskRun = dateNow.AddMinutes(15);
                return;
            }

            // Schedule next check to run in 1 minute
            nextTaskRun = dateNow.AddMinutes(1);
        }
    }

    private (int hours, int minutes) GetCurrentInGameTime()
    {
        var time = this.timeOfDay;

        if (time < 0) 
            time *= -1;

        time %= 24000;

        var hours = 6 + (int)(time / 1000);

        if (hours >= 24) 
            hours -= 24;

        double minutesDouble = (double)(time % 1000) / 1000 * 60;
        int minutes = (int)Math.Round(minutesDouble);

        if (minutes == 60)
        {
            minutes = 0;
            hours++;
        }

        return (hours, minutes);
    }
}