Minecraft-Console-Client/MinecraftClient/CommandHandler/CmdResult.cs
breadbyte 8756ff5b3c
[skip ci] Miscellaneous scripting QoL improvements and fixes (#2740)
* Update CI to detect the word "skipci"

* Make script compilation errors more verbose

Rather than just giving the line in which the error has been found, return the actual text content of the line itself

* Attempt to bubble up errors in the script chain, so it says the reason for any NotRun errors.

The exception message gets eaten up when the script is running, and an exception happens.

Also put in a default result message for the CmdResult, instead of having it default to null.

* Trim the whitespace off returned script compilation error line
2024-06-22 06:40:23 +08:00

96 lines
3 KiB
C#

using System;
namespace MinecraftClient.CommandHandler
{
public class CmdResult
{
public static readonly CmdResult Empty = new();
internal static McClient? currentHandler;
public enum Status
{
NotRun = int.MinValue,
FailChunkNotLoad = -4,
FailNeedEntity = -3,
FailNeedInventory = -2,
FailNeedTerrain = -1,
Fail = 0,
Done = 1,
}
public CmdResult()
{
this.status = Status.NotRun;
this.result = "Command did not run, cannot determine the result of the command.";
}
public Status status;
public string? result;
public int SetAndReturn(Status status)
{
this.status = status;
this.result = status switch
{
#pragma warning disable format // @formatter:off
Status.NotRun => "Command did not run, cannot determine the result of the command.",
Status.FailChunkNotLoad => null,
Status.FailNeedEntity => Translations.extra_entity_required,
Status.FailNeedInventory => Translations.extra_inventory_required,
Status.FailNeedTerrain => Translations.extra_terrainandmovement_required,
Status.Fail => Translations.general_fail,
Status.Done => null,
_ => null,
#pragma warning restore format // @formatter:on
};
return Convert.ToInt32(this.status);
}
public int SetAndReturn(Status status, string? result)
{
this.status = status;
this.result = result;
return Convert.ToInt32(this.status);
}
public int SetAndReturn(int code, string? result)
{
this.status = (Status)Enum.ToObject(typeof(Status), code);
if (!Enum.IsDefined(typeof(Status), status))
throw new InvalidOperationException($"{code} is not a legal return value.");
this.result = result;
return code;
}
public int SetAndReturn(bool result)
{
this.status = result ? Status.Done : Status.Fail;
this.result = result ? Translations.general_done : Translations.general_fail;
return Convert.ToInt32(this.status);
}
public int SetAndReturn(string? result)
{
this.status = Status.Done;
this.result = result;
return Convert.ToInt32(this.status);
}
public int SetAndReturn(string? resultstr, bool result)
{
this.status = result ? Status.Done : Status.Fail;
this.result = resultstr;
return Convert.ToInt32(this.status);
}
public override string ToString()
{
if (result != null)
return result;
else
return status.ToString();
}
}
}