forked from Podshot/MCEdit-Unified
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fileEdits.py
130 lines (116 loc) · 4.87 KB
/
fileEdits.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
from editortools.operation import Operation
import itertools
from albow import alert
class fileEdit:
def __init__(self, filename, timeChanged, box, editor, level):
self.filename = filename
self.timeChanged = timeChanged
self.box = box
self.editor = editor
self.level = level
self.order = []
def makeChanges(self):
try:
f = open(self.filename, 'rb')
except:
alert("Couldn't open the file")
return
lines = []
for line in f.readlines():
line = line.replace("\r", "")
if line != "\n":
lines.append(line.replace("\n", ""))
f.close()
tileEntities = []
for (x, y, z) in self.order:
blockAtXYZ = self.level.blockAt(x, y, z)
if blockAtXYZ in (137, 210, 211, 188, 189):
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 writeCommandInFile(self, first, space, (x, y, z), fileTemp, skip, chain, done, order):
block = self.editor.level.tileEntityAt(x, y, z)
if chain:
if not block or (x, y, z) in done:
return
if not first:
if space:
fileTemp.write("\n\n")
else:
fileTemp.write("\n")
text = block["Command"].value
if text == "":
text = "\"\""
order.append((x, y, z))
fileTemp.write(text.encode('utf-8'))
if chain:
done.append((x, y, z))
blockData = self.editor.level.blockDataAt(x, y, z)
if blockData == 0 and self.level.blockAt(x, y-1, z) in (211, 189):
skip.append((x, y-1, z))
self.writeCommandInFile(False, space, (x, y-1, z), fileTemp, skip, True, done, order)
elif blockData == 1 and self.level.blockAt(x, y+1, z) in (211, 189):
skip.append((x, y+1, z))
self.writeCommandInFile(False, space, (x, y+1, z), fileTemp, skip, True, done, order)
elif blockData == 2 and self.level.blockAt(x, y, z-1) in (211, 189):
skip.append((x, y, z-1))
self.writeCommandInFile(False, space, (x, y, z-1), fileTemp, skip, True, done, order)
elif blockData == 3 and self.level.blockAt(x, y, z+1) in (211, 189):
skip.append((x, y, z+1))
self.writeCommandInFile(False, space, (x, y, z+1), fileTemp, skip, True, done, order)
elif blockData == 4 and self.level.blockAt(x-1, y, z) in (211, 189):
skip.append((x-1, y, z))
self.writeCommandInFile(False, space, (x-1, y, z), fileTemp, skip, True, done, order)
elif blockData == 5 and self.level.blockAt(x+1, y, z) in (211, 189):
skip.append((x+1, y, z))
self.writeCommandInFile(False, space, (x+1, y, z), fileTemp, skip, True, done, order)
class FileEditsOperation(Operation):
def __init__(self, editor, level, box, lines, tileEntities):
self.editor = editor
self.level = level
self.box = box
self.lines = lines
self.tileEntities = tileEntities
self.undoLevel = None
self.canUndo = False
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 dirtyBox(self):
return self.box
def GetSort(box, sorting):
if sorting == "xz" or sorting == "chain":
return itertools.product(
xrange(box.minx, box.maxx),
xrange(box.miny, box.maxy),
xrange(box.minz, box.maxz)
)
else:
return itertools.product(
xrange(box.minz, box.maxz),
xrange(box.miny, box.maxy),
xrange(box.minx, box.maxx)
)