Exemple #1
0
        public NBT MakeNBT(JToken jToken)
        {
            switch (jToken.Type)
            {
            case JTokenType.Null:
                return(null);

            case JTokenType.Object:
                Dictionary <string, NBT> objChildren = new Dictionary <string, NBT>();
                foreach (KeyValuePair <string, JToken> child in (JObject)jToken)
                {
                    string key        = child.Key;
                    NBT    childValue = MakeNBT(child.Value, ref key);
                    if (childValue != null)
                    {
                        objChildren.Add(key, childValue);
                    }
                }
                return(new NBTCompund(objChildren));

            case JTokenType.Array:
                List <NBT> arrChildren = new List <NBT>();
                foreach (JToken child in (JArray)jToken)
                {
                    NBT childValue = MakeNBT(child);
                    if (childValue != null)
                    {
                        arrChildren.Add(childValue);
                    }
                }
                return(new NBTList(arrChildren));

            case JTokenType.Integer:
                return(new NBTInt((int)(long)((JValue)jToken).Value));

            case JTokenType.Float:
                return(new NBTDouble((double)((JValue)jToken).Value));

            case JTokenType.String:
                return(new NBTString((string)((JValue)jToken).Value));

            default:
                return(null);
            }
        }
Exemple #2
0
 private List <NBT> TryMakeNBTListType(JToken jToken, Func <JToken, NBT> tryMakeRawJsonNBT)
 {
     if (jToken.Type == JTokenType.Array)
     {
         List <NBT> arrChildren = new List <NBT>();
         foreach (JToken child in (JArray)jToken)
         {
             NBT childValue = tryMakeRawJsonNBT(child);
             if (childValue != null)
             {
                 arrChildren.Add(childValue);
             }
         }
         return(arrChildren);
     }
     else
     {
         throw new FormatException($"<{jToken.Path}> value must be array.");
     }
 }
Exemple #3
0
        private NBT MakeNBT(JToken jToken, ref string key)
        {
            Match      matche   = Regex.Match(key, "^_([^_]+)_(.+)$");
            JTokenType jsonType = jToken.Type;
            NBT        nbt      = null;

            switch (matche.Groups[1].Value)
            {
            case "TAGByte":
                nbt = TryMakeNBTBtye(jToken);
                break;

            case "TAGShort":
                nbt = TryMakeNBTShort(jToken);
                break;

            case "TAGInt":
                nbt = TryMakeNBTInt(jToken);
                break;

            case "TAGLong":
                nbt = TryMakeNBTLong(jToken);
                break;

            case "TAGFloat":
                nbt = TryMakeNBTFloat(jToken);
                break;

            case "TAGDouble":
                nbt = TryMakeNBTDouble(jToken);
                break;

            case "TAGByteArray":
                nbt = new NBTByteArray(TryMakeNBTListType(jToken, TryMakeNBTBtye));
                break;

            case "TAGIntArray":
                nbt = new NBTIntArray(TryMakeNBTListType(jToken, TryMakeNBTInt));
                break;

            case "TAGLongArray":
                nbt = new NBTLongArray(TryMakeNBTListType(jToken, TryMakeNBTLong));
                break;

            case "RawJson":
                nbt = TryMakeRawJsonNBT(jToken);
                break;

            case "RawJsonArray":
                nbt = new NBTList(TryMakeNBTListType(jToken, TryMakeRawJsonNBT));
                break;

            case "RawJsonFile":
                nbt = TryMakeRawJsonFileNBT(jToken);
                break;

            case "RawJsonFileArray":
                nbt = new NBTList(TryMakeNBTListType(jToken, TryMakeRawJsonFileNBT));
                break;
            }
            if (nbt == null)
            {
                return(MakeNBT(jToken));
            }
            else
            {
                key = matche.Groups[2].Value;
                return(nbt);
            }
        }