File: PycraftMaterialsLibrary.py

package info (click to toggle)
minetest-mod-pycraft 0.22-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 2,616 kB
  • sloc: python: 79,282; makefile: 10
file content (58 lines) | stat: -rw-r--r-- 1,905 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import pycraft_minetest as pcmt
import pycraft_minetest.blocklist as blocklist

class PycraftMaterialsLibrary():

    matlibrary = {}

    def __init__(self):
        self.matlibrary = self.buildDictWithBlockNamesValues()

    def getBlockName(self, id):
        l = self.matlibrary.values()
        if id in l:
            i = l.index(id)
            return self.matlibrary.keys()[i]
        else:
            return "what is the name of block number {}?".format(id)

    def getBlockId(self, name):
        if name in self.matlibrary:
            return self.matlibrary[name]
        else:
            return pcmt.gold # default block if name is unknown

    def testNameInPcmt(self, name, id):
        for r in dir(pcmt):
            try:
                pcmtid = getattr(pcmt, r)
                if (pcmtid == id) and ((r == name) or (r+'_BLOCK'.lower() == name)): # _block removed from name
                    return r
            except:
                pass
        return None

    def buildDictWithBlockNamesValues(self):
        blockNames = dir(blocklist)  # get all variables in blocklist.py
        blockDict = {}
        bn = None
        for bn in blockNames:
            try:
                val = getattr(blocklist, bn)  # try to get the value of the symbol
                if isinstance(val, blocklist.Block):
                    bnpcmt = bn.lower()
                    bnpcmt_renamed = self.testNameInPcmt(bnpcmt, val.id)
                    if bnpcmt_renamed is not None:
                        blockDict[bnpcmt_renamed] = val.id
                        #print(bnpcmt,val.id)
                #bv = pcmt.getblock(bn.lower())
                #w[bn] = bv
            except:
                pass
        return blockDict

if __name__ == "__main__":
    w = PycraftMaterialsLibrary()
    print (w.matlibrary)
    print(w.getBlockName(w.matlibrary['fire']))
    print(w.getBlockName(89))