Advertisement
SforzandoCF

scrach

Aug 20th, 2024 (edited)
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 23.59 KB | None | 0 0
  1. package edu.mit.scratch;
  2.  
  3. public class ScratchFileReader {
  4.     public static ScratchProject read (File file) {
  5.         ZipFile zip = new ZipFile(file);
  6.         ZipEntry entry = null;
  7.         zip.stream().forEach((candidate) -> { if (candidate.getName().endsWith("project.json") && entry == null)
  8.             entry = candidate; });
  9.         if (entry == null) throw new NullPointerException("No project.json file found");
  10.         StringWriter writer = new StringWriter();
  11.         zip.getInputStream(entry).transferTo(writer);
  12.         JSONObject json = new JSONObject(writer.toString());
  13.         ScratchProject.Reference ref = new ScratchProject.Reference();
  14.         List<ScratchSprite> sprites = readSprites(json.getJSONArray("targets"), ref, zip);
  15.         ScratchStage stage = readStage(json.getJSONArray("targets"), ref, zip);
  16.         List<ScratchMonitor> monitors = readMonitors(json.getJSONArray("monitors"), ref);
  17.         List<String> extensions = List.<String>of();
  18.         json.getJSONArray("extensions").toList().forEach((obj) -> { extensions.add(obj.toString()); });
  19.         ScratchMetadata meta = readMetadata(json.getJSONObject("meta"));
  20.         return new ScratchProject(targets, monitors, extensions, meta, ref);
  21.     }
  22.    
  23.     public static List<ScratchSprite> readSprites (JSONArray json, ScratchProject.Reference ref, ZipFile zip) {
  24.         List<ScratchSprite> sprites = List.<ScratchSprite>of();
  25.         for (Object e : json)
  26.             if (e instanceof JSONObject j && !j.getBoolean("isStage"))
  27.                 sprites.add(ScratchTargetParser.readSprite(e, ref, zip));
  28.         return sprites;
  29.     }
  30.    
  31.     public static ScratchStage readStage (JSONArray json, ScratchProject.Reference ref, ZipFile zip) {
  32.         for (Object e : json)
  33.             if (e instanceof JSONObject j && j.getBoolean("isStage"))
  34.                 return ScratchTargetParser.readStage(e, ref, zip);
  35.     }
  36.    
  37.     public static List<ScratchMonitor> readMonitors (JSONArray json) {
  38.         List<ScratchMonitor> monitors = List.<ScratchMonitor>of();
  39.         for (Object e : json)
  40.             if (e instanceof JSONObject j)
  41.                 monitors.add(ScratchMonitor.parse(json, ref));
  42.         return monitors;
  43.     }
  44.    
  45.     public static ScratchMetadata readMetadata (JSONObject json) {
  46.         return new ScratchMetadata(json.getString("semver"), json.getString("vm"), json.getString("agent"));
  47.     }
  48. }
  49.  
  50. public class ScratchTargetParser {    
  51.     public static void readSprite (JSONObject json, ScratchProject.Reference ref, ZipFile zip) {
  52.         ScratchTargetReference ref2 = ref.referenceSprite(json.getString("name"));
  53.         ScratchSprite sprite = new ScratchSprite(json.getString("name"), readVariables(json.getJSONObject("variables"), ref2), readLists(json.getJSONObject("lists"), ref2), readBroadcasts(json.optJSONObject("broadcasts", new JSONObject()), ref2), readBlocks(json.getJSONObject("blocks"), ref2), readComments(json.getJSONObject("comments"), ref2), readCostumes(json.getJSONArray("costumes"), zip), readSounds(json.getJSONArray("sounds"), zip));
  54.         sprite.setCostume(json.getInt("currentCostume"));
  55.         sprite.setLayer(json.getInt("layerOrder"));
  56.         sprite.setVolume(json.getFloat("volume"));
  57.         sprite.initialize(ref2);
  58.         return sprite;
  59.     }
  60.    
  61.     public static List<ScratchVariable> readVariables (JSONObject json, ScratchTargetReference ref) {
  62.         List<ScratchVariable> variables = List.<ScratchVariable>of();
  63.         for (String key : json.keySet()) {
  64.             JSONArray array = json.getJSONArray(key);
  65.             ScratchVariable var = new ScratchVariable(array.getString(0), array.optBoolean(2, false));
  66.             var.initialize(ref.referenceVariable(key, array.getString(1)));
  67.             variables.add(var);
  68.         }
  69.         return variables;
  70.     }
  71.    
  72.     public static void writeVariables (JSONObject json, ScratchTargetReference ref) {
  73.         for (String s : ref.getVariables()) {
  74.             JSONArray array = new JSONArray().put(ref.getVariable(s).name()).put(ref.getVariable(s).getValue()).put(ref.getVariable(s).cloud());
  75.             json.put(s, array);
  76.         }
  77.     }
  78.    
  79.     public static List<ScratchList> readLists (JSONObject json, ScratchProject.Reference ref) {
  80.         List<ScratchList> lists = List.<ScratchList>of();
  81.         for (String key : json.keySet()) {
  82.             JSONArray array = json.getJSONArray(key);
  83.             List<ScratchVariable.Reference> list = List.<ScratchVariable.Reference>of();
  84.             List<String> values = List.<String>of();
  85.             array.get(1).toList().forEach((obj) -> { values.add(obj.toString()); });
  86.             for (int i = 0; i <= values.size(); i++)
  87.                 list.put(ref.referenceVariable(key + "[" + i + "]", values.get(i)));
  88.             ScratchList rList = new ScratchList(array.get(0));
  89.             rList.initialize(ref.referenceList(key, list));
  90.             lists.add(rList);
  91.         }
  92.         return lists;
  93.     }
  94.    
  95.     public static void writeLists (JSONObject json, ScratchTargetReference ref) {
  96.         for (String s : ref.getLists()) {
  97.             List<String> list = List.<String>of();
  98.             ref.getList(s).getValues().forEach((ref) -> { list.add(ref.getValue()); });
  99.             JSONArray array = new JSONArray().put(ref.getList(s).getName()).put(new JSONArray(list));
  100.             json.put(s, array);
  101.         }
  102.     }
  103.    
  104.     public static List<String> readBroadcasts (JSONObject json, ScratchTargetReference ref) {
  105.         List<String> strings = List.<String>of();
  106.         for (String key : json.keySet()) {
  107.             ref.referenceBroadcast(key, json.getString(key));
  108.             strings.put(json.getString(key));
  109.         }
  110.         return strings;
  111.     }
  112.    
  113.     public static void writeBroadcasts (JSONObject json, ScratchTargetReference ref) {
  114.         for (String s : ref.getBroadcasts())
  115.             json.put(s, ref.getBroadcast(s));
  116.     }
  117.    
  118.     public static List<ScratchBlock> readBlocks (JSONObject json, ScratchTargetReference ref) {
  119.         List<ScratchBlock> blocks = List.<ScratchBlock>of();
  120.         for (String s : json.keySet()) {
  121.             try {
  122.                 json.getJSONObject(s);
  123.             } catch (JSONException je) {
  124.                 JSONArray array = json.getJSONArray(s);
  125.                 int type = array.getInt(0);
  126.                 if (type < 9)
  127.                     type = 4;
  128.                 ScratchBlock block;
  129.                 switch (type) {
  130.                     case 4:
  131.                         block = ScratchPrimitiveBlock.intPrimitive(array.getInt(1));
  132.                         break;
  133.                     case 9:
  134.                         block = ScratchPrimitiveBlock.colorPrimitive(parseColor(array.getString(1)));
  135.                         break;
  136.                     case 10:
  137.                         block = ScratchPrimitiveBlock.stringPrimitive(array.getString(1));
  138.                         break;
  139.                     case 12:
  140.                         block = new ScratchBroadcastReporterBlock(array.getString(2));
  141.                         break;
  142.                     case 12:
  143.                         block = new ScratchVariableReporterBlock(array.getString(2));
  144.                         break;
  145.                     case 13:
  146.                         block = new ScratchListReporterBlock(array.getString(2));
  147.                         break;
  148.                 }
  149.                 if (array.has(3)) {
  150.                     block.setX(array.getInt(3));
  151.                     block.setY(array.getInt(4));
  152.                 }
  153.                 blocks.put(block);
  154.                 ref.referenceBlock(s, block);
  155.                 continue;
  156.             }
  157.             JSONObject obj = json.getJSONObject(s);
  158.             ScratchBlock block = ScratchBlock.fromOpcode(obj.getString("opcode"), obj.getJSONObject("inputs"), obj.getJSONObject("fields"));
  159.             String child = obj.getString("next");
  160.             if (ref.getBlock(child) != null) {
  161.                 block.setChild(ref.getBlock(child));
  162.                 ref.getBlock(child).setParent(block);
  163.             }
  164.             if (obj.getBoolean("topLevel")) {
  165.                 String parent = obj.getString("parent");
  166.                 if (ref.getBlock(parent) != null) {
  167.                     block.setParent(ref.getBlock(parent));
  168.                     ref.getBlock(parent).setChild(block);
  169.                 }
  170.                 block.setX(obj.getInt("x"));
  171.                 block.setY(obj.getInt("y"));
  172.             }
  173.             block.setShadow(obj.getBoolean("shadow"));
  174.             try {
  175.                 JSONObject j2 = obj.getJSONObject("mutation");
  176.                 block.parseMutation(j2);
  177.             } catch (JSONException je) {}
  178.         }
  179.         return blocks;
  180.     }
  181.    
  182.     public static void writeBlocks (JSONObject json, ScratchTargetReference ref) {
  183.         for (String s : ref.getBlocks()) {
  184.             ScratchBlock block = ref.getBlock(s);
  185.             if (block instanceof ScratchPrimitiveBlock spb) {
  186.                 JSONArray array = new JSONArray();
  187.                 spb.save(array);
  188.                 json.put(s, array);
  189.                 continue;
  190.             }
  191.             JSONObject j2 = new JSONObject();
  192.             j2.put("opcode", block.getOpcode());
  193.             j2.put("next", ref.getID(block.getChild()));
  194.             j2.put("parent", ref.getID(block.getParent()));
  195.             block.saveInputs(j2);
  196.             block.saveFields(j2);
  197.             j2.put("shadow", block.isShadow());
  198.             j2.put("topLevel", block.getParent() == null);
  199.             if (block.getParent() == null) {
  200.                 j2.put("x", block.getX());
  201.                 j2.put("y", block.getY());
  202.             }
  203.             for (String t : ref.getComments()) {
  204.                 ScratchComment comment = ref.getComment(t);
  205.                 if (comment.block() == block) {
  206.                     j2.put("comment", ref.getID(comment));
  207.                     break;
  208.                 }
  209.             }
  210.             if (block.hasMutation()) block.saveMutation(j2);
  211.         }
  212.     }
  213.    
  214.     public static List<ScratchComment> readComments (JSONObject json, ScratchTargetReference ref) {
  215.         List<ScratchComment> comments = List.<ScratchComment>of();
  216.         for (String key : json.keySet()) {
  217.             JSONObject j = json.getJSONObject(key);
  218.             ScratchComment comment = new ScratchComment(ref.getBlock(j.getString("blockId")), new Rectangle(j.getInt("x"), j.getInt("y"), j.getInt("width"), j.getInt("height")), j.getBoolean("minimized"), j.getString("text"));
  219.             ref.referenceComment(key, comment);
  220.             comments.add(comment);
  221.         }
  222.         return comments;
  223.     }
  224.    
  225.     public static JSONObject writeComments (JSONObject json, ScratchTargetReference ref) {
  226.         Map<String, ScratchComment> comments = ref.getComments();
  227.         for (String s : comments) {
  228.             JSONObject obj = new JSONObject().put("blockId", ref.getID(comments.get(s).block())).put("x", comments.get(s).area().getX()).put("y", comments.get(s).area().getY()).put("width", comments.get(s).area().getWidth()).put("height", comments.get(s).area().getHeight()).put("minimized", comments.get(s).minimized()).put("text", comments.get(s).text());
  229.             json.put(s, obj);
  230.         }
  231.         return json;
  232.     }
  233.    
  234.     public static Map<String, Image> readCostumes (JSONArray json, ZipFile zip) {
  235.         Map<String, Image> images = Map.<String, Image>of();
  236.         for (int i = 0; i <= json.toList().size(); i++) {
  237.             JSONObject asset = json.getJSONObject(0);
  238.             ZipEntry entry = null;
  239.             zip.stream().forEach((candidate) -> { if (candidate.getName().endsWith(asset.getString("md5ext")) && entry == null)
  240.                 entry = candidate; });
  241.             if (entry == null) throw new NullPointerException("Asset " + asset.getString("assetId") + " not found");
  242.             try {
  243.                 if (asset.getString("dataFormat") == "png") {
  244.                     images.put(asset.getString(name), readPNG(zip.getInputStream(entry)));
  245.                 } else if (asset.getString("dataFormat") == "svg") {
  246.                     images.put(asset.getString(name), readSVG(new InputStreamReader(zip.getInputStream(entry))));
  247.                 }
  248.             } catch (IOException ioe) {
  249.                 ioe.printStackTrace();
  250.             }
  251.         }
  252.         return images;
  253.     }
  254.    
  255.     public static Holder<ZipOutputStream, JSONArray> writeCostumes (JSONArray json, ZipOutputStream out, Map<String, Image> images) {
  256.         try {
  257.             for (String s : images.keySet()) {
  258.                 byte[] bytes;
  259.                 zs.putNextEntry(new ZipEntry()
  260.                                 .setLastAccessTime(FileTime.from(Instant.now()))
  261.                                 .setLastModifiedTime(FileTime.from(Instant.now())));
  262.                 ByteArrayOutputStream o2 = new ByteArrayOutputStream();
  263.                 if (images.get(s) instanceof BufferedImage img) {
  264.                     writePNG(o2, img);
  265.                     bytes = o2.toByteArray();
  266.                     writePNG(zs, img);
  267.                 } else if (images.get(s) instanceof VectorImage svg) {
  268.                     OutputStreamWriter o3 = new OutputStreamWriter(o2);
  269.                     writeSVG(o3, svg);
  270.                     bytes = o2.toByteArray();
  271.                     writeSVG(new OutputStreamWriter(out));
  272.                 }
  273.                 JSONObject asset = new JSONObject().put("assetId", getMD5(bytes)).put("name", s).put("md5ext", getMD5(bytes) + ".png").put("dataFormat", "png").put("bitmapResolution", 2).put("rotationCenterX", 0).put("rotationCenterY", 0);
  274.                 json.put(asset);
  275.             }
  276.             return new Holder<ZipOutputStream, JSONArray>(out, json);
  277.         } catch (IOException ioe) {
  278.             ioe.printStackTrace();
  279.         }
  280.     }
  281.    
  282.     public static BufferedImage readPNG (InputStream stream) throws IOException {
  283.         return ImageIO.read(stream);
  284.     }
  285.    
  286.     public static void writePNG (OutputStream stream, BufferedImage image) throws IOException {
  287.         ImageIO.write(stream, "png", image);
  288.     }
  289.    
  290.     public static VectorImage readSVG (Reader reader) throws IOExcpetion {
  291.         StringWriter writer = new StringWriter();
  292.         reader.transferTo(writer);
  293.         String svg = writer.toString();
  294.         return VectorImage.parse(svg);
  295.     }
  296.    
  297.     public static void writeSVG (Writer writer, VectorImage image) throws IOException {
  298.         writer.write(image.toString());
  299.     }
  300.    
  301.     public static Map<String, AudioInputStream> readSounds (JSONArray json, ZipFile zip) {
  302.         Map<String, AudioInputStream> streams = Map.<String, AudioInputStream>of();
  303.         for (int i = 0; i <= json.toList().size(); i++) {
  304.             JSONObject asset = json.getJSONObject(0);
  305.             ZipEntry entry = null;
  306.             zip.stream().forEach((candidate) -> { if (candidate.getName().endsWith(asset.getString("md5ext")) && entry == null)
  307.                 entry = candidate; });
  308.             if (entry == null) throw new NullPointerException("Asset " + asset.getString("name") + " not found");
  309.             try {
  310.                 streams.put(asset.getString(name), AudioSystem.getAudioInputStream(zip.getInputStream(entry)));
  311.             } catch (IOException ioe) {
  312.                 ioe.printStackTrace();
  313.             }
  314.         }
  315.         return streams;
  316.     }
  317.    
  318.     public static void writeSounds (JSONArray json, ZipOutputStream out, Map<String, AudioInputStream> audio) {
  319.         try {
  320.             for (String s : audio.keySet()) {
  321.                 byte[] data = audio.get(s).readAllBytes();
  322.                 zs.putNextEntry(new ZipEntry()
  323.                                 .setLastAccessTime(FileTime.from(Instant.now()))
  324.                                 .setLastModifiedTime(FileTime.from(Instant.now())));
  325.                 AudioSystem.write(audio.get(s), AudioFileFormat.Type.WAVE, zs);
  326.                 zs.closeEntry();
  327.                 if (audio.get(s).getFormat().toString())
  328.                 JSONObject j = new JSONObject().put("assetId", getMD5(data)).put("name", s).put("md5ext", getMD5(data) + ".wav").put("dataFormat", "wav").put("rate", audio.get(s).getFormat().getSampleRate()).put("sampleCount", getSampleCount(audio.get(s)));
  329.                 json.put(j);
  330.             }
  331.             return new Holder<ZipOutputStream, JSONArray>(out, json);
  332.         } catch (IOException ioe) {
  333.             ioe.printStackTrace();
  334.         }
  335.     }
  336.    
  337.     public static int getSampleCount (AudioInputStream ais) {
  338.         return (int)((ais.readAllBytes().length * 8) / ais.getFormat().getSampleSizeInBits());
  339.     }
  340.    
  341.     public static String getMD5 (byte[] data) {
  342.         try {
  343.             String hashtext = new BigInteger(1, MessageDigest.getInstance("MD5").digest(data)).toString(16);
  344.             while (hashtext.length() < 32) {
  345.                 hashtext = 0 + hashtext;
  346.             }
  347.            
  348.             return hashtext;
  349.         } catch (NoSuchAlgorithmException e) {
  350.             e.printStackTrace();
  351.             System.exit(404);
  352.         }
  353.     }
  354. }
  355.  
  356. public class ScratchProject {
  357.     public ScratchProject (List<ScratchSprite> sprites, ScratchStage stage, List<ScratchMonitor> monitors, ScratchMetadata meta, ScratchProject.Reference ref) {
  358.         this.sprites = sprites;
  359.         this.stage = stage;
  360.         this.monitors = monitors;
  361.         this.meta = meta;
  362.         this.ref = ref;
  363.     }
  364. }
  365.  
  366. public record ScratchSprite (String name, List<ScratchVariable> variables, List<ScratchList> lists, List<String> broadcasts, List<ScratchBlock> blocks, List<ScratchComment> comments, Map<String, Image> costumes, Map<String, AudioInputStream> sounds) {
  367.     private int costume, layer;
  368.     private float volume;
  369.     private ScratchTargetReference ref;
  370.     private ScratchBlockExecutor exe;
  371.    
  372.     public int getCostume () {
  373.         return this.ref.getCostume();
  374.     }
  375.    
  376.     protected void setCostume (int costume) {
  377.         this.ref.setCostume(costume);
  378.     }
  379.    
  380.     public int getLayer () {
  381.         return this.ref.getLayer();
  382.     }
  383.    
  384.     protected void setLayer (int layer) {
  385.         this.ref.requestLayerChange(layer);
  386.     }
  387.    
  388.     public float getVolume () {
  389.         return this.ref.getVolume();
  390.     }
  391.    
  392.     protected void setVolume (float volume) {
  393.         this.ref.setVolume(volume);
  394.     }
  395. }
  396.  
  397. public class ScratchBlock<T> {
  398.     private static Map<String, ScratchBlock> blocks = Map.<String, ScratchBlock>ofEntries(
  399.         new AbstractMap.SimpleEntry("motion_movesteps", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveForward(inputs.get(0).getInt()); })),
  400.         new AbstractMap.SimpleEntry("motion_turnright", new ScratchBlock<Void>((sprite, inputs) -> { sprite.rotate(inputs.get(0).getAngle()); })),
  401.         new AbstractMap.SimpleEntry("motion_turnleft", new ScratchBlock<Void>((sprite, inputs) -> { sprite.rotate(360 - inputs.get(0).getAngle()); })),
  402.         new AbstractMap.SimpleEntry("motion_goto", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(((PointReporter)inputs.get(0)).get()); })),
  403.         new AbstractMap.SimpleEntry("motion_gotoxy", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(new Point(inputs.get(0).getInt(), inputs.get(1).getInt())); })),
  404.         new AbstractMap.SimpleEntry("motion_glideto", new ScratchBlock<Void>((sprite, inputs) -> { sprite.glideTo(inputs.get(0).getFloat(), ((PointReporter)inputs.get(1)).get()); })),
  405.         new AbstractMap.SimpleEntry("motion_glidesecstoxy", new ScratchBlock<Void>((sprite, inputs) -> { sprite.glideTo(inputs.get(0).getFloat(), new Point(inputs.get(1).getInt(), inputs.get(2).getInt())); })),
  406.         new AbstractMap.SimpleEntry("motion_pointindirection", new ScratchBlock<Void>((sprite, inputs) -> { sprite.pointToward(inputs.get(0).getAngle()); })),
  407.         new AbstractMap.SimpleEntry("motion_pointtowards", new ScratchBlock<Void>((sprite, inputs) -> { sprite.pointToward(MouseInfo.getPointerInfo().getLocation()); })),
  408.         new AbstractMap.SimpleEntry("motion_changexby", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(sprite.getX() + inputs.get(0).getInt(), sprite.getY()); })),
  409.         new AbstractMap.SimpleEntry("motion_setx", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(inputs.get(0).getInt(), sprite.getY()); })),
  410.         new AbstractMap.SimpleEntry("motion_changeyby", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(sprite.getX(), sprite.getY() + inputs.get(0).getInt()); })),
  411.         new AbstractMap.SimpleEntry("motion_sety", new ScratchBlock<Void>((sprite, inputs) -> { sprite.moveTo(sprite.getX(), inputs.get(0).getInt()); })),
  412.         new AbstractMap.SimpleEntry("motion_ifonedgebounce", new ScratchBlock<Void>((sprite, inputs) -> { if (sprite.onEdge()) sprite.setMovementModifier(180); })),
  413.         new AbstractMap.SimpleEntry("motion_setrotationstyle", new ScratchBlock<Void>((sprite, inputs) -> { sprite.setRotationStyle(((RotationStyleReporter)inputs.get(0)).get()); })),
  414.         new AbstractMap.SimpleEntry("motion_xposition", new ScratchBlock<Integer>((sprite, inputs) -> { return Integer.valueOf(sprite.getX()); })),
  415.         new AbstractMap.SimpleEntry("motion_yposition", new ScratchBlock<Integer>((sprite, inputs) -> { return Integer.valueOf(sprite.getY()); })),
  416.         new AbstractMap.SimpleEntry("motion_direction", new ScratchBlock<Integer>((sprite, inputs) -> { return Integer.valueOf(sprite.getDirection()); })),
  417.         new AbstractMap.SimpleEntry("looks_sayforsecs", new ScratchBlock<Void>((sprite, inputs) -> { sprite.showSpeechBubble(inputs.get(0).getString(), inputs.get(1).getInt(), false); })),
  418.         new AbstractMap.SimpleEntry("looks_say", new ScratchBlock<Void>((sprite, inputs) -> { sprite.showSpeechBubble(inputs.get(0).getString(), 2, false); })),
  419.         new AbstractMap.SimpleEntry("looks_thinkforsecs", new ScratchBlock<Void>((sprite, inputs) -> { sprite.showSpeechBubble(inputs.get(0).getString(), inputs.get(1).getInt(), true); })),
  420.         new AbstractMap.SimpleEntry("looks_think", new ScratchBlock<Void>((sprite, inputs) -> { sprite.showSpeechBubble(inputs.get(0).getString(), 2, true); })),
  421.         new AbstractMap.SimpleEntry("looks_switchcostumeto", new ScratchBlock<Void>((sprite, inputs) -> { sprite.setCostume(((CostumeReporter)inputs.get(0)).getInt()); })),
  422.         new AbstractMap.SimpleEntry("looks_nextcostume", new ScratchBlock<Void>((sprite, inputs) -> { sprite.setCostume(sprite.getCostume() + 1); })),
  423.         new AbstractMap.SimpleEntry("looks_switchbackdropto", new ScratchBlock<Void>((sprite, inputs) -> { sprite.getProject().getStage().setBackdrop(((CostumeReporter)inputs.get(0)).getInt()); })),
  424.         new AbstractMap.SimpleEntry("looks_switchbackdroptoandwait", new ScratchWaitBlock((sprite, inputs) -> { sprite.getProject().getStage().setBackdrop(((CostumeReporter)inputs.get(0)).getInt()); })),
  425.    
  426.     private BiConsumer<ScratchSprite, List<Input>> consumer = null;
  427.     private BiFunction<ScratchSprite, List<Input>> function = null;
  428.    
  429.     private ScratchSprite.Reference ref = null;
  430.     private List<Input> inputs = null;
  431.    
  432.     ScratchBlock (BiConsumer<ScratchSprite, List<Input>> call) {
  433.         this.consumer = call;
  434.     }
  435.    
  436.     ScratchBlock (BiFunction<ScratchSprite, List<Input>, T> call) {
  437.         this.function = call;
  438.     }
  439.    
  440.     public void execute () {
  441.         if (this.consumer != null) {
  442.             this.consumer.accept(this.ref.getSprite(), this.inputs);
  443.             this.child.execute();
  444.         } else {
  445.             throw new IllegalStateException("No block code to execute");
  446.         }
  447.     }
  448.    
  449.     public T report () {
  450.         if (this.function != null) {
  451.             return this.function.accept(this.ref.getSprite(), this.inputs);
  452.         } else {
  453.             throw new IllegalStateException("No block code to execute");
  454.         }
  455.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement