blazing-console/Types/Location.cs
2024-12-23 15:37:42 -05:00

37 lines
No EOL
1.4 KiB
C#

using System.Text.Json;
using System.Text.Json.Serialization;
namespace blazingconsole.Types
{
[JsonConverter(typeof(LocationJsonConverter))]
public class Location(string worldName, double x, double y, double z, float yaw, float pitch)
{
public string WorldName { get; set; } = worldName;
public double X { get; set; } = x;
public double Y { get; set; } = y;
public double Z { get; set; } = z;
public float Yaw { get; set; } = yaw;
public float Pitch { get; set; } = pitch;
}
public class LocationJsonConverter : JsonConverter<Location>
{
public override void Write(Utf8JsonWriter writer, Location value, JsonSerializerOptions options)
{
if (value == null) {
writer.WriteNullValue();
} else {
writer.WriteStringValue($"{value.WorldName} {value.X} {value.Y} {value.Z} {value.Yaw} {value.Pitch}");
}
}
public override Location? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) {
var value = reader.GetString();
if (value == null) {
return null;
}
var parts = value.Split(' ');
return new Location(parts[0], double.Parse(parts[1]), double.Parse(parts[2]), double.Parse(parts[3]), float.Parse(parts[4]), float.Parse(parts[5]));
}
}
}