MCCTeam / Minecraft-Console-Client

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

cs script says must return type #555

Closed Twi5TeD closed 5 years ago

Twi5TeD commented 6 years ago

`//MCCScript 1.0

MCC.LoadBot(new RejoinHandler());

//MCCScript Extensions class RejoinHandler : ChatBot { System.Diagnostics.Stopwatch stopwatch; static int hours = 60 60 1000; int maxSessionLength = hours / 30; int reconnectTimeout = 2; string server = play.server.net; bool andReconnect = true; bool result = Settings.SetServerIP(server);

public override void AfterGameJoined()
{
    LogToConsole("Rejoin handler loaded.");
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
}

public override Update()
{
    try
    {
        if (stopwatch.ElapsedMilliseconds > maxSessionLength)
        {
            LogToConsole("It's been " + maxSessionLength.ToString() + " milliseconds, attempting to relog to server.");
            stopwatch.Restart();
        if (result && andReconnect)
            ReconnectToTheServer();
        return result;
        }
    }
}

}`

this is the script that i want to do it will reconnect every alt back every 60 seconds(just for testing atm) but the error i get is [MCC] [Script] Script 'rejoin.cs' failed to run (LoadError). [MCC] [Script] System.InvalidOperationException: Method must have a return type

TheSnoozer commented 6 years ago

Instead of public override Update() use public override void Update() as done in here

Twi5TeD commented 6 years ago

now i get [MCC] [Script] Script 'rejoin.cs' failed to run (LoadError). [MCC] [Script] System.InvalidOperationException: Expected catch or finally

Twi5TeD commented 6 years ago

changed script to this now i get `//MCCScript 1.0

MCC.LoadBot(new RejoinHandler());

//MCCScript Extensions class RejoinHandler : ChatBot { System.Diagnostics.Stopwatch stopwatch; static int hours = 60 60 1000; int maxSessionLength = hours / 30; int reconnectTimeout = 2; string server = play.server.net; bool andReconnect = true; bool result = Settings.SetServerIP(server);

public override void AfterGameJoined()
{
    LogToConsole("Rejoin handler loaded.");
    stopwatch = new System.Diagnostics.Stopwatch();
    stopwatch.Start();
}

public override void Update()
{
    try
    {
        if (stopwatch.ElapsedMilliseconds > maxSessionLength)
        {
            LogToConsole("It's been " + maxSessionLength.ToString() + " milliseconds, attempting to relog to server.");
            stopwatch.Restart();
        if (result && andReconnect)
            ReconnectToTheServer();
        return result;
        }
    }
    catch (Exception e)
    {
        LogToConsole("Something went wrong in Update method. " + e.ToString());
    }
}

}`

[MCC] [Script] Script 'rejoin.cs' failed to run (LoadError). [MCC] [Script] System.InvalidOperationException: Since 'ScriptLoader.Script.RejoinHandler.Update()' returns void, a return keyword must not be followed by an object expression

TheSnoozer commented 6 years ago

Why do you try to return result; in Update()?

The error that you get is telling you the following: your method Update() doesn't declare a return type (that's the void). However by using return result; inside the method Update() you attempt to return a bool and thus C# is complaining.

Either remove return result; (which is kinda pointless) or change the method to public override bool Update(). Not sure if the second approach would even work, but you can simply test that out.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/methods#return-values

Twi5TeD commented 6 years ago

public override bool Update() this wouldnt work and i need it to return result how else could i do it?

TheSnoozer commented 6 years ago

Lets start with what do you want to achieve? And why do you think you need bool result = Settings.SetServerIP(server);?

Twi5TeD commented 6 years ago

so i want my list of alts to rejoin the server every couple hours but sometimes they wont connect so instead i want to restart the console client instead

TheSnoozer commented 6 years ago

Why would you even try to write a c# script for that? What you need and want is the Auto Relog Bot.

Inside your MinecraftClient.ini put for example the following:

[AutoRelog]
enabled=true
delay=10
retries=-1 #-1 = unlimited
kickmessagesfile=kickmessages.txt

inside your kickmessages.txt:

Connection has been lost
Server is restarting
Server is full
too many people
disconnect.spam
You seem to be logged in already.n
Internal Exception: java.io.IOException: An existing connection was forcibly closed by the remote host.
...

Or whatever kickmessages your server has. Search on google and youtube for tutorials full configuration examples (not really hard to find).

Twi5TeD commented 6 years ago

already have this but sometimes the alts just stay at the hub on rejoin does execute the commands i have in the anti afk part

TheSnoozer commented 6 years ago

Try adding a ScriptScheduler or AutoRespond

via ScriptScheduler

MinecraftClient.ini

[ScriptScheduler]
enabled=true
tasksfile=tasks.ini

tasks.ini

[Task]
triggerOnInterval=true
triggerOnFirstLogin=true
timeInterval=60
script=joinyourserver.txt

joinyourserver.txt

send /server [REALM]

via AutoRespond

MinecraftClient.ini

[AutoRespond]
enabled=true
matchesfile=matches.ini

matches.ini

[Match]
match=--------------------[Hub]--------------------
actionother=script joinyourserver
# or simply actionother=send /server [REALM]
Twi5TeD commented 6 years ago

is there a placeholder to get the ign of the player so i can use matches.ini?

match=Welcome, %ign% to My Server!

TheSnoozer commented 6 years ago

Yes, see https://github.com/ORelio/Minecraft-Console-Client/blob/master/MinecraftClient/config/sample-matches.ini

[Match]
match=hi
action=send hi, $u!
actionprivate=send /tell $u Hello!
actionother=log detected "hi" message
ownersonly=false
ORelio commented 5 years ago

As a response has been given, I'll close this thread. Feel free to reopen if you still need help.