Cleanup, ordering and added host variable to client script
This commit is contained in:
22
MinecraftDiscordBot/Models/Fluid.cs
Normal file
22
MinecraftDiscordBot/Models/Fluid.cs
Normal file
@ -0,0 +1,22 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes a fluid in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Fluid {
|
||||
[JsonProperty("amount", Required = Required.Always)]
|
||||
public int Amount { get; set; }
|
||||
[JsonProperty("displayName", Required = Required.Always)]
|
||||
public string DisplayName { get; set; } = default!;
|
||||
[JsonProperty("tags", Required = Required.DisallowNull)]
|
||||
public string[]? Tags { get; set; } = default;
|
||||
[JsonProperty("name", Required = Required.Always)]
|
||||
public ModItemId ItemId { get; set; } = default!;
|
||||
public override string ToString() => Amount > 10000
|
||||
? $"{Amount / 1000.0f:n2} B of {DisplayName}"
|
||||
: $"{Amount:n0} mB of {DisplayName}";
|
||||
[JsonIgnore]
|
||||
public string CleanDisplayName => DisplayName[1..^1];
|
||||
}
|
14
MinecraftDiscordBot/Models/Item.cs
Normal file
14
MinecraftDiscordBot/Models/Item.cs
Normal file
@ -0,0 +1,14 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonObject(MemberSerialization.OptIn, Description = "Describes an item in a Refined Storage system.", MissingMemberHandling = MissingMemberHandling.Ignore)]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Item : Fluid {
|
||||
[JsonProperty("fingerprint", Required = Required.Always)]
|
||||
public Md5Hash Fingerprint { get; set; } = default!;
|
||||
[JsonProperty("nbt", Required = Required.DisallowNull)]
|
||||
public dynamic? NBT { get; set; }
|
||||
public override string ToString() => $"{Amount:n0}x {DisplayName}";
|
||||
}
|
34
MinecraftDiscordBot/Models/Md5Hash.cs
Normal file
34
MinecraftDiscordBot/Models/Md5Hash.cs
Normal file
@ -0,0 +1,34 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonConverter(typeof(Md5JsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class Md5Hash : IEquatable<Md5Hash?> {
|
||||
private readonly byte[] _hash;
|
||||
public Md5Hash(string hash) : this(Convert.FromHexString(hash)) { }
|
||||
public Md5Hash(byte[] hash) {
|
||||
if (hash is not { Length: 16 }) throw new ArgumentException("Invalid digest size!", nameof(hash));
|
||||
_hash = hash;
|
||||
}
|
||||
public override bool Equals(object? obj) => Equals(obj as Md5Hash);
|
||||
public bool Equals(Md5Hash? other) => other != null && _hash.SequenceEqual(other._hash);
|
||||
public override int GetHashCode() {
|
||||
var hashCode = new HashCode();
|
||||
hashCode.AddBytes(_hash);
|
||||
return hashCode.ToHashCode();
|
||||
}
|
||||
public override string ToString() => Convert.ToHexString(_hash);
|
||||
|
||||
public class Md5JsonConverter : JsonConverter<Md5Hash> {
|
||||
public override Md5Hash? ReadJson(JsonReader reader, Type objectType, Md5Hash? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string { Length: 32 } value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse MD5 hash with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, Md5Hash? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
65
MinecraftDiscordBot/Models/Message.cs
Normal file
65
MinecraftDiscordBot/Models/Message.cs
Normal file
@ -0,0 +1,65 @@
|
||||
using Discord.WebSocket;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
public abstract class Message {
|
||||
[JsonProperty("type")]
|
||||
public abstract string Type { get; }
|
||||
}
|
||||
|
||||
public class CapabilityMessage : Message {
|
||||
public override string Type => "roles";
|
||||
[JsonProperty("role", Required = Required.Always)]
|
||||
public string[] Role { get; set; } = default!;
|
||||
}
|
||||
|
||||
public class TextMessage : Message {
|
||||
public TextMessage(SocketMessage arg) : this(arg.Author.Username, arg.Content) { }
|
||||
public TextMessage(string author, string content) {
|
||||
Author = author;
|
||||
Content = content;
|
||||
}
|
||||
public override string Type => "text";
|
||||
[JsonProperty("author", Required = Required.Always)]
|
||||
public string Author { get; set; }
|
||||
[JsonProperty("message", Required = Required.Always)]
|
||||
public string Content { get; set; }
|
||||
}
|
||||
|
||||
public class ReplyMessage : Message {
|
||||
public ReplyMessage(int answerId, string result) {
|
||||
AnswerId = answerId;
|
||||
Result = result;
|
||||
}
|
||||
[JsonProperty("id", Required = Required.Always)]
|
||||
public int AnswerId { get; set; }
|
||||
[JsonProperty("result", Required = Required.Always)]
|
||||
public string Result { get; set; }
|
||||
[JsonProperty("chunk", Required = Required.DisallowNull)]
|
||||
public int Chunk { get; set; } = 1;
|
||||
[JsonProperty("total", Required = Required.DisallowNull)]
|
||||
public int Total { get; set; } = 1;
|
||||
/// <summary>
|
||||
/// If at least one packet was received where
|
||||
/// </summary>
|
||||
[JsonProperty("success", Required = Required.DisallowNull)]
|
||||
public bool Success { get; set; } = true;
|
||||
public override string Type => "reply";
|
||||
}
|
||||
|
||||
public class RequestMessage : Message {
|
||||
public RequestMessage(int answerId, string method, Dictionary<string, object>? parameters = null) {
|
||||
AnswerId = answerId;
|
||||
Method = method;
|
||||
Parameters = (parameters ?? Enumerable.Empty<KeyValuePair<string, object>>())
|
||||
.ToDictionary(i => i.Key, i => i.Value);
|
||||
}
|
||||
[JsonProperty("id")]
|
||||
public int AnswerId { get; set; }
|
||||
[JsonProperty("method")]
|
||||
public string Method { get; set; }
|
||||
[JsonProperty("params")]
|
||||
public Dictionary<string, object> Parameters { get; }
|
||||
public override string Type => "request";
|
||||
}
|
30
MinecraftDiscordBot/Models/ModItemId.cs
Normal file
30
MinecraftDiscordBot/Models/ModItemId.cs
Normal file
@ -0,0 +1,30 @@
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace MinecraftDiscordBot.Models;
|
||||
|
||||
[JsonConverter(typeof(ModItemIdJsonConverter))]
|
||||
[DebuggerDisplay($"{{{nameof(ToString)}(),nq}}")]
|
||||
public class ModItemId {
|
||||
public ModItemId(string name) {
|
||||
var colon = name.IndexOf(':');
|
||||
if (colon < 0) throw new ArgumentException("Invalid mod item id!", nameof(name));
|
||||
ModName = name[..colon];
|
||||
ModItem = name[(colon + 1)..];
|
||||
if (ToString() != name) throw new InvalidProgramException("Bad Parsing!");
|
||||
}
|
||||
public override string ToString() => $"{ModName}:{ModItem}";
|
||||
public string ModName { get; }
|
||||
public string ModItem { get; }
|
||||
|
||||
public class ModItemIdJsonConverter : JsonConverter<ModItemId> {
|
||||
public override ModItemId? ReadJson(JsonReader reader, Type objectType, ModItemId? existingValue, bool hasExistingValue, JsonSerializer serializer)
|
||||
=> reader.Value is string value
|
||||
? new(value)
|
||||
: throw new JsonException($"Could not parse mod name with token '{reader.Value}'");
|
||||
public override void WriteJson(JsonWriter writer, ModItemId? value, JsonSerializer serializer) {
|
||||
if (value is null) writer.WriteNull();
|
||||
else writer.WriteValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user