planet/planet/mcpiedit.py

178 lines
4.8 KiB
Python
Raw Normal View History

2022-04-08 11:59:20 +00:00
# MCPIEdit
# This is a different editor from revival's MCPIedit!
# This one is intended to work with Planet or it can work on its own
import sys
import os
import pathlib
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
import nbt
USER = os.getenv("USER") # Get the username, used for later
absolute_path = pathlib.Path(__file__).parent.absolute()
if str(absolute_path).startswith("/usr/bin"):
absolute_path = "/usr/lib/planet-launcher/"
sys.path.append(absolute_path)
if os.path.exists("/usr/lib/planet-launcher/"):
sys.path.append("/usr/lib/planet-launcher/")
if not os.path.exists(f"/home/{USER}/.minecraft-pi/games/com.mojang/minecraftWorlds/"):
os.makedirs(f"/home/{USER}/.minecraft-pi/games/com.mojang/minecraftWorlds/")
GAME_TYPES = {
"Survival" : nbt.nbtlib.Int(0),
"Creative" : nbt.nbtlib.Int(1),
}
GAME_INTREGERS = {
"0" : "Survival",
"1" : "Creative"
}
class FileSelectorTab(QWidget):
def __init__(self):
super().__init__()
layout = QGridLayout()
self.load_button = QPushButton("Select NBT File")
layout.addWidget(self.load_button, 0, 0)
self.setLayout(layout)
class EditorTab(QWidget):
def __init__(self, filename):
super().__init__()
layout = QVBoxLayout()
self.nbt = nbt.load_nbt(filename, True)
self.filename = filename
self.tabs = QTabWidget()
self.tabs.setTabPosition(QTabWidget.West)
self.tabs.setMovable(True)
self.tabs.addTab(self.main_tab(), "World")
self.name_edit.setText(str(self.nbt["LevelName"]))
self.timestamp_box.setValue(int(self.nbt["LastPlayed"]))
self.game_box.setCurrentText(GAME_INTREGERS[str(int(self.nbt["GameType"]))])
self.seed_edit.setText(str(int(self.nbt["RandomSeed"])))
layout.addWidget(self.tabs)
self.setLayout(layout)
def main_tab(self):
widget = QWidget()
layout = QGridLayout()
self.name_label = QLabel("World name")
self.name_edit = QLineEdit()
self.name_edit.setPlaceholderText("OneChunk")
self.seed_label = QLabel("World Seed")
self.seed_edit = QLineEdit()
self.seed_edit.setPlaceholderText("-121542953")
self.seed_edit.setValidator(QIntValidator())
self.timestamp_label = QLabel("Last Played Timestamp")
self.timestamp_box = QSpinBox()
self.timestamp_box.setMaximum(2147483647)
self.game_label = QLabel("Game mode")
self.game_box = QComboBox()
self.game_box.addItems(
[
"Survival",
"Creative"
]
)
self.save_button = QPushButton("Save")
self.save_button.clicked.connect(self.save)
layout.addWidget(self.name_label, 0, 0)
layout.addWidget(self.name_edit, 0, 1)
layout.addWidget(self.seed_label, 1, 0)
layout.addWidget(self.seed_edit, 1, 1)
layout.addWidget(self.timestamp_label, 2, 0)
layout.addWidget(self.timestamp_box, 2, 1)
layout.addWidget(self.game_label, 3, 0)
layout.addWidget(self.game_box, 3, 1)
layout.addWidget(self.save_button, 4, 1)
widget.setLayout(layout)
return widget
def save(self):
self.nbt['LevelName'] = nbt.nbtlib.String(self.name_edit.text())
self.nbt["LastPlayed"] = nbt.nbtlib.Long(self.timestamp_box.value())
self.nbt["GameType"] = GAME_TYPES[self.game_box.currentText()]
self.nbt["RandomSeed"] = nbt.nbtlib.Long(int(self.seed_edit.text()))
nbt.save_nbt(self.nbt, self.filename)
class NBTEditor(QWidget):
def __init__(self):
super().__init__()
self.nbt_file = ""
self.layout = QStackedLayout()
selector = FileSelectorTab()
selector.load_button.clicked.connect(self.load_nbt)
self.layout.addWidget(selector)
self.setLayout(self.layout)
def load_nbt(self):
print("Hellow, Cruel World!")
fname = QFileDialog.getOpenFileName(self, 'Open NBT File', f'/home/{USER}/.minecraft-pi/games/com.mojang/minecraftWorlds/',"Minecraft Pi Level NBT (level.dat)")
self.layout.addWidget(EditorTab(fname[0]))
self.layout.setCurrentIndex(1)
self.setLayout(self.layout)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = QMainWindow()
window.setCentralWidget(NBTEditor())
window.setWindowTitle("MCPIEdit")
window.show()
app.exec()