- D1 Meta Docs - Denizen Script -
Home Page / Pi to one million places / Contact mcmonkey / Donate / Paste Scripts / Denizen Help /
You are browsing as a guest.
Login | Register








The script repo is an archive of historical scripts. For modern scripts, or to post your own, please use the Scripts forum section.





Staff Pick: dHungerGames


By Talamar1
Created: 2015/04/11 13:16:03 UTC-07:00 (9 years and 230 days ago)
Edited: 2019/05/26 06:13:40 UTC-07:00 (5 years and 184 days ago)
Likes: 2

Staff pick as of: 2015/04/13 14:46:08 UTC-07:00 (9 years and 228 days ago)
Denizen Version:
Script Version:
Description:




Download script | View raw script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
152800


# dHunger system
# Arena admins require op or dhunger.command permission
# Arenas are stored in dHungers_saves.yml and it is editable and reloadable.
# Created by Talamar1

dHunger:
  type: world
  debug: false
  events:
    on server start:
    - run dhunger_task_init delay:1t

    on shutdown:
    - run dhunger_task_init delay:1t
    
    on player clicks wall_sign:
    - define sign <context.location.block>
    - wait 1t
    - foreach <yaml[dhunger_saves].list_keys[dhg.arenas].exclude[default]> {
      - if <yaml[dhunger_saves].read[dhg.arenas.%value%.board].as_list.contains[%sign%]||false> && <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.enable]||false> && <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.max_players]||20> >= <server.flag[dhg.%value%.players].size||0> {
        - if <player.has_flag[dhg.play]> determine cancelled
        - flag <player> dhg.play:%value%
        - inject dhunger_command_join
        - foreach stop
        }
      }
    
    on player joins:
    - if <player.has_flag[dhg]> {
      - flag <player> dhg:!
      }
    - if <yaml[dhunger_saves].contains[dhg.player.<player.uuid>]> {
      - inject dhunger_task_cleanup_player
      }
    - teleport <player> <yaml[dhunger_saves].read[dhg.lobby]||<player.location.world.spawn_location>>
    
    on player quits:
    - if <player.has_flag[dhg.play]> {
      - define arena <player.flag[dhg.play]>
      - inject dhunger_remove_player
      - run dhunger_check_end_of_game def:%arena% instantly
      - flag <player> dhg:!
      }
    
    on player killed:
    - if <player.has_flag[dhg.play]> {
      - define arena <player.flag[dhg.play]>
      - narrate "<context.entity.name> was killed by <tern[<context.damager.is_player>]:<context.damager.name>||<context.cause>> in arena %arena%" targets:<server.flag[dhg.%arena%.players].as_list||li@>
      - inject dhunger_remove_player
      - run dhunger_check_end_of_game def:%arena% instantly
      - determine cancelled
      }

    on player breaks block in notable cuboid:
    - foreach <context.location.cuboids.parse[notable_name].filter[starts_with[dhg_]].parse[after[dhg_]]> {
      - if <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.locked]||false> && <yaml[dhunger_saves].contains[dhg.arenas.%value%.settings.breakable]> {
        - if !<yaml[dhunger_saves].read[dhg.arenas.%value%.settings.breakable].contains[<context.material>]> {
          - determine cancelled
          - foreach stop
          }
          else {
          - run dhunger_task_block_track def:break|%value%|<context.location>|<context.material>
          - foreach stop
          }
        }
      }

    on player places block in notable cuboid:
    - foreach <context.location.cuboids.parse[notable_name].filter[starts_with[dhg_]].parse[after[dhg_]]> {
      - if <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.locked]||false> && <yaml[dhunger_saves].contains[dhg.arenas.%value%.settings.placeable]> {
        - if !<yaml[dhunger_saves].read[dhg.arenas.%value%.settings.placeable].contains[<context.material>]> {
          - determine cancelled
          - foreach stop
          }
          else {
          - run dhunger_task_block_track def:place|%value%|<context.location>|<context.material>
          - foreach stop
          }
        }
      }

    on block ignites:
    - inject dhunger_task_block_change_check

    on block burns:
    - inject dhunger_task_block_change_check

    on block forms:
    - inject dhunger_task_block_change_check

    on block fades:
    - inject dhunger_task_block_change_check

    on block builds:
    - inject dhunger_task_block_change_check

    on entity spawns:
    - if !<server.has_flag[dhg.loaded]> queue clear
    - foreach <yaml[dhunger_saves].list_keys[dhg.arenas].exclude[default]> {
      - if <context.entity.location.is_within[cu@dhg_%value%]> {
        - if <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.locked]||false> {
          - if <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.spawnable].as_list||li@> !contains <context.entity> {
            - determine cancelled
            }
          }
        }
      }      

dhunger_task_init:
  type: task
  debug: false
  script:
    - ^flag server dhg:!
    - inject dhunger_command_reload
    - ^foreach <yaml[dhunger_saves].list_keys[dhg.arenas].filter[is[!=].to[default]]> {
      - if <yaml[dhunger_saves].contains[dhg.arenas.%value%.cuboid]> {
        - define area <yaml[dhunger_saves].read[dhg.arenas.%value%.cuboid]>
        - if <def[area].as_cuboid.notable_name> == null {
          - note %area% as:dhg_%value%
          }
        }
      - run dhunger_check_end_of_game def:%value%
      }

dhunger_dhg:
  type: command
  debug: false
  name: dhg
  description: dHunger Command
  usage: /dhg <&lt>arg1<&pipe>arg2...<&gt>
  aliases:
    - dhunger
  script:
    - if <context.args.size||0> == 0 {
      - inject dhunger_command_help
      - queue clear
      }
    - define arg1 <context.args.get[1].escaped>
    - define cmdList li@create|save|delete|spawn|rename|set|breakable|placeable|lock|unlock|resize|board|disable|enable|start|stop|quit|select|list|clear|reload|lobby|vote|tier1|tier2|scan_chests|reset_default|join|spawnable
    - if <def[cmdList].contains[%arg1%]> {
      - if <context.server> || <player.has_permission[dhunger.%arg1%]||false> {
        - if li@placeable|breakable contains %arg1% inject dhunger_command_blockmgmt
          else if li@tier1|tier2 contains %arg1% inject dhunger_command_tier
          else inject dhunger_command_%arg1%
        - queue clear
        }
        else {
        - narrate "<&6>You do not have permission to use the dhunger command."
        - queue clear
        }
      }
    - inject dhunger_command_help

