src/AnvilPacker/Data/NBT.cs
/// <summary>  Gets a tag, or the value of a primitive tag. </summary>
/// <param name="mode">The action to take if the tag doesn't exist. </param>
Gets a tag, or the value of a primitive tag.
Gets a tag or the value of a primitive tag .
public T Get<T>(string name, TagGetMode mode = TagGetMode.Throw)
        {
            if (_tags.TryGetValue(name, out NbtTag? tag)) {
                if (typeof(NbtTag).IsAssignableFrom(typeof(T))) {
                    return (T)(object)tag;
                }
                if (tag is PrimitiveTag prim) {
                    return prim.Value<T>();
                }
                throw new InvalidCastException($"Cannot cast tag '{name}' value from {tag.Type} to {typeof(T)}");
            }
            switch (mode) {
                default:
                case TagGetMode.Throw:
                    throw new KeyNotFoundException($"Tag '{name}' not found.");
                case TagGetMode.Null: 
                    return default!;
                case TagGetMode.Create: {
                    if (!typeof(NbtTag).IsAssignableFrom(typeof(T))) {
                        throw new InvalidOperationException("Mode cannot be 'Create' for primitive values.");
                    }
                    var val = Activator.CreateInstance<T>()!;
                    _tags.Add(name, (NbtTag)(object)val);
                    return val;
                }
            }
        }
public T Get < T > ( string name , TagGetMode mode = TagGetMode . Throw ) { if ( _tags . TryGetValue ( name , out NbtTag ? tag ) ) { if ( typeof ( NbtTag ) . IsAssignableFrom ( typeof ( T ) ) ) { return ( T ) ( object ) tag ; } if ( tag is PrimitiveTag prim ) { return prim . Value < T > ( ) ; } throw new InvalidCastException ( $" Cannot cast tag ' { name } ' value from  { tag . Type }  to  { typeof ( T ) } " ) ; } switch ( mode ) { default : case TagGetMode . Throw : throw new KeyNotFoundException ( $" Tag ' { name } ' not found. " ) ; case TagGetMode . Null : return default ! ; case TagGetMode . Create : { if ( ! typeof ( NbtTag ) . IsAssignableFrom ( typeof ( T ) ) ) { throw new InvalidOperationException ( " " ) ; } var val = Activator . CreateInstance < T > ( ) ! ; _tags . Add ( name , ( NbtTag ) ( object ) val ) ; return val ; } } }
Gets a tag, or the value of a primitive tag.
Gets a tag or the value of a primitive tag .
name
string
mode
TagGetMode
The action to take if the tag doesn't exist.
The action to take if the tag doesn ' t exist .