Filter python code to add chest

Solution 1:

After digging into the source for MCE and also source for some of SethBling's filters I managed to whip up some code.

The following functions assume a global object named levelOBJ which is set to the incoming level object in your perform() function. This way you don't have to keep passing level or box.

# Just so I don't have to keep doing the math        
def getChunkAt(x, z):
    chunk = levelObj.getChunk(x / 16, z / 16)
    return chunk

# Creates a TAG_Compound Item (for use with the CreateChestAt function)
def CreateChestItem(itemid, damage=0, count=1, slot=0):
    item = TAG_Compound()
    item["id"] = TAG_Short(itemid)
    item["Damage"] = TAG_Short(damage)
    item["Count"] = TAG_Byte(count)
    item["Slot"] = TAG_Byte(slot)
    return item

# Creates a chest at the specified coords containing the items passed    
def CreateChestAt(x, y, z, Items=None, Direction=2, CustomName=""):
    levelObj.setBlockAt(x, y, z, 54) # Chest Block (single = 27 slots 0-26), 54 == chest, 146 == Trapped Chest
    levelObj.setBlockDataAt(x, y, z, Direction) # 2==N, 3==S, 4==W, 5==E anything else == N

    # Now Create Entity Block and add it to the chunk
    chunk = getChunkAt(x, z)
    chest = TileEntity.Create("Chest")
    TileEntity.setpos(chest, (x, y, z))
    if Items <> None:
        chest["Items"] = Items
    if CustomName <> "":
        chest["CustomName"] = CustomName
    chunk.TileEntities.append(chest)

Then you can use my functions in your filter by calling them as described in the samples below. Below, x,y,z assumes they have been populated by proper coordinates you wish the chest to be placed at.

Also, double chests are simply two side by side chests. Call CreateChestAt twice (two coordinates 1 apart E-W or N-S) to create a double chest. You can create 3 in a row but Minecraft will invalidate the 3rd chest making it inaccessible in-game so pay attention to how you place them.

To Create an empty chest:

CreateChestAt(x, y, z)

To Create a chest with items:

# Build item list (4 Jungle Planks and 11 chests
ChestItems = TAG_List()
ChestItems.append( CreateChestItem(5, 3, 4, 0) )
ChestItems.append( CreateChestItem(54, 0, 11, 1) )

# Make a chest with the items.
CreateChestAt(x, y, z, ChestItems)

Optional parameters of Direction and CustomName can be specified too...

Creates an empty chest facing West named "My Chest"

CreateChestAt(x, y, z, None, 4, "My Chest")