-
Notifications
You must be signed in to change notification settings - Fork 15
/
BlockEditor.v1_2.py
307 lines (258 loc) · 11.4 KB
/
BlockEditor.v1_2.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from typing import TYPE_CHECKING, Tuple
import amulet_nbt
from amulet.api.wrapper import Interface, EntityIDType, EntityCoordType
import numpy
import urllib.request
import wx
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.api.selection import SelectionBox
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 datetime
from pathlib import Path
from amulet_map_editor.programs.edit.api.behaviour import StaticSelectionBehaviour
from amulet_map_editor.programs.edit.api.events import EVT_SELECTION_CHANGE
from amulet_map_editor.programs.edit.api.behaviour import BlockSelectionBehaviour
from amulet_map_editor.programs.edit.api.behaviour.pointer_behaviour import EVT_POINT_CHANGE
from amulet_map_editor.programs.edit.api.behaviour.pointer_behaviour import PointChangeEvent
from amulet_map_editor.programs.edit.api.behaviour.pointer_behaviour import PointerBehaviour
from amulet_map_editor.programs.edit.api.key_config import ACT_BOX_CLICK
from amulet_map_editor.programs.edit.api.events import (
EVT_PASTE,
InputPressEvent,
EVT_INPUT_PRESS,
)
# 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
def saveHistoy(data):
f = open(os.path.join(os.path.dirname(__file__), "history.txt"), "a", encoding="utf-8")
f.write(data)
f.close()
historyFile = Path(os.path.join(os.path.dirname(__file__), "history.txt"))
if historyFile.is_file():
print("Making Histoy.txt fileReady")
else:
d = "History of Get and Set~~Your History Will Be here\n"
saveHistoy(d)
def LoadHistory():
with open(os.path.join(os.path.dirname(__file__), "history.txt"), "r", encoding="utf-8") as f:
list2 = []
historyTime = []
historyDatas = []
for item in f:
list2.append(item.replace('\n', ''))
for lz in list2:
lb = lz.split("~~")
historyTime.append(lb[0])
historyDatas.append(lb[1])
return historyTime, historyDatas
TT, DD = LoadHistory()
res = {TT[i]: DD[i] for i in range(len(TT))}
operation_modes = {
"Java": "java",
"Bedrock": "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
self._is_enabled = True
self._moving = True
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)
options = self._load_options({})
top_sizer = wx.BoxSizer(wx.VERTICAL)
side_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)
# choicebox for operation mode selection.
self._mode = wx.Choice(self, choices=list(operation_modes.keys()))
self._mode.SetSelection(1)
self._history = wx.Choice(self, choices=list(res.keys()))
self._history.SetSelection(0)
self._history.Bind(wx.EVT_CHOICE, self.onFocus)
side_sizer.Add(self._mode, 0, wx.TOP | wx.LEFT, 5)
side_sizer.Add(self._history, 0, wx.TOP | wx.LEFT, 5)
self._run_button = wx.Button(self, label="Get Selected\n Block SNBT")
self._run_button.Bind(wx.EVT_BUTTON, self._run2_operation)
side_sizer.Add(self._run_button, 0, wx.TOP | wx.LEFT, 5)
self._run_button = wx.Button(self, label="Set Selected\n Block SNBT")
self._run_button.Bind(wx.EVT_BUTTON, self._run_operation)
side_sizer.Add(self._run_button, 10, wx.TOP | wx.LEFT, 5)
self.sel = wx.Button(self, label="Select")
self.sel.Bind(wx.EVT_BUTTON, self._sel)
side_sizer.Add(self.sel, 10, wx.TOP | wx.LEFT, 5)
self._mode_description = wx.TextCtrl(
self, style=wx.TE_MULTILINE | wx.TE_BESTWRAP
)
self._sizer.Add(self._mode_description, 25, wx.EXPAND | wx.LEFT | 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 Onmsgbox(self):
wx.MessageBox("You need to have valid snbt data in the Text field.\n This field cant be Empty.",
"Something Went Wrong", wx.OK | wx.ICON_INFORMATION)
def onFocus(self, evt):
setdata = res[self._history.GetString(self._history.GetSelection())]
self._mode_description.SetValue(setdata)
def _sel(self, _):
self._selection = StaticSelectionBehaviour(self.canvas)
self._curs = PointerBehaviour(self.canvas)
self._selection.bind_events()
self._curs.bind_events()
self.canvas.Bind(EVT_POINT_CHANGE, self._on_pointer_change)
self.canvas.Bind(EVT_INPUT_PRESS, self._on_input_press)
self.click = True
self._is_enabled = True
self._moving = True
self._on_pointer_change
def _on_pointer_change(self, evt: PointChangeEvent):
if self._is_enabled:
self.canvas.renderer.fake_levels.active_transform = (
evt.point
)
x, y, z = evt.point
a, b, c = 1, 1, 1 # self.abc
sg = SelectionGroup(SelectionBox((x, y, z), (x + a, y + b, z + c)))
self.canvas.selection.set_selection_group(sg)
evt.Skip()
def _on_input_press(self, evt: InputPressEvent):
if evt.action_id == ACT_BOX_CLICK:
if self._is_enabled == True:
self._moving = not self._moving
self._is_enabled = False
return
if self._is_enabled == False:
self._is_enabled = True
self._on_pointer_change
return
if self._moving:
self.canvas.renderer.fake_levels.active_transform = ()
evt.Skip()
def _run_operation(self, _):
block_platform = "bedrock"
block_version = (1, 17, 0)
data = self._mode_description.GetValue()
if data == "":
self.Onmsgbox()
return
data = str(data).replace('NBTFile({','NBTFile("":{')
print(data)
dataSplit = data.split("|")
dataPartBlock = dataSplit[0]
print(dataSplit[0])
dataPartEntity = dataSplit[1]
NBT = None
direction = 2
Snbt = from_snbt("[]")
posBlocknameStart = dataPartBlock.find(":") + 1
if "[" in dataPartBlock:
posBlocknameEnd = dataPartBlock.find("[")
else:
posBlocknameEnd = None
posBlockDataStart = dataPartBlock.find("[") + 1
posBlockDataEnd = dataPartBlock.find("]")
blockName = dataPartBlock[posBlocknameStart:posBlocknameEnd]
fixedBlockFormat = dataPartBlock[posBlockDataStart:posBlockDataEnd]
if "=" in dataPartBlock:
fixedBlockFormat = dataPartBlock[posBlockDataStart:posBlockDataEnd].replace("=", ":") # .replace(",","],[")
if "[" in data:
Snbt = from_snbt("{" + fixedBlockFormat + "}")
block = Block("minecraft", blockName, dict(Snbt))
else:
Snbt = from_snbt("{" + fixedBlockFormat + "}")
block = Block("minecraft", blockName, dict(Snbt))
if "BlockEntity" in data:
entity = data.find('NBTFile') + 11
NBT = from_snbt(data[entity:])
blockEntity = BlockEntity("minecraft", blockName.replace('_', '').capitalize(), 0, 0, 0, NBTFile(NBT))
else:
blockEntity = None
historyData = '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()) + "~~" + str(block) + "|" + str(
blockEntity).replace('\n\n\n', '') + "\n"
saveHistoy(historyData)
hs = historyData.split("~~")
res.update({hs[0]: hs[1]})
self._history.Clear()
self._history.Append(list(res.keys()))
self._history.SetSelection(0)
for (box) in (self.canvas.selection.selection_group):
pos = px, py, pz = box.min_x, box.min_y, box.min_z
self.world.set_version_block(px, py, pz, self.canvas.dimension, (block_platform, block_version), block,
blockEntity)
self.canvas.run_operation(lambda: self._refresh_chunk(self.canvas.dimension, self.world, px, pz))
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 _run2_operation(self, _):
block_platform = operation_modes[self._mode.GetString(self._mode.GetSelection())]
block_version = (1, 17, 0)
for (box) in (self.canvas.selection.selection_group):
pos = x, y, z = box.min_x, box.min_y, box.min_z
#pmax = x, y, z = box.min_x, box.min_y, box.min_z
# for rx in range (box.min_x, box.max_x):
# for ry in range(box.min_y, box.max_y):
# for rz in range(box.min_z, box.max_z):
block, blockEntity = self.world.get_version_block(x, y, z, self.canvas.dimension,
(block_platform, block_version))
bbb = self.world.get_block(x, y, z, self.canvas.dimension)
print(block)
print(blockEntity)
print(bbb)
SpData = str(blockEntity).split("\"\":")
#self._mode_description.SetValue(str(block) + "|" + str(blockEntity).replace('\n\n\n', ''))
try:
headerData = amulet_nbt.from_snbt(SpData[0])
blockEData = amulet_nbt.from_snbt(SpData[1])
self._mode_description.SetValue( str(block) + "|\n" + SpData[0] + blockEData.to_snbt(2))
except:
self._mode_description.SetValue(str(block) + "|" + str(blockEntity) )
historyData = '{:%Y-%b-%d %H:%M:%S}'.format(datetime.datetime.now()) + "~~" + str(block) + "|" + str(
blockEntity).replace('\n\n\n', '') + "\n"
saveHistoy(historyData)
hs = historyData.split("~~")
res.update({hs[0]: hs[1]})
self._history.Clear()
self._history.Append(list(res.keys()))
self._history.SetSelection(0)
pass
# simple export options.
export = dict(name="A Block editor, v1.2", operation=SetBlock) #By PremiereHell