Skip to content

Commit

Permalink
Formalize quote usage
Browse files Browse the repository at this point in the history
  • Loading branch information
DonoA committed Aug 21, 2018
1 parent d5a4114 commit 93c5ef8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 59 deletions.
10 changes: 5 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import sys, world

with world.World("New World", save_location="/home/dallen/snap/minecraft/common/.minecraft/saves") as wrld:
print("World loaded!")
# results = world.get_chunk((6, 6)).find_like("redstone_wall_torch")
with world.World('New World', save_location='/home/dallen/snap/minecraft/common/.minecraft/saves') as wrld:
print('World loaded!')
# results = world.get_chunk((6, 6)).find_like('redstone_wall_torch')
# for r in results:
# print(r[0], r[1])
# print((r[0][0] % 16) + (r[0][2] % 16) * 16 + (r[0][1] % 16) * 16 ** 2)
ns = world.BlockState("minecraft:air", {})
ns = world.BlockState('minecraft:air', {})
chnk = wrld.get_chunk((6, 4))
for s in chnk.sections:
sec = chnk.sections[s]
for b in sec.blocks:
b.set_state(ns)

print("Saved!")
print('Saved!')
52 changes: 26 additions & 26 deletions nbt.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def __init__(self, tag_name, tag_value):
self.tag_name = tag_name
self.tag_value = tag_value

def print(self, indent=""):
print(indent + type(self).clazz_name + ": '" + self.tag_name + "' = " + str(self.tag_value))
def print(self, indent=''):
print(indent + type(self).clazz_name + ': '' + self.tag_name + '' = ' + str(self.tag_value))

def get(self):
return self.tag_value
Expand All @@ -61,15 +61,15 @@ class DataNBTTag:
@classmethod
def parse(cls, stream, name):
payload_length = int.from_bytes(stream.read(2), byteorder='big', signed=False)
payload = stream.read(payload_length).decode("utf-8")
payload = stream.read(payload_length).decode('utf-8')
return cls(name, payload)

def __init__(self, tag_name, tag_value):
self.tag_name = tag_name
self.tag_value = tag_value

def print(self, indent=""):
print(indent + "String: '" + self.tag_name + "' = " + str(self.tag_value))
def print(self, indent=''):
print(indent + 'String: '' + self.tag_name + '' = ' + str(self.tag_value))

def get(self):
return self.tag_value
Expand Down Expand Up @@ -102,7 +102,7 @@ def parse(cls, stream, name):
payload_length = int.from_bytes(stream.read(4), byteorder='big', signed=True)
tag = cls(name)
for i in range(payload_length):
tag.add_child(cls.clazz_sub_type.parse(stream, "None"))
tag.add_child(cls.clazz_sub_type.parse(stream, 'None'))
return tag

def __init__(self, tag_name, children=[]):
Expand All @@ -115,10 +115,10 @@ def add_child(self, tag):
def name(self):
return self.tag_name

def print(self, indent=""):
print(indent + type(self).clazz_name + ": '" + self.tag_name + "' size " + str(len(self.children)))
def print(self, indent=''):
print(indent + type(self).clazz_name + ': '' + self.tag_name + '' size ' + str(len(self.children)))
for c in self.children:
c.print(indent + " ")
c.print(indent + ' ')

def serialize(self, stream, include_name=True):
if include_name:
Expand Down Expand Up @@ -147,7 +147,7 @@ def parse(cls, stream, name):
payload_length = int.from_bytes(stream.read(4), byteorder='big', signed=True)
tag = cls(name, sub_type)
for i in range(payload_length):
tag.add_child(_parsers[sub_type].parse(stream, "None"))
tag.add_child(_parsers[sub_type].parse(stream, 'None'))
return tag

def __init__(self, tag_name, sub_type_id, children=[]):
Expand All @@ -161,10 +161,10 @@ def add_child(self, tag):
def name(self):
return self.tag_name

def print(self, indent=""):
print(indent + "List: '" + self.tag_name + "' size " + str(len(self.children)))
def print(self, indent=''):
print(indent + 'List: '' + self.tag_name + '' size ' + str(len(self.children)))
for c in self.children:
c.print(indent + " ")
c.print(indent + ' ')

def serialize(self, stream, include_name=True):
if include_name:
Expand Down Expand Up @@ -216,10 +216,10 @@ def to_dict(self):
nd[p] = self.children[p].get()
return nd

def print(self, indent=""):
print(indent + "Compound: '" + self.tag_name + "' size " + str(len(self.children)))
def print(self, indent=''):
print(indent + 'Compound: '' + self.tag_name + '' size ' + str(len(self.children)))
for c in self.children:
self.children[c].print(indent + " ")
self.children[c].print(indent + ' ')

def serialize(self, stream, include_name=True):
if include_name:
Expand All @@ -237,28 +237,28 @@ def serialize(self, stream, include_name=True):

_parsers = {}

