import { world, Vector, ItemStack, BlockType, BlockTypes } from "@minecraft/server";

const flowerDrop = [
  {
      parentState: "sunflower",
      oldDrop: "minecraft:double_plant:9",
      newDrop: "minecraft:stick"
  },
  {
    parentState: "rose",
    oldDrop: "minecraft:double_plant:9",
    newDrop: "minecraft:stick"
},
];

world.afterEvents.itemUseOn.subscribe(data => {



    if(data.itemStack.typeId == 'better_on_bedrock:rose_bush'){
      if(data.block.typeId == 'minecraft:grass') {
        data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} double_plant["double_plant_type"="rose"]`)
    data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
    data.source.runCommandAsync("clear @p better_on_bedrock:rose_bush 0 1")
  }
}
if(data.itemStack.typeId == 'better_on_bedrock:sunflower'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} double_plant[]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:sunflower 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:paeonia'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} double_plant["double_plant_type"="paeonia"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:paeonia 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:syringa'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} double_plant["double_plant_type"="syringa"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:syringa 0 1")

}
}
if(data.itemStack.typeId == 'better_on_bedrock:allium'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="allium"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:allium 0 1")

}
}
if(data.itemStack.typeId == 'better_on_bedrock:flower_blue'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="orchid"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:flower_blue 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:blue_rose'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower[]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:blue_rose 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:cornflower'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="cornflower"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:cornflower 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:houstinia'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="houstonia"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:houstinia 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:lilly'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="lily_of_the_valley"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:lilly 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:orange_tulip'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="tulip_orange"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:orange_tulip 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:red_tulip'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="tulip_red"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:red_tulip 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:white_tulip'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="tulip_white"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:white_tulip 0 1")
}
}
if(data.itemStack.typeId == 'better_on_bedrock:pink_tulip'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="tulip_pink"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:pink_tulip 0 1")

}
}
if(data.itemStack.typeId == 'better_on_bedrock:oxeye'){
  if(data.block.typeId == 'minecraft:grass') {
    data.source.runCommand(`setblock ${data.block.location.x} ${data.block.location.y+1} ${data.block.location.z} red_flower["flower_type"="oxeye"]`)
data.source.playSound('dig.grass', {location: new Vector(data.block.location.x, data.block.location.y, data.block.location.z)})
data.source.runCommandAsync("clear @p better_on_bedrock:oxeye 0 1")
}
}
})

world.afterEvents.playerBreakBlock.subscribe(({block, brokenBlockPermutation}) => {

  if(brokenBlockPermutation.getState('double_plant_type') == 'sunflower') {
    const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'sunflower'}).forEach((entity) => entity.kill())
  world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:sunflower', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('double_plant_type') == 'rose') {
 const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'rose bush'}).forEach((entity) => entity.kill())

world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:rose_bush', 1), new Vector(block.location.x, block.location.y, block.location.z))

}
if(brokenBlockPermutation.getState('double_plant_type') == 'syringa') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'lilac'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:syringa', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('double_plant_type') == 'paeonia') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'peony'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:paeonia', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'allium') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'allium'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:allium', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'orchid') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'blue orchid'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:flower_blue', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'poppy') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'poppy'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:blue_rose', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'cornflower') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'cornflower'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:cornflower', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'houstonia') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'azure bluet'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:houstinia', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'lily_of_the_valley') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'lily of the valley'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:lilly', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'oxeye') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'oxeye daisy'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:oxeye', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'tulip_orange') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'orange tulip'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:orange_tulip', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'tulip_red') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'red tulip'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:red_tulip', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'tulip_white') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'white tulip'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:white_tulip', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
if(brokenBlockPermutation.getState('flower_type') == 'tulip_pink') {
  const entity = world.getDimension('overworld').getEntities({ type: "minecraft:item", name: 'pink tulip'}).forEach((entity) => entity.kill())
world.getDimension('overworld').spawnItem(new ItemStack('better_on_bedrock:pink_tulip', 1), new Vector(block.location.x, block.location.y, block.location.z))
}
})