dhunger_command_help:
  type: task
  debug: false
  script:
    - if <player.has_permission[dhunger.create]>         narrate "<&6>/dhg create <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.save]>           narrate "<&6>/dhg save"
    - if <player.has_permission[dhunger.delete]>         narrate "<&6>/dhg delete <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.spawn]>          narrate "<&6>/dhg spawn <&lb>add<&rb>/<&lb>del <&lt>id<&gt><&rb>/<&lb>list<&rb>/<&lb>show<&rb>"
    - if <player.has_permission[dhunger.breakable]>      narrate "<&6>/dhg breakable <&lb>add<&rb>/<&lb>del<&rb>/<&lb>list<&rb>/<&lb>clear<&rb>"
    - if <player.has_permission[dhunger.placeable]>      narrate "<&6>/dhg placeable <&lb>add<&rb>/<&lb>del<&rb>/<&lb>list<&rb>/<&lb>clear<&rb>"
    - if <player.has_permission[dhunger.tier1]>          narrate "<&6>/dhg tier1 <&lb>add<&rb>/<&lb>del<&rb>/<&lb>list<&rb>/<&lb>clear<&rb>"
    - if <player.has_permission[dhunger.tier2]>          narrate "<&6>/dhg tier2 <&lb>add<&rb>/<&lb>del<&rb>/<&lb>list<&rb>/<&lb>clear<&rb>"
    - if <player.has_permission[dhunger.set]>            narrate "<&6>/dhg set <&lt>setting<&gt> <&lt>value<&gt>"
    - if <player.has_permission[dhunger.board]>          narrate "<&6>/dhg board <&lb>add<&rb>/<&lb>del<&rb>"
    - if <player.has_permission[dhunger.lock]>           narrate "<&6>/dhg lock <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.unlock]>         narrate "<&6>/dhg unlock <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.disable]>        narrate "<&6>/dhg disable <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.enable]>         narrate "<&6>/dhg enable <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.start]>          narrate "<&6>/dhg start <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.stop]>           narrate "<&6>/dhg stop <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.select]>         narrate "<&6>/dhg select <&lt>arena name<&gt>"
    - if <player.has_permission[dhunger.resize]>         narrate "<&6>/dhg resize"
    - if <player.has_permission[dhunger.reload]>         narrate "<&6>/dhg reload"
    - if <player.has_permission[dhunger.clear]>          narrate "<&6>/dhg clear"
    - if <player.has_permission[dhunger.reset_default]>  narrate "<&6>/dhg reset_default"
    - if <player.has_permission[dhunger.join]>           narrate "<&6>/dhg join <&lb>Arena Name<&rb>"
    - if <player.has_permission[dhunger.list]>           narrate "<&6>/dhg list"
    - if <player.has_permission[dhunger.quit]>           narrate "<&6>/dhg quit"

dhunger_command_create:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> { 
      - narrate "<&6>/dhg create <&lt>arena name<&gt>" 
      - narrate "<&6>arena name - name for your arena" 
      - queue clear 
      }
    - define arg2 <context.args.get[2]>
    - if %arg2% != <def[arg2].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].contains[dhg.arenas.%arg2%]> {
      - narrate "<&6>Arena with that name already exists."
      - queue clear
      }
    - if li@help|false|true|null contains %arg2% {
      - narrate "<&6>That is not a valid arena name."
      - queue clear
      }
    - if <player.selected_region.is[==].to[null]||true> { 
      - narrate "<&6>You don't have an area selected with WorldEdit wand." 
      - queue clear 
      }
    - narrate "Creating new arena"
    - run dhunger_task_yaml_copy def:dhg.arenas.default|dhg.arenas.%arg2% instantly
    - inject dhunger_command_save
    - note <player.selected_region> as:dhg_%arg2%
    - flag player dhg.edit:%arg2%
    - narrate "Arena created."

dhunger_command_save:
  type: task
  debug: false
  script:
#    - ^if <player.is_online> narrate "Saving Hunger Games"
    - ^yaml savefile:dhunger_saves.yml id:dhunger_saves

dhunger_command_delete:
  type: task
  debug: false
  script:
    - define arena <context.args.get[2]||<player.flag[dhg.edit]||<def[1]||help>>>
    - if %arena% == help {
      - narrate "<&6>/dhg delete <&lt>Arena Name<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - narrate "Deleting selected arena."
    - yaml id:dhunger_saves set dhg.arenas.%arena%:!
    - flag player dhg.edit:!
    - note remove as:dhg_%arena%
    - inject dhunger_command_save

dhunger_command_reset_default:
  type: task
  debug: false
  script:
    - narrate "Setting up default arena values."
    - ^yaml id:dhunger_saves set dhg.arenas.default:!
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.max_players:20
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.min_players:2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.lightning:2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.lightning_delay:60
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.locked:false
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.enable:false
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.timer:30
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.countdown:10
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.restock:0
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.placeable:|:m@melon_block|m@tnt|m@cake
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.spawnable:|:e@bat|e@pig|e@squid|e@villager|e@rabbit|e@sheep|e@cow|e@horse|e@chicken|e@donkey|e@wolf|e@ocelot|e@mule
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@leather_chestplate:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@carrot:|:25|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@rabbit:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@stone_sword:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@fishing_rod:|:25|3
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@iron_ingot:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@feather:|:20|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@gold_sword:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@wood_sword:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@pork:|:25|3
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@stick:|:25|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@leather_helmet:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@raw_beef:|:20|4
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@leather_boots:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@string:|:25|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@arrow:|:25|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@flint:|:15|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@leather_leggings:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@cookie:|:5|2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@raw_fish:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@gold_ingot:|:10|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@raw_chicken:|:20|4
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@potato:|:15|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@bow:|:25|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@apple:|:25|3
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@pumpkin_pie:|:5|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier1.i@baked_potato:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@cooked_chicken:|:20|2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@cake:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@ender_pearl:|:20|2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@cooked_rabbit:|:10|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@chainmail_chestplate:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@grilled_pork:|:20|3
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@gold_boots:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@melon_block:|:15|4
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@gold_leggings:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@iron_leggings:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@mushroom_soup:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@chainmail_helmet:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@iron_chestplate:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@nether_warts:|:5|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@iron_boots:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@diamond:|:5|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@chainmail_boots:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@flint_and_steel:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@golden_apple:|:10|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@golden_carrot:|:5|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@glass_bottle:|:10|5
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@iron_helmet:|:15|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@gold_helmet:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@gold_chestplate:|:20|1
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@cooked_beef:|:20|2
    - ^yaml id:dhunger_saves set dhg.arenas.default.settings.tier2.i@chainmail_leggings:|:15|1
    - narrate "Finished setting up Default File."
    - inject dhunger_command_save

dhunger_command_reload:
  type: task
  debug: false
  script:
    - ^if <yaml.list.contains[dhunger_saves]> yaml unload id:dhunger_saves
    - ^if <server.has_file[dhunger_saves.yml]> { 
      - announce "<red><&lb>dHunger<&rb> <green>Loading Hunger Config File"
      - yaml load:dhunger_saves.yml id:dhunger_saves
      }
      else { 
      - announce "<red><&lb>dHunger<&rb> <green>Creating Hunger Config File"
      - yaml create id:dhunger_saves
      - yaml savefile:dhunger_saves.yml id:dhunger_saves
      - inject dhunger_command_reset_default
      }
    - flag server dhg.loaded

dhunger_command_clear:
  type: task
  debug: false
  script:
    - yaml unload id:dhunger_saves
    - yaml create id:dhunger_saves
    - inject dhunger_command_save

dhunger_command_select:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> { 
      - narrate "<&6>/dhg select <&lt>arena name<&gt>" 
      - narrate "<&6>arena name - name for your arena" 
      - queue clear 
      }
    - if <context.args.get[2].is[!=].to[<context.args.get[2].escaped>]> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - define arg2 <context.args.get[2]>
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arg2%]> {
      - narrate "<&6>Selecting arena with name<&co> %arg2%"
      - flag player dhg.edit:%arg2%
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }


