// priority: 0

BlockEvents.rightClicked(event => {
  if(event.block.id == 'minecraft:cactus'){
    if (event.item === 'minecraft:glass_bottle') {
          event.player.give(Item.of('minecraft:potion', '{Potion:"minecraft:water"}'))
          event.item.setCount(event.item.getCount() - 1)
      }
    }
})

BlockEvents.broken(event => {
  if(event.block.id == 'minecraft:cactus'){
    event.server.runCommandSilent('damage ' + `@p` + ' 2 minecraft:cactus')
  }
})


const MapItem = Java.loadClass("net.minecraft.world.item.MapItem");
const MapItemSavedData = Java.loadClass("net.minecraft.world.level.saveddata.maps.MapItemSavedData");
function createMapItem(level, position) {
    if(position == null) {
        return Item.empty;
    }

    const mapViewScale = 2;
    const trackPos = true;
    const unlimitedTracking = true;
    const item = MapItem.create(level, position.x, position.z, mapViewScale, trackPos, unlimitedTracking);
    MapItem.renderBiomePreviewMap(level, item);
    MapItemSavedData.addTargetDecoration(item, position, "+", "red_x") // maybe it has to be RED_X
  //  item.setHoverName(Text.of("Map to ")); // Not sure if it was `Text.of` in kubejs
    return item;
}

ItemEvents.rightClicked(event => {

  if(event.item.hasTag('desertopolis:maps')) {

    const player = event.getPlayer();
    const blockPos = event.getPlayer().getOnPos();
    const posPillager = MoreJS.findStructure(blockPos, event.getLevel(), "minecraft:pillager_outpost", 32);
    const posVillage = MoreJS.findStructure(blockPos, event.getLevel(), "minecraft:village_desert", 32);
    const posMineshaft = MoreJS.findStructure(blockPos, event.getLevel(), "#minecraft:mineshaft", 32);
    const posPyramid = MoreJS.findStructure(blockPos, event.getLevel(), "betterdeserttemples:desert_temple", 32);
    const posRedTemple = MoreJS.findStructure(blockPos, event.getLevel(), "desertopolis:red_sandstone_pyramid", 32);

    let badlandsBiome = MoreJS.findBiome(blockPos, event.getLevel(), "minecraft:badlands", 128)

    if (event.item === 'desertopolis:badlands_map') {

      const item = createMapItem(player.getLevel(), badlandsBiome).setHoverName("Map To Badlands");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }

    if (event.item === 'desertopolis:pillager_map') {

      const item = createMapItem(player.getLevel(), posPillager).setHoverName("Map To Pillager Outpost");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }

    if (event.item === 'desertopolis:village_map') {
      
      const item = createMapItem(player.getLevel(), posVillage).setHoverName("Map To Village");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }

    if (event.item === 'desertopolis:mineshaft_map') {

      const item = createMapItem(player.getLevel(), posMineshaft).setHoverName("Map To Mineshaft");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }

    if (event.item === 'desertopolis:great_pyramid_map') {

      const item = createMapItem(player.getLevel(), posPyramid).setHoverName("Map To The Great Pyramid");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }

    if (event.item === 'desertopolis:red_sandstone_temple_map') {

      const item = createMapItem(player.getLevel(), posRedTemple).setHoverName("Map To The Red Sandstone Temple");
      player.give(item);
      event.item.setCount(event.item.getCount() - 1)
    }
  }
})








/*
  const item = createMapItem(event.getLevel, event.player.position)
  event.player.give(item)

});
*/


