async def stop(self, args=None): self.keyboard.reset() await self.ws.send(message_utils.info(ref_strings.midiplayer.stopping)) self.playing = False while self.isPlaying: time.sleep(0.05) await self.ws.send(message_utils.info(ref_strings.midiplayer.stopped)) return
async def set_keyboard(self, args): if len(args) == 0: return if args[0] == '0': self.config['displayKeyboard'] = False await self.ws.send( message_utils.info(ref_strings.midiplayer.keyboard_disable)) elif args[0] == '1': self.config['displayKeyboard'] = True await self.ws.send( message_utils.info(ref_strings.midiplayer.keyboard_enable))
async def set_pan_by_pitch(self, args): if len(args) == 0: return if args[0] == '0': self.config['panByPitch'] = False await self.ws.send( message_utils.info(ref_strings.midiplayer.by_pitch_disable)) else: self.config['panByPitch'] = True await self.ws.send( message_utils.info(ref_strings.midiplayer.by_pitch_enable))
async def search_file(self, args): if len(args) == 0: await self.ws.send(message_utils.error(ref_strings.search_error)) return results = self.search(args) if len(results) == 0: await self.ws.send(message_utils.info(ref_strings.empty_result)) else: string = '\n' for i in results: string += ref_strings.list_format.format(i[0], i[1]) + '\n' await self.ws.send(message_utils.info(string))
async def draw(self, img, position): size = img.size pal = img.getpalette() pxs = [] for y in range(size[1]): pxs.append([]) for x in range(size[0]): px = img.getpixel((x, y)) # PAL8 图片 if isinstance(px, int): index = px * 3 color = (pal[index] << 16) | ( pal[index + 1] << 8) | pal[index + 2] # RGB 图片 elif len(px) == 3: color = (px[0] << 16) | (px[1] << 8) | px[2] # RGBA 图片 elif len(px) == 4: # 检测透明度,如果小于128则不放置方块 if px[3] < 0x80: pxs[y].append(None) continue # 获取整数颜色值 color = (px[0] << 16) | (px[1] << 8) | px[2] else: raise NotImplementedError('Image not supported.') blockToPlace = colorToBlock(color) fmt = "{0} {1}".format(blockToPlace[0], blockToPlace[1]) pxs[y].append(blockToPlace) colorMap = getColorRegion(pxs, 0, 0, size[0], size[1]) await self.ws.send(message_utils.info(ref_strings.pixel.start)) for i in colorMap: if i[4] is None: continue else: pos = self.get_position(position, size, i[0:4], False) await self.we.fill(pos[0], pos[1], i[4][0], i[4][1]) await self.ws.recv() await self.ws.send(message_utils.info(ref_strings.pixel.finish))
async def set_mode(self, args): if len(args) > 0: if args[0] in self.modes: self.config['mode'] = args[0] await self.ws.send( message_utils.info( ref_strings.pixel.set_mode.format(self.config['mode'])) ) else: await self.ws.send( message_utils.error(ref_strings.pixel.invaild_mode)) else: await self.ws.send( message_utils.info( ref_strings.pixel.current_mode.format(self.config['mode'])) )
async def set_dither(self, args): if len(args) > 0: if args[0] == '0': self.config['dither'] = False elif args[0] == '1': self.config['dither'] = True else: await self.ws.send( message_utils.info( ref_strings.pixel.current_mode.format(self.config['mode'])) )
async def parseCmd(self, args): if args[0] == ".set": if (len(args) == 1 or args[1] == ''): await self.ws.send( message_utils.error(ref_strings.command_error)) return if args[1] == "1": self.pos1 = await self.getPlayerBlockPos() if self.pos2 == None: self.pos2 = self.pos1 await self.ws.send( message_utils.info( ref_strings.worldedit.coor_1_msg.format( str(self.pos1), self.pos1 * self.pos2))) if args[1] == "2": self.pos2 = await self.getPlayerBlockPos() if self.pos1 == None: self.pos1 = self.pos2 await self.ws.send( message_utils.info( ref_strings.worldedit.coor_2_msg.format( str(self.pos2), self.pos1 * self.pos2))) await self.ws.send(message_utils.autocmd('closechat')) if args[0] == ".fill": if (len(args) == 1 or args[1] == ''): await self.ws.send( message_utils.error(ref_strings.command_error)) return if self.pos1 == None or self.pos2 == None: await self.ws.send( message_utils.error(ref_strings.worldedit.no_coordinate)) return if len(args) == 2: result = await self.fillAny(args[1]) else: result = await self.fillAny(args[1], args[2]) if result["success"]: await self.ws.send(message_utils.info(result["data"])) else: await self.ws.send(message_utils.error(result["data"]))
async def open(self, index, stop=True): if self.playing and stop: await self.stop() filename = self.file_list[index] await self.ws.send( message_utils.info( ref_strings.midiplayer.load_song.format( self.index, filename, message_utils.fileSize(filename)))) if filename.endswith('.mcs') or filename.endswith('.mcz'): self.mcsMode = True self.mcs = mcsparser.read(filename) else: self.mcsMode = False self.set_midi(filename) if stop: self.play()
async def from_url(self, args): await self.ws.send( message_utils.info(ref_strings.pixel.download_image.format( args[0]))) if len(args) == 0: return code = downloader.download_midi(args[0]) if code[0] == -1: await self.ws.send( message_utils.error(ref_strings.pixel.web_error.format( args[0]))) return if code[0] == 1: await self.ws.send( message_utils.error( ref_strings.pixel.mime_error.format(code[1]))) return await self.open('files/cache/midi')
async def from_url(self, args): pos = await self.we.getPlayerBlockPos() await self.ws.send( message_utils.info(ref_strings.pixel.download_image.format( args[0]))) if len(args) == 0: return code = downloader.download_image(args[0]) if code[0] == -1: await self.ws.send( message_utils.error(ref_strings.pixel.web_error.format( args[0]))) return if code[0] == 1: await self.ws.send( message_utils.error( ref_strings.pixel.mime_error.format(code[1]))) return await self.generate('files/cache/img', pos)
async def reload(self, args): self.get_file_list() await self.ws.send(message_utils.info(ref_strings.module.reload))
async def show_playing(self, args): if self.playing: await self.ws.send( message_utils.info( ref_strings.midiplayer.playing.format( self.index, self.file_list[self.index])))
async def man_mode(self, args): await self.ws.send( message_utils.info("\n".join(ref_strings.pixel.mode_help))) await self.ws.send(message_utils.info("\n".join(self.modes)))
async def generate(self, filename, position): img = Image.open(filename) size = img.size await self.ws.send(message_utils.autocmd('closechat')) await self.ws.send( message_utils.info( ref_strings.pixel.image_info.format( filename, size[0], size[1], message_utils.fileSize(filename)))) max_width = 16 << 4 if (not self.config['big']) and size[0] > max_width: ratio = size[0] / size[1] resize = max_width, int(max_width / ratio) img = img.resize(resize) size = resize await self.ws.send( message_utils.info( ref_strings.pixel.resize_info.format(size[0], size[1]))) if self.config['dither']: try: palimage = Image.new('P', (16, 16)) palimage.putpalette(pal * 7) img = img.convert('RGB').quantize(palette=palimage) img.save('files/cache/test.png', 'png') except Exception as e: print(e) return if self.config['big']: block_size = 128 work = 0 required = math.ceil(size[0] / block_size) * math.ceil( size[1] / block_size) await self.ws.send( message_utils.info('block size = {0}'.format(block_size))) for x in range(0, size[0], block_size): for y in range(0, size[1], block_size): if size[0] - x < block_size: x2 = size[0] else: x2 = x + block_size - 1 if size[1] - y < block_size: y2 = size[1] else: y2 = y + block_size - 1 print(x, y, x2, y2) tempimage = img.crop((x, y, x2 + 1, y2 + 1)) temppos = worldedit.Position(position.x + x, position.y, position.z + y) print(temppos) await self.ws.send( message_utils.info('{0}/{1}'.format(work, required))) sleep(1) await self.ws.send( message_utils.autocmd('tp @p ' + str(temppos))) await self.draw(tempimage, temppos) work += 1 sleep(1) return await self.draw(img, position)
async def parse_command(self, msg): if msg["header"]["messagePurpose"] == "event": if msg["body"]["eventName"] == "PlayerMessage" and msg["body"][ "properties"]['MessageType'] == 'chat': self.log.log(msg) raw = message_utils.getChat(msg) args = raw.split(" ") executor = msg["body"]["properties"]["Sender"] if executor == self.log.host: await self.host_only(args) if args[0] == ".info": await self.ws.send( message_utils.info(ref_strings.mcws.info)) await self.ws.send( message_utils.info(ref_strings.pyversion)) if args[0] == ".help": for i in ref_strings.mcws.help: await self.ws.send( message_utils.info(i + " - " + ref_strings.mcws.help[i])) if args[0] == ".function": arg1 = raw[10:] if arg1 == "-ls": for filename in glob.glob( "files/functions/*.mcfunction"): await self.ws.send(message_utils.info(filename)) else: if os.path.exists("functions/" + arg1 + ".mcfunction"): with open("functions/" + arg1 + ".mcfunction", "r") as file: for i in file.readlines(): await self.ws.send(message_utils.autocmd(i) ) else: await self.ws.send( message_utils.error( ref_strings.file_not_exists)) if args[0] == ".midi" and import_midiplayer: await self.player.parse_command(args[1:]) if args[0] == ".sier": pass ''' px = 50200 py = 100 pz = 50000 for x in range(-50, 50): for z in range(-50, 50): y = x ^ z await ws.send(setBlock(px + x, py + y, pz + z, "redstone_block")) time.sleep(0.001)''' elif msg["header"]["messagePurpose"] == "commandResponse": pass
async def print_data(self, args): await self.ws.send(message_utils.info(dumps(self.get_data(), indent=4)))
async def host_only(self, args): await self.we.parseCmd(args) if args[0] == ".getscore": await self.ws.send( message_utils.autocmd("scoreboard players list @s")) msg2 = json.loads(await self.ws.recv()) match = re.findall(scoreRegex, msg2.get("body").get("statusMessage")) out = {} for i in match: out[i[2]] = i[4] print(out) if args[0] == ".pixelart" and import_pixel: try: await self.pixelgen.parse_command(args[1:]) except FileNotFoundError: await self.ws.send( message_utils.error(ref_strings.file_not_exists)) if args[0] == ".test": pass if args[0] == ".debug": if len(args) == 1: return if args[1] == '0': message_utils.log_command = False elif args[1] == '1': message_utils.log_command = True if args[0] == '.exit': await self.ws.send(message_utils.info('bye!')) raise KeyboardInterrupt if args[0] == '.copy': origin = worldedit.Position(2, 71, 10) destination = worldedit.Position(-60, 4, -75) for i in range(100): isEnd = await self.we.isBlock( worldedit.Position(origin.x + i * 2, origin.y - 1, origin.z), 'command_block') if isEnd: break for j in range(4): for k in range(31): detectPosition = worldedit.Position( origin.x + i * 2, origin.y + k, origin.z + j * 2) isBlock = await self.we.isBlock( detectPosition, 'command_block') isBlock2 = await self.we.isBlock( detectPosition, 'chain_command_block') if isBlock or isBlock2: await self.we.copyBlock( detectPosition, worldedit.Position(destination.x - (j + i * 4), destination.y + k, destination.z)) else: break if args[0] == '.save': self.save()
async def info(self, args): await self.ws.send(message_utils.info(ref_strings.midiplayer.info)) await self.ws.send( message_utils.info( ref_strings.midiplayer.midicount.format(len(self.file_list))))
async def help(self, args): string = '\n' + '{0} - {1}'.format(self.module_name, self.description) + '\n' for i in self.commands: string += str(self.commands[i]["command"]) + '\n\u00a7d' await self.ws.send(message_utils.info(string))