dhunger_command_resize:
  type: task
  debug: false
  script:
    - define arena <context.args.get[2]||<player.flag[dhg.edit]||<def[1]||help>>>
    - if %arena% == help {
      - narrate "<&6>/dhg resize <&lt>Arena Name<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if <player.selected_region.is[==].to[null]||true> { 
      - narrate "<&6>You don't have an area selected with WorldEdit wand." 
      - queue clear 
      }
    - narrate "Resizing arena %arena%"
    - yaml id:dhunger_saves set dhg.arenas.<player.flag[dhg.edit]>.cuboid:<player.selected_region>
    - inject dhunger_command_save
    - note <player.selected_region> as:dhg_<player.flag[dhg.edit]>
    - narrate "Arena resized."

dhunger_command_quit:
  type: task
  debug: false
  script:
    - if !<player.has_flag[dhg.play]> {
      - narrate "You don't appear to be in a game."
      - queue clear
      }
    - define arena <player.flag[dhg.play]>
    - narrate "Quitting arena game."
    - narrate "<player.name> quit the arena." targets:<server.flag[dhg.%arena%.players].as_list||li@>
    - inject dhunger_remove_player
    - run dhunger_check_end_of_game def:%arena%

dhunger_command_spawn:
  type: task
  debug: false
  script:
    - define arg2 <context.args.get[2]||help>
    - define arg3 <context.args.get[3]||0>
    - if %arg2% == help {  
      - narrate "<&6>/dhg spawn add - add a new spawn"
      - narrate "<&6>/dhg spawn del <&lt>ID<&gt> - delete spawn at ID number (use list)"
      - narrate "<&6>/dhg spawn list - list all spawns"
      - narrate "<&6>/dhg spawn clear - clear all spawns"
      - narrate "<&6>/dhg spawn show - visually show spawns"
      - queue clear 
      }
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - if li@add|del|list|show|clear contains %arg2% {
      - inject dhunger_spawn_%arg2%
      }
      else {
      - narrate "Invalid setting"
      }

dhunger_spawn_add:
  type: task
  debug: false
  script:
    - if %arg3% == help { 
      - narrate "<&6>/dhg spawn add" 
      - narrate "<&6>Adds a spawnpoint to the selected arena." 
      - queue clear 
      }
    - narrate "Adding new spawnpoint."
    - define scount <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].size||0>
    - define spawnpoint l@<player.location.block.x.add[.5]>,<player.location.block.y>,<player.location.block.z.add[.5]>,<player.location.pitch>,<player.location.yaw>,<player.location.world.name>
    - yaml id:dhunger_saves set dhg.arenas.%arena%.spawnpoint.<def[scount].add[1]>:%spawnpoint%
    - inject dhunger_command_save

dhunger_spawn_del:
  type: task
  debug: false
  script:
    - if %arg3% == help || %arg3% == 0 { 
      - narrate "<&6>/dhg spawn del <&lt>spawnpoint<&gt>" 
      - narrate "<&6>spawnpoint - ID Number for spawnpoint" 
      - queue clear 
      }
    - if <yaml[dhunger_saves].contains[dhg.arenas.%arena%.spawnpoint.%arg3%]> {
      - narrate "Removing spawnpoint."
      - yaml id:dhunger_saves set dhg.arenas.%arena%.spawnpoint.%arg3%:!
      - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].numerical> {
        - define spawnpoint <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.%value%]>
        - yaml id:dhunger_saves set dhg.arenas.%arena%.spawnpoint.%value%:!
        - yaml id:dhunger_saves set dhg.arenas.%arena%.spawnpoint.%loop_index%:%spawnpoint%
        }
      - inject dhunger_command_save
      }
      else {
      - narrate "Could not find that spawnpoint."
      }

dhunger_spawn_list:
  type: task
  debug: false
  script:
    - narrate "Listing spawnpoints:"
    - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].numerical> {
      - narrate "<&lt>%value%<&gt> <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.%value%]>"
      }

dhunger_spawn_show:
  type: task
  debug: false
  script:
    - narrate "Showing spawnpoints:"
    - repeat 10 {
      - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].numerical> {
        - playeffect <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.%value%]> effect:red_dust qty:15
        }
      - wait 1s
      }

dhunger_spawn_clear:
  type: task
  debug: false
  script:
    - yaml id:dhunger_saves set dhg.arenas.%arena%.spawnpoint:!
    - inject dhunger_command_save

