-
Notifications
You must be signed in to change notification settings - Fork 15
/
Embed-Edit McStructureFile.py
246 lines (197 loc) · 8.96 KB
/
Embed-Edit McStructureFile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
from typing import TYPE_CHECKING, Tuple
import amulet.level
from amulet.api.wrapper import Interface, EntityIDType, EntityCoordType
import numpy
import urllib.request
import wx
import wx.dataview
import ast
import os
import string
import os.path
from os import path
from ctypes import windll
from distutils.version import LooseVersion, StrictVersion
from amulet.api.data_types import Dimension
from amulet.api.selection import SelectionGroup
from amulet_nbt import *
from amulet.api.block_entity import BlockEntity
from amulet_map_editor.api.wx.ui.simple import SimpleDialog
from amulet_map_editor.api.wx.ui.block_select import BlockDefine
from amulet_map_editor.api.wx.ui.block_select import BlockSelect
from amulet_map_editor.programs.edit.api.operations import DefaultOperationUI
from amulet_map_editor.api.wx.ui.base_select import BaseSelect
from amulet_map_editor.api import image
from amulet.utils import block_coords_to_chunk_coords
from amulet.api.block import Block
import PyMCTranslate
from amulet_map_editor.api.wx.ui.base_select import BaseSelect
from amulet.libs.leveldb.leveldb import LevelDB
import amulet_nbt
from amulet.level.formats import mcstructure
import datetime
from pathlib import Path
#from amulet.level.formats.leveldb_world import format
if TYPE_CHECKING:
from amulet.api.level import BaseLevel
from amulet_map_editor.programs.edit.api.canvas import EditCanvas
operation_modes = {
#"Java": "java",
"Bedrock ONLY for now": "bedrock",
}
class SetBlock(wx.Panel, DefaultOperationUI):
def __init__(
self,
parent: wx.Window,
canvas: "EditCanvas",
world: "BaseLevel",
options_path: str,
):
platform = world.level_wrapper.platform
world_version = world.level_wrapper.version
plat = (platform, world_version)
hbox = wx.wxEVT_VLBOX
wx.Panel.__init__(self, parent)
DefaultOperationUI.__init__(self, parent, canvas, world, options_path)
self.Freeze()
self._sizer = wx.BoxSizer(wx.VERTICAL)
self.SetSizer(self._sizer)
top_sizer = wx.BoxSizer(wx.VERTICAL)
side_sizer = wx.BoxSizer(wx.HORIZONTAL)
bottom_sizer = wx.BoxSizer(wx.HORIZONTAL)
self._sizer.Add(side_sizer, 1, wx.TOP | wx.LEFT, 0)
self._sizer.Add(top_sizer, 0, wx.TOP| wx.LEFT, 290)
self._sizer.Add(bottom_sizer, 1, wx.BOTTOM | wx.LEFT,0)
# choicebox for operation mode selection.
self._mode = wx.Choice(self, choices=list(operation_modes.keys()))
self._mode.SetSelection(0)
self._structlist = wx.Choice(self, choices=self._run_get_slist())
self._structlist.SetSelection(0)
self._structlist.Bind(wx.EVT_CHOICE,self.onFocus)
side_sizer.Add(self._mode, 0, wx.TOP | wx.LEFT , 5)
side_sizer.Add(self._structlist, 0, wx.TOP | wx.LEFT, 9)
self._run_getSData_button = wx.Button(self, label="Get Structure Data")
self._run_getSData_button.Bind(wx.EVT_BUTTON, self._run_get_sdata)
side_sizer.Add(self._run_getSData_button, 25, wx.BOTTOM | wx.LEFT, 20)
self._run_text = wx.TextCtrl(
self, style=wx.TE_LEFT | wx.TE_BESTWRAP, size=(290,20)
)
bottom_sizer.Add(self._run_text,0 , wx.LEFT, 0)
self._run_text.SetValue("Name Structure Here")
self._run_setSData_button = wx.Button(self, label="Save Structure \n Data to world ")
self._run_setSData_button.Bind(wx.EVT_BUTTON, self._run_set_sdata)
bottom_sizer.Add(self._run_setSData_button, 0, wx.TOP | wx.LEFT, 0)
self._run_setSData_button.Fit()
self._run_setLData_button = wx.Button(self, label="Load MC \n Stucture File")
self._run_setLData_button.Bind(wx.EVT_BUTTON, self._run_load)
bottom_sizer.Add(self._run_setLData_button, 0, wx.LEFT , 0)
self._run_Del_button = wx.Button(self, label="DELETE Structure \n from World")
self._run_Del_button.Bind(wx.EVT_BUTTON, self._run_Del)
bottom_sizer.Add(self._run_Del_button, 0, wx.LEFT, 0)
self._mode_description = wx.TextCtrl(
self, style=wx.TE_MULTILINE | wx.TE_BESTWRAP
)
self._sizer.Add(self._mode_description, 25, wx.EXPAND | wx.TOP | wx.RIGHT, 0)
self._mode_description.Fit()
self.Layout()
self.Thaw()
@property
def wx_add_options(self) -> Tuple[int, ...]:
return (0,)
def _cls(self):
print("\033c\033[3J", end='')
def _fileSave(self):
print("SAVE")
def onFocus(self,evt):
setdata = self._structlist.GetString(self._structlist.GetSelection())
self._run_text.SetValue(setdata)
def _refresh_chunk(self, dimension, world, x, z):
cx, cz = block_coords_to_chunk_coords(x, z)
chunk = world.get_chunk(cx, cz, dimension)
chunk.changed = True
def _run_get_data(self, _):
world = self.world.level_wrapper._level_manager._db.keys()
for w in world:
if b'\xff' not in w:
if b'\x00' not in w:
print(w)
player = self.world.level_wrapper._level_manager._db.get(b'~local_player')
data = amulet_nbt.load(player, little_endian=True)
data2 = []
self._mode_description.SetValue(str(data))
#back to bytes
data2 = data.save_to(compressed=False,little_endian=True)
print(data2)
def _run_set_data(self, _): #b'structuretemplate_mystructure:test'
player = self.world.level_wrapper._level_manager._db.get(b'~local_player')
data = self._mode_description.GetValue()
nbt = from_snbt(data.replace('NBTFile("":',''))
nbtf = NBTFile(nbt)
#back to bytes
data2 = nbtf.save_to(compressed=False,little_endian=True)
print(data2)
self.world.level_wrapper._level_manager._db.put(b'~local_player', data2)
#for p in player:
#print(p)
def _run_get_sdata(self, _):
#sel = self._structlist.GetSelection()
setdata = self._run_text.GetValue()#self._structlist.GetString(self._structlist.GetSelection())
enS = setdata.encode("utf-8")
player = self.world.level_wrapper._level_manager._db.get(enS)
#
data = amulet_nbt.load(player, little_endian=True)
data2 = []
self._mode_description.SetValue(str(data))
# back to bytes
data2 = data.save_to(compressed=False, little_endian=True)
self._structlist.Append(setdata)
# for p in player:
# print(p)
def _run_set_sdata(self, _): # b'structuretemplate_mystructure:test'
if 'structuretemplate_mystructure:' not in self._run_text.GetValue():
fixKey = "structuretemplate_mystructure:" + self._run_text.GetValue()
theKey = fixKey.encode("utf-8")
else:
theKey = self._run_text.GetValue().encode("utf-8")
print(theKey)
data = self._mode_description.GetValue()
nbt = from_snbt(data.replace('NBTFile("":', ''))
nbtf = NBTFile(nbt)
# back to bytes
data2 = nbtf.save_to(compressed=False, little_endian=True)
print(data2)
self.world.level_wrapper._level_manager._db.put(theKey, data2)
# for p in player:
# print(p)
def _run_load(self, _):
with wx.FileDialog(self, "Open McStructure file", wildcard="mcstructure files (*.mcstructure)|*.mcstructure",
style=wx.FD_OPEN | wx.FD_FILE_MUST_EXIST) as fileDialog:
if fileDialog.ShowModal() == wx.ID_CANCEL:
return
pathname = fileDialog.GetPath()
data = amulet_nbt.load(pathname,compressed=False,little_endian=True)
self._mode_description.SetValue(str(data))
def _run_Del(self, _):
if 'structuretemplate_mystructure:' not in self._run_text.GetValue():
fixKey = "structuretemplate_mystructure:"+self._run_text.GetValue()
theKey = fixKey.encode("utf-8")
else:
theKey = self._run_text.GetValue().encode("utf-8")
wxx = wx.MessageBox("You are going to deleted \n " + str(theKey),
"This can't be undone Are you Sure?", wx.OK | wx.CANCEL | wx.ICON_INFORMATION)
if wxx == int(16):
return
self.world.level_wrapper._level_manager._db.delete(theKey)
def _run_get_slist(self):
world = self.world.level_wrapper._level_manager._db.keys()
currentStructure = []
currentStructure.append("Structures In this World")
for w in world:
if b'\xff' not in w:
if b'\x00' not in w:
if b'structuretemplate_mystructure' in w:
currentStructure.append(w)
return currentStructure
pass
#simple export options.
export = dict(name="Edit / Embed Mc Structure", operation=SetBlock)