ByteTag = create_simple_nbt_class(1, "Byte", 1, ">b")
ShortTag = create_simple_nbt_class(2, "Short", 2, ">h")
IntTag = create_simple_nbt_class(3, "Int", 4, ">i")
LongTag = create_simple_nbt_class(4, "Long", 8, ">q")
FloatTag = create_simple_nbt_class(5, "Float", 4, ">f")
DoubleTag = create_simple_nbt_class(6, "Double", 8, ">d")
ByteTag = create_simple_nbt_class(1, 'Byte', 1, '>b')
ShortTag = create_simple_nbt_class(2, 'Short', 2, '>h')
IntTag = create_simple_nbt_class(3, 'Int', 4, '>i')
LongTag = create_simple_nbt_class(4, 'Long', 8, '>q')
FloatTag = create_simple_nbt_class(5, 'Float', 4, '>f')
DoubleTag = create_simple_nbt_class(6, 'Double', 8, '>d')

ByteArrayTag = create_array_nbt_class(7, "ByteArray", ByteTag)
ByteArrayTag = create_array_nbt_class(7, 'ByteArray', ByteTag)

StringTag = create_string_nbt_class(8)
ListTag = create_list_nbt_class(9)
CompoundTag = create_compund_nbt_class(10)

IntArrayTag = create_array_nbt_class(11, "IntArray", IntTag)
LongArrayTag = create_array_nbt_class(12, "LongArray", LongTag)
IntArrayTag = create_array_nbt_class(11, 'IntArray', IntTag)
LongArrayTag = create_array_nbt_class(12, 'LongArray', LongTag)

def parse_nbt(stream):
global _parsers

tag_type = int.from_bytes(stream.read(1), byteorder='big', signed=False)
tag_name_length = int.from_bytes(stream.read(2), byteorder='big', signed=False)
tag_name = stream.read(tag_name_length).decode("utf-8")
tag_name = stream.read(tag_name_length).decode('utf-8')

return _parsers[tag_type].parse(stream, tag_name)

56 changes: 28 additions & 28 deletions world.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ def __init__(self, name, props):
self.props = props

def __str__(self):
return "BlockState(" + self.name + "," + str(self.props) + ")"
return 'BlockState(' + self.name + ',' + str(self.props) + ')'

class Block:
def __init__(self, state):
self.state = state
self.dirty = True

def __str__(self):
return "Block(" + str(self.state) + ")"
return 'Block(' + str(self.state) + ')'

def set_state(self, state):
self.dirty = True
Expand Down Expand Up @@ -47,15 +47,15 @@ def serialize(self):
return serial_section