dhunger_command_tier:
  type: task
  debug: false
  script:
    - define arg2 <context.args.get[2]||help>
    - define arg3 <context.args.get[3]||li@>
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - if %arg2% == help || ( li@add|del contains %arg2% && <def[arg3].as_list.size> <= 0 ) || ( li@add|del|list|clear|help !contains %arg2% ) { 
      - narrate "<gold>/dhg tier<&lb>1<&pipe>2<&rb> (add<&pipe>del) (item)(<&pc>percent)(<&ns>qty)"
      - narrate "<gold><&sp><&sp><&sp>1 or 2 <green>the tier you want to modify"
      - narrate "<gold><&sp><&sp><&sp>add or del <green>(optional<&cm> leaving it out overwites the whole list)"
      - narrate "<gold><&sp><&sp><&sp>item <green>the item to modify"
      - narrate "<gold><&sp><&sp><&sp><&pc>value <green>the chance item will be in chest (optional<&cm> default 25)"
      - narrate "<gold><&sp><&sp><&sp><&ns>value <green>the max quantity to be in a chest (optional<&cm> default 1)"
      - narrate "<gold>/dhg tier<&lb>1<&pipe>2<&rb> list"
      - narrate "<gold>/dhg tier<&lb>1<&pipe>2<&rb> clear"
      - queue clear
      }
      else if %arg2% == clear {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%:!
      - inject dhunger_command_save
      - queue clear
      }
      else if %arg2% == list {
      - narrate "%arg1% item list"
      - if !<yaml[dhunger_saves].contains[dhg.arenas.%arena%.settings.%arg1%]> queue clear
      - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.settings.%arg1%]> {
        - define itemvals <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%arg1%.%value%]>
        - narrate "<gold><&sp><&sp><&sp><def[value]><&sp><green><def[itemvals].get[1].pad_left[3]><&pc><&sp><white>qty<&co><def[itemvals].get[2]>"
        }
      - queue clear
      }
    - foreach %arg3% {
      - define newitem <el@val[%value%].before[%].before[#].as_item||null>
      - define percent <el@val[%value%].after[%].before[#].as_int||25>
      - define qty <el@val[%value%].after[#].before[%].as_int||1>
      - if <def[newitem].is[!=].to[null]> {
        - narrate "%value% is %newitem% at %percent% qty %qty%"
        - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%.%newitem%:!
        - if %arg2% == add {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%.%newitem%:|:%percent%|%qty%
          }
        }
        else {
        - narrate "%newitem% is not a valid item"
        }
      }
    - inject dhunger_command_save

dhunger_task_scan_chests:
  type: task
  debug: false
  script:
    - wait 1t
    - if <context.args.get[2].is[==].to[null]||true> && !<player.has_flag[dhg.edit]> && <def[arena].exists> { 
      - narrate "<&6>/dhg locked <&lt>Arena ID<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if !<def[arena].exists> {
      - if <context.args.get[2].is[==].to[null]||true> {
        - define arena <player.flag[dhg.edit]>
        }
        else {
        - define arena <context.args.get[2]>
        }
      }
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].contains[dhg.arenas.%arena%]> && <server.list_notables[cuboids].contains[cu@dhg_%arena%]> {
      - narrate "Processing chests in arena %arena"
      - define length <cu@dhg_%arena%.min.x.sub[<cu@dhg_%arena%.max.x>].abs>
      - define width <cu@dhg_%arena%.min.z.sub[<cu@dhg_%arena%.max.z>].abs>
      - define height <cu@dhg_%arena%.min.y.sub[<cu@dhg_%arena%.max.y>].abs>
      - define chunkie 100
      - define chests li@
      - repeat <def[length].div[%chunkie%].round_up> {
        - define x_tick <def[value].sub[1]>
        - repeat <def[width].div[%chunkie%].round_up> {
          - define z_tick <def[value].sub[1]>
          - repeat <def[height].div[%chunkie%].round_up> {
            - define y_tick <def[value].sub[1]>
            - foreach li@x|y|z {
              - define %value%_min <cu@dhg_%arena%.min.%value%.add[<def[%value%_tick].mul[%chunkie%]>]>
              - define %value%_max <cu@dhg_%arena%.min.%value%.add[<def[%value%_tick].add[1].mul[%chunkie%].sub[1]>]>
              - if <def[%value%_max]> > <cu@dhg_%arena%.max.%value%> define %value%_max <cu@dhg_%arena%.max.%value%>
              }
            - define subcuboid cu@%x_min%,%y_min%,%z_min%,<cu@dhg_%arena%.min.world.name>|%x_max%,%y_max%,%z_max%,<cu@dhg_%arena%.min.world.name>
            - define chests <def[chests].include[<def[subcuboid].get_blocks[chest,1|chest,2|chest,3|chest,4|chest,5|chest,6|trapped_chest,1|trapped_chest,2|trapped_chest,3|trapped_chest,4|trapped_chest,5|trapped_chest,6]>]>
            - wait 1t
            }
          }
        }
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.tier1_chests:!
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.tier2_chests:!
      - foreach %chests% {
        - narrate "Chest %value% material <def[value].material.name>"
        - if <def[value].material.name> == chest {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.tier1_chests:->:%value%
          }
          else {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.tier2_chests:->:%value%
          }
        }
      - inject dhunger_command_save
      - narrate "found <def[chests].size> chests "
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }
    - narrate "Chest processing for arena %arena% complete"


dhunger_command_spawnable:
  type: task
  debug: false
  script:
    - define arg2 <context.args.get[2]||help>
    - define arg3 <context.args.get[3]||li@>
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - if %arg2% == help || ( li@add|del contains %arg2% && <def[arg3].as_list.size> <= 0 ) || ( li@add|del|list|clear|help !contains %arg2% ) { 
      - narrate "<gold>/dhg spawnable (add<&pipe>del) (entity|entity)"
      - narrate "<gold><&sp><&sp><&sp>entity <green>the creature to allow spawning"
      - narrate "<gold>/dhg spawnable list"
      - narrate "<gold>/dhg spawnable clear"
      - queue clear
      }
      else if %arg2% == clear {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.spawnable:!
      - narrate "Spawnable list cleared for arena %arena%"
      - inject dhunger_command_save
      - queue clear
      }
      else if %arg2% == list {
      - narrate "Spawnable item list"
      - if <yaml[dhunger_saves].contains[dhg.arenas.%arena%.settings.spawnable].not> queue clear
      - foreach <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.spawnable]> {
        - narrate "<gold><&sp><&sp><&sp><def[value]>"
        }
      - queue clear
      }
    - foreach %arg3% {
      - define entity <el@val[%value%].as_entity||false>
      - if <def[entity].is[!=].to[false]> {
        - narrate "%value% is %entity%"
        - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.spawnable:<-:%entity%
        - if %arg2% == add {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.spawnable:->:%entity%
          }
        }
        else {
        - narrate "%entity% is not a valid item"
        }
      }
    - inject dhunger_command_save

dhunger_command_blockmgmt:
  type: task
  debug: false
  script:
    - define arg2 <context.args.get[2]||help>
    - define arg3 <context.args.get[3]||li@>
    - if %arg2% == help || ( li@add|del contains %arg2% && <def[arg3].as_list.size> <= 0 ) { 
        - narrate "<&6>/dhg %arg1% add - add block(s) to list"
        - narrate "<&6>/dhg %arg1% del - delete block(s) from list"
        - narrate "<&6>/dhg %arg1% list - list all blocks"
        - narrate "<&6>/dhg %arg1% clear - clear all blocks in list"
        - queue clear
      }
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - define settings li@add|del|list|clear
    - if <def[settings].contains[%arg2%]> inject dhunger_blockmgmt_%arg2%
      else narrate "Invalid %arg1% command"

dhunger_blockmgmt_add:
  type: task
  debug: false
  script:
    - define arena <def[arena]||<def[1]||0>>
    - define arg1 <def[arg1]||<def[1]||0>>
    - define arg3 <def[arg3]||<def[3].unescaped||li@>>
    - if %arg3% == help {
      - narrate "<&6>/dhg %arg1% add <&lt>value<&pipe>value<&pipe>...<&gt>"
      - narrate "Where <&dq>value<&dq> is a <&pipe> separated list of block or material IDs."
      - queue clear
    }
    - foreach <el@val[%arg3%].as_list> {
      - define newmat <el@val[%value%].as_material||null>
      - if <def[newmat].is[!=].to[null]> {
        - narrate "%value% is %newmat%"
        - if !<yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%arg1%].contains[%newmat%]> {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%:->:%newmat%
          - narrate "Added blocks to %arg1% list for %arena%"
          }
          else {
          - narrate "Item already in list"
          }
        }
        else {
        - narrate "%value% is not a valid block or item"
        }
      }
    - inject dhunger_command_save

dhunger_blockmgmt_del:
  type: task
  debug: false
  script:
    - if %arg3% == help {
      - narrate "<&6>/dhg %arg1% del <&lt>value<&pipe>value<&pipe>...<&gt>"
      - narrate "Where <&dq>value<&dq> is a <&pipe> separated list of block or material IDs."
      - queue clear
    }
    - foreach <def[arg3]> {
      - define newmat <el@val[%value%].unescaped.as_material||null>
      - if <def[newmat].is[!=].to[null]> {
        - narrate "%newmat% is valid"
        - if <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%arg1%].contains[%newmat%]> {
          - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%:<-:%newmat%
          - narrate "Deleted blocks from %arg1% list for %arena%"
          }
        }
        else {
        - narrate "%value% is not a valid block or item"
        }
      }
    - inject dhunger_command_save

dhunger_blockmgmt_list:
  type: task
  debug: false
  script:
    - if <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%arg1%].size||0> > 0 {
      - define blocklist <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%arg1%].replace[li@]>
      - narrate "%blocklist%"
      }

dhunger_blockmgmt_clear:
  type: task
  debug: false
  script:
    - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.%arg1%:!
    - narrate "Cleared blocks from %arg1% list for %arena%"
    - inject dhunger_command_save

dhunger_command_rename:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> || <context.args.get[3].is[==].to[null]||true> { 
      - narrate "<&6>/dhg rename <&lt>arena name<&gt>" 
      - narrate "<&6>arena name - name for your arena" 
      - queue clear 
      }
    - if <context.args.get[2].is[!=].to[<context.args.get[2].escaped>]> || <context.args.get[3].is[!=].to[<context.args.get[3].escaped>]> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - define arg2 <context.args.get[2]>
    - define arg3 <context.args.get[3]>
    - if li@help|false|true|null|%arg2% contains %arg3% {
      - narrate "<&6>That is not a valid arena name."
      - queue clear
      }
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arg2%]> {
      - narrate "<&6>Renaming arena with name<&co> %arg2% to<&co> %arg3%"
      - run dhunger_task_yaml_copy def:dhg.arenas.%arg2%|dhg.arenas.%arg3% instantly
      - run dhunger_command_delete def:%arg2% instantly
      - note <yaml[dhunger_saves].read[dhg.arenas.%arg3%.cuboid]||cu@0,0,0,world|0,0,0,world> as:dhg_%arg3%
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }


dhunger_command_lock:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> && !<player.has_flag[dhg.edit]> { 
      - narrate "<&6>/dhg locked <&lt>Arena ID<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if <context.args.get[2].is[==].to[null]||true> define arena <player.flag[dhg.edit]>
      else define arena <context.args.get[2]>
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arena%]> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.locked:true
      - narrate "<&6>Locking arena with name%arena%"
      - inject dhunger_task_scan_chests
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }

dhunger_command_unlock:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> && !<player.has_flag[dhg.edit]> { 
      - narrate "<&6>/dhg locked <&lt>Arena ID<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if <context.args.get[2].is[==].to[null]||true> define arena <player.flag[dhg.edit]>
      else define arena <context.args.get[2]>
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arena%]> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.locked:false
      - narrate "<&6>Unlocking arena with name%arena%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }

dhunger_command_enable:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> && !<player.has_flag[dhg.edit]> { 
      - narrate "<&6>/dhg enable <&lt>Arena ID<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if <context.args.get[2].is[==].to[null]||true> define arena <player.flag[dhg.edit]>
      else define arena <context.args.get[2]>
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arena%]> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.enable:true
      - narrate "<&6>Enabling arena with name%arena%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }

dhunger_command_disable:
  type: task
  debug: false
  script:
    - if <context.args.get[2].is[==].to[null]||true> && !<player.has_flag[dhg.edit]> { 
      - narrate "<&6>/dhg enable <&lt>Arena ID<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if <context.args.get[2].is[==].to[null]||true> define arena <player.flag[dhg.edit]>
      else define arena <context.args.get[2]>
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <yaml[dhunger_saves].list_keys[dhg.arenas].contains[%arena%]> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.enable:false
      - narrate "<&6>Disabling arena with name%arena%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>Arena with that name not found."
      }
    
dhunger_command_list:
  type: task
  debug: false
  script:
    - foreach <yaml[dhunger_saves].list_keys[dhg.arenas]> {
      - narrate "<&6>Arena: %value%"
      }

dhunger_command_set:
  type: task
  debug: false
  script:
    - define arg2 <context.args.get[2]||help>
    - define arg3 <context.args.get[3]||help>
    - if %arg2% == help { 
      - narrate "<&6>/dhg set <&lt>setting<&gt> <&lt>value<&gt>"
      - narrate "<&6>setting - setting to change" 
      - narrate "<&6><&sp><&sp><&sp>restock"
      - narrate "<&6><&sp><&sp><&sp>min_players"
      - narrate "<&6><&sp><&sp><&sp>max_players"
      - narrate "<&6><&sp><&sp><&sp>lightning"
      - narrate "<&6><&sp><&sp><&sp>lightning_delay"
      - narrate "<&6><&sp><&sp><&sp>countdown"
      - narrate "<&6><&sp><&sp><&sp>timer"
      - narrate "<&6>value - value to assign to setting" 
      - queue clear 
      }
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - define settings li@restock|min_players|max_players|lightning|lightning_delay|countdown|timer
    - if <def[settings].contains[%arg2%]> {
      - inject dhunger_setting_%arg2%
      }
      else {
      - narrate "Invalid setting"
      - queue clear
      }

dhunger_setting_restock:
  type: task
  debug: false
  script:
    - if %arg3% != help && ( %arg3% matches integer && %arg3% > 0 ) {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.restock:%arg3%
      - narrate "<&6>Changing RESTOCK on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set restock <&lt>value<&gt>"
      - narrate "Where <&dq>value<&dq> is the number of seconds between restocks."
      - narrate "Use 0 for no restocking."
      }

dhunger_setting_min_players:
  type: task
  debug: false
  script:
    - if %arg3% matches integer && %arg3% >= 2 {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.min_players:%arg3%
      - narrate "<&6>Changing MIN_PLAYERS on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set min_players <&lt>value<&gt>"
      - narrate "Where <&dq>value<&dq> is the minimum number of players for a game."
      - narrate "Default is 2."
      }

dhunger_setting_max_players:
  type: task
  debug: false
  script:
    - if %arg3% matches integer && %arg3% >= <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.min_players]||2> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.max_players:%arg3%
      - narrate "<&6>Changing MAX_PLAYERS on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set max_players <&lt>value<&gt>"
      - narrate "Where <&dq>value<&dq> is the maximum number of players for a game."
      - narrate "Default is 20."
      }

dhunger_setting_lightning:
  type: task
  debug: false
  script:
    - define allowed li@always|2|off
    - if <def[allowed].contains[%arg3%]> {
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.lightning:%arg3%
      - narrate "<&6>Changing LIGHTNING on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set lightning <&lt>value<&gt>"
      - narrate "<&6>Available values are: always, 2 or off"
      - narrate "<&6>Always = Use lightning through the whole game"
      - narrate "<&6>2 = Use lightning when game is down to 2 players"
      - narrate "<&6>off = Don't use lightning at all"
      }

dhunger_setting_lightning_delay:
  type: task
  debug: false
  script:
    - if <def[arg3].is[matches].to[integer]> { 
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.lightning_delay:%arg3%
      - narrate "<&6>Changing LIGHTNING_DELAY on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set lightning_delay <&lt>value<&gt>"
      - narrate "<&6>value = Seconds between lightning strikes."
      - narrate "<&6>Available values are numbers > 0 "
      }

dhunger_setting_countdown:
  type: task
  debug: false
  script:
    - if <def[arg3].is[matches].to[integer]> { 
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.countdown:%arg3%
      - narrate "<&6>Changing COUNTDOWN on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set countdown <&lt>value<&gt>"
      - narrate "<&6>Number = Seconds to countdown at start of game."
      - narrate "<&6>Available values are numbers > 0 "
      - narrate "<&6>Default is 10"
      }

dhunger_setting_timer:
  type: task
  debug: false
  script:
    - if <def[arg3].is[matches].to[integer]> { 
      - yaml id:dhunger_saves set dhg.arenas.%arena%.settings.timer:%arg3%
      - narrate "<&6>Changing TIMER on arena <&lt>%arena%<&gt> with value%arg3%"
      - inject dhunger_command_save
      }
      else {
      - narrate "<&6>/dhg set countdown <&lt>value<&gt>"
      - narrate "<&6>Number = Timer to wait for players to join."
      - narrate "<&6>Available values are numbers > 0 "
      - narrate "<&6>Default is 30"
      }

dhunger_command_board:
  type: task
  debug: false
  script:
    - if !<player.has_flag[dhg.edit]> {
      - narrate "You don't have an arena selected."
      - queue clear
      }
    - define arena <player.flag[dhg.edit]>
    - define arg2 <context.args.get[2]>
    - define settings li@add|del|reset
    - if <def[settings].contains[%arg2%]> {
      - if <def[arg2].is[==].to[help]> inject dhunger_help_board_%arg2%
        else inject dhunger_board_%arg2%
      }
      else {
      - narrate "<&6>/dhg board <&lb>add<&rb><&pipe><&lb>del<&rb><&pipe><&lb>reset<&rb>"
      - narrate "add - Add a sign board"
      - narrate "del - Delete the arena sign configuration"
      - narrate "reset - Reset a board"
      }
      
dhunger_board_add:
  type: task
  debug: false
  script:
    - define signs <player.selected_region.get_blocks.filter[material.bukkit_enum.is[==].to[wall_sign]]||0>
    - if <def[signs].size.is[OR_MORE].to[3]> {
      - narrate "Found your sign"
      - define sdir <def[signs].get[1].material.data>
      - define slist li@<def[signs].get[1]>
      - define signs <def[signs].remove[1]>
      - narrate "Direction %sdir%"
      # West
      - foreach %signs% {
        - define nsign %value%
        - define found false
        - foreach <def[slist]> {
          - if <def[nsign].as_location.y.is[MORE].than[<def[value].as_location.y>]> {
            - define slist <def[slist].insert[%nsign%].at[<def[loop_index]>]>
            - define found true
            - foreach stop
            }
            # West Facing
            else if <def[sdir].is[==].to[4]> {
            - if <def[nsign].as_location.z.is[LESS].than[<def[value].as_location.z>]> {
              - define slist <def[slist].insert[%nsign%].at[<def[loop_index]>]>
              - define found true
              - foreach stop
              }
            }
            # East Facing
            else if <def[sdir].is[==].to[5]> {
            - if <def[nsign].as_location.z.is[MORE].than[<def[value].as_location.z>]> {
              - define slist <def[slist].insert[%nsign%].at[<def[loop_index]>]>
              - define found true
              - foreach stop
              }
            }
            # North Facing
            else if <def[sdir].is[==].to[2]> && <def[nsign].as_location.y.is[==].than[<def[value].as_location.y>]> {
            - if <def[nsign].as_location.x.is[MORE].than[<def[value].as_location.x>]> {
              - define slist <def[slist].insert[%nsign%].at[<def[loop_index]>]>
              - define found true
              - foreach stop
              }
            }
            # South Facing
            else if <def[sdir].is[==].to[3]> {
            - if <def[nsign].as_location.x.is[LESS].than[<def[value].as_location.x>]> {
              - define slist <def[slist].insert[%nsign%].at[<def[loop_index]>]>
              - define found true
              - foreach stop
              }
            }
          }
        - if !<def[found]> {
          - if <def[sdir].is[==].to[4]> || <def[sdir].is[==].to[3]> {
            - define slist <def[slist].insert[%nsign%].at[<def[slist].size.add[1]>]>
            }
            else {
            - define slist <def[slist].insert[%nsign%].at[<def[slist].size>]>
            }
          }
        }
      - yaml id:dhunger_saves set dhg.arenas.%arena%.board:%slist%
      - inject dhunger_command_save
      - inject dhunger_board_reset
      }
      else {
      - narrate "sign wall missing or not big enough"
      }

dhunger_board_del:
  type: task
  debug: false
  script:
    - define sign <yaml[dhunger_saves].read[dhg.arenas.%arena%.board]||li@>
    - foreach %sign% {
      - sign type:automatic "" %value%
      }
    - yaml id:dhunger_saves set dhg.arenas.%arena%.board:!
    - inject dhunger_command_save
    - narrate "The board in arena %arena% was removed."

dhunger_board_reset:
  type: task
  debug: false
  script:
    - ^define sign <yaml[dhunger_saves].read[dhg.arenas.%arena%.board]||li@>
    - ^if <def[sign].size> > 2 {
      - define p_list <tern[<server.has_flag[dhg.%arena%.players]>]:<server.flag[dhg.%arena%.players].parse[name].alphanumeric>||li@>
      - define p_count <def[p_list].size>
      - define alive <server.get_online_players_flagged[dhg.play].filter[flag[dhg.play].is[==].to[%arena%]].size>
      # Set panel 1
      - sign type:automatic "Hunger Game|%arena%|<&lt>Click to Join<&gt>" <def[sign].get[1]>
      # Set panel 2
      - sign type:automatic "%arena%|<white>%p_count%<&0>/<&2>%alive%<&0>/<&4><server.flag[dhg.%arena%.dead].size||0><&0>/<&0><yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].size||20>" <def[sign].get[2]>
      # Clear remaining boards
      - define dead_list <server.flag[dhg.%arena%.dead]||li@>
      - define p_sign li@
      - foreach <def[sign].get[3].to[<def[sign].size>]> {
        - if <def[p_list].size> = 0 {
          - sign type:automatic "" %value%
          - foreach next
          }
        - define l_sign %value%
        - define p_sign <def[p_list].get[1].to[3]||li@>
        - foreach p_sign {
          - if <def[dead_list].contains[%value%]> define p_sign <def[p_sign].remove[%loop_index%].insert[<red>%value%].at[%loop_index%]>
          }
        - define p_list <def[p_list].remove[1|2|3]||li@>
        - sign type:automatic "%p_sign%" %l_sign%
        }
      }

dhunger_command_lobby:
  type: task
  debug: false
  script:
    - yaml id:dhunger_saves set dhg.lobby:l@<player.location.block.x.add[.5]>,<player.location.block.y>,<player.location.block.z.add[.5]>,<player.location.pitch>,<player.location.yaw>,<player.location.world.name>
    - inject dhunger_command_save

