Beispiel #1
0
def make_on_load_function(
        main: FunctionNode, backend: McDatapackBackend,
        init_constants: Optional[FunctionNode],
        init_scoreboards: Optional[FunctionNode]) -> FunctionNode:
    """
    Creates the mcscript-datapack runtime on-load function.

    When the datapack is loaded, the following things should happen:
        * initialize all scoreboards
        * initialize all scoreboard constants
        * If in debug, set the main scoreboard as sidebar
        * Make a installation message
        * run main

    Args:
        main: The main function
        backend: the datapack backend
        init_constants: the function to initialize constants
        init_scoreboards: the function to initialize the scoreboards

    Returns:
        A new function node that executes those steps
    """
    commands = []

    if init_scoreboards:
        commands.extend(init_scoreboards.inner_nodes)

    if init_constants:
        commands.extend(init_constants.inner_nodes)

    if not backend.config.is_release and len(
            backend.ir_master.scoreboards) > 0:
        commands.append(
            CommandNode(
                f"scoreboard objectives setdisplay sidebar {backend.ir_master.scoreboards[0]}"
            ))

    message = format_text("["), format_color(
        format_text(backend.config.project_name),
        "gold"), format_text("] loaded!")
    commands.append(
        MessageNode(MessageNode.MessageType.CHAT,
                    json.dumps(message),
                    selector=Selector("a", [])))

    commands.append(FunctionCallNode(main))

    return FunctionNode(backend.config.resource_specifier_main("load"),
                        commands)
Beispiel #2
0
def set_score(compile_state: CompileState, scoreboard: StringResource,
              score: StringResource, resource: Resource) -> Resource:
    """ Extremely temporary test function"""
    if not isinstance(resource, ValueResource):
        raise McScriptUnexpectedTypeError("set_score", resource.type(),
                                          "<Supports scoreboard>",
                                          compile_state)

    scoreboard = Scoreboard(scoreboard.static_value, True,
                            len(compile_state.ir.scoreboards))
    compile_state.ir.scoreboards.append(scoreboard)

    resource = resource.store(compile_state)
    scoreboard_value = ScoreboardValue(score.static_value, scoreboard)
    compile_state.ir.append(
        StoreFastVarNode(scoreboard_value, resource.scoreboard_value))

    # to prevent the node from getting optimized out
    compile_state.ir.append(
        MessageNode(
            MessageNode.MessageType.CHAT,
            json.dumps([
                format_text("The test result is: "),
                format_score(scoreboard_value)
            ]), Selector("a", [])))
    return NullResource()
Beispiel #3
0
    def _compact_data(cls, data: List[Union[list, dict]], current_text: List[str] = None) \
            -> Tuple[List[Union[list, dict]], List[str]]:
        compacted_data = []
        current_text = current_text or []
        for value in data:
            if isinstance(value, list):
                compacted_data.append(cls._compact_data(value, current_text))
            elif len(value.keys()) == 1 and value.get("text", None) is not None:
                current_text.append(value["text"])
            else:
                if current_text:
                    compacted_data.append(format_text("".join(current_text)))
                    current_text.clear()
                compacted_data.append(value)

        return compacted_data, current_text
Beispiel #4
0
 def to_json_text(self, compile_state: CompileState,
                  formatter: ResourceTextFormatter):
     if self.static_value is not None:
         return format_text(str(int(self.static_value)))
     return format_score(self.scoreboard_value)
Beispiel #5
0
 def to_json_text(self, compileState: CompileState,
                  formatter: ResourceTextFormatter) -> Dict:
     return format_text(self.static_value)
Beispiel #6
0
 def compact_data(cls, data: List[Union[list, dict]]) -> List[Union[list, dict]]:
     result, rest = cls._compact_data(data)
     if rest:
         result.append(format_text("".join(rest)))
     return result
Beispiel #7
0
 def string(self, tree):
     string, = tree.children
     return format_text(str(string).replace("\\[", "[").replace("\\]", "]"))