-1

I have this code from a MCEDit filter I was editing:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt(x, y, z)
                                if t and t["id"].value == "Control":
                                        if "id" in t: del t["id"]
                                        if "x" in t: del t["x"]
                                        if "y" in t: del t["y"]
                                        if "z" in t: del t["z"]
                                        return (t, x, y, z)
        return (None, None, None, None)

And I get this error:

'KeyError: 'Key x not found.'

Please help!

EDIT:

Fixed, thanks @Texelelf:

def getCommand(level, box):
for x in xrange(box.minx,box.maxx):
    for y in xrange(box.miny,box.maxy):
        for z in xrange(box.minz,box.maxz):
            t = deepcopy(level.tileEntityAt(x,y,z))
            if t and t["id"].value == "Control":
                if "id" in t: del t["id"]
                if "x" in t: del t["x"]
                if "y" in t: del t["y"]
                if "z" in t: del t["z"]
                return (t, x, y, z)
    return (None, None, None, None)
  • What line is the error? I assume it is at `if "x" in t: del t["x"]`. Can you print what `level.tileEntityAt(x, y, z)` does? – A.J. Uppal Apr 13 '14 at 19:30
  • It is actually at line: t = level.tileEntityAt(x, y, z), I don't really know if MCEdit can print what "level.tileEntityAt(x, y, z)"does, sorry. –  Apr 13 '14 at 19:33
  • If you have a solution you should post it as an answer and mark it accepted. It's OK to answer your own question. –  Apr 15 '14 at 20:57
  • @ Mike W Ok, did that, thanks –  Apr 16 '14 at 01:04

2 Answers2

0

Here is your function:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt(x, y, z)
                                if t and t["id"].value == "Control":
                                        if "id" in t: del t["id"]
                                        if "x" in t: del t["x"]
                                        if "y" in t: del t["y"]
                                        if "z" in t: del t["z"]
                                        return (t, x, y, z)
        return (None, None, None, None)

In the second line of your code, you assign x in a for loop. You then proceed to call t = level.tileEntityat(x, y, z), which means you are looking for the value of x, as defined in your second line. Instead, surround your x, y, and z in quotations, to make them strings. I am not completely sure this is what you want because I don't know what is in level.tileEntityAt(x, y, z), but I'm making my best guess.

Edited code:

def getCommand(level, box):
    for x in xrange(box.minx,box.maxx):
        for y in xrange(box.miny,box.maxy):
            for z in xrange(box.minz,box.maxz):
                t = level.tileEntityAt("x", "y", "z")
                    if t and t["id"].value == "Control":
                        if "id" in t: del t["id"]
                        if "x" in t: del t["x"]
                        if "y" in t: del t["y"]
                        if "z" in t: del t["z"]
                        return (t, x, y, z)
        return (None, None, None, None)
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0
def getCommand(level, box):
for x in xrange(box.minx,box.maxx):
    for y in xrange(box.miny,box.maxy):
        for z in xrange(box.minz,box.maxz):
            t = deepcopy(level.tileEntityAt(x,y,z))
            if t and t["id"].value == "Control":
                if "id" in t: del t["id"]
                if "x" in t: del t["x"]
                if "y" in t: del t["y"]
                if "z" in t: del t["z"]
                return (t, x, y, z)
    return (None, None, None, None)

Got the answer, thanks to @Texelelf on Twitter