BlockTypes.allBlocks now remains sorted and enforces uniqueness

This commit is contained in:
David Vierra 2016-02-08 17:12:06 -10:00
parent 1b49c25b36
commit 114e3341ad
3 changed files with 56 additions and 12 deletions

View File

@ -621,10 +621,7 @@ class EditorSession(QtCore.QObject):
deadJsons.append(json)
deadIDs = set((j['internalName'], j['meta']) for j in deadJsons)
blocktypes.allBlocks[:] = [
bt for bt in blocktypes.allBlocks
if (bt.internalName, bt.meta) not in deadIDs
]
blocktypes.discardInternalNameMeta(deadIDs)
for json in deadJsons:
internalName = json['internalName']
@ -657,7 +654,7 @@ class EditorSession(QtCore.QObject):
'meta': blockDef.meta,
}
blockType = BlockType(ID, blockDef.meta, blocktypes)
blocktypes.allBlocks.append(blockType)
blocktypes.allBlocks.add(blockType)
blocktypes.IDsByState[nameAndState] = ID, blockDef.meta
blocktypes.statesByID[ID, blockDef.meta] = nameAndState

View File

@ -610,10 +610,9 @@ class AnvilWorldAdapter(object):
itemTypes.addFMLIDMapping(name, ID)
replacedIDsSet = set(replacedIDs)
blocktypes.allBlocks[:] = [b for b in blocktypes if b.ID not in replacedIDsSet]
blocktypes.allBlocks.extend(BlockType(newID, 0, blocktypes) for newID in replacedIDs)
blocktypes.discardIDs(replacedIDsSet)
blocktypes.addBlocktypes(BlockType(newID, 0, blocktypes) for newID in replacedIDs)
blocktypes.allBlocks.sort()
log.info("Added %d blocks.", count)

View File

@ -5,7 +5,8 @@
from __future__ import absolute_import
from logging import getLogger
import traceback
from collections import defaultdict, namedtuple
from collections import defaultdict, namedtuple, MutableSet
import bisect
import numpy
from mceditlib.blocktypes import itemtypes
@ -56,12 +57,41 @@ class BlockType(namedtuple("_BlockType", "ID meta blocktypeSet")):
id_limit = 4096
class SortedSet(MutableSet):
def __iter__(self):
return iter(self._sorted)
def discard(self, value):
self._set.discard(value)
self._sorted.remove(value)
def add(self, value):
if value in self._set:
return
self._set.add(value)
bisect.insort(self._sorted, value)
def __contains__(self, x):
return x in self._set
def __len__(self):
return len(self._set)
def __init__(self):
self._sorted = []
self._set = set()
class BlockTypeSet(object):
defaultColor = (0xc9, 0x77, 0xf0, 0xff)
def __init__(self, defaultName="Unused Block", idMapping=None):
object.__init__(self)
self.allBlocks = []
self.allBlocks = SortedSet()
self.blockJsons = {}
self.IDsByState = {} # internalName[blockstates] -> (id, meta)
self.statesByID = {} # (id, meta) -> internalName[blockstates]
@ -69,7 +99,6 @@ class BlockTypeSet(object):
self.IDsByName = {} # internalName -> id
self.namesByID = {} # id -> internalName
self.defaultBlockstates = {} # internalName -> [blockstates]
self.defaults = {
@ -237,6 +266,25 @@ class BlockTypeSet(object):
return BlockType(ID, meta, self)
def discardIDs(self, blockIDs):
blockIDs = set(blockIDs)
blocktypes = [b for b in self.allBlocks if b.ID in blockIDs]
for b in blocktypes:
self.allBlocks.discard(b)
def discardInternalNameMetas(self, nameMetas):
nameMetas = set(nameMetas)
blocktypes = [bt for bt in self.allBlocks
if (bt.internalName, bt.meta) in nameMetas]
for b in blocktypes:
self.allBlocks.discard(b)
def addBlocktypes(self, blocktypes):
for b in blocktypes:
self.allBlocks.add(b)
def blocksMatching(self, name):
name = name.lower()
return [v for v in self.allBlocks if name in v.displayName.lower() or name in v.aka.lower()]
@ -306,7 +354,7 @@ class BlockTypeSet(object):
log.info("No ID mapping for %s, skipping...", internalName)
return
ID, meta = IDmeta
self.allBlocks.append(BlockType(ID, meta, self))
self.allBlocks.add(BlockType(ID, meta, self))
oldJson = self.blockJsons.get(internalName + blockState)
if oldJson is None: