Blame view

mcplayerstat.c 3.41 KB
Imanol-Mikel Barba Sabariego authored
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <stdio.h>

#include "nbt.h"

void printInventory(TagList *inv) {
    for(int j = 0; j < inv->size; ++j) {
        TagCompound item = *(TagCompound*)inv->list[j].payload;
        uint8_t slot = 0;
        char* id;
        uint8_t count = 0;
        for(int k = 0; k < item.numTags; ++k) {
            Tag attr = item.list[k];
            if(!strncmp(attr.name,"Slot",attr.nameLength)) {
                slot = *((uint8_t*)attr.payload);
            } else if(!strncmp(attr.name,"id",attr.nameLength)) {
Imanol-Mikel Barba Sabariego authored
16
17
                id = calloc(attr.payloadLength+1,sizeof(char));
                memcpy(id,(char*)attr.payload,attr.payloadLength);
Imanol-Mikel Barba Sabariego authored
18
19
20
21
22
            } else if(!strncmp(attr.name,"Count",attr.nameLength)) {
                count = *((uint8_t*)attr.payload);
            }
        }
        printf("\t- [%u] %ux %s\n",slot,count,id);
Imanol-Mikel Barba Sabariego authored
23
        free(id);
Imanol-Mikel Barba Sabariego authored
24
25
26
    }
}
Imanol-Mikel Barba Sabariego authored
27
28
29
30
31
32
33
/*
 * int playerGameType
 * int XpLevel
 * list(compound) Inventory
 * list(compound) EnderItems
 * list(float) Pos
 */
Imanol-Mikel Barba Sabariego authored
34
35
int parseMCPlayerFile(const char* filename) {
    unsigned int pos = 0;
Imanol-Mikel Barba Sabariego authored
36
    void* data;
Imanol-Mikel Barba Sabariego authored
37
Imanol-Mikel Barba Sabariego authored
38
    ssize_t dblen = loadDB(filename, &data);
39
40
41
42
43
44
45
46
47
48
49
    if(dblen < 0) {
        switch(dblen) {
            case ACCESS_ERROR:
                fprintf(stderr,"Error calling access() on database %s\n",filename);
                break;
            case OPEN_ERROR:
                fprintf(stderr,"Error opening database %s\n",filename);
                break;
            case READ_ERROR:
                fprintf(stderr,"Error reading database %s\n",filename);
                break;
Imanol-Mikel Barba Sabariego authored
50
        }
51
52
53
54
55
56
57
58
59
60
61
        return dblen;
    }
    Tag t;
    pos = parseTag((data + pos),&t);
    if(pos < 0) {
        fprintf(stderr,"Error parsing root tag: code %d\n",pos);
        return pos;
    } else if(pos != dblen) {
        fprintf(stderr,"Didn't reach end of NBT file\n");
        return 2;
    }
Imanol-Mikel Barba Sabariego authored
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
    TagCompound root = *(TagCompound*) t.payload;
    for(int i = 0; i < root.numTags; ++i) {
        Tag node = root.list[i];
        if(node.type == TAG_INT) {
            if(!strncmp(node.name,"playerGameType",node.nameLength)) {
                int playerGameType = *((uint32_t*)node.payload);
                printf("playerGameType: %s\n",(playerGameType == 0 ? "Survival" : (playerGameType == 1 ? "Creative" : (playerGameType == 2 ? "Adventure" : (playerGameType == 3 ? "Spectator" : "UNKNOWN")))));
            } else if (!strncmp(node.name,"XpLevel",node.nameLength)) {
                int xpLevel = *((uint32_t*)node.payload);
                printf("XpLevel: %u\n",xpLevel);
            }
        } else if(node.type == TAG_LIST) {
            TagList* l = (TagList*)node.payload;
            if(!strncmp(node.name,"Inventory",node.nameLength)) {
                printf("Inventory:\n");
                printInventory(l);
            } else if (!strncmp(node.name,"EnderItems",node.nameLength)) {
                printf("Ender Inventory:\n");
                printInventory(l);
            } else if (!strncmp(node.name,"Pos",node.nameLength)) {
                double x = (*(double*)l->list[0].payload);
                double y = (*(double*)l->list[1].payload);
                double z = (*(double*)l->list[2].payload);
                printf("Pos: (x: %f,y: %f,z: %f)\n",x,y,z);
Imanol-Mikel Barba Sabariego authored
87
88
89
            }
        }
    }
90
91
92
93

    destroyTag(&t);
    free(data);
    return 0;
Imanol-Mikel Barba Sabariego authored
94
95
96
97
98
}

int main(int argc, char** argv) {
    if(argc != 2) {
        fprintf(stderr,"No file specified\n");
99
        return 2;
Imanol-Mikel Barba Sabariego authored
100
101
102
    }

    return parseMCPlayerFile(argv[1]);
Imanol-Mikel Barba Sabariego authored
103
}