Paste #51370: Edit of P#51368 - Edit of P#51363 - Edit of P#51361 - Edit of P#51355 - Edit of P#51353 - Edit of P#

Date: 2019/01/06 16:14:48 UTC-08:00
Type: Denizen Script

View Raw Paste Download This Paste
Copy Link


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


StorekeeperPlayerOwned:
  type: assignment
  debug: true

  default constants:
    transaction_data: true
    transaction_log: true
    server_price_requirment: true
    open_limit: false
    online_only: true
    store greetings: li@Welcome to my store!|Feel free to look around!|Just give me a click if you need something!

  actions:

    # Just some setup of the triggers
    on remove:
    - flag server store:<-:<npc.flag[store]>
    on assignment:
    - trigger name:proximity toggle:true radius:30 cooldown:3s
    - trigger name:click toggle:true
    - trigger name:chat toggle:true
    - trigger name:damage toggle:true
    - flag npc cashregister:5
    on enter proximity:
    - if <npc.has_flag[storeitems]||false>:
      - foreach <npc.flag[store].as_cuboid.list_entities[item_frame]||li@>:
        - if !<def[value].has_framed_item>:
          - adjust %value% framed:<npc.flag[storeitems].get[<npc.flag[storeitemlocations].find[<def[value].location>]>]>



    on exit proximity:
    - define name <player.name>
    - if <npc.has_flag[closed]> && <npc.has_flag[customer]||true> == false:
      - if !'<server.flag[storeclosed].as_list.escaped>' contains '<npc.flag[store].as_cuboid.escaped||false>':
        - flag server storeclosed:->:<npc.flag[store].as_cuboid>
        - announce "store closed"
        - foreach <npc.flag[store].as_cuboid.get_blocks.filter[material.name.contains[door]]||li@>:
          - switch %value% state:off

        - despawn


    - if <npc.has_flag[store]> && <player.location.is_within[<npc.flag[store].as_cuboid>]||false>:
      - flag player storekeepernpc:<npc>
      - if !<npc.flag[customer]||true> contains <player.name> && <player> != <npc.owner>:
        - flag npc customer:->:<player.name>


    - if <npc.has_flag[store]> && !<player.location.is_within[<npc.flag[store].as_cuboid>]> && '<player.inventory.contains[i@shopping_bag]>':
      - inventory add o:in@def[name]_shopping_bag d:<def[keeper].inventory||null>
      - take "i@shopping_bag"
      - narrate "<dark_red>No stealing items from my store!!"
    - else if <npc.flag[customer]||false> contains <player.name> && !<player.location.is_within[<npc.flag[store].as_cuboid>]||false>:
       - flag npc customer:<-:<player.name>

    - if <player> == <npc.owner> && <npc.can_see[<player>]||null> && !<player.flag[greeted]||false>:
      - narrate "<dark_purple>Hello <player.name> just ask for <green>'help' <dark_purple>if you need a command refresher for your shop"
      - flag player greeted duration:5m
    - else if !<player.flag[greeted]||false> && <npc.can_see[<player>]||null>:
      - if !<npc.flag[greeting]||null> == null:
        - narrate "<aqua><npc.flag[greeting].random>"

      - else:
        - narrate "<aqua><el@val[<npc.constant[store greetings]>].as_list.random>"

      - flag player greeted duration:30s

    on damage:
    - define name <player.name>
    - if <player.uuid> == <npc.owner.uuid||npc.owner>:
      - narrate "<dark_purple> The balance is <green><npc.flag[cashregister].as_int||null>"
      - narrate "<dark_purple>would you like to <green>'give' <dark_purple>money to your store or <green>'take' <dark_purple>money from your store?"
    - else if '!<player.inventory.contains[i@shopping_bag]>' && '!<player.inventory.contains[i@selling_bag]||>':
      - note 'in@player bag' as:<player.name>_shopping_bag
      - animate <npc> arm_swing
      - flag player storekeepernpc:<npc>
      - narrate '<aqua>Here, have a complimentary shopping_bag!'
      - give 'i@shopping_bag'
    - else if <player.item_in_hand.simple> == 'i@shopping_bag' && <npc.owner> == server:
      - define total_price 0
      - foreach <in@%name%_shopping_bag.list_contents>:
        - adjust %value% quantity:1 save:item2
        - if <server.flag[<entry[item2].result.escaped>]||null> == null:
          - flag server <entry[item2].result.escaped>:1.00

        - adjust %value% quantity:1 save:item
        - define price <yaml[prices].read[prices.<entry[item].result.escaped>].mul[2].mul[<server.flag[<entry[item].result.escaped>]>].mul[<def[value].qty>]||null>
        - define total_price <el@val[%total_price%].add[%price%]||null>

      - narrate "<aqua>Ready to check out? Just click me with your bag!"
      - narrate "<aqua>your total price is <green>%total_price%"
    - else if <player.item_in_hand.simple> == 'i@shopping_bag':
      - define total_price 0
      - define name <player.name>
      - foreach <in@%name%_shopping_bag.list_contents>:
        - if !<npc.flag[sell_<def[value].escaped>].mul[<%value%.qty>]||null> == null:
          - define price <npc.flag[sell_<def[value].escaped>].mul[<def[value].qty>]||null>

        - else:
          - define price <el@val[<npc.flag[sell_<def[value].escaped>]>].mul[<def[value].qty>]||null>
          - define total_price <el@val[%total_price%].add[%price%]||null>


      - narrate "<aqua>Ready to check out? Just click me with your bag!"
      - narrate "<aqua>your total price is <green>%total_price%"
    - else if <player.item_in_hand.simple> == 'i@selling_bag' && <npc.owner> == server:
      - define total_price 0
      - foreach <in@%name%_selling_bag.list_contents.exclude[i@air]>:
        - adjust %value% quantity:1 save:item
        - define price <yaml[prices].read[prices.<entry[item].result.escaped>].mul[<def[value].qty>]>
        - define total_price <el@val[%total_price%].add[%price%]>

      - narrate "<aqua>Ready to sell items? Just right click me with your bag!"
      - narrate "<aqua>your total price is <green>%total_price%"
    - else if <player.item_in_hand.simple> == 'i@selling_bag':
      - define total_price 0
      - foreach <in@%name%_selling_bag.list_contents.exclude[i@air]>:
        - define newvalue <def[value].escaped>
        - define price <npc.flag[buy_%newvalue%]||null>
        - define total_price <el@val[%total_price%].add[%price%]||null>

      - narrate "<aqua>Ready to sell items? Just right click me with your bag!"
      - narrate "<aqua>your total price is <green>%total_price%"

    - else:
      - flag player storekeepernpc:n@<npc>


    on chat:
    - if <context.message> == greeting && <player> == <npc.owner>:
      - narrate "<dark_purple>Would you like to <green>add <dark_purple>or <green>remove<dark_purple> a greeting?"
      - flag player greeting d:15s
      - narrate "<dark_purple>Your current greetings are<&co>"
      - foreach <npc.flag[greeting].as_list||li@>:
          - narrate "%loop_index%=%value%"

      - determine cancelled

    - if <player.has_flag[greeting]> == true && <player> == <npc.owner>:
      - if <context.message> == add:
        - if <npc.flag[greeting].as_list.size||0> > 4:
          - narrate "You cannot exceed five greetings!"
          - flag player greeting:!
          - determine cancelled
          - queue clear

        - else:
          - narrate "<dark_purple>Please say the greeting you would like to add to your store"
          - flag player greeting:!
          - flag player addgreeting
          - determine cancelled


      - else if <context.message> == remove && <npc.owner> == <player>:
        - flag player greeting:!
        - flag player removegreeting
        - narrate "What line number would you like to remove?"
        - foreach <npc.flag[greeting].as_list||li@>:
          - narrate "%loop_index%=%value%"

        - determine cancelled


    - if <player.has_flag[removegreeting]||false> == true && <player> == <npc.owner>:
      - narrate "<npc.flag[greeting].get[<context.message>]>"
      - narrate "Has been removed"
      - flag npc greeting[<context.message>]:<-
      - flag player removegreeting:!
      - determine cancelled

    - if <player.has_flag[addgreeting]||false> == true && <player> == <npc.owner>:
      - flag npc greeting:->:<context.message>
      - flag player addgreeting:!
      - narrate "<dark_purple>You added a new greeting to your list. Your new greeting is"
      - narrate "<green><context.message>"
      - determine cancelled

    - if <context.message> == expand && <player> == <npc.owner>:
      - execute as_op "npc select"
      - flag server store:<-:<npc.flag[store]>
      - give "storelocationstick"
      - narrate "<dark_purple>please reselect the bottom and top corners of your cuboid then drop the selector"
      - determine cancelled

    - if <context.message> == help && <player> == <npc.owner>:
      - narrate "<dark_purple>your shop commands are <green> Greeting, items, give, take, set, unset, expand, open, close."
      - narrate "<dark_purple>Click the npc with an item to set a price for it"
      - narrate "<dark_purple>Right click the npc without an item to view inventory for the store"
      - determine cancelled

    - if <context.message> == items && <player> == <npc.owner>:
      - if <npc.flag[buyback]||false> == false:
        - narrate "<aqua>No items have been sold to your shop"

      - else:
        - give <npc.flag[buyback]>
        - flag npc buyback:!
        - determine cancelled


    - if <player.has_flag[givingtocashregister]||null> == true && <player> == <npc.owner>:
      - flag npc cashregister:+:<context.message>
      - take money qty:<context.message>
      - narrate "<dark_purple>You give <green><context.message> <dark_purple>to the cash register. The balance is now <green><npc.flag[cashregister].as_int||null>"
      - flag player givingtocashregister:!
      - determine cancelled

    - if <context.message> == give && <player> == <npc.owner>:
      - flag player givingtocashregister
      - narrate "<dark_purple>How much would you like to give to the store?"
      - determine cancelled

    - if <context.message> == take && <player> == <npc.owner>:
      - flag player takingfromcashregister
      - narrate "<dark_purple>How much would you like to take from the store?"
      - determine cancelled

    - if <player.has_flag[takingfromcashregister]||null> == true && <player> == <npc.owner>:
      - if <npc.flag[cashregister]> > <context.message>:
        - flag npc cashregister:-:<context.message>
        - give money qty:<context.message>
        - narrate "<dark_purple>You take <green><context.message> <dark_purple>from the cash register. The balance is now <green><npc.flag[cashregister].as_int||null>"
        - flag player takingfromcashregister:!
        - determine cancelled

      - else if <npc.flag[cashregister]||false> < <context.message>:
        - narrate "<dark_red>You havnt got that much money in your cashregister!"


    - if <context.message> == unset && <player> == <npc.owner>:
      - flag global store:<-:<npc.flag[store]>
      - narrate "<dark_red>your store is now unprotected! tell the npc to 'set' the store back up when you are done editing"
      - determine cancelled

    - if <context.message> == sell:
      - if <npc.owner> == server:
        - note 'in@selling bag' as:<player.name>_selling_bag
        - animate <npc> arm_swing
        - narrate '<aqua>Here is your item selling bag!'

      - else:
        - note 'in@player bag' as:<player.name>_selling_bag
        - animate <npc> arm_swing
        - narrate '<aqua>Here is your item selling bag!'
        - narrate "<aqua>We buy the following items that you have"
        - foreach <player.inventory.list_contents.simple>:
          - if !<npc.flag[buy_<%value%.escaped>]||null> == null:
            - if <%value%.display||null> == null:
              - narrate "<aqua>We buy <green><%value%.material.name||null> <aqua>for <green><npc.flag[buy_<%value%.escaped||null>]||null> coins a piece"

            - else:
              - narrate "<aqua>We buy <green><%value%.display||null> <aqua>for <green><npc.flag[buy_<%value%.escaped||null>]||null> coins a piece"




      - give i@selling_bag
      - determine cancelled

    - if <context.message> == set && <player> == <npc.owner> && <server.flag[store].as_list.escaped> !contains <npc.flag[store].escaped>:
      - determine passively cancelled
      - narrate "<&b>your store is now open for business!"
      - flag npc stock:!
      - flag server store:->:<npc.flag[store]||null>
      - flag npc storeitems:!
      - foreach <npc.flag[store].as_cuboid.list_entities||<player>>:
        - if <def[value].entity_type> == item_frame:
          - flag npc storeitems:->:<def[value].framed_item||null>
          - flag npc storeitemlocations:->:<def[value].location||null>


      - wait 1t
      - foreach <npc.flag[store].as_cuboid.get_blocks[m@chest,5|m@chest,4|m@chest,3|m@chest,2|m@trapped_chest,5|m@trapped_chest,4|m@trapped_chest,3|m@trapped_chest,2]||li@>:
        - flag npc stock:->:<def[value].inventory||null>


    - if <context.message> == buying && <player> == <npc.owner>:
      - if <npc.constant[server_price_requirment]> == true:
        - narrate "<dark_purple>please set a price below <green><yaml[prices].read[prices.<npc.flag[currentitem]||null>]||null> <dark_purple>for that item."
        - flag player settype:buy
        - determine cancelled

      - else:
        - flag player settype:buy
        - narrate "<dark_purple>please set a price for that item"
        - determine cancelled


    - if <context.message> == selling && <player> == <npc.owner>:
      - if <npc.constant[server_price_requirment]||false> == true:
        - narrate "<dark_purple>please set a price above <green><yaml[prices].read[prices.<npc.flag[currentitem]||null>]||null> <dark_purple>for that item."
        - flag player settype:sell
        - determine cancelled

      - else:
        - flag player settype:sell
        - narrate "<dark_purple>please set a price for that item"
        - determine cancelled


    - if <npc.has_flag[currentitem]> == true && <player> == <npc.owner>:
      - if <player.flag[settype]||null> == buy && <npc.constant[server_price_requirment]||false> == true:
        - if <context.message> > <yaml[prices].read[prices.<npc.flag[currentitem]||null>]||null> && <context.message.is[matches].to[integer]||false>:
          - narrate "<dark_red>your price needs to be below the base price. this price will not be set for this item."
          - flag npc currentitem:!
          - flag player settype:!
          - determine cancelled

        - else if <context.message> < <yaml[prices].read[prices.<npc.flag[currentitem]||false>]||null> && <context.message.is[matches].to[integer]||false>:
          - flag npc <player.flag[settype]>_<npc.flag[currentitem]>:<context.message>
          - if <npc.flag[currentitem].unescaped.display||null> == null:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.as_item.material.name||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - else:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.display||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - flag npc currentitem:!
          - flag player settype:!
          - determine cancelled

        - else:
          - narrate "<dark_red>That doesnt not match a valid number! please set a proper number for a price"
          - determine cancelled



    - if <npc.has_flag[currentitem]> == true && <player> == <npc.owner>:
      - if <player.flag[settype]||false> == sell && <npc.constant[server_price_requirment]> == true:
        - if <context.message> < <yaml[prices].read[prices.<npc.flag[currentitem]||null>]||null> && <context.message.is[matches].to[integer]||false>:
          - narrate "<dark_red>your price needs to be above the base price. this price will not be set for this item."
          - flag npc currentitem:!
          - flag player settype:!
          - determine cancelled

        - else if <context.message> > <yaml[prices].read[prices.<npc.flag[currentitem]>]> && <context.message.is[matches].to[integer]>:
          - flag npc <player.flag[settype]||null>_<npc.flag[currentitem]||null>:<context.message>
          - if <npc.has_flag[currentitem].unescaped.display||null> == null:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.as_item.material.name||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - else:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.display||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - flag npc currentitem:!
          - flag player settype:!
          - determine cancelled

        - else:
          - narrate "<dark_red>That doesn not match a valid number! please set a proper number for a price"
          - determine cancelled



    - if !<npc.has_flag[currentitem]> && <player> == <npc.owner>:
      - if !<npc.constant[server_price_requirment]||false> == true:
        - if <context.message.is[matches].to[integer]||false>:
          - flag npc <player.flag[settype]||null>_<npc.flag[currentitem]||null>:<context.message>
          - if <npc.flag[currentitem].unescaped.display||null> == null:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.material.name||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - else:
            - narrate "<dark_purple><npc.flag[currentitem].unescaped.display||null> <player.flag[settype]||null> price is now set to <green><context.message>"

          - flag npc currentitem:!
          - flag player settype:!
          - determine cancelled

        - else:
          - narrate "<dark_red>That is not a valid number. please select a valid number for the price"
          - determine cancelled


    - if <context.message> == open && <player> == <npc.owner>:
      - flag player openhour
      - narrate "<dark_purple>Please select a opening hour between <green>0 <dark_purple>and <green>23 <dark_purple>If you no longer want an opening hour then just say none."
      - determine cancelled

    - if <context.message> == close && <player> == <npc.owner> && <npc.constant[open_limit]> == false:
      - flag player closehour
      - narrate "<dark_purple>Please select a closing hour between <green>0 <dark_purple>and <green>23"
      - determine cancelled
    - else if <npc.constant[open_limit]> != false:
        - narrate "<red> This server has an open limit and doesn not allow for a store closing change. Please make your opening hour and then the store will close automaticly when the time limit is reached."
    - if <player.has_flag[openhour]>:
      - flag npc open:<c.message>
      - flag player openhour:!
      - narrate "<green><c.message> <dark_purple>Is now your opening hour!"
      - determine cancelled

    - if <player.has_flag[closehour]>:
      - flag npc close:<c.message>
      - flag player closehour:!
      - narrate "<green><c.message> <dark_purple>Is now your closing hour!"
      - determine cancelled

    on click:
    - define name <player.name>
    - if <player> == <npc.owner> && <player.item_in_hand> != i@air:
      - adjust <player.item_in_hand> quantity:1 save:item8
      - flag npc currentitem:<entry[item8].result.escaped>
      - narrate "<dark_purple>will you be <green>'selling' <dark_purple>or <green>'buying' <dark_purple>for this item?"
      - queue clear
      - determine cancelled

    - if <player.item_in_hand.simple> == "i@shopping_bag":
      - if <in@%name%_shopping_bag.qty> == 0:
        - narrate "<aqua>your shopping_bag is empty!"
      - else if <npc.owner> == server:
        - flag player delay duration:3s
        - chat "ok let's total these items up for you then!"
        - define total_price 0
        - foreach <in@%name%_shopping_bag.list_contents>:
          - adjust %value% quantity:1 save:item2
          - if <server.flag[<entry[item2].result.escaped>]||null> == null:
            - flag server <entry[item2].result.escaped>:1.00

          - adjust %value% quantity:1 save:item
          - define price <yaml[prices].read[prices.<entry[item].result.escaped||null>].mul[2].mul[<server.flag[<entry[item].result.escaped||null>]||null>].mul[<def[value].qty||null>]>
          - define total_price <el@val[%total_price%].add[%price%]||null>
          - if <npc.constant[transaction_data]||false> == true:
            - yaml id:transaction_data set transaction.<player.name>.bought.from_<npc.owner>.<def[value].scriptname||<def[value].material.name>>:+:<def[value].qty>
            - yaml savefile:transaction_data.yml id:transaction_data

          - if <npc.constant[transaction_log]||false> == true:
            - if <server.flag[loglimiter]> == 1000:
              - flag server logname:+:1
              - flag server loglimiter:!

            - log "<player.name> bought <def[value].qty> <def[value].scriptname||<def[value].material.name>> from_<npc.owner> for %price%" file:transaction_log<server.flag[logname]||<&sp>>


        - if <player.money> < %total_price%:
          - narrate "<dark_red>YOU NEED MONEYS D="
          - queue clear

        - flag player bagnumber:+:1
        - note "in@player bag" "as:<player.name> <player.flag[bagnumber].as_int>_item_bag"
        - take money qty:%total_price%
        - if <npc.constant[transaction_data]||false> == true:
            - yaml id:transaction_data set transaction.<player.name>.money.spent:+:<def[total_price].as_int>
            - yaml savefile:transaction_data.yml id:transaction_data

        - run "itemgiverPlayer Owned" delay:1s
        - take 'i@shopping_bag'
        - narrate "<aqua>The shopkeeper takes <green>%total_price% coins"
        - narrate "<gold>You are all checked out. have a great day!"
        - narrate "<red>Do not open your item bag until you are ready to take all the items out"
        - foreach <in@%name%_shopping_bag.list_contents>:
          - adjust %value% quantity:1 save:item9
          - flag server <entry[item9].result.escaped>adjust:+:<def[value].qty>
          - run script:price_adjuster def:<entry[item9].result>


      - else:
        - flag player delay duration:3s
        - chat "ok let's total these items up for you then!"
        - define total_price 0
        - foreach <in@%name%_shopping_bag.list_contents>:
          - adjust %value% quantity:1 save:item2
          - if <server.flag[<<entry[item2].result>.escaped>]> == null:
            - flag server <<entry[item2].result>.escaped>:1.00

          - define price <el@val[<npc.flag[sell_<entry[item2].result.escaped>]>].mul[<def[value].qty>]>
          - define total_price <el@val[%total_price%].add[%price%]>
          - if <npc.constant[transaction_data]||false>:
            - yaml id:transaction_data set transaction.<player.name>.bought.from_<npc.owner>.<def[value].scriptname||<def[value].material.name>>:+:<def[value].qty>
            - yaml savefile:transaction_data.yml id:transaction_data

          - if <npc.constant[transaction_log]||false> == true:
            - if <server.flag[loglimiter]> == 1000:
              - flag server logname:+:1
              - flag server loglimiter:!

            - log "<player.name> bought <def[value].qty> <def[value].scriptname||<def[value].material.name>> from_<npc.owner> for %price%" file:transaction_log<server.flag[logname]||<&sp>>


        - if <player.money> < %total_price%:
          - narrate "<dark_red>YOU NEED MONEYS D="
          - queue clear

        - if <npc.inventory.list_contents.simple> contains <in@<player.name>_shopping_bag.list_contents.simple>:
          - narrate "<aqua>I am sorry but we just ran out of some of those items!"
          - queue clear

        - flag player bagnumber:+:1
        - note "in@player bag" "as:<player.name> <player.flag[bagnumber].as_int>_item_bag"
        - take money qty:%total_price%
        - flag npc cashregister:+:%total_price%
        - if <npc.constant[transaction_data]||false> == true:
            - yaml id:transaction_data set transaction.<player.name>.money.spent:+:<def[total_price].as_int>
            - yaml savefile:transaction_data.yml id:transaction_data

        - run "itemgiverPlayer Owned" delay:1s
        - take 'i@shopping_bag'
        - narrate "<aqua>The shopkeeper takes <green>%total_price% coins"
        - narrate "<gold>You are all checked out. have a great day!"
        - narrate "<red>Do not open your item bag until you are ready to take all the items out"
        - foreach <in@%name%_shopping_bag.list_contents.escaped>:
          - adjust %value% quantity:1 save:item9
          - flag server <entry[item9].result>adjust:+:<def[value].qty>
          - run script:price_adjuster def:<entry[item9].result>


      - else if <player.item_in_hand.simple> == "i@selling_bag":
      - flag player delay duration:3s
      - if <in@%name%_selling_bag.qty> == 0:
        - narrate "<dark_red>your selling bag is empty!"

      - else if <in@%name%_selling_bag.contains['i@shopping_bag'in@%name%_selling_bag.contains['i@shopping_bag']>:
        - chat "i am sorry but i do not buy back my shopping bags."

      - else if <npc.owner> == server:
        - chat "ok let's total these items up for you then!"
        - define total_price 0
        - foreach <in@%name%_selling_bag.list_contents.exclude[i@air]>:
          - adjust %value% quantity:1 save:item
          - define price <yaml[prices].read[prices.<entry[item].result.escaped>].mul[<def[value].qty>]||null>
          - define total_price <el@val[%total_price%].add[%price%]||null>
          - if <npc.constant[transaction_data]||false>:
            - yaml id:transaction_data set transaction.<player.name>.sold.to_<npc.owner>.<def[value].scriptname||<def[value].material.name>>:+:<def[value].qty>
            - yaml savefile:transaction_data.yml id:transaction_data

          - if <npc.constant[transaction_log]||false> == true:
            - if <server.flag[loglimiter]> == 1000:
              - flag server logname:+:1
              - flag server loglimiter:!

            - log "<player.name> sold <def[value].qty> <def[value].scriptname||def[value].material.name> from_<npc.owner> for %price%" file:transaction_log<server.flag[logname]||<&sp>>


        - note as:<player.name>_selling_bag remove
        - give money qty:%total_price%
        - if <npc.constant[transaction_data]||false> == true:
            - yaml id:transaction_data set transaction.<player.name>.money.recieved:+:<def[total_price].as_int>
            - yaml savefile:transaction_data.yml id:transaction_data

        - take 'i@selling_bag'
        - narrate "<aqua>The shopkeeper gives you <green>%total_price% coins"
        - foreach <in@%name%_shopping_bag.list_contents.escaped>:
          - adjust %value% quantity:1 save:item9
          - flag server <entry[item9].result>adjust:-:<def[value].qty>
          - run script:price_adjuster def:<entry[item9].result>

        - queue clear

      - else:
        - chat "ok let's total these items up for you then!"
        - define total_price 0
        - foreach <in@%name%_selling_bag.list_contents.exclude[i@air]||li@>:
          - adjust %value% quantity:1 save:item
          - define price <el@val[<npc.flag[buy_<def[value].escaped>]>].mul[<def[value].qty>]>
          - define total_price <el@val[%total_price%].add[%price%]||null>
          - if <npc.constant[transaction_data]||false>:
            - yaml id:transaction_data set transaction.<player.name>.sold.to_<npc.owner>.<def[value].scriptname||<def[value].material.name>>:+:<def[value].qty>
            - yaml savefile:transaction_data.yml id:transaction_data

          - if <npc.constant[transaction_log]||false> == true:
            - if <server.flag[loglimiter]> == 1000:
              - flag server logname:+:1
              - flag server loglimiter:!

            - flag server loglimiter:+:1
            - log "<player.name> sold <def[value].qty> <def[value].scriptname||<def[value].material.name>> from_<npc.owner> for %price%" file:transaction_log<server.flag[logname]||<&sp>>


        - if %total_price% < <npc.flag[cashregister].as_int||null>:
          - flag npc buyback:->:<in@%name%_selling_bag.list_contents>
          - note 'in@player bag' as:<player.name>_selling_bag
          - give money qty:%total_price%
          - flag npc cashregister:-:%total_price%
          - if <npc.constant[transaction_data]||false> == true:
            - yaml id:transaction_data set transaction.<player.name>.money.recieved:+:<def[total_price].as_int>
            - yaml savefile:transaction_data.yml id:transaction_data

          - take 'i@selling_bag'
          - narrate "<aqua>The shopkeeper gives you <green>%total_price% coins"
          - foreach <in@%name%_shopping_bag.list_contents.escaped>:
            - adjust %value% quantity:1 save:item9
            - flag server <entry[item9].result>adjust:-:<def[value].qty>
            - run script:price_adjuster def:<entry[item9].result>

          - queue clear

        - else:
          - narrate "<aqua>I am sorry I don't have the funds to buy all of that back!"



    - else if <player.inventory> contains 'i@shopping_bag' || <player.inventory> contains 'i@selling_bag':
      - queue clear

    - else if <player.uuid> == <npc.owner.uuid>:
      - narrate "<green>Hey boss!"
      - queue clear

    - else:
      - chat "Please visit http://youtu.be/2f3Dk4cPCXs for a short tutorial of how to use our stores."



itemgiverPlayer Owned:
  type: task
  debug: false
  script:
  - define name <player.name>
  - inventory add o:in@%name%_shopping_bag 'd:in@<player.name> <player.flag[bagnumber].as_int>_item_bag'
  - ^adjust 'i@item_bag' "lore:<player.name> <player.flag[bagnumber].as_int>" save:playersbag
  - give <entry[playersbag].result>
Item Frame Shop Player Owned:
  type: world
  debug: true
  constants:
    permission_for_prices: true
  events:
    on player right clicks with polar_bear_spawn_egg:
      - determine cancelled
    on server start:
    - yaml load:prices.yml id:prices
    - if !<server.has_file[transaction_data]||false>:
      - yaml create id:transaction_data
      - wait 1t
      - yaml id:transaction_data set transaction:data
      - yaml savefile:transaction_data.yml id:transaction_data

    - else:
      - yaml load:transaction_data.yml id:transaction_data

    on time changes:
    - foreach <server.get_npcs_flagged[store]||li@>:
      - define shopkeeper %value%
      - if <def[shopkeeper].location.world> == <c.world>:
        - if !<def[shopkeeper].constant[open_limit]> == false:
          - flag <def[shopkeeper]> openhours:+:1
          - if <def[shopkeeper].flag[openhours]> == <def[shopkeeper].constant[open_limit]>:
            - flag <def[shopkeeper]> openhours:!
            - if <def[shopkeeper].flag[customer]||false> == false:
              - if !'<server.flag[storeclosed].as_list.escaped>' contains '<def[shopkeeper].flag[store].as_cuboid.escaped>':
                - announce "shop closed"
                - despawn <def[shopkeeper]>
                - foreach <def[shopkeeper].flag[store].as_cuboid.get_blocks.filter[material.name.contains[door]]||li@>:
                  - switch %value% state:off

                - flag server storeclosed:->:<def[shopkeeper].flag[store].as_cuboid>


            - else:
              - flag <def[shopkeeper]> closed



        - else if <def[shopkeeper].flag[close]||false> == <c.time> && <def[shopkeeper].flag[customer]||false> == false:
          - despawn <def[shopkeeper]>
          - if !'<server.flag[storeclosed].as_list.escaped>' contains '<def[shopkeeper].flag[store].as_cuboid.escaped>':
            - announce "shop closed"
            - foreach <def[shopkeeper].flag[store].as_cuboid.get_blocks.filter[material.name.contains[door]]||li@>:
              - switch %value% state:off

            - flag server storeclosed:->:<def[shopkeeper].flag[store].as_cuboid>


        - else if <def[shopkeeper].flag[close]||false> == <c.time>:
          - flag <def[shopkeeper]> closed

        - if <def[shopkeeper].constant[online_only]> == true:
          - if <def[shopkeeper].owner.as_player.is_online||false> && <def[shopkeeper].flag[open]||false> == <c.time>:
            - spawn <def[shopkeeper]> <def[shopkeeper].location>
            - if '<server.flag[storeclosed].as_list.escaped>' contains '<def[shopkeeper].flag[store].as_cuboid.escaped>':
              - announce "shop opened"
              - flag server storeclosed:<-:<def[shopkeeper].flag[store].as_cuboid>
              - flag <def[shopkeeper]> closed:!



        - else if <def[shopkeeper].flag[open]||false> == <c.time>:
          - spawn <def[shopkeeper]> <def[shopkeeper].location>
          - if '<server.flag[storeclosed].as_list.escaped>' contains '<def[shopkeeper].flag[store].as_cuboid.escaped>':
            - announce "shop opened"
            - flag server storeclosed:<-:<def[shopkeeper].flag[store].as_cuboid>
            - flag <def[shopkeeper]> closed:!




    on player clicks wooden_door:
    - foreach <server.get_npcs_flagged[store]||li@>:
      - if <def[value].owner> == <player>:
        - if <c.location.is_within[<def[value].flag[store].as_cuboid]>]||false>:
          - queue clear


      - else:
        - foreach <global.flag[storeclosed].escape_contents||li@>:
          - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
            - narrate "<red>This shop is closed and the door is locked!!!"
            - determine cancelled




    on price command:
    - if <player.is_op>:
      - adjust <player.item_in_hand> quantity:1 save:item3
      - if !<yaml[prices].contains[prices.<entry[item3].result.escaped>]||false>:
        - flag player setpriceserver:<entry[item3].result.escaped>
        - narrate "<dark_purple>what is the base price you want for that item?"
        - determine cancelled

      - else:
        - narrate "<dark_purple>That item already has a current price of <green><yaml[prices].read[prices.<entry[item3].result.escaped>]||null>"
        - narrate "<dark_purple>To adjust that items price use the /adjust_price command"
        - determine cancelled


    on player right clicks chest:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.flag[storekeepernpc].as_npc.owner.uuid> != <player.uuid>:
          - narrate "<red>This chest is locked"
          - determine cancelled



    on player right clicks trapped_chest:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.flag[storekeepernpc].as_npc.owner> != <player.uuid>:
          - narrate "<red>This chest is locked"
          - determine cancelled



    on adjust_price command:
    - adjust <player.item_in_hand> quantity:1 save:item3
    - yaml id:prices set prices.<entry[item3].result.escaped>:!
    - flag player setpriceserver:<entry[item3].result.escaped>
    - narrate "<dark_purple>what is the base price you want for that item?"
    - determine fulfilled
    on player chats:
    - if <player.is_op> && <player.has_flag[setpriceserver]||null>:
      - if <context.message.is[matches].to[integer]||false>:
        - yaml id:prices set prices.<player.flag[setpriceserver]>:<context.message>
        - yaml savefile:prices.yml id:prices
        - narrate "<dark_purple><player.flag[setpriceserver].unescaped.display||<player.flag[setpriceserver].unescaped.scriptname||<player.flag[setpriceserver].unescaped.as_item.material.name>>> price is now set to <green><yaml[prices].read[prices.<player.flag[setpriceserver]>]||null>"
        - flag setpriceserver:!
        - determine cancelled

      - else:
        - narrate "<dark_red>That is not a valid number"
        - narrate "<dark_purple>what is the base price you want for that item?"
        - determine cancelled


    on stop command:
    - foreach <server.flag[storenpcs].as_list||li@>:
      - flag %value% storeinventory:<def[value].inventory.list_contents>

    on player teleports:
    - if '!<player.inventory.contains[i@shopping_bag]||null>':
      - queue clear

    - else if '<player.inventory.contains[i@shopping_bag]>':
      - define name <player.name>
      - if !<player.flag[storekeepernpc].as_npc.owner> == server:
        - foreach <in@%name%_shopping_bag.list_contents>:
          - define item %value%
          - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
            - if <def[value].list_contents.simple> contains <def[item].simple> && <def[value].stacks[%item%]> < 64 && !<def[value].stacks> == <def[value].size>:
              - give %item% qty:<def[item].qty> to:%value%
              - foreach stop

            - else if %loop_index% == <player.flag[storekeepernpc].as_npc.flag[stock].as_list.size>:
              - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
                - if !<def[value].stacks> == <def[value].size>:
                  - give %item% qty:<def[item].qty> to:%value%






      - narrate "<dark_red>No stealing items from my store!!"

    on player quits:
    - if '<player.inventory.contains[i@shopping_bag]>':
      - define name <player.name>
      - if !<player.flag[storekeepernpc].as_npc.owner> == server:
        - foreach <in@%name%_shopping_bag.list_contents>:
          - define item %value%
          - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
            - if <def[value].list_contents.simple> contains <def[item].simple> && <def[value].stacks[%item%]> < 64 && !<def[value].stacks> == <def[value].size>:
              - give %item% qty:<def[item].qty> to:%value%
              - foreach stop

            - else if %loop_index% == <player.flag[storekeepernpc].as_npc.flag[stock].as_list.size>:
              - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list>:
                - if !<def[value].stacks> == <def[value].size>:
                  - give %item% qty:<def[item].qty> to:%value%







    - foreach <server.get_npcs_flagged[store]||li@>:
      - if <def[value].constant[online_only]> == true:
        - if <def[value].owner> == <player>:
          - if <def[value].flag[customer]||false> == false:
            - if !'<server.flag[storeclosed].as_list.escaped>' contains '<def[value].flag[store].as_cuboid.escaped>':
              - announce "shop closed"
              - despawn <def[value]>
              - flag server storeclosed:->:<def[value].flag[store].as_cuboid>



        - else:
          - flag <def[value]> closed



    on store_setup command:
    - give i@storekeeperegg
    - determine fulfilled
    on player clicks with storekeeperegg:
    - if <global.flag[store].escape_contents||true>:
      - create player storekeeper <player.location.cursor_on.add[0.5,1,0.5]> "save:mystorekeeper"
      - wait 2t
      - flag player storenpc:<entry[mystorekeeper].created_npc>
      - narrate "<dark_purple>please select the bottom and top corners of your cuboid then drop the selector"
      - take i@storekeeperegg
      - wait 1t
      - lookclose <entry[mystorekeeper].created_npc> state:true
      - assignment set npc:<entry[mystorekeeper].created_npc.id> script:StorekeeperPlayerOwned
      - give i@storelocationstick
      - queue clear

    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid||false>]>:
        - narrate "you cannot place your store inside of another store"
        - determine cancelled


    - foreach <global.flag[store].escape_contents||li@>:
      - if !<context.location.is_within[<def[value].unescaped.as_cuboid||false>]>:
        - create player storekeeper <player.location.cursor_on.add[0.5,1,0.5]> "save:mystorekeeper"
        - execute as_op "npc select"
        - execute as_op "npc owner <player>" <player.selected_npc>
        - narrate "<dark_purple>please select the bottom and top corners of your cuboid then drop the selector"
        - take i@storekeeperegg
        - flag player storenpc:<entry[mystorekeeper].created_npc>
        - lookclose <entry[mystorekeeper].created_npc> state:true
        - wait 1t
        - assignment set npc:<entry[mystorekeeper].created_npc> script:StorekeeperPlayerOwned
        - give i@storelocationstick
        - queue clear


    on item_frame damaged by entity_explosion:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - determine cancelled


    on item_frame damaged by block_explosion:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - determine cancelled


    on player right clicks with storelocationstick:
    - if <global.flag[store].escape_contents||null> == null:
      - flag player location1:<context.location>
      - narrate "<dark_purple>Cuboid location 1 selected"
      - determine passively cancelled
      - wait 1t
      - showfake m@red_wool <c.location> to:<player> d:10s

    - else:
      - foreach <global.flag[store].escape_contents||li@>:
        - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
          - narrate "<red>You cannot have your store in another store location please select a different location"
          - determine cancelled

        - else:
          - flag player location1:<context.location>
          - narrate "<dark_purple>Cuboid location 1 selected"
          - determine passively cancelled
          - wait 1t
          - showfake m@red_wool <c.location> to:<player> d:10s



    on player left clicks with storelocationstick:
    - if <global.flag[store].escape_contents||null> == null:
      - flag player location2:<context.location>
      - narrate "<dark_purple>Cuboid location 2 selected"
      - determine passively cancelled
      - wait 1t
      - showfake m@red_wool <c.location> to:<player> d:10s

    - else:
      - foreach <global.flag[store].escape_contents||li@>:
        - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
          - narrate "<red>You cannot have your store in another store location please select a different location"
          - determine cancelled

        - else:
          - flag player location2:<context.location>
          - narrate "<dark_purple>Cuboid location 2 selected"
          - determine passively cancelled
          - wait 1t
          - showfake m@red_wool <c.location> to:<player> d:10s



    on player drops storelocationstick:
    - execute as_op "npc select" "<player.name> --id <player.flag[storenpc].id>"
    - execute as_op "npc owner <player.name> --id <player.selected_npc.id>"
    - flag <player.flag[storenpc]> store:cu@<player.flag[location1]>|<player.flag[location2]>
    - flag player location1:!
    - flag player location2:!
    - flag server storenpcs:->:<player.flag[storenpc]>
    - narrate "<dark_purple>tell the npc <green>'SET' <dark_purple>to set the items in the frame"
    - determine passively cancelled
    - wait 1t
    - take storelocationstick
    on player right clicks item_frame:
    - define name <player.name>
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_op> && <player.is_sneaking>:
          - queue clear

        - determine passively cancelled
        - if <player.item_in_hand.simple||false> == 'i@shopping_bag':
          - if <in@%name%_shopping_bag.stacks> = 18:
            - narrate "<aqua>Your shopping bag is full!"
          - else if <player.flag[storekeepernpc].as_npc.owner> == server && <yaml[prices].read[prices.<context.entity.framed_item.escaped>]||null> == null:
            - foreach <server.list_online_players>:
              - if !'<def[value].has_permission[<script.constant[permission_for_prices]>]||null>' == null:
                - narrate "<red>Warning<&co><dark_purple><c.entity.framed_item> has no price! please set a price for this item by holding it in your hand ant typing /price" target:%value%

              - else if !<server.flag[pricemessage].contains[<c.entity.framed_item>]>:
                - flag server pricemessage:->:<c.entity.framed_item>


            - narrate "<dark_red>This item has no price. please contact the servers administrators to let them know"
            - determine cancelled

          - else if <player.flag[storekeepernpc].as_npc.owner> == server:
            - inventory add o:<c.entity.framed_item> d:in@<player.name>_shopping_bag
            - if <c.entity.framed_item.display||null> != null:
              - narrate "<aqua>You add a <green><c.entity.framed_item.display> <aqua>to your shopping bag."

            - else:
              - narrate "<aqua>You add <green><c.entity.framed_item.material.name.replace[_].with[ ]> <aqua>to your shopping bag."
              - playsound <player> sound:DIG_GRASS


          - else if <player.flag[storekeepernpc].as_npc.flag[sell_<context.entity.framed_item.escaped>]||null> == null:
            - if <player.flag[storekeepernpc].as_npc.owner.as_player.is_online> == true:
              - narrate "<red>Warning<&co><dark_purple><context.entity.framed_item.display.name||<context.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>> does not have a price set for it! Please set a price in your shop for this item" targets:<player.flag[storekeepernpc].as_npc.owner.as_player>

            - else:
              - if !<player.flag[storekeepernpc].as_npc.owner.as_player.flag[storepricemessage].as_list.contains[<context.entity.framed_item>]||false>:
                - flag <player.flag[storekeepernpc].as_npc.owner.as_player> "storepricemessage:->:<context.entity.framed_item.display||<c.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>>"


            - narrate "<dark_red>This item has no price and cannot be sold till a price is set for it."
            - determine cancelled

          - else:
            - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
              - if <def[value].contains[<context.entity.framed_item>]>:
                - inventory add o:<c.entity.framed_item> d:in@<player.name>_shopping_bag
                - take <c.entity.framed_item> from:<def[value]>
                - narrate "<aqua>You add a <green><context.entity.framed_item.display.name||<context.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>> <aqua>to your shopping bag."
                - playsound <player> sound:DIG_GRASS
                - define true true
                - foreach stop

              - if %loop_index% == <player.flag[storekeepernpc].as_npc.flag[stock].as_list.size> && <def[true]||false> == false:
                - if <player.flag[storekeepernpc].as_npc.owner.as_player.is_online> == true:
                  - narrate "<red>Warning<&co><dark_purple><context.entity.framed_item.display.name||<context.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>> is out of stock! Please restock this item." targets:<player.flag[storekeepernpc].as_npc.owner.as_player>

                - else:
                  - if !<player.flag[storekeepernpc].as_npc.owner.as_player.flag[storestockmessage].as_list||false> contains <c.entity.framed_item||false>:
                    - flag <player.flag[storekeepernpc].as_npc.owner.as_player> "storestockmessage:->:<context.entity.framed_item.display||<c.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>>"


                - narrate "<dark_red>That item is out of stock! Please check back later"



          - determine cancelled



    on player places block:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_sneaking> && <player.is_op>:
          - queue clear

        - else:
          - determine cancelled



    on player breaks item_frame:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_sneaking> && <player.is_op>:
          - queue clear

        - else:
          - if <c.hanging.framed_item.display||null> != null:
            - narrate "It's a <c.hanging.framed_item.display>."

          - else:
            - narrate "It's <c.hanging.framed_item.material.formatted>."

          - narrate "Right click it with your shopping bag to add it to your transaction."
          - determine cancelled




    on player breaks block:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_sneaking> && <player.is_op>:
          - queue clear

        - else:
          - determine cancelled

        - if <player.flag[storekeeper_warned]||null> == null:
            - narrate '<aqua>[storekeeper] -> You<&co> <dark_red>Hey! <aqua>You break it, you buy it! <dark_red>Be careful!'
            - flag <player> storekeeper_warned duration:5s



    on entity damages item_frame:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.entity.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_sneaking> && <player.is_op>:
          - queue clear

        - else:
          - foreach <global.flag[store].escape_contents||li@>:
            - if <context.entity.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
              - if <server.flag[<context.entity.framed_item.escaped>]||null> == null:
                - flag server <context.entity.framed_item.escaped>:1.00
                - determine cancelled
              - else if <player.flag[storekeepernpc].as_npc.owner> == server && <yaml[prices].read[prices.<context.entity.framed_item.escaped>]||null> == null:
                - foreach <server.list_online_players>:
                  - if '<def[value].has_permission[<script.constant[permission_for_prices]>]||null>' != null:
                    - narrate "<red>Warning<&co><dark_purple><c.entity.framed_item> has no price! please set a price for this item by holding it in your hand ant typing /price" target:%value%

                  - else if !<server.flag[pricemessage].contains[<c.entity.framed_item>]>:
                    - flag server pricemessage:->:<c.entity.framed_item>


                - narrate "<dark_red>This item has no price. please contact the servers administrators to let them know"
                - determine cancelled

              - else if <player.flag[storekeepernpc].as_npc.owner> == server:
                - define price <yaml[prices].read[prices.<context.entity.framed_item.escaped>].mul[2].mul[<server.flag[<context.entity.framed_item.escaped>]>].as_int||null>
                - narrate "<green>%price%"
                - determine cancelled

              - else if <player.flag[storekeepernpc].as_npc.flag[sell_<context.entity.framed_item.escaped>]||null> == null:
                - if <player.flag[storekeepernpc].as_npc.owner.as_player.is_online> == true:
                  - narrate "<red>Warning<&co><dark_purple><context.entity.framed_item.display.name||<context.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>> does not have a price set for it! Please set a price in your shop for this item" targets:<player.flag[storekeepernpc].as_npc.owner.as_player>

                - else:
                  - if <player.flag[storekeepernpc].as_npc.owner.as_player.flag[storepricemessage].contains[<context.entity.framed_item>]||true>:
                    - flag <player.flag[storekeepernpc].as_npc.owner.as_player> "storepricemessage:->:<context.entity.framed_item.display||<c.entity.framed_item.scriptname||<context.entity.framed_item.material.name>>>"


                - narrate "<dark_red>This item has no price and cannot be sold till a price is set for it."
                - determine cancelled

              - else:
                - define price <player.flag[storekeepernpc].as_npc.flag[sell_<context.entity.framed_item.escaped>]||null>
                - narrate "<green>%price%"
                - determine cancelled







    on player joins:
    - foreach <server.get_npcs_flagged[store]||li@>:
      - if <def[value].owner> == <player> && <def[value].constant[open_limit]> == false:
        - spawn <def[value]> <def[value].location>
        - if '<server.flag[storeclosed].as_list.escaped>' contains '<def[value].flag[store].as_cuboid.escaped>':
          - announce "shop opened"
          - flag server storeclosed:<-:<def[value].flag[store].as_cuboid>
          - flag <def[value]> closed:!



    - if !<player.flag[storepricemessage]||null> == null:
      - wait 5s
      - foreach <player.flag[storepricemessage].as_list||li@>:
        - wait 3t
        - narrate "<red>Warning<&co><dark_purple>%value% does not have a price set for it! Please set a price in your shop for this item"
        - flag player storepricemessage:<-:%value%


    - if !<player.flag[storestockmessage]||null> == null:
      - wait 8s
      - foreach <player.flag[storestockmessage].as_list>:
        - wait 3t
        - narrate "<red>Warning<&co><dark_purple>%value% is out of stock! Please restock this item."
        - flag player storestockmessage:<-:%value%


    - if '<player.has_permission[<script.constant[permission_for_prices]||false>]||false>':
      - wait 12s
      - foreach <server.flag[pricemessage].as_list||li@>:
        - narrate "<red>Warning<&co><dark_purple>%value% has no price! please set a price for this item by holding it in your hand and typing /price"
        - flag server pricemessage:<-:%value%


    on player right clicks entity with shopping_bag:
    - if <context.entity.is_npc||null>:
      - flag player shopkeeper:<npc>

    on player places item_frame:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false>:
        - if <player.is_sneaking> && <player.is_op>:
          - queue clear

        - else:
          - determine cancelled



    on player right clicks with shopping_bag:
    - define name <player.name>
    - define is_shopping false
    - foreach <global.flag[store].escape_contents||li@>:
      - if <player.location.is_within[<def[value].unescaped>]>:
        - define is_shopping true
        - foreach stop


    - if !<player.inventory.qty['shopping_bag'player.inventory.qty['shopping_bag']> >= 2:
      - take 'i@shopping_bag' qty:36
      - note 'in@player bag' as:<player.name>_shopping_bag

    - if !%is_shopping%:
      - inventory clear d:in@<player.name>_shopping_bag
      - take 'i@shopping_bag'
      - narrate '<dark_red>You are not shopping right now!'

    - else if in@<player.name>_shopping_bag == null && %is_shopping%:
      - note 'in@player bag' as:<player.name>_shopping_bag

    - else if %is_shopping%:
      - narrate '<aqua>You take a look in your shopping bag...'
      - inventory open d:in@<player.name>_shopping_bag

    - determine cancelled

    on player right clicks with selling_bag:
    - define name <player.name>
    - define is_shopping false
    - foreach <global.flag[store].escape_contents||li@>:
      - if <player.location.is_within[<def[value].unescaped>]>:
        - define is_shopping true
        - foreach stop


    - if !%is_shopping%:
      - define name <player.name>
      - foreach <in@%name%_selling_bag.list_contents>:
        - drop %value% <player.location>

      - inventory clear d:in@<player.name>_selling_bag
      - narrate '<dark_red>You are not in a shop right now!'
      - take 'i@selling_bag'
      - determine cancelled

    - else if <in@%name%_shopping_bag||null> == null && %is_shopping%:
      - note 'in@player bag' as:<player.name>_selling_bag
      - inventory open d:in@<player.name>_selling_bag
      - determine cancelled

    - else if %is_shopping%:
      - inventory open d:in@<player.name>_selling_bag
      - determine cancelled

    - determine cancelled

    on player right clicks with item_bag:
    - foreach <global.flag[store].escape_contents||li@>:
      - if <context.location.is_within[<def[value].unescaped.as_cuboid>]||false> && <player.location.facing[<player.flag[shopkeeper]>]>:

      - else:
        - define item <player.item_in_hand.lore.as_string.strip_color>
        - foreach <in@%item%_item_bag.list_contents>:
          - drop %value% <player.location>

        - narrate "<dark_red>your shopping bag ripped"
        - take iteminhand

      - determine cancelled

    on player drops shopping_bag:
    - define name <player.name>
    - narrate '<aqua>You ditch your shopping bag.'
    - if !<player.flag[storekeepernpc].as_npc.owner> == server:
      - foreach <in@%name%_shopping_bag.list_contents>:
        - define item %value%
        - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
          - if <def[value].list_contents.simple> contains <def[item].simple> && <def[value].stacks[%item%]> < 64 && !<def[value].stacks> == <def[value].size>:
            - give %item% qty:<def[item].qty> to:%value%
            - foreach stop

          - else if %loop_index% == <player.flag[storekeepernpc].as_npc.flag[stock].as_list.size>:
            - foreach <player.flag[storekeepernpc].as_npc.flag[stock].as_list||li@>:
              - if !<def[value].stacks> == <def[value].size>:
                - give %item% qty:<def[item].qty> to:%value%






    - determine passively cancelled
    - wait 1t
    - take 'i@shopping_bag'
    on player drops selling_bag:
    - define name <player.name>
    - foreach <in@%name%_selling_bag.list_contents>:
      - drop %value% <player.location>

    - narrate '<aqua>You ditch your selling bag.'
    - note in@<player.name>_selling_bag remove
    - determine passively cancelled
    - wait 1t
    - take 'i@selling_bag'
    on player clicks in inventory:
    - define name <player.name>
    - if <c.inventory> == 'in@<player.name>_shopping_bag' && <player.item_in_hand.simple> == 'i@shopping_bag':
      - determine passively cancelled
      - inventory update d:<player.inventory>

    - if <c.inventory> == 'in@<player.name>_shopping_bag' && <player.item_in_hand.simple> == 'i@shopping_bag':
      - if <context.is_shift_click>:
        - determine passively cancelled
        - inventory update d:<player.inventory>


    - if <c.inventory> == 'in@<player.name>_selling_bag':
      - if "li@i@shopping_bag|i@selling_bag|i@item_bag" contains <context.item>:
        - inventory update
        - determine cancelled


    on player picks up shopping_bag:
    - if '<player.inventory.contains[i@shopping_bag]>':
      - determine cancelled

    - else:
      - note 'in@player bag' as:<player.name>_shopping_bag
      - narrate "<aqua>You pick up a brand new shopping bag. It's empty!"

    on player picks up storekeeperegg:
    - narrate "<aqua>To learn how to set up the frame shop please visit <green>https://www.youtube.com/watch?v=FlYEog5-KxU"
    on player clicks storekeeperegg in inventory:
    - narrate "<aqua>To learn how to set up the frame shop please visit <green>https://www.youtube.com/watch?v=FlYEog5-KxU"
'player bag':
  type: inventory
  title: <aqua>shopping bag
  size: 18

  # It's empty!
'selling bag':
  type: inventory
  title:  <aqua>selling bag
  size: 18

shopping_bag:
  type: item
  material: "player_head[skull_skin=02d014dd-6d1a-4056-ba55-977918442838|eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3R
leHR1cmUvYmY0MGM3NzE2MjY3YjFmNDY5YWU3MzBkMDEzYjE0MDAyN2Q4NDE5YzEyZDcxZjA3MTk4MTdhYzBhMWY3ZjA5ZCJ9fX0=]"
  display name: "shopping bag"
  lore:
  - Give this to a shopkeeper to check out
  - by right clicking him with it.
  - Right click to see the contents.
  - Left click the shopkeeper to see the price.

'item_bag':
  type: item
  material: "player_head[skull_skin=02d014dd-6d1a-4056-ba55-977918442838|eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3R
leHR1cmUvYmY0MGM3NzE2MjY3YjFmNDY5YWU3MzBkMDEzYjE0MDAyN2Q4NDE5YzEyZDcxZjA3MTk4MTdhYzBhMWY3ZjA5ZCJ9fX0=]"
  display name: item bag
  lore:
  - <red>this bag contains the items you bought

selling_bag:
  type: item
  material: "player_head[skull_skin=02d014dd-6d1a-4056-ba55-977918442838|eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3R
leHR1cmUvYmY0MGM3NzE2MjY3YjFmNDY5YWU3MzBkMDEzYjE0MDAyN2Q4NDE5YzEyZDcxZjA3MTk4MTdhYzBhMWY3ZjA5ZCJ9fX0=]"
  display name: selling bag
  lore:
  - <green>Right click the shopkeeper with this bag to sell the items in it.
  - <green>Left click the shopkeeper with this bag to see what the
  - <green>total price of your items will be.

storelocationstick:
  type: item
  material: iron_hoe
  display name: store_selector_tool

storekeeperegg:
  type: item
  material: polar_bear_spawn_egg
  display name: shop_keeper