def _serialize_palette(self):
serial_palette = nbt.ListTag("Palette", nbt.CompoundTag.clazz_id)
serial_palette = nbt.ListTag('Palette', nbt.CompoundTag.clazz_id)
for i in range(len(self.palette)):
state = self.palette[i]
state.id = i
palette_item = nbt.CompoundTag("None", children=[
nbt.StringTag("Name", state.name)
palette_item = nbt.CompoundTag('None', children=[
nbt.StringTag('Name', state.name)
])
if len(state.props) != 0:
serial_props = nbt.CompoundTag("Properties")
serial_props = nbt.CompoundTag('Properties')
for name, val in state.props.items():
serial_props.add_child(nbt.StringTag(name, str(val)))
palette_item.add_child(serial_props)
Expand All @@ -64,7 +64,7 @@ def _serialize_palette(self):
return serial_palette

def _serialize_blockstates(self):
serial_states = nbt.LongArrayTag("BlockStates")
serial_states = nbt.LongArrayTag('BlockStates')
width = math.ceil(math.log(len(self.palette), 2))
if width < 4:
width = 4
Expand All @@ -75,8 +75,8 @@ def _serialize_blockstates(self):
mask = (2 ** 64) - 1
for i in range(int((len(self.blocks) * width)/64)):
lng = data & mask
lng = int.from_bytes(lng.to_bytes(8, byteorder="big", signed=False), byteorder="big", signed=True)
serial_states.add_child(nbt.LongTag("", lng))
lng = int.from_bytes(lng.to_bytes(8, byteorder='big', signed=False), byteorder='big', signed=True)
serial_states.add_child(nbt.LongTag('', lng))
data = data >> 64
return serial_states

Expand Down Expand Up @@ -112,30 +112,30 @@ def find_like(self, string):
# This selects the pack size, then splits out the ids
def unpack(raw_nbt):
sections = {}
for section in raw_nbt.get("Level").get("Sections").children:
flatstates = [c.get() for c in section.get("BlockStates").children]
for section in raw_nbt.get('Level').get('Sections').children:
flatstates = [c.get() for c in section.get('BlockStates').children]
pack_size = int((len(flatstates) * 64) / (16**3))
states = [
Chunk._read_width_from_loc(flatstates, pack_size, i) for i in range(16**3)
]
palette = [
BlockState(
state.get("Name").get(),
state.get("Properties").to_dict() if state.has("Properties") else {}
) for state in section.get("Palette").children
state.get('Name').get(),
state.get('Properties').to_dict() if state.has('Properties') else {}
) for state in section.get('Palette').children
]
blocks = [
Block(palette[state]) for state in states
]
sections[section.get("Y").get()] = ChunkSection(blocks, palette, section)
sections[section.get('Y').get()] = ChunkSection(blocks, palette, section)

return sections

def pack(self):
new_sections = nbt.ListTag("Sections", nbt.CompoundTag.clazz_id, children=[
new_sections = nbt.ListTag('Sections', nbt.CompoundTag.clazz_id, children=[
self.sections[sec].serialize() for sec in self.sections
])
self.raw_nbt.get("Level").add_child(new_sections)
self.raw_nbt.get('Level').add_child(new_sections)

return self.raw_nbt

Expand Down Expand Up @@ -169,7 +169,7 @@ def _read_bits(num, width, start):
return comp

class World:
def __init__(self, file_name, save_location=""):
def __init__(self, file_name, save_location=''):
self.file_name = file_name
self.save_location = save_location
self.chunks = {}
Expand All @@ -182,7 +182,7 @@ def __exit__(self, typ, val, trace):

def close(self):
for chunk_pos, chunk in self.chunks.items():
with open(self.save_location + "/" + self.file_name + "/region/" + self._get_region_file(chunk_pos), mode="r+b") as region:
with open(self.save_location + '/' + self.file_name + '/region/' + self._get_region_file(chunk_pos), mode='r+b') as region:
locations = [[
int.from_bytes(region.read(3), byteorder='big', signed=False) * 4096,
int.from_bytes(region.read(1), byteorder='big', signed=False) * 4096
Expand All @@ -207,19 +207,19 @@ def close(self):
if data_len_diff != 0:
print(data_len_diff, block_data_len)
print(block_data_len - datalen)
print("diff is not 0, I would stop now")
print('diff is not 0, I would stop now')
# sys.exit(0)

# shift file as needed handle new data
region.seek(loc[0])
region.write(datalen.to_bytes(4, byteorder='big', signed=False))
region.write((2).to_bytes(1, byteorder="big", signed=False))
region.write((2).to_bytes(1, byteorder='big', signed=False))
region.write(data)
region.write((0).to_bytes(block_data_len - datalen, byteorder="big", signed=False))
region.write((0).to_bytes(block_data_len - datalen, byteorder='big', signed=False))

region.write(rest_of_file)
required_padding = (math.ceil(region.tell()/4096.0) * 4096) - region.tell()
region.write((0).to_bytes(required_padding, byteorder="big", signed=False))
region.write((0).to_bytes(required_padding, byteorder='big', signed=False))

# print(datalen, chunk_len, len(strm.get_data()))
# write in the location and length we will be using
Expand All @@ -232,8 +232,8 @@ def close(self):
# print(max([l[1] for l in locations]))
# print(locations)
for c_loc in locations:
region.write(int(c_loc[0]/4096).to_bytes(3, byteorder="big", signed=False))
region.write(int(c_loc[1]/4096).to_bytes(1, byteorder="big", signed=False))
region.write(int(c_loc[0]/4096).to_bytes(3, byteorder='big', signed=False))
region.write(int(c_loc[1]/4096).to_bytes(1, byteorder='big', signed=False))

# make sure the last chunk is padded to make the whole file a multiple of 4 KB

Expand All @@ -250,7 +250,7 @@ def get_chunk(self, chunk_pos):
return self.chunks[chunk_pos]

def _load_chunk(self, chunk_pos):
with open(self.save_location + "/" + self.file_name + "/region/" + self._get_region_file(chunk_pos), mode="rb") as region:
with open(self.save_location + '/' + self.file_name + '/region/' + self._get_region_file(chunk_pos), mode='rb') as region:
locations = [[
int.from_bytes(region.read(3), byteorder='big', signed=False) * 4096,
int.from_bytes(region.read(1), byteorder='big', signed=False) * 4096
Expand All @@ -272,7 +272,7 @@ def _load_binary_chunk_at(self, region_file, offset):
# nstrm = stream.OutputStream()
# data.serialize(nstrm)
# print(len(nstrm.get_data()), len(zlib.compress(nstrm.get_data())))
chunk_pos = (data.get("Level").get("xPos").get(), data.get("Level").get("zPos").get())
chunk_pos = (data.get('Level').get('xPos').get(), data.get('Level').get('zPos').get())
chunk = Chunk(
chunk_pos[0],
chunk_pos[1],
Expand All @@ -288,7 +288,7 @@ def _load_binary_chunk_at(self, region_file, offset):
return chunk

def _get_region_file(self, chunk_pos):
return "r." + '.'.join([str(x) for x in self._get_region(chunk_pos)]) + '.mca'
return 'r.' + '.'.join([str(x) for x in self._get_region(chunk_pos)]) + '.mca'


def _get_chunk(self, block_pos):
Expand Down

0 comments on commit 93c5ef8

Please sign in to comment.