Add "Remove Tags" commands to Find/Replace NBT results

This commit is contained in:
David Vierra 2016-09-07 14:33:20 -10:00
parent 64eb73dd63
commit 34ec62bae1
3 changed files with 303 additions and 223 deletions

View File

@ -35,7 +35,7 @@ nbtReplaceSettings.replaceValueTagType = nbtReplaceSettings.getOption("replaceVa
class NBTResultsEntry(object):
# namedtuple("NBTResultsEntry", "tagName value id path position uuid resultType")):
def __init__(self, model, tagNameIndex, tagName, value, ID, path, position, uuid, resultType):
def __init__(self, model, tagNameIndex, tagName, value, ID, path, position, uuid, resultType, dimension):
self.model = model
self.tagNameIndex = tagNameIndex # xxx REALLY SHOULD change model data through the model itself
self.tagName = tagName
@ -45,6 +45,7 @@ class NBTResultsEntry(object):
self.position = position
self.uuid = uuid
self.resultType = resultType
self.dimension = dimension
EntityResult = "ENTITY"
TileEntityResult = "TILE_ENTITY"
@ -57,13 +58,26 @@ class NBTResultsEntry(object):
self.tagName = value
self.model.dataChanged.emit(self.tagNameIndex, self.tagNameIndex)
def getEntity(self, dim):
def getEntity(self):
assert self.resultType == self.EntityResult
dim = self.dimension
box = BoundingBox(self.position.intfloor(), (1, 1, 1)).chunkBox(dim)
entities = dim.getEntities(box, UUID=self.uuid)
for entity in entities:
return entity
return None
def getTargetRef(self):
dim = self.dimension
if self.resultType == self.TileEntityResult:
return dim.getTileEntity(self.position)
if self.resultType == self.EntityResult:
return self.getEntity()
# if result.resultType == result.ItemResult: # xxx
class NBTResultsModel(QtCore.QAbstractItemModel):
@ -78,7 +92,12 @@ class NBTResultsModel(QtCore.QAbstractItemModel):
self.results.append(entry)
self.endInsertRows()
return entry
def removeEntries(self, entries):
self.beginResetModel()
self.results = [r for r in self.results if r not in entries]
self.endResetModel()
# --- Shape ---
def rowCount(self, parent):
@ -144,6 +163,9 @@ class NBTResultsModel(QtCore.QAbstractItemModel):
# return "Unknown value %s" % value
# else:
# return value
if role == Qt.UserRole:
return entry
def addResults(self, results):
size = len(self.results)
@ -215,6 +237,9 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
self.resultsWidget.replaceSelectedButton.clicked.connect(self.replaceSelected)
self.resultsWidget.replaceAllButton.clicked.connect(self.replaceAll)
self.resultsWidget.removeSelectedButton.clicked.connect(self.removeSelected)
self.resultsWidget.removeAllButton.clicked.connect(self.removeAll)
self.searchNameCheckbox.toggled.connect(self.searchForToggled)
self.searchValueCheckbox.toggled.connect(self.searchForToggled)
self.findTimer = None
@ -317,7 +342,7 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
row = modelIndex.row()
result = self.resultsModel.results[row]
if result.resultType == result.EntityResult:
entity = result.getEntity(self.editorSession.currentDimension)
entity = result.getEntity()
if entity is not None:
self.editorSession.zoomAndInspectEntity(entity) # xxxxxxx!!!
else:
@ -395,7 +420,8 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
path=[],
position=entity.Position,
uuid=uuid,
resultType=NBTResultsEntry.EntityResult)
resultType=NBTResultsEntry.EntityResult,
dimension=self.editorSession.currentDimension)
continue
tag = entity.raw_tag()
@ -411,7 +437,8 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
path=path,
position=entity.Position,
uuid=uuid,
resultType=NBTResultsEntry.EntityResult)
resultType=NBTResultsEntry.EntityResult,
dimension=self.editorSession.currentDimension)
def _findTileEntitiesInChunk(chunk):
for tileEntity in chunk.TileEntities:
@ -428,7 +455,8 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
path=[],
position=tileEntity.Position,
uuid=None,
resultType=NBTResultsEntry.TileEntityResult)
resultType=NBTResultsEntry.TileEntityResult,
dimension=self.editorSession.currentDimension)
continue
tag = tileEntity.raw_tag()
@ -443,7 +471,8 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
path=path,
position=tileEntity.Position,
uuid=None,
resultType=NBTResultsEntry.TileEntityResult)
resultType=NBTResultsEntry.TileEntityResult,
dimension=self.editorSession.currentDimension)
def _find():
self.resultsDockWidget.show()
@ -518,30 +547,21 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
else:
value = newValue
subtag.value = value
result.value = value
def _replace():
for result in entries:
if result.resultType == result.TileEntityResult:
tileEntity = self.editorSession.currentDimension.getTileEntity(result.position)
if tileEntity:
tag = tileEntity.raw_tag()
_replaceInTag(result, tag)
tileEntity.dirty = True
ref = result.getTargetRef()
tag = ref.raw_tag()
_replaceInTag(result, tag)
ref.dirty = True
if result.resultType == result.EntityResult:
entity = result.getEntity(self.editorSession.currentDimension) # xxx put dimension in result!!!!
if entity:
tag = entity.raw_tag()
_replaceInTag(result, tag)
entity.dirty = True
# if result.resultType == result.ItemResult: # xxx
yield
command = NBTReplaceCommand(self.editorSession, "Replace NBT data") # xxx replace details
with command.begin():
replacer = _replace()
showProgress("Replacing NBT data...", replacer)
showProgress("Replacing NBT data...", _replace())
self.editorSession.pushCommand(command)
@ -549,4 +569,40 @@ class FindReplaceNBT(QtGui.QWidget, Ui_findNBTWidget):
self.replaceEntries(self.resultsModel.results)
def replaceSelected(self):
pass
entries = []
for index in self.resultsWidget.resultsView.selectedIndices():
entries.append(self.resultsModel.data(index, role=Qt.UserRole))
self.replaceEntries(entries)
def removeEntries(self, entries):
def _remove():
for result in entries:
ref = result.getTargetRef()
tag = ref.raw_tag()
for component in result.path[:-1]:
tag = tag[component]
del tag[result.tagName]
ref.dirty = True
yield
self.resultsModel.removeEntries(entries)
command = NBTReplaceCommand(self.editorSession, "Remove NBT tags")
with command.begin():
showProgress("Removing NBT tags...", _remove())
self.editorSession.pushCommand(command)
def removeAll(self):
self.removeEntries(self.resultsModel.results)
def removeSelected(self):
entries = []
for index in self.resultsWidget.resultsView.selectedIndices():
entries.append(self.resultsModel.data(index, role=Qt.UserRole))
self.removeEntries(entries)

View File

@ -6,14 +6,14 @@
<rect>
<x>0</x>
<y>0</y>
<width>741</width>
<height>394</height>
<width>429</width>
<height>390</height>
</rect>
</property>
<property name="windowTitle">
<string>Form</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<widget class="QGroupBox" name="groupBox">
<property name="title">
@ -21,22 +21,18 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<item>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<widget class="QCheckBox" name="searchNameCheckbox">
<property name="text">
<string>Tag Names</string>
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<widget class="QLineEdit" name="nameField"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_23">
<item>
<item row="1" column="0">
<widget class="QCheckBox" name="searchValueCheckbox">
<property name="text">
<string>Values</string>
@ -46,129 +42,133 @@
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="valueField"/>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLineEdit" name="valueField"/>
</item>
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Tag Type:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="tagTypeComboBox">
<item>
<property name="text">
<string>Any</string>
</property>
</item>
<item>
<property name="text">
<string>Any Numeric</string>
</property>
</item>
<item>
<property name="text">
<string>Any Integer</string>
</property>
</item>
<item>
<property name="text">
<string>Any Floating-Point</string>
</property>
</item>
<item>
<property name="text">
<string>Any Array</string>
</property>
</item>
<item>
<property name="text">
<string>String</string>
</property>
</item>
<item>
<property name="text">
<string>Byte</string>
</property>
</item>
<item>
<property name="text">
<string>Short</string>
</property>
</item>
<item>
<property name="text">
<string>Int</string>
</property>
</item>
<item>
<property name="text">
<string>Long</string>
</property>
</item>
<item>
<property name="text">
<string>Float</string>
</property>
</item>
<item>
<property name="text">
<string>Double</string>
</property>
</item>
<item>
<property name="text">
<string>List</string>
</property>
</item>
<item>
<property name="text">
<string>Compound</string>
</property>
</item>
<item>
<property name="text">
<string>Byte Array</string>
</property>
</item>
<item>
<property name="text">
<string>Int Array</string>
</property>
</item>
<item>
<property name="text">
<string>Short Array</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="label_9">
<property name="text">
<string>Tag Type:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="tagTypeComboBox">
<item>
<property name="text">
<string>Any</string>
</property>
</item>
<item>
<property name="text">
<string>Any Numeric</string>
</property>
</item>
<item>
<property name="text">
<string>Any Integer</string>
</property>
</item>
<item>
<property name="text">
<string>Any Floating-Point</string>
</property>
</item>
<item>
<property name="text">
<string>Any Array</string>
</property>
</item>
<item>
<property name="text">
<string>String</string>
</property>
</item>
<item>
<property name="text">
<string>Byte</string>
</property>
</item>
<item>
<property name="text">
<string>Short</string>
</property>
</item>
<item>
<property name="text">
<string>Int</string>
</property>
</item>
<item>
<property name="text">
<string>Long</string>
</property>
</item>
<item>
<property name="text">
<string>Float</string>
</property>
</item>
<item>
<property name="text">
<string>Double</string>
</property>
</item>
<item>
<property name="text">
<string>List</string>
</property>
</item>
<item>
<property name="text">
<string>Compound</string>
</property>
</item>
<item>
<property name="text">
<string>Byte Array</string>
</property>
</item>
<item>
<property name="text">
<string>Int Array</string>
</property>
</item>
<item>
<property name="text">
<string>Short Array</string>
</property>
</item>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<item row="2" column="0">
<widget class="QLabel" name="label_10">
<property name="text">
<string>Tag Path:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="tagPathBox">
<property name="placeholderText">
<string>Path to search in, such as 'Data/Player/Inventory'</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="tagPathHelpButton">
<property name="text">
<string>?</string>
</property>
</widget>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLineEdit" name="tagPathBox">
<property name="placeholderText">
<string>Path to search in, such as 'Data/Player/Inventory'</string>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="tagPathHelpButton">
<property name="text">
<string>?</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</item>
@ -182,81 +182,84 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_26">
<item>
<layout class="QFormLayout" name="formLayout">
<property name="fieldGrowthPolicy">
<enum>QFormLayout::AllNonFixedFieldsGrow</enum>
</property>
<item row="0" column="0">
<widget class="QCheckBox" name="replaceNameCheckbox">
<property name="text">
<string>Replace Name With:</string>
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<widget class="QLineEdit" name="replaceNameField"/>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_27">
<item>
<item row="1" column="0">
<widget class="QCheckBox" name="replaceValueCheckbox">
<property name="text">
<string>Replace Value With:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="replaceValueField"/>
</item>
<item>
<widget class="QLabel" name="label_12">
<property name="text">
<string>Tag Type:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="replaceValueTagTypeComboBox">
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<property name="text">
<string>Existing</string>
</property>
<widget class="QLineEdit" name="replaceValueField"/>
</item>
<item>
<property name="text">
<string>String</string>
</property>
<widget class="QLabel" name="label_12">
<property name="text">
<string>Tag Type:</string>
</property>
</widget>
</item>
<item>
<property name="text">
<string>Byte</string>
</property>
<widget class="QComboBox" name="replaceValueTagTypeComboBox">
<item>
<property name="text">
<string>Existing</string>
</property>
</item>
<item>
<property name="text">
<string>String</string>
</property>
</item>
<item>
<property name="text">
<string>Byte</string>
</property>
</item>
<item>
<property name="text">
<string>Short</string>
</property>
</item>
<item>
<property name="text">
<string>Int</string>
</property>
</item>
<item>
<property name="text">
<string>Long</string>
</property>
</item>
<item>
<property name="text">
<string>Float</string>
</property>
</item>
<item>
<property name="text">
<string>Double</string>
</property>
</item>
</widget>
</item>
<item>
<property name="text">
<string>Short</string>
</property>
</item>
<item>
<property name="text">
<string>Int</string>
</property>
</item>
<item>
<property name="text">
<string>Long</string>
</property>
</item>
<item>
<property name="text">
<string>Float</string>
</property>
</item>
<item>
<property name="text">
<string>Double</string>
</property>
</item>
</widget>
</layout>
</item>
</layout>
</item>
@ -268,54 +271,52 @@
<property name="title">
<string>Search in...</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_4">
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_19">
<item>
<layout class="QFormLayout" name="formLayout_3">
<item row="0" column="0">
<widget class="QCheckBox" name="searchEntitiesCheckbox">
<property name="text">
<string>Search Entities</string>
</property>
</widget>
</item>
<item>
<item row="0" column="1">
<widget class="QLineEdit" name="entityIDField">
<property name="placeholderText">
<string>Entity IDs, separted by ';'</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_24">
<item>
<item row="1" column="0">
<widget class="QCheckBox" name="searchTileEntitiesCheckbox">
<property name="text">
<string>Search Tile Entities</string>
</property>
</widget>
</item>
<item>
<item row="1" column="1">
<widget class="QLineEdit" name="tileEntityIDField">
<property name="placeholderText">
<string>TileEntity IDs, separted by ';'</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_22">
<item>
<item row="2" column="0">
<widget class="QCheckBox" name="searchItemsCheckbox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Search Items</string>
</property>
</widget>
</item>
<item>
<item row="2" column="1">
<widget class="QLineEdit" name="itemIDField">
<property name="enabled">
<bool>false</bool>
</property>
<property name="placeholderText">
<string>Item IDs, separated by ';'</string>
</property>
@ -327,6 +328,9 @@
<layout class="QHBoxLayout" name="horizontalLayout_20">
<item>
<widget class="QCheckBox" name="searchPlayersCheckbox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Search Players</string>
</property>
@ -334,6 +338,9 @@
</item>
<item>
<widget class="QCheckBox" name="searchChunksCheckbox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Search Chunks</string>
</property>
@ -341,6 +348,9 @@
</item>
<item>
<widget class="QCheckBox" name="searchOtherDatsCheckbox">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Search other .dat files</string>
</property>

View File

@ -132,6 +132,20 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeAllButton">
<property name="text">
<string>Remove Tags</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeSelectedButton">
<property name="text">
<string>Remove Selected</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="stopButton">
<property name="text">