-
Notifications
You must be signed in to change notification settings - Fork 0
/
lobotomob.py
202 lines (177 loc) · 6.96 KB
/
lobotomob.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
#!/usr/bin/python3.9
import argparse
from quarry.types import nbt
from quarry.types.buffer import Buffer
import os
import glob
import time
import zlib
def valid_dir(arg_parser, arg):
if not os.path.isdir(arg):
arg_parser.error(f'The directory {arg} does not exist.')
os.chdir(arg)
if not os.path.isfile('level.dat'):
arg_parser.error(f'The directory does not appear to be a valid Minecraft world.')
if not os.path.isdir('entities/'):
arg_parser.error(f'No entities folder found. Try updating the world to the latest Minecraft version and try '
f'again.')
os.chdir('entities/')
return glob.glob('*.mca')
parser = argparse.ArgumentParser(description='Disable mob AI and other shit on Minecraft worlds.')
parser.add_argument('world_folder', type=lambda x: valid_dir(parser, x), help='Path to the Minecraft world folder.')
args = parser.parse_args()
class EntityRegion(nbt.RegionFile):
"""
Function `save_chunk` is modified from https://github.com/barneygale/quarry/blob/master/quarry/types/nbt.py
------
Copyright (C) 2020 Barnaby Gale
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies of the Software and its documentation and acknowledgment shall be
given in the documentation and software packages that this Software was
used.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
def save_chunk(self, chunk, chunk_x, chunk_z):
"""
Saves the given chunk, which should be a ``TagRoot``, to the region
file.
"""
# Compress chunk
chunk = zlib.compress(chunk.to_bytes())
chunk = Buffer.pack('IB', len(chunk), 2) + chunk
chunk_length = 1 + (len(chunk) - 1) // 4096
# Load extents
extents = [(0, 2)]
self.fd.seek(0)
buff = Buffer(self.fd.read(4096))
for idx in range(1024):
z, x = divmod(idx, 32)
entry = buff.unpack('I')
offset, length = entry >> 8, entry & 0xFF
if offset > 0 and not (x == chunk_x and z == chunk_z):
extents.append((offset, length))
extents.sort()
extents.append((extents[-1][0] + extents[-1][1] + chunk_length, 0))
# Compute new extent
for idx in range(len(extents) - 1):
start = extents[idx][0] + extents[idx][1]
end = extents[idx + 1][0]
if (end - start) >= chunk_length:
chunk_offset = start
extents.insert(idx + 1, (chunk_offset, chunk_length))
break
# Write extent header
self.fd.seek(4 * (32 * chunk_z + chunk_x))
self.fd.write(Buffer.pack(
'I', (chunk_offset << 8) | (chunk_length & 0xFF)))
# Write timestamp header
self.fd.seek(4096 + 4 * (32 * chunk_z + chunk_x))
self.fd.write(Buffer.pack('I', int(time.time())))
# Write chunk
self.fd.seek(4096 * chunk_offset)
self.fd.write(chunk)
# Truncate file
self.fd.seek(4096 * extents[-1][0])
self.fd.truncate()
tags = nbt.TagCompound({
'Invulnerable': nbt.TagInt(1),
'NoAI': nbt.TagInt(1),
'NoGravity': nbt.TagInt(1),
'PersistenceRequired': nbt.TagInt(1),
'Silent': nbt.TagInt(1),
'Fire': nbt.TagInt(1)
})
entity_blacklist = [
'minecraft:area_effect_cloud',
'minecraft:armor_stand',
'minecraft:arrow',
'minecraft:boat',
'minecraft:chest_minecart',
'minecraft:command_block_minecart',
'minecraft:dragon_fireball',
'minecraft:egg',
'minecraft:end_crystal',
'minecraft:ender_pearl',
'minecraft:evoker_fangs',
'minecraft:experience_bottle',
'minecraft:experience_orb',
'minecraft:eye_of_ender',
'minecraft:falling_block',
'minecraft:fireball',
'minecraft:firework_rocket',
'minecraft:fox',
'minecraft:furnace_minecart',
'minecraft:glow_item_frame',
'minecraft:hopper_minecart',
'minecraft:item',
'minecraft:item_frame',
'minecraft:leash_knot',
'minecraft:lightning_bolt',
'minecraft:llama_spit',
'minecraft:marker',
'minecraft:minecart',
'minecraft:painting',
'minecraft:potion',
'minecraft:small_fireball',
'minecraft:snowball',
'minecraft:spawner_minecart',
'minecraft:spectral_arrow',
'minecraft:tnt',
'minecraft:tnt_minecart',
'minecraft:trident',
'minecraft:wither_skull'
]
heliophobic = [
'minecraft:skeleton',
'minecraft:zombie',
'minecraft:zombie_villager',
'minecraft:stray',
'minecraft:drowned'
]
files = args.world_folder
total_files = len(files)
print(f'Found {total_files} region files.')
entity_count = 0
entity_total_count = 0
file_count = 0
chunk_count = 0
for file in files:
file_count += 1
print(f'Opening region file {file_count}/{total_files}: {file}')
region = EntityRegion(file)
for chunk_x in range(0, 32):
for chunk_z in range(0, 32):
try:
chunk = region.load_chunk(chunk_x, chunk_z)
except:
continue
chunk_count += 1
compound_data = chunk.body.value
entities = compound_data['Entities'].value
for entity in entities:
if entity.value['id'].value not in entity_blacklist:
entity.update(tags)
# Let's stop them from burning for eternity
if entity.value['id'].value in heliophobic:
armor_items = entity.value.get('ArmorItems', nbt.TagList(
[nbt.TagCompound({}), nbt.TagCompound({}), nbt.TagCompound({}), nbt.TagCompound({})]))
if not armor_items.value[3].value:
new_armor = armor_items.value
new_armor[3] = nbt.TagCompound(
{'Count': nbt.TagInt(1), 'id': nbt.TagString('stone_button')})
entity_count += 1
entity_total_count += 1
region.save_chunk(chunk, chunk_x, chunk_z)
region.close()
print(f'Done! Updated {entity_count} out of {entity_total_count} entities over {chunk_count} chunks.')