1
0
Files
named-binary-tag/Nbt/Tag/NbtValue.cs
2024-03-15 14:44:21 +01:00

65 lines
2.0 KiB
C#
Executable File

namespace Nbt.Tag;
public interface INbtValue : INbtTag
{
object Value { get; set; }
}
public interface INbtValue<T> : INbtValue where T : notnull
{
new T Value { get; set; }
}
public abstract class NbtValue<T> : INbtValue<T> where T : notnull
{
private static readonly EqualityComparer<T> ValueComparer = EqualityComparer<T>.Default;
protected T value;
internal protected NbtValue(T value) : base() => this.value = value;
public abstract NbtTagType Type { get; }
INbtTag INbtTag.this[string tagName] { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
INbtTag INbtTag.this[int tagIndex] { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
INbtTag INbtTag.this[INbtPathElement tagPath] { get => throw new NotSupportedException(); set => throw new NotSupportedException(); }
INbtList INbtTag.AsList() => throw new NotSupportedException();
INbtCompound INbtTag.AsCompound() => throw new NotSupportedException();
INbtValue INbtTag.AsValue() => this;
INbtValue<U> INbtTag.AsValue<U>() => (INbtValue<U>)this;
INbtArray INbtTag.AsArray() => (INbtArray)this;
INbtArray<U> INbtTag.AsArray<U>() => (INbtArray<U>)this;
INbtTag INbtTag.Copy() => Copy();
public NbtValue<T> Copy() => NewInstance(CopyValue());
object INbtValue.Value
{
get => Value;
set => Value = (T)Convert.ChangeType(value, typeof(T));
}
public T Value
{
get => value;
set => this.value = value ?? throw new ArgumentNullException(nameof(value));
}
protected virtual T CopyValue() => value;
protected abstract NbtValue<T> NewInstance(T value);
public bool Equals(INbtTag? other) => ReferenceEquals(other, this) || other is NbtValue<T> o && ValueComparer.Equals(value, o.value);
public override bool Equals(object? obj) => ReferenceEquals(obj, this) || obj is NbtValue<T> other && ValueComparer.Equals(value, other.value);
public override int GetHashCode() => value.GetHashCode();
}