dhunger_command_join:
  type: task
  debug: false
  script:
    - define arena <player.flag[dhg.play]||<context.args.get[2]||help>>
    - narrate "Joining game %arena%"
    - if %arena% == help {
      - narrate "<&6>/dhg join <&lt>Arena Name<&gt>" 
      - narrate "<&6>Either select an arena or provide an arena name" 
      - queue clear 
      }
    - if %arena% != <def[arena].escaped> { 
      - narrate "<&6>That value doesn't look right." 
      - queue clear 
      }
    - if <server.has_flag[dhg.%arena%.started]> {
      - narrate "That game has already started"
      - queue clear
      }
    - define spawnpoints <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].numerical>
    - define order <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.order]||ordered>
    - define ingame false
    - if <def[order].matches[random]> {
      - while <def[spawnpoints].size> > 0 {
        - define randomnum <util.random.int[1].to[<def[spawnpoints].size>]>
        - define spawnpt <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.<def[spawnpoints].get[%randomnum%]>]>
        - if <def[spawnpt].find.players.within[3].size> == 0 {
          - define ingame true
          - while stop
          }
        - define spawnpoints <def[spawnpoints].remove[%randomnum%]>
        }
      }
      else {
      - foreach %spawnpoints% {
        - define spawnpt <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.%value%]>
        - if <def[spawnpt].find.players.within[3].size> == 0 {
          - define ingame true
          - foreach stop
          }
        }
      }
    - if !<def[ingame]> {
      - narrate "Game spawns are full"
      }
      else {
      - flag server dhg.%arena%.players:->:<player>
      - inject dhunger_task_prep_player
      - define state up
      - inject dhunger_task_barricade
      - teleport <player> %spawnpt%
      - adjust <player> gamemode:survival
      - narrate "<player.name> is joining the game." targets:<server.flag[dhg.%arena%.players].exclude[<player>]||li@>
      - inject dhunger_board_reset
      - if <server.has_flag[dhg.%arena%.timer]> {
        - flag server dhg.%arena%.timer:<yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.timer]||30>
        }
        else {
        - run dhunger_game_check_start def:%arena%
        }
      }

dhunger_game_check_start:
  type: task
  debug: false
  script:
    - wait 1t
    - ^define arena %1%
    - ^define timer <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.timer]||30>
    - ^flag server dhg.%arena%.timer:%timer%
    - ^define steps li@%timer%|15|10|5|1
    - ^if <def[timer].is[MORE].than[30]> {
      - repeat <def[timer].div[30].round_up> {
        - define steps <def[steps].insert[<def[value].mul[30]>].at[0]>
        }
      - define steps <def[steps].numerical.deduplicate>
      }
    - ^while <server.has_flag[dhg.%arena%.timer]> {
      - if <server.flag[dhg.%arena%.players].size||0> == 0 {
        - run dhunger_check_end_of_game def:%arena%
        - while stop
        }
      - ^if <def[steps].contains[<server.flag[dhg.%arena%.timer].as_int>]> {
        - if <server.flag[dhg.%arena%.players].size> >= <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.min_player]||2> {
          - narrate "Game will start in<server.flag[dhg.%arena%.timer].as_int> seconds" targets:<server.flag[dhg.%arena%.players].as_list||li@>
          }
          else {
          - narrate "Waiting for players...<server.flag[dhg.%arena%.timer].as_int> seconds to go." targets:<server.flag[dhg.%arena%.players].as_list||li@>
          }
        }
        else if <server.flag[dhg.%arena%.timer].is[or_less].than[0]> {
        - if <server.flag[dhg.%arena%.players].size.is[less].than[<yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.min_player]||2>]> {
          - define state down
          - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.spawnpoint].numerical> {
            - define spawnpt <yaml[dhunger_saves].read[dhg.arenas.%arena%.spawnpoint.%value%].as_location.block>
            - inject dhunger_task_barricade
            }
          - narrate "Not enough players, cancelling game" targets:<server.flag[dhg.%arena%.players].as_list||li@>
          - run dhunger_check_end_of_game def:%arena%|cancelled
          - flag server dhg.%arena%:!
          - while stop
          }
        - run dhunger_game_start def:%arena%
        - while stop
        }
      - ^flag server dhg.%arena%.timer:--
      - wait 1s
      }

dhunger_game_start:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^define timer <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.countdown]||10>
    - ^repeat %timer% {
      - define count <def[timer].sub[%value%].add[1]>
      - narrate "Beginning in <red>%count%" targets:<server.flag[dhg.%arena%.players].as_list||li@>
      - wait 1s
      }
    - ^run dhunger_stock_chests def:%arena% instantly
    - ^define state down
    - ^flag server dhg.%arena%.started:true
    - ^foreach <server.flag[dhg.%arena%.players].as_list> {
      - define spawnpt <def[value].location.block>
      - inject dhunger_task_barricade
      }
    - ^narrate "<green>GO!" targets:<server.flag[dhg.%arena%.players].as_list||li@>
    - ^if <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.lightning]||2> == always {
      - run dhunger_game_lightning def:%arena% id:dhg_lightning_%arena%
      }
    - ^if <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.restock]||0> > 0 {
      - run dhunger_game_restock def:%arena% id:dhg_restock_%arena%
      }

dhunger_task_prep_player:
  type: task
  debug: false
  script:
    #- ^if <player.is_online> narrate "Remove Inventory"
    - ^define equipment <player.flag[dhg.equipment]||li@>
    - ^yaml id:dhunger_saves set 'dhg.player.<player.uuid>.equipment:<player.equipment>'
    - ^yaml id:dhunger_saves set 'dhg.player.<player.uuid>.inventory:<player.inventory.list_contents>'
    - ^yaml id:dhunger_saves set 'dhg.player.<player.uuid>.gamemode:<player.gamemode>'
    - ^inventory clear d:<player>
    - ^equip <player> 'boots:<def[equipment].get[1]||i@air>' 'legs:<def[equipment].get[2]||i@air>' 'chest:<def[equipment].get[3]||i@air>' 'head:<def[equipment].get[4]||i@air>'
    - inject dhunger_command_save

dhunger_task_cleanup_player:
  type: task
  debug: false
  script:
    #- ^if <player.is_online> narrate "Replace Inventory"
    - ^inventory clear d:<player>
    - ^define equipment <yaml[dhunger_saves].read[dhg.player.<player.uuid>.equipment]||li@>
    - ^equip <player> 'boots:<def[equipment].get[1]||i@air>' 'legs:<def[equipment].get[2]||i@air>' 'chest:<def[equipment].get[3]||i@air>' 'head:<def[equipment].get[4]||i@air>'
    - ^inventory set 'd:<player.inventory>' 'o:<yaml[dhunger_saves].read[dhg.player.<player.uuid>.inventory]||li@>'
    - ^adjust <player> gamemode:<yaml[dhunger_saves].read[dhg.player.<player.uuid>.gamemode]||survival]>
    - ^yaml id:dhunger_saves set 'dhg.player.<player.uuid>:!'
    - ^inject dhunger_command_save
    - ^flag player dhg.play:!
    - ^adjust <player> health:20
    - ^adjust <player> food_level:100
    - ^adjust <player> saturation:100

