'''
Created on Jul 23, 2011

@author: Rio
'''
from mclevelbase import *

__all__ = "Entity, TileEntity".split(", ")

EntityId = "EntityId"
id = "id"

Motion = "Motion"
Pos = "Pos"
Rotation = "Rotation"

class TileEntity(object):
    knownIDs = "Furnace, Sign, MonsterSpawner, Chest, Music, Trap, RecordPlayer".split(", ")
    @classmethod
    def Create(cls, tileEntityID, **kw):
        tileEntityTag = TAG_Compound()
        tileEntityTag[id] = TAG_String(tileEntityID)
        cls.setpos(tileEntityTag, (0, 0, 0))
        return tileEntityTag

    @classmethod
    def pos(cls, tag):
        return [tag[a].value for a in 'xyz']

    @classmethod
    def setpos(cls, tag, pos):
        for a, p in zip('xyz', pos):
            tag[a] = TAG_Int(p)

    @classmethod
    def copyWithOffset(cls, tileEntity, copyOffset):
        eTag = deepcopy(tileEntity)
        eTag['x'] = TAG_Int(tileEntity['x'].value + copyOffset[0])
        eTag['y'] = TAG_Int(tileEntity['y'].value + copyOffset[1])
        eTag['z'] = TAG_Int(tileEntity['z'].value + copyOffset[2])
        return eTag


class Entity(object):
    @classmethod
    def Create(cls, entityID, **kw):
        entityTag = TAG_Compound()
        entityTag[EntityId] = TAG_String(entityID)
        Entity.setpos(entityTag, (0, 0, 0))
        return entityTag

    @classmethod
    def pos(cls, tag):
        if Pos not in tag:
            raise InvalidEntity, tag
        return [a.value for a in tag[Pos]]

    @classmethod
    def setpos(cls, tag, pos):
        tag["Pos"] = TAG_List([TAG_Double(p) for p in pos])

    @classmethod
    def copyWithOffset(cls, entity, copyOffset):
        eTag = deepcopy(entity)

        positionTags = map(lambda p, co: nbt.TAG_Double(p.value + co), eTag["Pos"], copyOffset)
        eTag["Pos"] = TAG_List(positionTags)

        if eTag["id"].value == "Painting":
            eTag["TileX"].value += copyOffset[0]
            eTag["TileY"].value += copyOffset[1]
            eTag["TileZ"].value += copyOffset[2]

        return eTag

class InvalidEntity(ValueError): pass
class InvalidTileEntity(ValueError): pass

@@ -323,153 +323,954 @@
     
-@classmethod%0A    def Create(cls, tileEntityID, **kw):%0A        tileEntityTag = TAG_Compound()%0A        tileEntityTag%5Bid%5D = TAG_String(tileEntityID)
+baseStructures = %7B%0A        %22Furnace%22: (%0A            (%22BurnTime%22, TAG_Short),%0A            (%22CookTime%22, TAG_Short),%0A            (%22Items%22, TAG_List),%0A        ),%0A        %22Sign%22: (%0A            (%22Items%22, TAG_List),%0A        ),%0A        %22MobSpawner%22: (%0A            (%22Items%22, TAG_List),%0A        ),%0A        %22Chest%22: (%0A            (%22Items%22, TAG_List),%0A        ),%0A        %22Music%22: (%0A            (%22note%22, TAG_Byte),%0A        ),%0A        %22Trap%22: (%0A            (%22Items%22, TAG_List),%0A        ),%0A        %22RecordPlayer%22: (%0A            (%22Record%22, TAG_Int),%0A        ),%0A%0A    %7D%0A    maxItems = %7B%0A        %22Furnace%22 : 3,%0A        %22Chest%22 : 27,%0A        %22Trap%22 : 9,%0A    %7D%0A    @classmethod%0A    def Create(cls, tileEntityID, **kw):%0A        tileEntityTag = TAG_Compound()%0A        tileEntityTag%5Bid%5D = TAG_String(tileEntityID)%0A        base = cls.baseStructures.get(tileEntityID, None)%0A        if base:%0A            for (name, tag) in base:%0A                tileEntityTag%5Bname%5D = tag()%0A
 %0A   

add base tag structures for known tile entities