-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmineflayer.py
331 lines (278 loc) · 12.2 KB
/
mineflayer.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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
import time
import logging
from javascript import require, On, Once, AsyncTask, once, off
import os
from threading import Thread
mineflayer = require('mineflayer')
Vec3 = require('vec3').Vec3
Buffer = require('buffer').Buffer or bot._global.Buffer
# Configure logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Environment variables with defaults
HOST = os.getenv('HOST', '127.0.0.1')
PORT = int(os.getenv('PORT', '25565'))
VERSION = os.getenv('VERSION', '1.20.4')
USERNAME = os.getenv('USERNAME', 'builder')
DELAY = int(os.getenv('DELAY', '1000')) # Increased delay to prevent spamming
STRUCTURE_NAME = os.getenv('STRUCTURE_NAME', f'structure_{time.strftime("%Y-%m-%dT%H-%M-%S")}')
def build_structure(function_definition, metadata=None):
"""Build a structure from a function definition"""
try:
global bot, commandQueue, coordinateTracker
logger.info("Starting build_structure function")
# Log metadata if provided
if metadata:
logger.info(f"Building structure: {metadata.get('name', 'Unnamed')}")
logger.info(f"Author: {metadata.get('author', 'Unknown')}")
logger.info(f"Description: {metadata.get('description', 'No description')}")
BOT_USERNAME = 'Builder'
# Add retry logic for bot connection
max_retries = 3
retry_delay = 5
last_error = None
for attempt in range(max_retries):
try:
logger.info(f"Attempting to connect bot (attempt {attempt + 1}/{max_retries})")
bot = mineflayer.createBot({
'host': HOST,
'port': PORT,
'username': BOT_USERNAME,
'version': VERSION,
'hideErrors': False,
'connectTimeout': 30000, # 30 seconds timeout
})
logger.info("Bot connection successful")
break
except Exception as e:
last_error = e
logger.error(f"Connection attempt {attempt + 1} failed: {e}")
if attempt < max_retries - 1:
logger.info(f"Retrying in {retry_delay} seconds...")
time.sleep(retry_delay)
else:
raise Exception(f"Failed to connect after {max_retries} attempts: {last_error}")
commandQueue = CommandQueue()
coordinateTracker = CoordinateTracker()
# Set up event handlers
@On(bot, 'kicked')
def on_kicked(this, reason, logged_in):
logger.warning(f'Bot was kicked! Reason: {reason}')
@On(bot, 'error')
def on_error(this, err):
logger.error(f'Bot encountered an error: {err}')
# Wait for spawn with timeout
logger.info('Waiting for login...')
spawn_timeout = 30 # 30 seconds timeout
spawn_start = time.time()
while time.time() - spawn_start < spawn_timeout:
try:
once(bot, 'spawn')
logger.info('Bot spawned successfully')
break
except Exception as e:
if time.time() - spawn_start >= spawn_timeout:
raise Exception(f"Bot spawn timeout after {spawn_timeout} seconds")
time.sleep(1)
# Short delay for spawn
time.sleep(1)
# Execute the build
logger.info("Starting build execution")
buildCreation(function_definition)
# Wait for commands to complete
logger.info("Waiting for build commands to complete")
while commandQueue.isProcessing:
time.sleep(0.5)
# Save structure
structure_name = STRUCTURE_NAME
logger.info(f"Saving structure as: {structure_name}")
saveStructure(structure_name)
# Wait for commands to complete
logger.info("Waiting for all commands to complete")
while commandQueue.isProcessing:
time.sleep(0.5)
# Wait for commands to complete
time.sleep(1)
# Clean exit
logger.info("Build completed, disconnecting bot")
bot.quit()
dimensions = coordinateTracker.getDimensions()
logger.info(f"Build dimensions: {dimensions}")
return {
'status': 'success',
'structure_name': structure_name,
'dimensions': dimensions,
'metadata': metadata
}
except Exception as e:
logger.exception(f'Build failed: {str(e)}')
if 'bot' in globals():
bot.quit()
return {
'status': 'error',
'error': str(e),
'metadata': metadata
}
# Command queue system
class CommandQueue:
def __init__(self, delay=DELAY):
self.queue = []
self.isProcessing = False
self.DELAY = delay
self.logger = logging.getLogger(__name__ + '.CommandQueue')
def add(self, command):
self.queue.append(command)
self.logger.debug(f"Added command to queue: {command}")
if not self.isProcessing:
self.isProcessing = True
thread = Thread(target=self.processQueue)
thread.start()
def processQueue(self):
self.logger.info("Started processing command queue")
while self.queue:
command = self.queue.pop(0)
try:
def handle_chat(username, message):
if username == 'Server':
self.logger.info(f"Command response: {message}")
bot.remove_listener('chat', handle_chat)
bot.on('chat', handle_chat)
bot.chat(command)
self.logger.info(f'Executed command: {command}')
except Exception as e:
self.logger.error(f'Error executing command "{command}": {e}')
time.sleep(self.DELAY / 1000)
self.isProcessing = False
self.logger.info("Finished processing command queue")
# Coordinate tracking system
class CoordinateTracker:
def __init__(self):
self.coordinates = []
self.boundingBox = None
self.logger = logging.getLogger(__name__ + '.CoordinateTracker')
def addCoordinate(self, x, y, z):
self.coordinates.append({'x': x, 'y': y, 'z': z})
self.updateBoundingBox()
self.logger.debug(f"Added coordinate: ({x}, {y}, {z})")
def updateBoundingBox(self):
if not self.coordinates:
return
xs = [c['x'] for c in self.coordinates]
ys = [c['y'] for c in self.coordinates]
zs = [c['z'] for c in self.coordinates]
self.boundingBox = {
'min': {
'x': min(xs),
'y': min(ys),
'z': min(zs)
},
'max': {
'x': max(xs),
'y': max(ys),
'z': max(zs)
}
}
self.logger.debug(f"Updated bounding box: {self.boundingBox}")
def getBoundingBox(self):
return self.boundingBox
def getDimensions(self):
if not self.boundingBox:
return None
dimensions = {
'width': self.boundingBox['max']['x'] - self.boundingBox['min']['x'] + 1,
'height': self.boundingBox['max']['y'] - self.boundingBox['min']['y'] + 1,
'depth': self.boundingBox['max']['z'] - self.boundingBox['min']['z'] + 1
}
self.logger.info(f"Structure dimensions: {dimensions}")
return dimensions
def safeSetBlock(x, y, z, blockType, options={}):
logger = logging.getLogger(__name__ + '.safeSetBlock')
# Ensure coordinates are integers
x, y, z = map(int, (x, y, z))
try:
# Add minecraft: namespace if not present
fullBlockType = blockType if ':' in blockType else f'minecraft:{blockType}'
command = f"/setblock {x} {y} {z} {fullBlockType}"
# Add block states if provided
blockStates = options.get('blockStates')
if blockStates and blockStates.keys():
stateString = ','.join([f"{key}={value}" for key, value in blockStates.items()])
command += f'[{stateString}]'
# Add placement mode if provided
mode = options.get('mode')
if mode:
validModes = ['replace', 'destroy', 'keep']
if mode not in validModes:
raise ValueError(f"Invalid placement mode: {mode}. Must be one of: {', '.join(validModes)}")
command += f' {mode}'
commandQueue.add(command)
coordinateTracker.addCoordinate(x, y, z)
logger.debug(f"Block placed at ({x}, {y}, {z}): {fullBlockType}")
except Exception as e:
logger.error(f"Error placing block at {x} {y} {z}: {e}")
raise e
def safeFill(x1, y1, z1, x2, y2, z2, blockType, options={}):
logger = logging.getLogger(__name__ + '.safeFill')
# Ensure coordinates are integers
x1, y1, z1, x2, y2, z2 = map(int, (x1, y1, z1, x2, y2, z2))
try:
# Add minecraft: namespace if not present
fullBlockType = blockType if ':' in blockType else f'minecraft:{blockType}'
command = f"/fill {x1} {y1} {z1} {x2} {y2} {z2} {fullBlockType}"
# Add block states if provided
blockStates = options.get('blockStates')
if blockStates and blockStates.keys():
stateString = ','.join([f"{key}={value}" for key, value in blockStates.items()])
command += f'[{stateString}]'
# Handle fill modes and replace filter
mode = options.get('mode')
if mode:
validModes = ['destroy', 'hollow', 'keep', 'outline', 'replace']
if mode not in validModes:
raise ValueError(f"Invalid fill mode: {mode}. Must be one of: {', '.join(validModes)}")
command += f' {mode}'
# Handle replace filter if specified
if mode == 'replace' and options.get('replaceFilter'):
fullReplaceFilter = options['replaceFilter'] if ':' in options['replaceFilter'] else f"minecraft:{options['replaceFilter']}"
command += f' {fullReplaceFilter}'
# Add replace filter block states if provided
replaceFilterStates = options.get('replaceFilterStates')
if replaceFilterStates and replaceFilterStates.keys():
filterStateString = ','.join([f"{key}={value}" for key, value in replaceFilterStates.items()])
command += f'[{filterStateString}]'
commandQueue.add(command)
# Track corners of the filled region
for x in [x1, x2]:
for y in [y1, y2]:
for z in [z1, z2]:
coordinateTracker.addCoordinate(x, y, z)
logger.debug(f"Fill command executed from ({x1},{y1},{z1}) to ({x2},{y2},{z2}): {fullBlockType}")
except Exception as e:
logger.error(f"Error filling from ({x1},{y1},{z1}) to ({x2},{y2},{z2}): {e}")
raise e
def saveStructure(name):
logger = logging.getLogger(__name__ + '.saveStructure')
boundingBox = coordinateTracker.getBoundingBox()
if not boundingBox:
logger.warning('No blocks placed yet to create structure')
return
min_x, min_y, min_z = boundingBox['min']['x'], boundingBox['min']['y'], boundingBox['min']['z']
max_x, max_y, max_z = boundingBox['max']['x'], boundingBox['max']['y'], boundingBox['max']['z']
# Use WorldEdit commands to select and copy the region
commandQueue.add(f"//pos1 {min_x},{min_y},{min_z}")
commandQueue.add(f"//pos2 {max_x},{max_y},{max_z}")
commandQueue.add("//copy") # Copy the selection to clipboard
# Save the copied selection to a schematic file
commandQueue.add(f"//schem save {name}")
# Verify the save worked
commandQueue.add(f"//schem list")
logger.info(f"Structure saved: {name} from ({min_x}, {min_y}, {min_z}) to ({max_x}, {max_y}, {max_z})")
def buildCreation(functionDefinition):
logger = logging.getLogger(__name__ + '.buildCreation')
logger.info("Starting build creation")
try:
exec(functionDefinition)
logger.info("Build creation completed successfully")
except Exception as e:
logger.error(f"Error during build creation: {e}")
raise e