dhunger_remove_player:
  type: task
  debug: true
  script:
    - ^define arena <player.flag[dhg.play]>
    - ^if <player.is_online> {
      - drop <player.inventory.list_contents>
      - ^teleport <player> <yaml[dhunger_saves].read[dhg.lobby]||<player.location.world.spawn_location>>
      }
    - ^define sound <tern[<yaml[dhunger_saves].contains[dhg.arena.%arena%.settings.sound]>]:<yaml[dhunger_saves].read[dhg.arena.%arena%.settings.sound]>||FIREWORK_LARGE_BLAST>
    - ^if <def[sound].contains[custom]> playsound <server.flag[dhg.%arena%.players].as_list||li@> sound:%sound% volume:10 custom
      else playsound <server.flag[dhg.%arena%.players].as_list||li@> sound:%sound% volume:10
    - ^if <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.lightning]||2> == 2 {
      - run dhunger_game_lightning def:%arena%
      }
    - ^flag server dhg.%arena%.dead:->:<player.name>
    - inject dhunger_board_reset
    - ^if <player.is_online> inject dhunger_task_cleanup_player


dhunger_check_end_of_game:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^if <server.get_online_players_flagged[dhg.play].filter[flag[dhg.play].is[==].to[%arena%]].size.is[==].to[1]> && <def[2]||0> != cancelled {
      - define winner <server.get_online_players_flagged[dhg.play].filter[flag[dhg.play].is[==].to[%arena%]].get[1]>
      - announce "<green><def[winner].name> has won the hunger game in %arena%"
      }
    - ^foreach <server.get_online_players_flagged[dhg.play].filter[flag[dhg.play].is[==].to[%arena%]]> {
      - run dhunger_remove_player player:%value% instantly
      }
    - ^run dhunger_task_block_rollback def:%arena%
    - ^remove <cu@dhg_%arena%.list_entities[dropped_item]>
    - if <queue.exists[dhg_lightning_%arena%]> queue queue:dhg_lightning_%arena% stop
    - if <queue.exists[dhg_restock_%arena%]> queue queue:dhg_restock_%arena% stop
    - ^flag server dhg.%arena%:!
    - inject dhunger_board_reset
    #- announce "Finished end_of_game"

dhunger_stock_chests:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^foreach li@tier1|tier2 {
      - define tier %value%
      - if !<yaml[dhunger_saves].contains[dhg.arenas.%arena%.settings.%tier%]> foreach next
      - if !<yaml[dhunger_saves].contains[dhg.arenas.%arena%.settings.%tier%_chests]> foreach next
      - foreach <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%tier%_chests].as_list> {
        - define chest %value%
        - inventory clear d:%chest%
        - define additems li@
        - foreach <yaml[dhunger_saves].list_keys[dhg.arenas.%arena%.settings.%tier%]> {
          - define itemvals <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.%tier%.%value%].as_list>
          - if <util.random.int[1].to[100].is[OR_LESS].than[<def[itemvals].get[1]||25>]> {
            - define additems <def[additems].insert[%value%[quantity=<util.random.int[1].to[<def[itemvals].get[2]||1>]>]].at[1]>
            }
          }
        - define additems <def[additems].get[1].to[27]||null>
        - if %additems% == null {
          - narrate "error finding items for chests"
          - queue clear
          }
        - foreach %additems% {
          - define item %value%
          - while true {
            - define slot <util.random.int[1].to[27]>
            - if <def[chest].inventory.slot[%slot%]> == i@air while stop
            }
          - inventory set d:%chest% o:%value% slot:%slot%
          }
        }
      }

dhunger_task_block_track:
  type: task
  debug: false
  script:
    - ^define action %1%
    - ^define arena %2%
    - ^define location %3%
    - ^define mat %4%
    - ^yaml id:dhunger_saves set dhg.arenas.%arena%.track.%action%:->:li@%location%|%mat%
    - inject dhunger_command_save
    
dhunger_task_block_rollback:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^if <yaml[dhunger_saves].contains[dhg.arenas.%arena%.track]||false> {
      - if <yaml[dhunger_saves].contains[dhg.arenas.%arena%.track.break]> {
        - foreach <yaml[dhunger_saves].read[dhg.arenas.%arena%.track.break]> {
          - modifyblock <def[value].get[1]> <def[value].get[2]>
          }
        }
      - if <yaml[dhunger_saves].contains[dhg.arenas.%arena%.track.place]> {
        - foreach <yaml[dhunger_saves].read[dhg.arenas.%arena%.track.place]> {
          - modifyblock <def[value].get[1]> m@air
          }
        }
      - yaml id:dhunger_saves set dhg.arenas.%arena%.track:!
      - inject dhunger_command_save
      }

dhunger_task_barricade:
  type: task
  debug: false
  script:
    - ^define barricade li@
    - ^define blocktype <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.barrier]||m@barrier>
    - ^foreach li@0|1|2 {
      - define y %value%
      - foreach li@-1|0|1 {
        - define x %value%
        - foreach li@-1|0|1 {
          - define z %value%
          - if %x% == 0 && %z% == 0 {
            - foreach next
            }
          - define block <def[spawnpt].add[%x%,%y%,%z%]>
          - if ( <def[state].is[==].to[up]> && <def[block].material.is[==].to[m@air]> ) || ( <def[state].is[==].to[down]> && <def[block].material.is[==].to[%blocktype%]> ) {
            - define barricade <def[barricade].insert[%block%].at[1]>
            }
          }
        }
      }
    - ^if <def[barricade].size.is[more].than[0]> modifyblock %barricade% <tern[<def[state].is[==].to[down]>]:m@air||%blocktype%> no_physics
dhunger_game_lightning:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^define timer <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.lightning_delay]||60>
    - ^while <server.has_flag[dhg.%arena%.started]> {
      - wait %timer%
      - foreach <server.get_online_players_flagged[dhg.play].filter[flag[dhg.play].is[==].to[%arena%]]> {
        - strike no_damage <def[value].location>
        }
      }

dhunger_game_restock:
  type: task
  debug: false
  script:
    - ^define arena %1%
    - ^define timer <yaml[dhunger_saves].read[dhg.arenas.%arena%.settings.restock]||0>
    - ^if %timer% == 0 queue stop
    - ^while <server.has_flag[dhg.%arena%.started]> {
      - wait %timer%
      - inject dhunger_stock_chests %arena%
      }
      
dhunger_task_yaml_copy:
  type: task
  debug: false
  script:
    - define sourcekey %1%
    - define destkey %2%
    - if <yaml[dhunger_saves].contains[%1%].not> {
      - narrate "Source invalid for yaml copy"
      - queue stop
      }
    - if <yaml[dhunger_saves].list_keys[%1%].size||0> > 0 {
      - foreach <yaml[dhunger_saves].list_keys[%1%]||li@> {
        - run dhunger_task_yaml_copy def:%1%.%value%|%2%.%value% instantly
        }
      }
      else {
      - if <yaml[dhunger_saves].is_list[%1%]> {
        - foreach <yaml[dhunger_saves].read[%1%]> {
          - yaml id:dhunger_saves set %2%:->:%value%
          }
        }
        else {
        - yaml id:dhunger_saves set %2%:<yaml[dhunger_saves].read[%1%]>
        }
      }

dhunger_task_block_change_check:
  type: task
  debug: false
  script:
    - if !<server.has_flag[dhg.loaded]> queue clear
    - foreach <context.location.cuboids.parse[notable_name].filter[starts_with[dhg_]].parse[after[dhg_]]> {
      - if <yaml[dhunger_saves].read[dhg.arenas.%value%.settings.locked]||false> determine cancelled
      }








View History