ServerEvents.recipes(event => {
    [
        'minecraft:iron_pickaxe',
        'minecraft:iron_shovel',
        'minecraft:iron_axe',
        'minecraft:iron_sword',
        'minecraft:iron_hoe',
        'annoyforge:hottonormal',
        'annoyforge:ironpickhead',
        'annoyforge:ironswordbladerecipe',
        'annoyforge:iron_shovel_headrecipe',
        'annoyforge:iron_axe_headrecipe',
        'annoyforge:iron_hoe_headrecipe',
        'annoyforge:pickaxeblueprint',
        'annoyforge:swordblueprint',
        'annoyforge:shovel_blueprint',
        'annoyforge:axeblueprint',
        'annoyforge:hoe_blueprint',
        'minecraft:anvil',
    ].forEach(recipe => event.remove({id: recipe}));

    event.shapeless('minecraft:iron_ingot', [
        'minecraft:water_bucket',
        'annoyforge:hot_iron_ingot'
    ]).replaceIngredient('minecraft:water_bucket', 'minecraft:bucket'); 
});

BlockEvents.leftClicked(event => {
  if (event.block.id == 'minecraft:magma_block') {
      const player = event.player
      const item = player.mainHandItem

      if (item.id == 'minecraft:iron_ingot') {
          item.count--
          player.give('annoyforge:hot_iron_ingot')
          event.level.playSound(null, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 'minecraft:block.lava.pop', 'players', 1.0, 1.0)
          event.level.playSound(null, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 'minecraft:block.fire.ambient', 'players', 0.7, 1.3)
          event.level.playSound(null, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 'minecraft:block.lava.extinguish', 'players', 0.7, 0.8)
          event.level.spawnParticles('minecraft:flame', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 8, 0.01)
          event.level.spawnParticles('minecraft:smoke', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.3, 0.2, 0.3, 6, 0.02)
          event.level.spawnParticles('minecraft:lava', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.1, 0.1, 0.1, 3, 0.05)
          event.level.spawnParticles('minecraft:crit', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 4, 0.01)
          event.cancel()
      }
  }
})

ServerEvents.recipes(event => {
    event.blasting('annoyforge:hot_iron_ingot', 'minecraft:iron_ingot')
        .cookingTime(100);
});


BlockEvents.rightClicked(event => {
  if (event.block.id == 'minecraft:anvil') {
    const player = event.player
    const hammer = player.mainHandItem
    const blueprint = player.offHandItem

    if (hammer.id == 'annoyforge:copper_hammer') {
      const recipes = {
        'annoyforge:iron_pickaxe_blueprint': 'minecraft:iron_pickaxe',
        'annoyforge:iron_sword_blueprint': 'minecraft:iron_sword',  
        'annoyforge:iron_axe_blueprint': 'minecraft:iron_axe',
        'annoyforge:iron_shovel_blueprint': 'minecraft:iron_shovel',
        'annoyforge:iron_hoe_blueprint': 'minecraft:iron_hoe',
      }

      if (blueprint.id in recipes) {
        if (Math.random() < 0.1) {
          player.give(recipes[blueprint.id])
          blueprint.count--
          event.level.playSound(null, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 'minecraft:block.anvil.use', 'players', 0.5, 1.0)
          event.level.spawnParticles('minecraft:happy_villager', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 8, 0.01)
          event.level.spawnParticles('minecraft:crit', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 4, 0.01)
        } else {
          event.level.playSound(null, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 'minecraft:block.anvil.place', 'players', 0.5, 1.0)
          event.level.spawnParticles('minecraft:flame', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 8, 0.01)
          event.level.spawnParticles('minecraft:smoke', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.3, 0.2, 0.3, 6, 0.02)
          event.level.spawnParticles('minecraft:lava', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.1, 0.1, 0.1, 3, 0.05)
          event.level.spawnParticles('minecraft:crit', true, event.block.x + 0.5, event.block.y + 1, event.block.z + 0.5, 0.2, 0.1, 0.2, 4, 0.01)
        }
        event.cancel()
      }
    }
  }
})

ServerEvents.tags('item', event => {
  event.add('minecraft:bookshelf_books', [
    'annoyforge:iron_pickaxe_blueprint',
    'annoyforge:iron_sword_blueprint',
    'annoyforge:iron_axe_blueprint',
    'annoyforge:iron_shovel_blueprint',
    'annoyforge:iron_hoe_blueprint'
  ])
})

ServerEvents.recipes(event => {
  event.shaped(
    'annoyforge:iron_pickaxe_head',
    [
      'HHH',
      '   ', 
      '   '   
    ],
    {
      H: 'annoyforge:hot_iron_ingot'
    }
  ).id('annoyforge:iron_pickaxe_head_recipe')

  event.shaped(
    'annoyforge:iron_sword_blade',
    [
      ' H ',  
      ' H ',
      '   '
    ],
    {
      H: 'annoyforge:hot_iron_ingot'
    }
  ).id('annoyforge:iron_sword_blade_recipe')

  event.shaped(
    'annoyforge:iron_hoe_head',
    [
      'HH ',  
      '   ',
      '   '
    ],
    {
      H: 'annoyforge:hot_iron_ingot'
    }
  ).id('annoyforge:iron_hoe_part_recipe')

  event.shaped(
    'annoyforge:iron_axe_head',
    [
      'HH ',  
      'H  ',
      '   '
    ],
    {
      H: 'annoyforge:hot_iron_ingot'
    }
  ).id('annoyforge:iron_axe_head_recipe')

  event.shaped(
    'annoyforge:iron_shovel_head',
    [
      ' H ',  
      '   ',
      '   '
    ],
    {
      H: 'annoyforge:hot_iron_ingot'
    }
  ).id('annoyforge:iron_shovel_head_recipe')

  event.shaped(
    'minecraft:anvil',
    [
      'IBI',  
      ' S ',
      'SSS'
    ],
    {
      B: 'minecraft:iron_block',
      I: 'minecraft:iron_ingot',
      S: 'minecraft:stone_slab'
    }
  ).id('annoyforge:anvil')
})
