def sub(self, string): while self.regex.search(string): found = self.regex.finditer(string) for find in found: params = self.param_regex.search(find.group()).group()[1:-1] params = re.sub(r",\s", ",", params) paraml = params.split(",") parsedparams = [] for i in paraml: if i: if i[0] == '"': i = i[1:-1].replace('\\"', '"').replace('\\\\', '\\') parsedparams.append(i) try: output = self.function(self.replacewith, self.params, parsedparams) except: color_print( "{params} is not a valid argument list for ${funcname}.", color=ansi_colors.RED, params=params, funcname=self.name) output = "" string = string.replace(find.group(), output) return string
def gen_cart_stack(init_commands, clock_commands, mode, loud=False): final_command_obj = None if clock_commands or init_commands: entities = [] entities.append(sands.normal_sand("activator_rail")) for command in init_commands: if hasattr(command, "cmd"): if loud: color_print(command.prettystr()) entities.append(cart(command.cmd)) offset = 1 for command in clock_commands: if offset == 1: command.block = "repeating_command_block" if loud: color_print(command.prettystr()) entities.append(cart_command_block(offset, command, 1, mode)) offset += 1 filloffset = offset+1 offset += 2 entities.append(cart_command_block(offset, Command(format("clone ~ ~-2 ~ ~ ~-{o1} ~ ~ ~-{o2} ~ replace move", o1=offset-1,o2=offset+2)))) offset += 1 entities.append(cart_command_block(offset, Command(format("fill ~ ~-{o1} ~ ~ ~-{o2} ~ air", o1=offset,o2=offset+2)))) offset += 1 activatesand = sands.normal_sand("command_block") activatesand["TileEntityData"] = {"auto": 1} entities.append(cart_block(offset, "air")) entities.append(cart(nbt.cmd(format("summon falling_block ~ ~{o} ~ ", o=offset), activatesand, True))) entities.append(cart_command_block(filloffset, Command(format("fill ~ ~ ~ ~ ~{o} ~ air", o=offset-filloffset)))) entities.append(cart("kill @e[r=1,type=commandblock_minecart]")) stack = ride(entities) final_stack = sands.ride([ stack, sands.normal_sand("redstone_block"), sands.normal_sand("barrier") ], False) final_command_obj = nbt.cmd("summon falling_block ~ ~1 ~ ", final_stack) final_command = nbt.JSON2Command(final_command_obj) return final_command
def sub(self, string): while self.regex.search(string): found = self.regex.finditer(string) for find in found: params = self.param_regex.search(find.group()).group()[1:-1] params = re.sub(r",\s", ",", params) paraml = params.split(",") parsedparams = [] for i in paraml: if i: if i[0] == '"': i = i[1:-1].replace('\\"', '"').replace('\\\\', '\\') parsedparams.append(i) try: output = self.function(self.replacewith, self.params, parsedparams) except: color_print("{params} is not a valid argument list for ${funcname}.", color=ansi_colors.RED, params=params, funcname=self.name) output = "" string = string.replace(find.group(), output) return string
def gen_cart_stack(init_commands, clock_commands, mode, loud=False): final_command_obj = None if clock_commands or init_commands: entities = [] entities.append(sands.normal_sand("activator_rail")) for command in init_commands: if hasattr(command, "cmd"): if loud: color_print(command.prettystr()) entities.append(cart(command.cmd)) offset = 1 for command in clock_commands: if offset == 1: command.block = "repeating_command_block" if loud: color_print(command.prettystr()) entities.append(cart_command_block(offset, command, 1, mode)) offset += 1 filloffset = offset + 1 offset += 2 entities.append( cart_command_block( offset, Command( format("clone ~ ~-2 ~ ~ ~-{o1} ~ ~ ~-{o2} ~ replace move", o1=offset - 1, o2=offset + 2)))) offset += 1 entities.append( cart_command_block( offset, Command( format("fill ~ ~-{o1} ~ ~ ~-{o2} ~ air", o1=offset, o2=offset + 2)))) offset += 1 activatesand = sands.normal_sand("command_block") activatesand["TileEntityData"] = {"auto": 1} entities.append(cart_block(offset, "air")) entities.append( cart( nbt.cmd(format("summon falling_block ~ ~{o} ~ ", o=offset), activatesand, True))) entities.append( cart_command_block( filloffset, Command( format("fill ~ ~ ~ ~ ~{o} ~ air", o=offset - filloffset)))) entities.append(cart("kill @e[r=1,type=commandblock_minecart]")) stack = ride(entities) final_stack = sands.ride([ stack, sands.normal_sand("redstone_block"), sands.normal_sand("barrier") ], False) final_command_obj = nbt.cmd("summon falling_block ~ ~1 ~ ", final_stack) final_command = nbt.JSON2Command(final_command_obj) return final_command
def parse_cmd(cindex, commands, functions, variables, func_regex): outcommands = [] command = commands[cindex].strip() if not command or comment_regex.match(command): return cindex, [], functions, variables, func_regex for var in variables: command = variables[var].sub(command) while func_regex.search(command): for macro in functions: command = functions[macro].sub(command) if define_regex.match(command): command_split = define_tag_regex.sub("", command).split() if len(command_split) < 2: return cindex, [], functions, variables, func_regex name = command_split[0] contents = " ".join(command_split[1:]) if macro_regex.match(name): params = param_regex.search(name).group()[1:-1].split(",") name = word_regex.search(name).group() functions[name] = CmdMacro(name, params, contents) func_regex = re.compile("\\$("+"|".join(map(lambda x: functions[x].name, functions))+")"+CmdMacro.param, re.IGNORECASE) return cindex, [], functions, variables, func_regex if word_regex.match(name): variables[name] = CmdVariable(name, contents) elif undefine_regex.match(command): command_split = undefine_regex.sub("", command).split() for i in command_split: if i in variables: del variables[i] if i in functions: del functions[i] elif import_regex.match(command): if context is None: return cindex, [], functions, variables, func_regex libraryname = import_regex.sub("", command).strip() if not libraryname: return cindex, [], functions, variables, func_regex if isinstance(context, str): if os.path.exists(os.path.join(context, libraryname)): importedcontext = context importedname = libraryname lib = open(os.path.join(context,libraryname)) elif os.path.exists(os.path.join(context, libraryname+".1cc")): importedcontext = context importedname = libraryname+".1cc" lib = open(os.path.join(context, libraryname+".1cc")) else: color_print("Failed to import {lib}. File not found.", color=ansi_colors.RED, lib=libraryname) return [] else: lib = None for i in context: if os.path.exists(os.path.join(i, libraryname)): importedcontext = i importedname = libraryname lib = open(os.path.join(i,libraryname)) break elif os.path.exists(os.path.join(i, libraryname+".1cc")): importedcontext = i importedname = libraryname+".1cc" lib = open(os.path.join(i, libraryname+".1cc")) break if not lib: color_print("Failed to import {lib}. File not found.", color=ansi_colors.RED, lib=libraryname) return [] outcommands += preprocess(lib.read().split("\n"), importedcontext, importedname) elif for_regex.match(command): cindex, out, functions, variables, func_regex = parse_for(cindex, commands, functions, variables, func_regex) outcommands += parse_section(out, functions, variables, func_regex) else: outcommands.append(command) return cindex, outcommands, functions, variables, func_regex
def parse_cmd(cindex, commands, functions, variables, func_regex): outcommands = [] command = commands[cindex].strip() if not command or comment_regex.match(command): return cindex, [], functions, variables, func_regex for var in variables: command = variables[var].sub(command) while func_regex.search(command): for macro in functions: command = functions[macro].sub(command) if define_regex.match(command): command_split = define_tag_regex.sub("", command).split() if len(command_split) < 2: return cindex, [], functions, variables, func_regex name = command_split[0] contents = " ".join(command_split[1:]) if macro_regex.match(name): params = param_regex.search(name).group()[1:-1].split(",") name = word_regex.search(name).group() functions[name] = CmdMacro(name, params, contents) func_regex = re.compile( "\\$(" + "|".join(map(lambda x: functions[x].name, functions)) + ")" + CmdMacro.param, re.IGNORECASE) return cindex, [], functions, variables, func_regex if word_regex.match(name): variables[name] = CmdVariable(name, contents) elif undefine_regex.match(command): command_split = undefine_regex.sub("", command).split() for i in command_split: if i in variables: del variables[i] if i in functions: del functions[i] elif import_regex.match(command): if context is None: return cindex, [], functions, variables, func_regex libraryname = import_regex.sub("", command).strip() if not libraryname: return cindex, [], functions, variables, func_regex if isinstance(context, str): if os.path.exists(os.path.join(context, libraryname)): importedcontext = context importedname = libraryname lib = open(os.path.join(context, libraryname)) elif os.path.exists(os.path.join(context, libraryname + ".1cc")): importedcontext = context importedname = libraryname + ".1cc" lib = open(os.path.join(context, libraryname + ".1cc")) else: color_print("Failed to import {lib}. File not found.", color=ansi_colors.RED, lib=libraryname) return [] else: lib = None for i in context: if os.path.exists(os.path.join(i, libraryname)): importedcontext = i importedname = libraryname lib = open(os.path.join(i, libraryname)) break elif os.path.exists(os.path.join(i, libraryname + ".1cc")): importedcontext = i importedname = libraryname + ".1cc" lib = open(os.path.join(i, libraryname + ".1cc")) break if not lib: color_print("Failed to import {lib}. File not found.", color=ansi_colors.RED, lib=libraryname) return [] outcommands += preprocess(lib.read().split("\n"), importedcontext, importedname) elif for_regex.match(command): cindex, out, functions, variables, func_regex = parse_for( cindex, commands, functions, variables, func_regex) outcommands += parse_section(out, functions, variables, func_regex) else: outcommands.append(command) return cindex, outcommands, functions, variables, func_regex
def gen_stack(init_commands, clock_commands, mode, loud=False): final_command_obj = None if clock_commands or init_commands: command_sands = [] repeatoffsets = [] if mode == 'i': if clock_commands and isinstance(clock_commands[0], Command): repeatoffsets.append(len(clock_commands) + 2) for command in clock_commands: if command.block == "repeating_command_block" and not command.cond and command is not clock_commands[ 0]: repeatoffsets.append( len(clock_commands) - clock_commands.index(command) + 2 + len(repeatoffsets)) filloffset = len(init_commands) + len(repeatoffsets) if filloffset: filloffset += 1 if filloffset: if loud: color_print("minecraft:command_block:0\n - Initialization", color=ansi_colors.DARKGRAY, allow_repeat=True) sand = normal_sand("command_block") if mode == 'i': sand["TileEntityData"] = {"auto": 1} command_sands.append(sand) for command in init_commands: if loud: color_print(command.prettystr(), allow_repeat=True) command_sands.append(generate_sand(command, 0)) for offset in repeatoffsets[::-1]: blockdata = Command(format("blockdata ~ ~-{offset} ~ {auto:1b}", offset=offset), init=True) if loud: color_print(blockdata.prettystr(), allow_repeat=True) sand = generate_sand(blockdata, 0) command_sands.append(sand) if filloffset: fill = Command(format("fill ~ ~-1 ~ ~ ~{offset} ~ air", offset=filloffset), init=True) if loud: color_print(fill.prettystr(), allow_repeat=True) color_print("minecraft:barrier\n - Initialization", color=ansi_colors.DARKGRAY, allow_repeat=True) command_sands.append(generate_sand(fill, 0)) command_sands.append(normal_sand("barrier")) for command in clock_commands[::-1]: if command is clock_commands[0] and isinstance(command, Command): command.block = "repeating_command_block" command_sands.append(generate_sand(command, 1)) else: sand = generate_sand(command, 1) if command.block == "repeating_command_block" and command.cond: sand["TileEntityData"]["auto"] = 1 command_sands.append(sand) if loud: color_print(command.prettystr(), allow_repeat=True) final_command_obj = nbt.cmd("summon falling_block ~ ~1 ~ ", ride(command_sands, False)) final_command = nbt.JSON2Command(final_command_obj) return final_command
def gen_stack(init_commands, clock_commands, mode, loud=False): final_command_obj = None if clock_commands or init_commands: command_sands = [] repeatoffsets = [] if mode == "i": if clock_commands and isinstance(clock_commands[0], Command): repeatoffsets.append(len(clock_commands) + 2) for command in clock_commands: if command.block == "repeating_command_block" and not command.cond and command is not clock_commands[0]: repeatoffsets.append(len(clock_commands) - clock_commands.index(command) + 2 + len(repeatoffsets)) filloffset = len(init_commands) + len(repeatoffsets) if filloffset: filloffset += 1 if filloffset: if loud: color_print( "minecraft:command_block:0\n - Initialization", color=ansi_colors.DARKGRAY, allow_repeat=True ) sand = normal_sand("command_block") if mode == "i": sand["TileEntityData"] = {"auto": 1} command_sands.append(sand) for command in init_commands: if loud: color_print(command.prettystr(), allow_repeat=True) command_sands.append(generate_sand(command, 0)) for offset in repeatoffsets[::-1]: blockdata = Command(format("blockdata ~ ~-{offset} ~ {auto:1b}", offset=offset), init=True) if loud: color_print(blockdata.prettystr(), allow_repeat=True) sand = generate_sand(blockdata, 0) command_sands.append(sand) if filloffset: fill = Command(format("fill ~ ~-1 ~ ~ ~{offset} ~ air", offset=filloffset), init=True) if loud: color_print(fill.prettystr(), allow_repeat=True) color_print("minecraft:barrier\n - Initialization", color=ansi_colors.DARKGRAY, allow_repeat=True) command_sands.append(generate_sand(fill, 0)) command_sands.append(normal_sand("barrier")) for command in clock_commands[::-1]: if command is clock_commands[0] and isinstance(command, Command): command.block = "repeating_command_block" command_sands.append(generate_sand(command, 1)) else: sand = generate_sand(command, 1) if command.block == "repeating_command_block" and command.cond: sand["TileEntityData"]["auto"] = 1 command_sands.append(sand) if loud: color_print(command.prettystr(), allow_repeat=True) final_command_obj = nbt.cmd("summon FallingSand ~ ~1 ~ ", ride(command_sands, False)) final_command = nbt.JSON2Command(final_command_obj) return final_command