#Start
team join pc_play @a[x=1013,y=102,z=1000,distance=..2]
#Teleport players to areas
tp @r[x=1013,y=102,z=1000,distance=..2] 990 101 1000 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 990 101 1030 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 990 101 1060 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 990 101 970 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 990 101 940 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1040 101 1000 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1040 101 1030 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1040 101 1060 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1040 101 970 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1040 101 940 90 -18
tp @r[x=1013,y=102,z=1000,distance=..2] 1090 101 1000 90 -18
#Select pumpkins for the round
tag @e[type=minecraft:armor_stand,limit=1,sort=random,tag=pc_marker,tag=!pc_done] add pc_selected

execute as @e[type=minecraft:armor_stand,tag=pc_selected] at @s run clone ~ ~ ~ ~9 ~9 ~9 980 80 1000
tag @e[type=minecraft:armor_stand,tag=pc_selected] add pc_done
tag @e[type=minecraft:armor_stand,tag=pc_selected] remove pc_selected
tag @e[type=minecraft:armor_stand,limit=1,sort=random,tag=pc_marker,tag=!pc_done] add pc_selected
execute as @e[type=minecraft:armor_stand,tag=pc_selected] at @s run clone ~ ~ ~ ~9 ~9 ~9 990 80 1000
tag @e[type=minecraft:armor_stand,tag=pc_selected] add pc_done
tag @e[type=minecraft:armor_stand,tag=pc_selected] remove pc_selected
tag @e[type=minecraft:armor_stand,limit=1,sort=random,tag=pc_marker,tag=!pc_done] add pc_selected
execute as @e[type=minecraft:armor_stand,tag=pc_selected] at @s run clone ~ ~ ~ ~9 ~9 ~9 1000 80 1000
tag @e[type=minecraft:armor_stand,tag=pc_selected] add pc_done
tag @e[type=minecraft:armor_stand,tag=pc_selected] remove pc_selected
tag @e[type=minecraft:armor_stand,limit=1,sort=random,tag=pc_marker,tag=!pc_done] add pc_selected
execute as @e[type=minecraft:armor_stand,tag=pc_selected] at @s run clone ~ ~ ~ ~9 ~9 ~9 1010 80 1000

tag @e[type=minecraft:armor_stand,tag=pc_selected] add pc_done
tag @e[type=minecraft:armor_stand,tag=pc_selected] remove pc_selected
tag @e[type=minecraft:armor_stand,limit=1,sort=random,tag=pc_marker,tag=!pc_done] add pc_selected
execute as @e[type=minecraft:armor_stand,tag=pc_selected] at @s run clone ~ ~ ~ ~9 ~9 ~9 1020 80 1000
tag @e[type=minecraft:armor_stand,tag=pc_selected] add pc_done
tag @e[type=minecraft:armor_stand,tag=pc_selected] remove pc_selected
#Clear tags
tag @e[type=minecraft:armor_stand,tag=pc_done] remove pc_done
scoreboard players reset * pc_progress
#Fill the bases
fill 980 100 1064 988 100 1056 smooth_red_sandstone replace air
fill 980 100 1034 988 100 1026 smooth_red_sandstone replace air
fill 980 100 1004 988 100 996 smooth_red_sandstone replace air
fill 980 100 974 988 100 966 smooth_red_sandstone replace air
fill 980 100 944 988 100 936 smooth_red_sandstone replace air

fill 1030 100 1064 1038 100 1056 smooth_red_sandstone replace air
fill 1030 100 1034 1038 100 1026 smooth_red_sandstone replace air
fill 1030 100 1004 1038 100 996 smooth_red_sandstone replace air
fill 1030 100 974 1038 100 966 smooth_red_sandstone replace air
fill 1030 100 944 1038 100 936 smooth_red_sandstone replace air
fill 1080 100 1004 1088 100 996 smooth_red_sandstone replace air
#Pickaxe
give @a[team=pc_play] minecraft:diamond_pickaxe{ench:[{id:32,lvl:4}],CanDestroy:["minecraft:smooth_red_sandstone","minecraft:red_sandstone"],HideFlags:31,Unbreakable:1,display:{Name:'"Pumpkin Carver"',Lore:['"Use this to carve the pumpkin"']}}
#Load in first pumpkin to carve
clone 980 81 1000 980 89 1008 975 102 1056
clone 980 81 1000 980 89 1008 975 102 1026
clone 980 81 1000 980 89 1008 975 102 996
clone 980 81 1000 980 89 1008 975 102 966
clone 980 81 1000 980 89 1008 975 102 936
clone 980 81 1000 980 89 1008 1025 102 1056

clone 980 81 1000 980 89 1008 1025 102 1026
clone 980 81 1000 980 89 1008 1025 102 996
clone 980 81 1000 980 89 1008 1025 102 966
clone 980 81 1000 980 89 1008 1025 102 936
clone 980 81 1000 980 89 1008 1075 102 996

#Setup
scoreboard players set @e[type=minecraft:armor_stand,tag=GEN] pc_timer2 4800
xp -10000l @a[team=pc_play]
xp 240l @a[team=pc_play]
blockdata 4 1 -48 {auto:1} #Timer clock
scoreboard players set @a[team=pc_play] pc_points 0
scoreboard objectives setdisplay sidebar pc_progress
scoreboard players set @a pc_progress 0
give @a[team=pc_play] minecraft:red_sandstone{ench:[],CanPlaceOn:["minecraft:black_terracotta","minecraft:smooth_red_sandstone"],HideFlags:31,Unbreakable:1,display:{Name:"Pumpkin Filler",Lore:['"Use this to fill in a block"','" you removed by mistake."','" "','"You only have a limited amount!"']}} 5
advancement grant @a only pc:minigame_play




