Move dead itemstack conversion code to entities.py and revive it. Use it later for world conversion?

This commit is contained in:
David Vierra 2015-05-08 19:22:41 -10:00
parent 69f7f3696f
commit 7f6e9bb9ef
2 changed files with 30 additions and 23 deletions

View File

@ -251,29 +251,6 @@ class AnvilChunkData(object):
log.debug(u"Saving chunk: {0}".format(self))
# log.debug("Converting ItemStacks")
# if self.blocktypes.itemStackVersion == VERSION_1_7:
# def convertStack(stack):
# if stack["id"].tagID == nbt.ID_STRING:
# stack["id"] = nbt.TAG_Short(self.blocktypes.itemTypes.internalNamesByID[stack["id"].value])
# elif self.blocktypes.itemStackVersion == VERSION_1_8:
# def convertStack(stack):
# if stack["id"].tagID == nbt.ID_SHORT:
# stack["id"] = nbt.TAG_Short(self.blocktypes.itemTypes[stack["id"].value].ID)
#
# def convertAllStacks(tags):
# for tag in tags:
# if ItemStackRef.tagIsItemStack(tag):
# convertStack(tag)
# if tag.tagID == nbt.ID_COMPOUND:
# convertAllStacks(tag.itervalues())
# if tag.tagID == nbt.ID_LIST and tag.list_type in (nbt.ID_LIST, nbt.ID_COMPOUND):
# convertAllStacks(tag)
#
# convertAllStacks(self.Entities)
# convertAllStacks(self.TileEntities)
chunkTag = self.rootTag.copy()
sections = nbt.TAG_List()

View File

@ -4,6 +4,7 @@
from __future__ import absolute_import, division, print_function, unicode_literals
import logging
from mceditlib import nbt
from mceditlib.blocktypes import VERSION_1_7, VERSION_1_8
from mceditlib.blocktypes.itemtypes import ItemType
from mceditlib.geometry import Vector
from mceditlib import nbtattr
@ -96,11 +97,40 @@ class PCTileEntityRefBase(object):
def blockTypes(self):
return self.chunk.blocktypes
def convertStackTo17(stack, blocktypes):
if stack["id"].tagID == nbt.ID_STRING:
stack["id"] = nbt.TAG_Short(blocktypes.itemTypes.internalNamesByID[stack["id"].value])
def convertStackTo18(stack, blocktypes):
if stack["id"].tagID == nbt.ID_SHORT:
stack["id"] = nbt.TAG_Short(blocktypes.itemTypes[stack["id"].value].ID)
def convertAllStacks(tags, blocktypes, version):
if version == VERSION_1_7:
convertStack = convertStackTo17
elif version == VERSION_1_8:
convertStack = convertStackTo18
else:
raise ValueError("Unknown item stack version %d" % version)
for tag in tags:
if ItemStackRef.tagIsItemStack(tag):
convertStack(tag)
if tag.tagID == nbt.ID_COMPOUND:
convertAllStacks(tag.itervalues(), blocktypes, version)
if tag.tagID == nbt.ID_LIST and tag.list_type in (nbt.ID_LIST, nbt.ID_COMPOUND):
convertAllStacks(tag, blocktypes, version)
class NoParentError(RuntimeError):
"""
Raised when setting the `id` of an ItemStack with no parent.
"""
class ItemStackRef(nbtattr.NBTCompoundRef):
def __init__(self, rootTag=None, parent=None):
if rootTag is None: