From 884374c60d98ba0229c0a665c0e942804b623ca9 Mon Sep 17 00:00:00 2001 From: KR0K0BIL <187011674+KR0K0BIL@users.noreply.github.com> Date: Sat, 2 Nov 2024 15:02:20 +0100 Subject: [PATCH] Makes block parameters optional --- .../other/project_json_capabilities.py | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/scratchattach/other/project_json_capabilities.py b/scratchattach/other/project_json_capabilities.py index 6e7d0858..c99c87f5 100644 --- a/scratchattach/other/project_json_capabilities.py +++ b/scratchattach/other/project_json_capabilities.py @@ -50,22 +50,20 @@ class Block(BaseProjectBodyComponent): # Thanks to @MonkeyBean2 for some scripts def from_json(self, data: dict): - self.opcode = data["opcode"] # The name of the block - self.next_id = data["next"] # The id of the block attached below this block + self.opcode = data["opcode"] # The name of the block + self.next_id = data.get("next", None) # The id of the block attached below this block self.parent_id = data.get("parent", None) # The id of the block that this block is attached to self.input_data = data.get("inputs", None) # The blocks inside of the block (if the block is a loop or an if clause for example) - self.fields = data["fields"] # The values inside the block's inputs - self.shadow = data["shadow"] # Whether the block is displayed with a shadow - self.topLevel = data.get("topLevel", False) - self.mutation = data.get("mutation",None) - self.x = data.get("x", 0) - self.y = data.get("y", 0) + self.fields = data.get("fields", None) # The values inside the block's inputs + self.shadow = data.get("shadow", False) # Whether the block is displayed with a shadow + self.topLevel = data.get("topLevel", False) # Whether the block has no parent + self.mutation = data.get("mutation", None) # For custom blocks + self.x = data.get("x", None) # x position if topLevel + self.y = data.get("y", None) # y position if topLevel def to_json(self): - output = {"opcode":self.opcode,"next":self.next_id,"parent":self.parent_id,"inputs":self.input_data,"fields":self.fields,"topLevel":self.topLevel,"shadow":self.shadow,"x":self.x,"y":self.y} - if self.mutation: - output["mutation"] = self.mutation - return output + output = {"opcode":self.opcode,"next":self.next_id,"parent":self.parent_id,"inputs":self.input_data,"fields":self.fields,"shadow":self.shadow,"topLevel":self.topLevel,"mutation":self.mutation,"x":self.x,"y":self.y} + return {k: v for k, v in output.items() if v} def attached_block(self): return self.sprite.block_by_id(self.next_id)