1- const util = require ( 'util' )
2- const fs = require ( 'fs' )
3- const { v4 : uuidv4 } = require ( 'uuid' )
4- const Backend = require ( './Backend' )
1+ import * as util from 'util'
2+ import * as fs from 'fs'
3+ import { v4 as uuidv4 } from 'uuid'
4+ import * as Backend from './Backend.js'
55
6- function compile ( tree ) {
6+ export function Compile ( tree , config , source , path ) {
77 //console.log(util.inspect(tree, false, null, true /* enable colors */))
88
9- fs . copyFileSync ( './data/world_runtime_template.json' , './output/world_runtime.json' )
9+ let worldRuntime = JSON . parse ( fs . readFileSync ( source ) . toString ( ) )
1010
11- if ( fs . existsSync ( './output' ) ) {
12- fs . rmSync ( './output' , { recursive : true } )
11+ if ( ! worldRuntime [ 'minecraft:entity' ] . description . animations ) {
12+ worldRuntime [ 'minecraft:entity' ] . description . animations = { }
1313 }
1414
15- fs . mkdirSync ( './output' )
16- fs . mkdirSync ( './output/animations' )
15+ if ( ! worldRuntime [ 'minecraft:entity' ] . description . properties ) {
16+ worldRuntime [ 'minecraft:entity' ] . description . properties = { }
17+ }
18+
19+ if ( ! worldRuntime [ 'minecraft:entity' ] . events ) {
20+ worldRuntime [ 'minecraft:entity' ] . events = { }
21+ }
22+
23+ if ( ! worldRuntime [ 'minecraft:entity' ] . description . scripts ) {
24+ worldRuntime [ 'minecraft:entity' ] . description . scripts = { }
25+ }
1726
18- let worldRuntime = JSON . parse ( fs . readFileSync ( './output/world_runtime.json' ) . toString ( ) )
27+ if ( ! worldRuntime [ 'minecraft:entity' ] . description . scripts . animate ) {
28+ worldRuntime [ 'minecraft:entity' ] . description . scripts . animate = [ ]
29+ }
30+
31+ worldRuntime [ 'minecraft:entity' ] . description . scripts . animate . push ( 'frw_update' )
32+ worldRuntime [ 'minecraft:entity' ] . description . scripts . animate . push ( 'frw_delay' )
1933
2034 let blocks = { }
2135
22- let dynamicVariables = { }
36+ let delays = { }
2337
2438 let dynamicValues = { }
2539
2640 let constants = { }
2741
2842 let flags = [ ]
2943
44+ let delaySteps = [ ]
45+
3046 function expressionToMolang ( expression ) {
3147 let result = ''
3248
@@ -67,6 +83,12 @@ function compile(tree){
6783 return result
6884 }
6985
86+ function indexFlag ( flag ) {
87+ if ( ! flags . includes ( flag . value ) ) {
88+ flags . push ( flag . value )
89+ }
90+ }
91+
7092 function optimizeExpression ( expression ) {
7193 let dynamic = false
7294
@@ -96,6 +118,14 @@ function compile(tree){
96118 }
97119
98120 if ( expression . value [ 1 ] . token == 'FLAG' || expression . value [ 2 ] . token == 'FLAG' ) {
121+ if ( expression . value [ 1 ] . token == 'FLAG' ) {
122+ indexFlag ( expression . value [ 1 ] )
123+ }
124+
125+ if ( expression . value [ 2 ] . token == 'FLAG' ) {
126+ indexFlag ( expression . value [ 2 ] )
127+ }
128+
99129 dynamic = true
100130 }
101131
@@ -119,6 +149,8 @@ function compile(tree){
119149
120150 if ( expression . value [ 1 ] . token == 'FLAG' ) {
121151 dynamic = true
152+
153+ indexFlag ( expression . value [ 1 ] )
122154 }
123155
124156 if ( expression . value [ 1 ] . token == 'MOLANG' ) {
@@ -270,6 +302,16 @@ function compile(tree){
270302 dynamicValues [ ID ] = {
271303 condition : deep
272304 }
305+ } else if ( mode == 'delay' ) {
306+ if ( condition . token != 'INTEGER' ) {
307+ return new Backend . Error ( `Delay must be an integer!` )
308+ }
309+
310+ if ( parseInt ( condition . value ) <= 0 ) {
311+ return new Backend . Error ( `Delay must be greater than 0!` )
312+ }
313+
314+ delays [ ID ] = parseInt ( condition . value )
273315 }
274316
275317 blocks [ ID ] = block . value
@@ -309,7 +351,7 @@ function compile(tree){
309351
310352 tree . value [ 1 ] = deep
311353 } else if ( tree . token == 'DELAY' ) {
312- const deep = indexCodeBlock ( tree . value [ 1 ] , 'delay' )
354+ const deep = indexCodeBlock ( tree . value [ 1 ] , 'delay' , tree . value [ 0 ] )
313355
314356 if ( deep instanceof Backend . Error ) {
315357 return deep
@@ -321,15 +363,8 @@ function compile(tree){
321363 return tree
322364 }
323365
324- function indexFlag ( flag ) {
325- if ( ! flags . includes ( flag . value ) ) {
326- flags . push ( flag . value )
327- }
328- }
329-
330366 function searchForFlags ( tree ) {
331367 if ( tree . token == 'DEFINITION' || tree . token == 'IF' || tree . token == 'DELAY' ) {
332-
333368 if ( tree . value [ 0 ] . token == 'EXPRESSION' ) {
334369 const deep = searchForFlags ( tree . value [ 0 ] )
335370
@@ -381,7 +416,7 @@ function compile(tree){
381416 }
382417
383418 for ( let i = 0 ; i < tree . length ; i ++ ) {
384- const deep = searchForCodeBlock ( tree [ i ] )
419+ const deep = searchForFlags ( tree [ i ] )
385420
386421 if ( deep instanceof Backend . Error ) {
387422 return deep
@@ -391,7 +426,7 @@ function compile(tree){
391426 }
392427
393428 for ( let i = 0 ; i < tree . length ; i ++ ) {
394- const deep = searchForFlags ( tree [ i ] )
429+ const deep = searchForCodeBlock ( tree [ i ] )
395430
396431 if ( deep instanceof Backend . Error ) {
397432 return deep
@@ -456,7 +491,7 @@ function compile(tree){
456491 "animation_length" : 0.001
457492 }
458493
459- fs . writeFileSync ( './output /animations/frw_' + dynamicValueNames [ i ] + '.json' , JSON . stringify ( animCont , null , 4 ) )
494+ fs . writeFileSync ( path + ' /animations/frw_' + dynamicValueNames [ i ] + '.json' , JSON . stringify ( animCont , null , 4 ) )
460495
461496 worldRuntime [ 'minecraft:entity' ] . description . animations [ dynamicValueNames [ i ] ] = 'animation.firework.runtime.' + dynamicValueNames [ i ]
462497
@@ -481,7 +516,7 @@ function compile(tree){
481516 "animation_length" : 0.001
482517 }
483518
484- fs . writeFileSync ( './output /animations/frw_' + dynamicValueNames [ i ] + '_inverse.json' , JSON . stringify ( animCont , null , 4 ) )
519+ fs . writeFileSync ( path + ' /animations/frw_' + dynamicValueNames [ i ] + '_inverse.json' , JSON . stringify ( animCont , null , 4 ) )
485520
486521 worldRuntime [ 'minecraft:entity' ] . description . animations [ dynamicValueNames [ i ] + '_inverse' ] = 'animation.firework.runtime.' + dynamicValueNames [ i ] + '.inverse'
487522
@@ -505,7 +540,7 @@ function compile(tree){
505540 data . sequence . push ( {
506541 run_command : {
507542 command : [
508- '/' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value
543+ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value
509544 ]
510545 }
511546 } )
@@ -514,45 +549,108 @@ function compile(tree){
514549 data . sequence . push ( {
515550 run_command : {
516551 command : [
517- `/ event entity @s frw:${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
552+ `event entity @s frw:${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
518553 ]
519554 }
520555 } )
556+ } else {
557+ return new Backend . Error ( `Attemped to call undefined method ${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } !` )
521558 }
522559 }
523560 } else if ( blocks [ blockNames [ i ] ] [ l ] . token == 'DEFINITION' || blocks [ blockNames [ i ] ] [ l ] . token == 'IF' || blocks [ blockNames [ i ] ] [ l ] . token == 'DELAY' ) {
524561 if ( blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 1 ] == 'normal' ) {
525562 data . sequence . push ( {
526563 run_command : {
527564 command : [
528- '/ event entity @s frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ]
565+ 'event entity @s frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ]
529566 ]
530567 }
531568 } )
532569 } else if ( blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 1 ] == 'conditional' ) {
533570 data . sequence . push ( {
534571 run_command : {
535572 command : [
536- `/ event entity @s[tag=frw_conditional_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } ] frw:` + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ]
573+ `event entity @s[tag=frw_conditional_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } ] frw:` + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ]
537574 ]
538575 }
539576 } )
540- } else if ( blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 1 ] == 'delay' ) {
577+ } else if ( blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 1 ] == 'delay' ) {
578+ //Delay Intialization
541579 data . sequence . push ( {
542580 run_command : {
543581 command : [
544- '/ event entity @s frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ]
582+ 'event entity @s frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_trigger'
545583 ]
546584 }
547585 } )
586+
587+ //Delay Triggering
588+ for ( let j = 0 ; j < config . delayChannels ; j ++ ) {
589+ let channelData = {
590+ run_command : {
591+ command : [
592+ `tag @s add frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } _time_${ delays [ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] ] } _channel_${ j } ` ,
593+ 'tag @s add added'
594+ ]
595+ }
596+ }
597+
598+ worldRuntime [ 'minecraft:entity' ] . events [ 'frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_channel_' + j . toString ( ) ] = channelData
599+ }
600+
601+ //Delay Channel Choosing
602+ let delayCallData = {
603+ run_command : {
604+ command : [
605+ ]
606+ }
607+ }
608+
609+ for ( let j = 0 ; j < config . delayChannels ; j ++ ) {
610+ delayCallData . run_command . command . push ( `event entity @s[tag=!added, tag=!frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } _time_${ delays [ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] ] } _channel_${ j } ] ${ 'frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_channel_' + j . toString ( ) } ` )
611+ }
612+
613+ delayCallData . run_command . command . push ( `tag @s remove added` )
614+
615+ worldRuntime [ 'minecraft:entity' ] . events [ 'frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_trigger' ] = delayCallData
616+
617+ //Delay Stepping
618+ for ( let j = 0 ; j < config . delayChannels ; j ++ ) {
619+ for ( let k = delays [ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] ] ; k > 0 ; k -- ) {
620+ let timeData = {
621+ run_command : {
622+ command : [
623+ `tag @s remove frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } _time_${ k } _channel_${ j } ` ,
624+ `tag @s add frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } _time_${ k - 1 } _channel_${ j } `
625+ ]
626+ }
627+ }
628+
629+ if ( k == 1 ) {
630+ timeData . run_command . command = [
631+ `tag @s remove frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } _time_${ k } _channel_${ j } ` ,
632+ `event entity @s frw:${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] } `
633+ ]
634+ }
635+
636+ worldRuntime [ 'minecraft:entity' ] . events [ 'frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_time_' + k . toString ( ) + '_channel_' + j . toString ( ) ] = timeData
637+ }
638+ }
639+
640+ //Add Delay Stepping
641+ for ( let j = 0 ; j < config . delayChannels ; j ++ ) {
642+ for ( let k = delays [ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] ] ; k > 0 ; k -- ) {
643+ delaySteps . unshift ( `event entity @s[tag=frw_${ blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_time_' + k . toString ( ) + '_channel_' + j . toString ( ) } ] ${ 'frw:' + blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value [ 0 ] + '_time_' + k . toString ( ) + '_channel_' + j . toString ( ) } ` )
644+ }
645+ }
548646 }
549647 } else if ( blocks [ blockNames [ i ] ] [ l ] . token == 'ASSIGN' ) {
550648 if ( blocks [ blockNames [ i ] ] [ l ] . value [ 1 ] . value == 'true' ) {
551649 data . sequence . push (
552650 {
553651 run_command : {
554652 command : [
555- `/ event entity @s frw:set_${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
653+ `event entity @s frw:set_${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
556654 ]
557655 }
558656 }
@@ -562,7 +660,7 @@ function compile(tree){
562660 {
563661 run_command : {
564662 command : [
565- `/ event entity @s frw:unset_${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
663+ `event entity @s frw:unset_${ blocks [ blockNames [ i ] ] [ l ] . value [ 0 ] . value } `
566664 ]
567665 }
568666 }
@@ -574,6 +672,12 @@ function compile(tree){
574672 worldRuntime [ 'minecraft:entity' ] . events [ 'frw:' + blockNames [ i ] ] = data
575673 }
576674
675+ worldRuntime [ 'minecraft:entity' ] . events [ 'frwb:delay' ] = {
676+ run_command : {
677+ command : delaySteps
678+ }
679+ }
680+
577681 let updateData = {
578682 "format_version" : "1.10.0" ,
579683 "animations" : { }
@@ -591,11 +695,23 @@ function compile(tree){
591695 "animation_length" : 0.001
592696 }
593697
594- fs . writeFileSync ( './output/animations/frw_' + updateID + '_update.json' , JSON . stringify ( updateData , null , 4 ) )
698+ const delayID = uuidv4 ( )
699+
700+ updateData . animations [ 'animation.firework.runtime.' + delayID + '.delay' ] = {
701+ "loop" : true ,
702+ "timeline" : {
703+ "0.0" : [
704+ `/event entity @s frwb:delay`
705+ ]
706+ } ,
707+ "animation_length" : 0.1
708+ }
709+
710+ fs . writeFileSync ( path + '/animations/frw_' + updateID + '_update.json' , JSON . stringify ( updateData , null , 4 ) )
595711
596712 worldRuntime [ 'minecraft:entity' ] . description . animations [ 'frw_update' ] = 'animation.firework.runtime.' + updateID + '.update'
713+ worldRuntime [ 'minecraft:entity' ] . description . animations [ 'frw_delay' ] = 'animation.firework.runtime.' + delayID + '.delay'
597714
598- fs . writeFileSync ( './output/world_runtime.json' , JSON . stringify ( worldRuntime , null , 4 ) )
599- }
600715
601- module . exports = { compile }
716+ fs . writeFileSync ( source , JSON . stringify ( worldRuntime , null , 4 ) )
717+ }
0 commit comments