def removePreset(self): """ Brings up a panel to remove presets. """ panel = Dialog() p = self.getBrushFileList() if not p: alert('No presets saved') return def okPressed(): panel.dismiss() name = p[presetTable.selectedIndex] + ".preset" os.remove(os.path.join(directories.brushesDir, name)) self.tool.showPanel() def selectTableRow(i, evt): presetTable.selectedIndex = i if evt.num_clicks == 2: okPressed() presetTable = TableView(columns=(TableColumn("", 200),)) presetTable.num_rows = lambda: len(p) presetTable.row_data = lambda i: (p[i],) presetTable.row_is_selected = lambda x: x == presetTable.selectedIndex presetTable.click_row = selectTableRow presetTable.selectedIndex = 0 choiceCol = Column((ValueDisplay(width=200, get_value=lambda: "Select preset to delete"), presetTable)) okButton = Button("OK", action=okPressed) cancelButton = Button("Cancel", action=panel.dismiss) row = Row([okButton, cancelButton]) panel.add(Column((choiceCol, row))) panel.shrink_wrap() panel.present()
def toolSelected(self): box = self.selectionBox() if box is None: self.editor.toolbar.selectTool(-1) return if box.volume > self.maxBlocks: self.editor.mouseLookOff() alert( _("Selection exceeds {0:n} blocks. Increase the block buffer setting and try again.").format( self.maxBlocks ) ) self.editor.toolbar.selectTool(-1) return self.rotation = 0 self.repeatCount = 1 self._scaleFactor = 1.0 if self.placeImmediately: self.destPoint = box.origin else: self.destPoint = None self.updateSchematic() self.cloneCameraDistance = max(self.cloneCameraDistance, self.safeToolDistance()) self.showPanel()
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if recordUndo: self.canUndo = True self.undoLevel = self.extractUndo(self.level, BoundingBox(self.destPoint, self.sourceBox.size)) blocksToCopy = None if not (self.copyAir and self.copyWater): blocksToCopy = range(pymclevel.materials.id_limit) if not self.copyAir: blocksToCopy.remove(0) if not self.copyWater: blocksToCopy.remove(8) if not self.copyWater: blocksToCopy.remove(9) with setWindowCaption("Copying - "): i = self.level.copyBlocksFromIter( self.sourceLevel, self.sourceBox, self.destPoint, blocksToCopy, create=True, biomes=self.copyBiomes, staticCommands=self.staticCommands, moveSpawnerPos=self.moveSpawnerPos, regenerateUUID=self.regenerateUUID, first=False, ) showProgress(_("Copying {0:n} blocks...").format(self.sourceBox.volume), i)
def dismiss(self, *args, **kwargs): """Used to change the language.""" lng = config.settings.langCode.get() try: o, n, sc = albow.translate.setLang(lng) except: o, n, sc = albow.translate.setLang(self.langs[lng]) if not sc and n != "en_US": albow.alert(_("{} is not a valid language").format("%s [%s]" % (self.sgnal[n], n))) if o == n: o = "en_US" config.settings.langCode.set(o) albow.translate.setLang(o) elif o != n: editor = self.mcedit.editor if editor and editor.unsavedEdits: result = albow.ask("You must restart MCEdit to see language changes", ["Save and Restart", "Restart", "Later"]) else: result = albow.ask("You must restart MCEdit to see language changes", ["Restart", "Later"]) if result == "Save and Restart": editor.saveFile() self.mcedit.restart() elif result == "Restart": self.mcedit.restart() elif result == "Later": pass Dialog.dismiss(self, *args, **kwargs)
def editNBTData(self): player = self.selectedPlayer if player == 'Player (Single Player)': alert("Not yet implemented.\nUse the NBT Explorer to edit this player.") elif player == '[No players]': return else: player = self.level.getPlayerTag(self.selectedPlayer) if player is not None: self.pages.remove_page(self.nbtpage) def close(): self.pages.show_page(self.col) self.nbttree = NBTExplorerToolPanel(self.tool.editor, nbtObject=player, fileName=None, savePolicy=-1, dataKeyName=None, height=self.max_height, no_header=True, close_text="Go Back", close_action=close, load_text=None, copy_data=False) self.nbtpage = Column([self.nbttree,]) self.nbtpage.shrink_wrap() self.pages.add_page("NBT Data", self.nbtpage) self.pages.show_page(self.nbtpage) else: alert(_("Unable to load player %s" % self.selectedPlayer()))
def showPanel(self, fName=None, nbtObject=None, savePolicy=0, dataKeyName="Data"): """...""" if self.panel is None and self.editor.currentTool in (self, None): # or nbtObject: # !# BAD HACK try: class fakeStdErr: def __init__(self, *args, **kwargs): pass def write(self, *args, **kwargs): pass stderr = os.sys.stderr os.sys.stderr = fakeStdErr() os.sys.stderr = stderr except: alert("Unattended data. File not loaded") return # !# self.panel = NBTExplorerToolPanel( self.editor, nbtObject=nbtObject, fileName=fName, savePolicy=savePolicy, dataKeyName=dataKeyName ) self.panel.centery = ( self.editor.mainViewport.height - self.editor.toolbar.height ) / 2 + self.editor.subwidgets[0].height self.panel.left = self.editor.left self.editor.add(self.panel)
def editNBTData(self): player = self.selectedPlayer if player == 'Player (Single Player)': alert("Not yet implemented.\nUse the NBT Explorer to edit this player.") elif player == '[No players]': return else: path = os.path.join(os.path.split(self.level.filename)[0], 'playerdata') if not os.path.exists(path): path = os.path.join(os.path.split(self.level.filename)[0], 'players') if player + '.dat' in os.listdir(path): fName = os.path.join(path, player + '.dat') nbtObject, dataKeyName, dontSaveRootTag, fn = loadFile(fName) self.pages.remove_page(self.nbtpage) def close(): self.pages.show_page(self.col) self.nbttree = NBTExplorerToolPanel(self.tool.editor, nbtObject=nbtObject, fileName=fName, dontSaveRootTag=dontSaveRootTag, dataKeyName=dataKeyName, height=self.max_height, no_header=True, close_text="Go Back", close_action=close, load_text=None) self.nbtpage = Column([self.nbttree,]) self.nbtpage.shrink_wrap() self.pages.add_page("NBT Data", self.nbtpage) self.pages.show_page(self.nbtpage) #elif self.selectedPlayer.isNew: else: alert(_("Error while getting player file.\n%s not found.")%(player + '.dat'), doNotTranslate=True)
def saveFile(fName, data, savePolicy): if fName is None: return if os.path.exists(fName): r = ask("File already exists.\nClick 'OK' to choose one.") if r == 'OK': folder, name = os.path.split(fName) suffix = os.path.splitext(name)[-1][1:] fName = mcplatform.askSaveFile(folder, "Choose a NBT file...", name, 'Folder\0*.dat\0*.*\0\0', suffix) else: return if savePolicy == -1: if hasattr(data, 'name'): data.name = "" if not os.path.isdir(fName): if savePolicy <= 0: data.save(fName) elif savePolicy == 1: with littleEndianNBT(): toSave = data.save(compressed=False) toSave = struct.Struct('<i').pack(4) + struct.Struct('<i').pack(len(toSave)) + toSave with open(fName, 'w') as f: f.write(toSave) else: alert("The selected object is not a file.\nCan't save it.")
def toolSelected(self): self.editor.mouseLookOff() if self.editor.testBoardKey == 1: self.editor.testBoardKey = 0 self.loadLevel(self.createTestBoard()) return self.editor.mouseLookOff() clipFilename = mcplatform.askOpenFile(title='Import a schematic or level...', schematics=True, suffixes=["bo2"]) # xxx mouthful if clipFilename: if str(clipFilename).split(".")[-1] in ("schematic", "schematic.gz", "zip", "inv"): self.loadSchematic(clipFilename) elif str(clipFilename).split(".")[-1].lower() == "bo2": self.loadLevel(BOParser.BO2(clipFilename).getSchematic()) elif str(clipFilename).split(".")[-1].lower() == "bo3": #self.loadLevel(BOParser.BO3(clipFilename).getSchematic()) alert("BO3 support is currently not available") else: self.loadSchematic(clipFilename) print "Canceled" if self.level is None: print "No level selected." self.editor.toolbar.selectTool(-1)
def togglePortable(self): if sys.platform == "darwin": return False textChoices = [ _("This will make your MCEdit \"portable\" by moving your settings and schematics into the same folder as {0}. Continue?").format( (sys.platform == "darwin" and _("the MCEdit application") or _("MCEditData"))), _("This will move your settings and schematics to your Documents folder. Continue?"), ] useExisting = False alertText = textChoices[directories.portable] if albow.ask(alertText) == "OK": if [directories.hasPreviousPortableInstallation, directories.hasPreviousFixedInstallation][directories.portable](): asked = albow.ask("Found a previous " + ["portable", "fixed"][directories.portable] + " installation", responses=["Use", "Overwrite", "Cancel"]) if asked == "Use": useExisting = True elif asked == "Overwrite": useExisting = False elif asked == "Cancel": return False try: [directories.goPortable, directories.goFixed][directories.portable](useExisting) except Exception, e: traceback.print_exc() albow.alert(_(u"Error while moving files: {0}").format(repr(e)))
def makeChanges(self): try: file = open(self.filename, 'rb') except: alert("Couldn't open the file") return lines = [] for line in file.readlines(): line = line.replace("\r", "") if line != "\n": lines.append(line.replace("\n", "")) file.close() tileEntities = [] for coords in GetSort(self.box, self.sorting): if self.sorting == "xz": (x, y, z) = coords else: (z, y, x) = coords if self.level.blockAt(x, y, z) == 137: tileEntities.append(self.level.tileEntityAt(x, y, z)) if len(lines) != len(tileEntities): alert("You have %d lines and %d command blocks, it should be the same." % (len(lines), len(tileEntities))) return op = FileEditsOperation(self.editor, self.level, self.box, lines, tileEntities) self.editor.addOperation(op) if op.canUndo: self.editor.addUnsavedEdit()
def loadFile(fName): if not fName: fName = mcplatform.askOpenFile(title=_("Select a NBT (.dat) file..."), suffixes=['dat',]) if fName: if not os.path.isfile(fName): alert("The selected object is not a file.\nCan't load it.") return dontSaveRootTag = False nbtObject = load(fName) if fName.endswith('.schematic'): nbtObject = TAG_Compound(name='Data', value=nbtObject) dontSaveRootTag = True dataKeyName = 'Data' elif nbtObject.get('Data', None): dataKeyName = 'Data' elif nbtObject.get('data', None): dataKeyName = 'data' else: nbtObject.name = 'Data' dataKeyName = 'Data' dontSaveRootTag = True nbtObject = TAG_Compound([nbtObject,]) # dontSaveRootTag = not fName.endswith('.schematic') return nbtObject, dataKeyName, dontSaveRootTag, fName return [None,] * 4
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return try: level = self.tool.editor.level try: self.undoPos = level.getPlayerPosition(self.player) self.undoDim = level.getPlayerDimension(self.player) self.undoYP = level.getPlayerOrientation(self.player) except Exception as e: log.info(_("Couldn't get player position! ({0!r})").format(e)) yaw, pitch = self.yp if yaw is not None and pitch is not None: level.setPlayerOrientation((yaw, pitch), self.player) level.setPlayerPosition(self.pos, self.player) level.setPlayerDimension(level.dimNo, self.player) self.tool.playerPos[tuple(self.pos)] = self.player self.tool.revPlayerPos[self.player] = self.pos self.tool.markerList.invalidate() self.canUndo = True except pymclevel.PlayerNotFound as e: print "Player move failed: ", e
def tryImport(self, name, dir): """ Imports a brush module. Called by importBrushModules :param name, name of the module to import. """ embeded = bool(dir == "stock-brushes") try: path = os.path.join(dir, (name + ".py")) if isinstance(path, unicode) and DEF_ENC != "UTF-8": path = path.encode(DEF_ENC) globals()[name] = m = imp.load_source(name, path) if not embeded: old_trn_path = albow.translate.getLangPath() if "trn" in sys.modules.keys(): del sys.modules["trn"] import albow.translate as trn trn_path = os.path.join(directories.brushesDir, name) if os.path.exists(trn_path): trn.setLangPath(trn_path) trn.buildTranslation(config.settings.langCode.get()) m.trn = trn albow.translate.setLangPath(old_trn_path) albow.translate.buildTranslation(config.settings.langCode.get()) self.editor.mcedit.set_update_ui(True) self.editor.mcedit.set_update_ui(False) m.materials = self.editor.level.materials m.tool = self m.createInputs(m) return m except Exception as e: print traceback.format_exc() alert(_(u"Exception while importing brush mode {}. See console for details.\n\n{}").format(name, e)) return object()
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if self.player == "Player": answer = ask(_("Are you sure you want to delete the default player?"), ["Yes", "Cancel"]) if answer == "Cancel": return if recordUndo: self.undoTag = self.level.getPlayerTag(self.player) self.level.players.remove(self.player) if self.tool.panel: if self.player != "Player": self.tool.panel.players.remove(version_utils.getPlayerNameFromUUID(self.player)) else: self.tool.panel.players.remove("Player") while self.tool.panel.table.index >= len(self.tool.panel.players): self.tool.panel.table.index -= 1 if len(self.tool.panel.players) == 0: self.tool.hidePanel() self.tool.showPanel() self.tool.markerList.invalidate() pos = self.tool.revPlayerPos[self.player] del self.tool.playerPos[pos] if self.player != "Player": del self.tool.playerTexture[self.player] else: del self.level.root_tag["Data"]["Player"] del self.tool.revPlayerPos[self.player] self.canUndo = True
def _alertException(*args, **kw): try: return func(*args, **kw) except root.Cancel: alert("Canceled.") except Exception, e: if ask("Error during {0}: {1!r}".format(func, e)[:1000], ["Report Error", "Okay"], default=1, cancel=0) == "Report Error": reportException(e)
def saveConfig(): try: cf = file(configFilePath(), 'w') config.write(cf) cf.close() except Exception, e: try: alert(u"Error saving configuration settings to mcedit.ini: {0}".format(e)) except: pass
def _alertException(*args, **kw): try: return func(*args, **kw) except root.Cancel: alert("Canceled.") except pymclevel.infiniteworld.SessionLockLost as e: alert(e.message + _("\n\nYour changes cannot be saved.")) except Exception, e: logging.exception("Exception:") ask(_("Error during {0}: {1!r}").format(func, e)[:1000], ["OK"], cancel=0)
def createNewWorld(self): level = self.editor.createNewLevel() if level: self.remove(self.fileOpener) self.editor.size = self.size self.add(self.editor) self.focus_switch = self.editor albow.alert( "World created. To expand this infinite world, explore the world in Minecraft or use the Chunk Control tool to add or delete chunks.")
def importPaste(self): """ Hack for paste to import a level. """ clipFilename = mcplatform.askOpenFile(title='Choose a schematic or level...', schematics=True) if clipFilename: try: self.loadLevel(pymclevel.fromFile(clipFilename, readonly=True)) except Exception: alert("Failed to load file %s" % clipFilename) self.brushMode = "Fill" return
def _alertException(*args, **kw): try: return func(*args, **kw) except root.Cancel: alert("Canceled.") except pymclevel.infiniteworld.SessionLockLost as e: alert(e.message + "\n\nYour changes cannot be saved.") except Exception, e: logging.exception("Exception:") if ask("Error during {0}: {1!r}".format(func, e)[:1000], ["Report Error", "Okay"], default=1, cancel=0) == "Report Error": reportException()
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if recordUndo: self.undoLevel = self.extractUndo(self.level, self.destBox) destBox = self.destBox if self.level.bounds == self.destBox: destBox = None fill = self.level.fillBlocksIter(destBox, self.blockInfo, blocksToReplace=self.blocksToReplace) showProgress("Replacing blocks...", fill, cancel=True)
def loadSchematic(self, filename): """ actually loads a schematic or a level """ try: level = pymclevel.fromFile(filename) self.loadLevel(level) except Exception, e: print u"Unable to import file {0} : {1}".format(filename, e) traceback.print_exc() if filename: # self.editor.toolbar.selectTool(-1) alert(u"I don't know how to import this file: {0}.\n\nError: {1!r}".format(os.path.basename(filename), e)) return
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return level = self.tool.editor.level if isinstance(level, pymclevel.MCInfdevOldLevel): if not positionValid(level, self.pos): if SpawnSettings.spawnProtection.get(): raise SpawnPositionInvalid( "You cannot have two air blocks at Y=63 and Y=64 in your spawn point's column. Additionally, you cannot have a solid block in the three blocks above your spawn point. It's weird, I know.") self.undoPos = level.playerSpawnPosition() level.setPlayerSpawnPosition(self.pos) self.tool.markerList.invalidate()
def perform(self, recordUndo=True): if self.toolPanel.nbtObject: orgNBT = self.toolPanel.nbtObject[self.toolPanel.dataKeyName] newNBT = self.toolPanel.data if "%s"%orgNBT != "%s"%newNBT: if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if recordUndo: self.canUndo = True self.undoLevel = self.extractUndo() self.toolPanel.nbtObject[self.toolPanel.dataKeyName] = self.toolPanel.data
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return with setWindowCaption("COPYING - "): self.editor.freezeStatus(_("Copying %0.1f million blocks") % (float(self._dirtyBox.volume) / 1048576.0,)) if recordUndo: chunks = set() for op in self.blockCopyOps: chunks.update(op.dirtyBox().chunkPositions) self.undoLevel = self.extractUndoChunks(self.level, chunks) [i.perform(False) for i in self.blockCopyOps] [i.perform(recordUndo) for i in self.selectionOps] self.canUndo = True
def okPressed(): panel.dismiss() name = nameField.value if name in ['Load Preset', 'Remove Presets', '__temp__']: alert("That preset name is reserved. Try pick another preset name.") return for p in ['<','>',':','\"', '/', '\\', '|', '?', '*', '.']: if p in name: alert('Invalid character in file name') return self.tool.saveBrushPreset(name) self.tool.showPanel()
def mouseDown(self, evt, pos, direction): pos = map(lambda p, d: p + d, pos, direction) op = PlayerSpawnMoveOperation(self, pos) try: self.editor.addOperation(op) if op.canUndo: self.editor.addUnsavedEdit() self.markerList.invalidate() except SpawnPositionInvalid, e: if "Okay" != ask(str(e), responses=["Okay", "Fix it for me!"]): level = self.editor.level status = "" if not okayAt63(level, pos): level.setBlockAt(pos[0], 63, pos[2], 1) status += _("Block added at y=63.\n") if 59 < pos[1] < 63: pos[1] = 63 status += _("Spawn point moved upward to y=63.\n") if not okayAboveSpawn(level, pos): if pos[1] > 63 or pos[1] < 59: lpos = (pos[0], pos[1] - 1, pos[2]) if level.blockAt(*pos) == 0 and level.blockAt( *lpos) != 0 and okayAboveSpawn(level, lpos): pos = lpos status += _( "Spawn point shifted down by one block.\n") if not okayAboveSpawn(level, pos): for i in xrange(1, 4): level.setBlockAt(pos[0], pos[1] + i, pos[2], 0) status += _("Blocks above spawn point cleared.\n") self.editor.invalidateChunks([(pos[0] // 16, pos[2] // 16)]) op = PlayerSpawnMoveOperation(self, pos) try: self.editor.addOperation(op) if op.canUndo: self.editor.addUnsavedEdit() self.markerList.invalidate() except SpawnPositionInvalid, e: alert(str(e)) return if len(status): alert(_("Spawn point fixed. Changes: \n\n") + status)
def togglePortable(self): if sys.platform == "darwin": return False textChoices = [ _("This will make your MCEdit \"portable\" by moving your settings and schematics into the same folder as {0}. Continue?").format( (sys.platform == "darwin" and _("the MCEdit application") or _("MCEditData"))), _("This will move your settings and schematics to your Documents folder. Continue?"), ] alertText = textChoices[directories.portable] if albow.ask(alertText) == "OK": try: [directories.goPortable, directories.goFixed][directories.portable]() except Exception, e: traceback.print_exc() albow.alert(_(u"Error while moving files: {0}").format(repr(e)))
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return level = self.tool.editor.level if isinstance(level, pymclevel.MCInfdevOldLevel): if not positionValid(level, self.pos): if config.spawn.spawnProtection.get(): raise SpawnPositionInvalid( "You cannot have two air blocks at Y=63 and Y=64 in your spawn point's column. Additionally, you cannot have a solid block in the three blocks above your spawn point. It's weird, I know." ) self.undoPos = level.playerSpawnPosition() level.setPlayerSpawnPosition(self.pos) self.tool.markerList.invalidate() self.canUndo = True
def loadSchematic(self, filename): """ actually loads a schematic or a level """ try: level = pymclevel.fromFile(filename, readonly=True) self.loadLevel(level) except Exception, e: logging.warn(u"Unable to import file %s : %s", filename, e) traceback.print_exc() if filename: # self.editor.toolbar.selectTool(-1) alert( _(u"I don't know how to import this file: {0}.\n\nError: {1!r}").format(os.path.basename(filename), e)) return
def saveFile(fName, data, dontSaveRootTag): if os.path.exists(fName): r = ask("File already exists.\nClick 'OK' to choose one.") if r == 'OK': folder, name = os.path.split(fName) suffix = os.path.splitext(name)[-1][1:] fName = mcplatform.askSaveFile(folder, "Choose a NBT file...", name, 'Folder\0*.dat\0*.*\0\0', suffix) else: return if dontSaveRootTag: if hasattr(data, 'name'): data.name = "" if not os.path.isdir(fName): data.save(fName) else: alert("The selected object is not a file.\nCan't save it.")
def mouseDown(self, evt, pos, direction): pos = map(lambda p, d: p + d, pos, direction) op = PlayerSpawnMoveOperation(self, pos) try: op.perform() self.editor.addOperation(op) self.editor.addUnsavedEdit() self.markerList.invalidate() except SpawnPositionInvalid, e: if "Okay" != ask(str(e), responses=["Okay", "Fix it for me!"]): level = self.editor.level status = "" if not okayAt63(level, pos): level.setBlockAt(pos[0], 63, pos[2], 1) status += "Block added at y=63.\n" if 59 < pos[1] < 63: pos[1] = 63 status += "Spawn point moved upward to y=63.\n" if not okayAboveSpawn(level, pos): if pos[1] > 63 or pos[1] < 59: lpos = (pos[0], pos[1] - 1, pos[2]) if level.blockAt(*pos) == 0 and level.blockAt(*lpos) != 0 and okayAboveSpawn(level, lpos): pos = lpos status += "Spawn point shifted down by one block.\n" if not okayAboveSpawn(level, pos): for i in range(1, 4): level.setBlockAt(pos[0], pos[1] + i, pos[2], 0) status += "Blocks above spawn point cleared.\n" self.editor.invalidateChunks([(pos[0] // 16, pos[2] // 16)]) op = PlayerSpawnMoveOperation(self, pos) try: op.perform() except SpawnPositionInvalid, e: alert(str(e)) return self.editor.addOperation(op) self.editor.addUnsavedEdit() self.markerList.invalidate() if len(status): alert("Spawn point fixed. Changes: \n\n" + status)
def perform(self, recordUndo=True): if self.level.saving: alert("Cannot perform action while saving is taking place") return if recordUndo: self.undoLevel = self.extractUndo(self.level, self.destBox) destBox = self.destBox if self.level.bounds == self.destBox: destBox = None fill = self.level.fillBlocksIter(destBox, self.blockInfo, blocksToReplace=self.blocksToReplace, noData=self.noData) showProgress("Replacing blocks...", fill, cancel=True) self.canUndo = True
def okPressed(): panel.dismiss() name = nameField.value if name in ['Load Preset', 'Remove Presets', '__temp__']: alert( "That preset name is reserved. Try pick another preset name." ) return for p in ['<', '>', ':', '\"', '/', '\\', '|', '?', '*', '.']: if p in name: alert('Invalid character in file name') return self.tool.saveBrushPreset(name) self.tool.showPanel()
def perform(self, recordUndo=True): if self.toolPanel.nbtObject: orgNBT = self.toolPanel.nbtObject[self.toolPanel.dataKeyName] newNBT = self.toolPanel.data if "%s" % orgNBT != "%s" % newNBT: if self.level.saving: alert( _("Cannot perform action while saving is taking place") ) return if recordUndo: self.canUndo = True self.undoLevel = self.extractUndo() self.toolPanel.nbtObject[ self.toolPanel.dataKeyName] = self.toolPanel.data
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return with setWindowCaption("COPYING - "): self.editor.freezeStatus( _("Copying %0.1f million blocks") % (float(self._dirtyBox.volume) / 1048576., )) if recordUndo: chunks = set() for op in self.blockCopyOps: chunks.update(op.dirtyBox().chunkPositions) self.undoLevel = self.extractUndoChunks(self.level, chunks) [i.perform(False) for i in self.blockCopyOps] [i.perform(recordUndo) for i in self.selectionOps] self.canUndo = True
def loadBrushPreset(self, name): """ Loads a brush preset name.preset :param name, name of the preset to load. """ name += '.preset' f = None try: f = open(os.path.join(directories.brushesDir, name), "r") except: alert( 'Exception while trying to load preset. See console for details.' ) loadedBrushOptions = ast.literal_eval(f.read()) if f: f.close() brushMode = self.brushModes.get(loadedBrushOptions.get("Mode", None), None) if brushMode is not None: self.selectedBrushMode = loadedBrushOptions["Mode"] self.brushMode = self.brushModes[self.selectedBrushMode] for key in loadedBrushOptions: if key.endswith('blockID'): key = key[:-7] self.options[ key] = self.editor.level.materials.blockWithID( loadedBrushOptions[key + 'blockID'], loadedBrushOptions[key + 'blockData']) if key + 'recentBlocks' in loadedBrushOptions: list = [] blockList = loadedBrushOptions[key + 'recentBlocks'] for b in blockList: list.append( self.editor.level.materials.blockWithID( b[0], b[1])) self.recentBlocks[key] = list elif key.endswith('blockData'): continue elif key.endswith('recentBlocks'): continue else: self.options[key] = loadedBrushOptions[key] self.showPanel() self.setupPreview()
def refreshLang(self=None, suppressAlert=False, build=True): """Refreshes and returns the current language string""" global oldlang import config import leveleditor from leveleditor import Settings try: cancel = False lang = Settings.langCode.get() #.langCode isRealLang = verifyLangCode(lang) if build: buildTranslation(lang) if not oldlang == lang and not suppressAlert and isRealLang: import albow if leveleditor.LevelEditor(self).unsavedEdits: result = albow.ask( "You must restart MCEdit to see language changes", ["Save and Restart", "Restart", "Cancel"]) else: result = albow.ask( "You must restart MCEdit to see language changes", ["Restart", "Cancel"]) if result == "Save and Restart": editor.saveFile() restart(self) elif result == "Restart": restart(self) else: isRealLang = False cancel = True elif not suppressAlert and not isRealLang: import albow albow.alert("{} is not a valid language".format(lang)) if not isRealLang: Settings.langCode.set(oldlang) else: oldlang = lang if cancel == True: return "" else: return lang except Exception as inst: print inst return ""
def check_for_version(self): new_version = release.check_for_new_version(self.version_info) if new_version is not False: answer = albow.ask( _('Version {} is available').format(new_version["tag_name"]), [ 'Download', 'View', 'Ignore' ], default=1, cancel=2 ) if answer == "View": platform_open(new_version["html_url"]) elif answer == "Download": platform_open(new_version["asset"]["browser_download_url"]) albow.alert(_(' {} should now be downloading via your browser. You will still need to extract the downloaded file to use the updated version.').format(new_version["asset"]["name"]))
def resized(self, dw, dh): """ Handle window resizing events. """ GLViewport.resized(self, dw, dh) (w, h) = self.size if w == 0 and h == 0: # The window has been minimized, no need to draw anything. self.editor.renderer.render = False return if not self.editor.renderer.render: self.editor.renderer.render = True dis = None if sys.platform == 'linux2' and mcplatform.hasXlibDisplay: dis = mcplatform.Xlib.display.Display() win = dis.create_resource_object('window', display.get_wm_info()['window']) geom = win.query_tree().parent.get_geometry() if w >= 1000 and h >= 700: config.settings.windowWidth.set(w) config.settings.windowHeight.set(h) config.save() if dis: win.configure(height=geom.height, width=geom.width) elif w != 0 and h != 0: config.settings.windowWidth.set(1000) config.settings.windowHeight.set(700) config.save() if dis: win.configure(height=700, width=1000) if dw > 20 or dh > 20: if not hasattr(self, 'resizeAlert'): self.resizeAlert = self.shouldResizeAlert if self.resizeAlert: albow.alert( "Window size increased. You may have problems using the cursor until MCEdit is restarted." ) self.resizeAlert = False if dis: dis.sync()
def editNBTData(self): player = self.selectedPlayer if player == 'Player (Single Player)': alert( "Not yet implemented.\nUse the NBT Explorer to edit this player." ) elif player == '[No players]': return else: path = os.path.join( os.path.split(self.level.filename)[0], 'playerdata') if not os.path.exists(path): path = os.path.join( os.path.split(self.level.filename)[0], 'players') if player + '.dat' in os.listdir(path): fName = os.path.join(path, player + '.dat') nbtObject, dataKeyName, dontSaveRootTag, fn = loadFile(fName) self.pages.remove_page(self.nbtpage) def close(): self.pages.show_page(self.col) self.nbttree = NBTExplorerToolPanel( self.tool.editor, nbtObject=nbtObject, fileName=fName, dontSaveRootTag=dontSaveRootTag, dataKeyName=dataKeyName, height=self.max_height, no_header=True, close_text="Go Back", close_action=close, load_text=None) self.nbtpage = Column([ self.nbttree, ]) self.nbtpage.shrink_wrap() self.pages.add_page("NBT Data", self.nbtpage) self.pages.show_page(self.nbtpage) #elif self.selectedPlayer.isNew: else: alert(_("Error while getting player file.\n%s not found.") % (player + '.dat'), doNotTranslate=True)
def createChunks(self): panel = GeneratorPanel() col = [panel] label = Label("Create chunks using the settings above? This cannot be undone.") col.append(Row([Label("")])) col.append(label) col = Column(col) if Dialog(client=col, responses=["OK", "Cancel"]).present() == "Cancel": return chunks = self.selectedChunks() createChunks = panel.generate(self.editor.level, chunks) try: with setWindowCaption("CREATING - "): showProgress("Creating {0} chunks...".format(len(chunks)), createChunks, cancel=True) except Exception, e: traceback.print_exc() alert(_("Failed to start the chunk generator. {0!r}").format(e))
def perform(self, recordUndo=True): if self.level.saving: alert("Cannot perform action while saving is taking place") return if recordUndo: self.undoLevel = self.extractUndo(self.level, self.box) for i, line in enumerate(self.lines): tileEntity = self.tileEntities[i] line = line.decode('utf-8') line = line.replace(u"\u201c\u202a", "\"") line = line.replace(u"\u201d\u202c", "\"") if line == "\"\"": line = "" if tileEntity["Command"].value != line: tileEntity["Command"].value = line self.level.addTileEntity(tileEntity) if not self.canUndo and recordUndo: self.canUndo = True
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if self.player == "Player (Single Player)": answer = ask( _("Are you sure you want to delete the default player?"), ["Yes", "Cancel"]) if answer == "Cancel": return self.player = "Player" if recordUndo: self.undoTag = self.level.getPlayerTag(self.player) self.level.players.remove(self.player) if self.tool.panel: if self.player != "Player": #self.tool.panel.players.remove(player_cache.getPlayerNameFromUUID(self.player)) #self.tool.panel.players.remove(self.playercache.getPlayerInfo(self.player)[0]) str() else: self.tool.panel.players.remove("Player (Single Player)") while self.tool.panel.table.index >= len(self.tool.panel.players): self.tool.panel.table.index -= 1 #if len(self.tool.panel.players) == 0: # self.tool.hidePanel() # self.tool.showPanel() self.tool.hidePanel() self.tool.showPanel() self.tool.markerList.invalidate() self.tool.movingPlayer = None pos = self.tool.revPlayerPos[self.editor.level.dimNo][self.player] del self.tool.playerPos[self.editor.level.dimNo][pos] if self.player != "Player": del self.tool.playerTexture[self.player] else: del self.level.root_tag["Data"]["Player"] del self.tool.revPlayerPos[self.editor.level.dimNo][self.player] self.canUndo = True
def showPanel(self, fName=None, nbtObject=None, dontSaveRootTag=False, dataKeyName='Data'): """...""" if (self.panel is None and self.editor.currentTool in (self, None)): # or nbtObject: #!# BAD HACK try: class fakeStdErr: def __init__(self, *args, **kwargs): pass def write(self, *args, **kwargs): pass stderr = os.sys.stderr os.sys.stderr = fakeStdErr() os.sys.stderr = stderr except: alert("Unattended data. File not loaded") return #!# self.panel = NBTExplorerToolPanel(self.editor, nbtObject=nbtObject, fileName=fName, dontSaveRootTag=dontSaveRootTag, dataKeyName=dataKeyName) self.panel.centery = (self.editor.mainViewport.height - self.editor.toolbar.height) / 2 + self.editor.subwidgets[0].height self.panel.left = self.editor.left self.editor.add(self.panel)
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return try: level = self.tool.editor.level try: self.undoPos = level.getPlayerPosition(self.player) self.undoDim = level.getPlayerDimension(self.player) self.undoYP = level.getPlayerOrientation(self.player) except Exception, e: log.info(_("Couldn't get player position! ({0!r})").format(e)) yaw, pitch = self.yp if yaw is not None and pitch is not None: level.setPlayerOrientation((yaw, pitch), self.player) level.setPlayerPosition(self.pos, self.player) level.setPlayerDimension(level.dimNo, self.player) self.tool.markerList.invalidate() self.canUndo = True
def loadFile(fName): if not fName: fName = mcplatform.askOpenFile(title=_("Select a NBT (.dat) file..."), suffixes=[ 'dat', ]) if fName: if not os.path.isfile(fName): alert("The selected object is not a file.\nCan't load it.") return savePolicy = 0 data = open(fName).read() if struct.Struct('<i').unpack(data[:4])[0] in (3, 4): if struct.Struct('<i').unpack(data[4:8])[0] != len(data[8:]): raise NBTFormatError() with littleEndianNBT(): nbtObject = load(buf=data[8:]) savePolicy = 1 elif struct.Struct('<i').unpack(data[:4])[0] in (1, 2): alert(_("Old PE level.dat, unsupported at the moment.")) else: nbtObject = load(buf=data) if fName.endswith('.schematic'): nbtObject = TAG_Compound(name='Data', value=nbtObject) savePolicy = -1 dataKeyName = 'Data' elif nbtObject.get('Data', None): dataKeyName = 'Data' elif nbtObject.get('data', None): dataKeyName = 'data' else: nbtObject.name = 'Data' dataKeyName = 'Data' if savePolicy == 0: savePolicy = -1 nbtObject = TAG_Compound([ nbtObject, ]) return nbtObject, dataKeyName, savePolicy, fName return [None] * 4
def _alertException(*args, **kw): try: return func(*args, **kw) except root.Cancel: alert("Canceled.") except pymclevel.infiniteworld.SessionLockLost as e: alert(e.message + "\n\nYour changes cannot be saved.") except Exception, e: logging.exception("Exception:") if ask("Error during {0}: {1!r}".format(func, e)[:1000], ["Report Error", "Okay"], default=1, cancel=0) == "Report Error": try: import squash_python squash_python.get_client().recordException(*sys.exc_info()) except ImportError: pass except Exception: logging.exception("Error while recording exception data:")
def loadFile(self, filename, addToRecent=True): if os.path.exists(filename): if filename.endswith(".mcworld"): filename = mcworld_support.open_world(filename) addToRecent = False try: self.editor.loadFile(filename, addToRecent=addToRecent) except NotImplementedError as e: albow.alert(e.message) return None except Exception as e: logging.error(u'Failed to load file {0}: {1!r}'.format( filename, e)) return None self.remove(self.fileOpener) self.fileOpener = None if self.editor.level: self.editor.size = self.size self.add(self.editor) self.focus_switch = self.editor
def makeChanges(self): try: file = open(self.filename, 'rb') except: alert("Couldn't open the file") return lines = [] for line in file.readlines(): line = line.replace("\r", "") if line != "\n": lines.append(line.replace("\n", "")) file.close() tileEntities = [] for (x, y, z) in self.order: blockAtXYZ = self.level.blockAt(x, y, z) if blockAtXYZ == 137 or blockAtXYZ == 210 or blockAtXYZ == 211: tileEntities.append(self.level.tileEntityAt(x, y, z)) else: alert("The blocks are different now!") return if len(lines) != len(tileEntities): alert( "You have %d lines and %d command blocks, it should be the same." % (len(lines), len(tileEntities))) return op = FileEditsOperation(self.editor, self.level, self.box, lines, tileEntities) self.editor.addOperation(op) if op.canUndo: self.editor.addUnsavedEdit()
def togglePortable(self): if sys.platform == "darwin": return False textChoices = [ _("This will make your MCEdit \"portable\" by moving your settings and schematics into the same folder as {0}. Continue?" ).format( (sys.platform == "darwin" and _("the MCEdit application") or _("MCEditData"))), _("This will move your settings and schematics to your Documents folder. Continue?" ), ] useExisting = False alertText = textChoices[directories.portable] if albow.ask(alertText) == "OK": if [ directories.hasPreviousPortableInstallation, directories.hasPreviousFixedInstallation ][directories.portable](): asked = albow.ask("Found a previous %s installation" % ["portable", "fixed"][directories.portable], responses=["Use", "Overwrite", "Cancel"]) if asked == "Use": useExisting = True elif asked == "Overwrite": useExisting = False elif asked == "Cancel": return False try: [directories.goPortable, directories.goFixed][directories.portable](useExisting) except Exception as e: traceback.print_exc() albow.alert( _(u"Error while moving files: {0}").format(repr(e))) else: self.goPortableButton.selectedChoice = self.saveOldPortable self.goPortableButton.tooltipText = self.portableButtonTooltip() return True
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if recordUndo: self.canUndo = True self.undoLevel = self.extractUndo(self.level, self._dirtyBox) def _perform(): yield 0, len(self.points), _("Applying {0} brush...").format(_(self.brushMode.displayName)) if hasattr(self.brushMode, 'apply'): for i, point in enumerate(self.points): f = self.brushMode.apply(self.brushMode, self, point) if hasattr(f, "__iter__"): for progress in f: yield progress else: yield i, len(self.points), _("Applying {0} brush...").format(_(self.brushMode.displayName)) if hasattr(self.brushMode, 'applyToChunkSlices'): for j, cPos in enumerate(self._dirtyBox.chunkPositions): if not self.level.containsChunk(*cPos): continue chunk = self.level.getChunk(*cPos) for i, point in enumerate(self.points): brushBox = self.tool.getDirtyBox(point, self.tool) brushBoxThisChunk, slices = chunk.getChunkSlicesForBox(brushBox) f = self.brushMode.applyToChunkSlices(self.brushMode, self, chunk, slices, brushBox, brushBoxThisChunk) if brushBoxThisChunk.volume == 0: f = None if hasattr(f, "__iter__"): for progress in f: yield progress else: yield j * len(self.points) + i, len(self.points) * self._dirtyBox.chunkCount, _("Applying {0} brush...").format(_(self.brushMode.displayName)) chunk.chunkChanged() if len(self.points) > 10: showProgress("Performing brush...", _perform(), cancel=True) else: exhaust(_perform())
def update_mcver(): num = mcver_updater.run() if num is None: albow.alert("Error Updating") elif num: albow.alert("Version Definitions have been updated!\n\nPlease restart MCEdit-Unified to apply the changes") else: albow.alert("Version Definitions are already up-to-date!")
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if self.player == "Player": answer = ask( _("Are you sure you want to delete the default player?"), ["Yes", "Cancel"]) if answer == "Cancel": return if recordUndo: self.undoTag = self.level.getPlayerTag(self.player) self.level.players.remove(self.player) if self.tool.panel: if self.player != "Player": self.tool.panel.players.remove( version_utils.getPlayerNameFromUUID(self.player)) else: self.tool.panel.players.remove("Player") while self.tool.panel.table.index >= len(self.tool.panel.players): self.tool.panel.table.index -= 1 if len(self.tool.panel.players) == 0: self.tool.hidePanel() self.tool.showPanel() self.tool.markerList.invalidate() pos = self.tool.revPlayerPos[self.player] del self.tool.playerPos[pos] if self.player != "Player": del self.tool.playerTexture[self.player] else: del self.level.root_tag["Data"]["Player"] del self.tool.revPlayerPos[self.player] self.canUndo = True
def perform(self, recordUndo=True): if self.level.saving: alert(_("Cannot perform action while saving is taking place")) return if recordUndo: self.canUndo = True self.undoLevel = self.extractUndo(self.level, BoundingBox(self.destPoint, self.sourceBox.size)) blocksToCopy = None if not (self.copyAir and self.copyWater): blocksToCopy = range(pymclevel.materials.id_limit) if not self.copyAir: blocksToCopy.remove(0) if not self.copyWater: blocksToCopy.remove(8) if not self.copyWater: blocksToCopy.remove(9) with setWindowCaption("Copying - "): i = self.level.copyBlocksFromIter(self.sourceLevel, self.sourceBox, self.destPoint, blocksToCopy, create=True, biomes=self.copyBiomes, staticCommands=self.staticCommands, moveSpawnerPos=self.moveSpawnerPos, regenerateUUID=self.regenerateUUID, first=False) showProgress(_("Copying {0:n} blocks...").format(self.sourceBox.volume), i)
def removePreset(self): """ Brings up a panel to remove presets. """ panel = Dialog() p = self.getBrushFileList() if not p: alert('No presets saved') return def okPressed(): panel.dismiss() name = p[presetTable.selectedIndex] + ".preset" os.remove(os.path.join(directories.brushesDir, name)) self.tool.showPanel() def selectTableRow(i, evt): presetTable.selectedIndex = i if evt.num_clicks == 2: okPressed() presetTable = TableView(columns=(TableColumn("", 200), )) presetTable.num_rows = lambda: len(p) presetTable.row_data = lambda i: (p[i], ) presetTable.row_is_selected = lambda x: x == presetTable.selectedIndex presetTable.click_row = selectTableRow presetTable.selectedIndex = 0 choiceCol = Column( (ValueDisplay(width=200, get_value=lambda: "Select preset to delete"), presetTable)) okButton = Button("OK", action=okPressed) cancelButton = Button("Cancel", action=panel.dismiss) row = Row([okButton, cancelButton]) panel.add(Column((choiceCol, row))) panel.shrink_wrap() panel.present()
def tryImport(self, name, dir): """ Imports a brush module. Called by importBrushModules :param name, name of the module to import. """ embeded = bool(dir == "stock-brushes") try: path = os.path.join(dir, (name + ".py")) if isinstance(path, str) and DEF_ENC != "UTF-8": path = path.encode(DEF_ENC) globals()[name] = m = imp.load_source(name, path) if not embeded: old_trn_path = albow.translate.getLangPath() if "trn" in list(sys.modules.keys()): del sys.modules["trn"] import albow.translate as trn trn_path = os.path.join(directories.brushesDir, name) if os.path.exists(trn_path): trn.setLangPath(trn_path) trn.buildTranslation(config.settings.langCode.get()) m.trn = trn albow.translate.setLangPath(old_trn_path) albow.translate.buildTranslation( config.settings.langCode.get()) self.editor.mcedit.set_update_ui(True) self.editor.mcedit.set_update_ui(False) m.materials = self.editor.level.materials m.tool = self m.createInputs(m) return m except Exception as e: print(traceback.format_exc()) alert( _("Exception while importing brush mode {}. See console for details.\n\n{}" ).format(name, e)) return object()