#R: Timer clock
scoreboard players remove @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer2=0..}] pc_timer2 1
scoreboard players operation @e[type=minecraft:armor_stand,tag=GEN] pc_timer = @e[type=minecraft:armor_stand,tag=GEN] pc_timer2
scoreboard players operation @e[type=minecraft:armor_stand,tag=GEN] pc_timer %= #20 pc_timer
execute as @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer=1}] at @s run xp -1l @a
execute as @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer2=-1] at @s run blockdata 4 1 -48 {auto:0}
scoreboard players reset @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer2=-1}] pc_timer2
execute as @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer2=0}] at @s run blockdata 6 1 -48 {auto:1} #Game Over (lose)
replaceitem entity @a[team=pc_play] hotbar.0 minecraft:diamond_pickaxe 1 0 {ench:[{id:32,lvl:4}],CanDestroy:["minecraft:double_stone_slab2","minecraft:red_sandstone"],HideFlags:31,Unbreakable:1,display:{Name:"Pumpkin Carver",Lore:["Use this to carve the pumpkin"]}}
execute as @a[scores={pc_progress=0..}] at @s run fill ~-20 100 ~-20 ~20 100 ~20 smooth_red_sandstone replace red_sandstone
execute as @a[scores={pc_progress=0..}] at @s run fill ~-20 101 ~-20 ~20 100 ~20 air replace red_sandstone
execute as @e[type=minecraft:armor_stand,tag=GEN,scores={pc_timer2=0}] at @s run blockdata 4 1 -48 {auto:0}


#Game over (loss)
tellraw @a ["",{"text":"Time is up!","color":"red"}]
team leave @a[team=pc_play]

#Game over (win)
tellraw @a ["",{"text":"Everyone finished carving.","color":"green"}]
scoreboard players reset @e[type=minecraft:armor_stand,tag=GEN] pc_timer2
xp -1000l @a

#reset
clear @a minecraft:diamond_pickaxe
scoreboard objectives setdisplay sidebar points
execute as @a at @s run scoreboard players operation @s points += @s pc_points
scoreboard players reset @a pc_progress
blockdata 14 1 -48 {auto:1} #win advancement
tp @a 397 69 500 90 -10
blockdata -24 1 -48 {auto:1} #pp:all_gamesend_game



#win advancement
scoreboard players set @e[type=minecraft:armor_stand,tag=GEN] pc_points 0
execute as @a ~ ~ ~ scoreboard players operation @e[type=minecraft:armor_stand,tag=GEN] pc_points > @s pc_points
execute as @a ~ ~ ~ scoreboard players operation @s pc_points -= @e[type=minecraft:armor_stand,tag=GEN] pc_points
advancement grant @a[scores={pc_points=0}] only pc:minigame_win
scoreboard players reset @a pc_points



#R: Countdown
scoreboard players add @e[tag=GEN,type=minecraft:armor_stand] GameTimer 1
testfor @e[tag=GEN,type=minecraft:armor_stand,scores={GameTimer=40..}]
    blockdata ~ ~ ~-2 {auto:0b} 
    scoreboard players reset @e[tag=GEN,type=minecraft:armor_stand,scores={GameTimer=40..}] GameTimer
    blockdata 0 1 -48 {auto:1} #Start



#player finishes
execute as @a[team=pc_play] at @s run scoreboard players add @p[team=pc_play,scores={pc_progress=5..}] pc_points 1
tellraw @a ["",{"selector":"@p[team=pc_play,scores={pc_progress=5..}]"},{"text":" finished all 5 pumpkins and gains ","color":"green"},{"score":{"name":"@p[team=pc_play,scores={pc_progress=5..}]","objective":"pc_points"},"color":"none"},{"text":" candies","color":"green"}]
team leave @a[team=pc_play,scores={pc_progress=5..}]
testfor @a[team=pc_play]
    gamemode spectator @a[scores={pc_progress=5..}]
testforblock ~ ~ ~-2 minecraft:chain_command_block -1 {SuccessCount:0}
    blockdata 8 1 -48 {auto:1} #game over (win)



#Advancement checks
testforblocks 980 100 1004 988 100 996 953 93 1008 
    advancement grant @p only pc:notch
testforblocks 980 100 1004 988 100 996 949 89 980
    advancement grant @p only pc:sssss



#1st carve
execute as @p[team=pc_play,scores={pc_progress=0},x=985,y=101,z=1000,distance=..20] at @s run testforblocks 980 100 1004 988 100 996 981 80 1000
    tag @p add pc_Improve
    clone 990 81 1000 990 89 1008 975 102 996
    tellraw @p ["",{"text":"Carve successful. Loading next pumpkin...","color":"green"}]


#Last carve
execute as @p[team=pc_play,scores={pc_progress=4},x=985,y=101,z=1000,distance=..20] at @s run testforblocks 980 100 1004 988 100 996 1021 80 1000
    tag @p add pc_Improve
    tellraw @p ["",{"text":"Carve successful. All pumpkins carved.","color":"green"}]
    blockdata 12 1 -48 {auto:1} #player finishes
    playsound minecraft:entity.player.levelup voice @p

#Fail carve 
tellraw @p ["",{"text":"Carve unsuccesful. Resetting pumpkin...","color":"red"}]
advancement grant @p only pc:big_mistakes



#Reset carve
scoreboard players add @p[tag=pc_Improve] pc_progress 1
tag @p[tag=pc_Improve] remove pc_Improve
fill 980 100 1004 988 100 996 smooth_red_sandstone replace air
blockdata ~-6 ~1 ~2 {auto:1}

