ItemEvents.rightClicked(event => {
	const item = event.item.id
	switch (item) {
		case 'minecraft:lava_bucket':
			if (event.item.count > 1) event.cancel()
			break
		case 'botania:crafting_halo':
		case 'botania:auto_crafting_halo':
			event.cancel()
			break
	}
})

BlockEvents.rightClicked(/.*/, event => {
	const dim = event.block.level.dimension
	const x = event.block.x
	const y = event.block.y
	const z = event.block.z

	if (event.item.id == 'tconstruct:magma_bucket') {
		event.item.count--
		event.player.give('minecraft:bucket')

		event.server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run summon magma_cube ~ ~1 ~ {Health:1f,Size:0}`)
		event.server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.bucket.empty_lava master @a ~ ~ ~ 1 0`)
		event.server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run particle lava ~ ~ ~ 0 0 0 0.5 10 force`)
	}

	if (event.item.id == 'miners_delight:copper_cup') {
		event.cancel()
	}
})

BlockEvents.broken(event => {
	const block = event.block.id
	const dim = event.block.level.dimension
	const x = event.block.x
	const y = event.block.y
	const z = event.block.z

	if (block === 'cfm_circuit_breaker:circuit_breaker') {
		event.server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if entity @e[tag=circuit_breaker,distance=..1] run function backrooms:powerhouse_depower`)
		return
	}
	if (block === 'mekanism:radioactive_waste_barrel') {
		event.server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if entity @e[tag=radioactive_waste_barrel,distance=..1] run function backrooms:radiation_barrel`)
		return
	}
})

BlockEvents.rightClicked(event => {
	const block = event.block.id
	const item = event.item.id
	const stack = event.item
	const dim = event.block.level.dimension
	const x = event.block.x
	const y = event.block.y
	const z = event.block.z
	const server = event.server
	const creative = event.player.isCreative()

	function sendActionbar(message) {
		event.server.runCommandSilent(
			`execute in ${dim} positioned ${x} ${y} ${z} run title @a[distance=..7] actionbar {"text":"${message}","color":"white"}`
		);
	}

	if (item.startsWith('tconstruct:') && stack.nbt?.tic_broken) {
	event.cancel()}

	if (block === 'kubejs:oil_carpet' && item === 'minecraft:sponge') {
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run function backrooms:oil_bubble`)
		event.cancel()
		return
	}

	if (block === 'kubejs:floor_tiles' && item === 'kubejs:concrete_piece') {
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:block.deepslate.place master @a ~ ~ ~ 1 0`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run execute as @a[distance=..1] at @s run tp @s ~ ~0.1 ~`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ kubejs:floor_tiles_full`)
		return
	}
	if (block === 'kubejs:floor_tiles_red' && item === 'kubejs:concrete_piece') {
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:block.deepslate.place master @a ~ ~ ~ 1 0`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run execute as @a[distance=..1] at @s run tp @s ~ ~0.1 ~`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ kubejs:floor_tiles_red_full`)
		return
	}
	if (block === 'kubejs:floor_tiles_green' && item === 'kubejs:concrete_piece') {
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:block.deepslate.place master @a ~ ~ ~ 1 0`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run execute as @a[distance=..1] at @s run tp @s ~ ~0.1 ~`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ kubejs:floor_tiles_green_full`)
		return
	}

	let stripWallpaper = (map) => {
		const axes = Ingredient.of('#minecraft:axes')
		const isAxe = axes.test(event.item)
		const isKnife = item === 'kubejs:putty_knife'
		if (!isAxe && !isKnife) return
		let entry = map[block]
		if (!entry) return
		if (isAxe && !creative) event.player.damageHeldItem(event.hand, 1)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ ${entry.to}`)
		event.player.giveInHand(entry.give)
	}
	stripWallpaper({
	'kubejs:wallpaper1': { to: 'kubejs:stripped_wallpaper',  give: 'kubejs:wallpaper' },
	'kubejs:wallpaper2': { to: 'kubejs:stripped_wallpaper2', give: 'kubejs:wallpaper' },
	'kubejs:wallpaper3': { to: 'kubejs:stripped_wallpaper3', give: 'kubejs:wallpaper' },
	'kubejs:wallpaper5': { to: 'kubejs:stripped_wallpaper5', give: 'kubejs:wallpaper_white' },
	'kubejs:wallpaper6': { to: 'kubejs:stripped_wallpaper6', give: 'kubejs:wallpaper' },
	'kubejs:wallpaper7': { to: 'kubejs:stripped_wallpaper7', give: 'kubejs:wallpaper_pink' },
	'kubejs:wallpaper8': { to: 'kubejs:stripped_wallpaper8', give: 'kubejs:wallpaper' }
	})

	if (block === 'kubejs:wallpaper_fake') {
		if (axes.test(event.item)) {
			if (!creative) event.player.damageHeldItem(event.hand, 1)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper')
			return
		}
		if (item === 'kubejs:putty_knife') {
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper')
			return
		}
	}
	if (block === 'kubejs:wallpaper_white_fake') {
		if (axes.test(event.item)) {
			if (!creative) event.player.damageHeldItem(event.hand, 1)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper_white')
			return
		}
		if (item === 'kubejs:putty_knife') {
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper_white')
			return
		}
	}
	if (block === 'kubejs:wallpaper_pink_fake') {
		if (axes.test(event.item)) {
			if (!creative) event.player.damageHeldItem(event.hand, 1)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper_pink')
			return
		}
		if (item === 'kubejs:putty_knife') {
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:item.axe.strip master @a ~ ~ ~ 1 0.6`)
			server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ supplementaries:timber_frame`)
			event.player.giveInHand('kubejs:wallpaper_pink')
			return
		}
	}

	let applyWallpaper = (map) => {
		let entry = map[block]
		if (!entry) return
		if (item !== entry.item) return
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run playsound minecraft:block.wool.place master @a ~ ~ ~ 1 0`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run setblock ~ ~ ~ ${entry.to}`)
	}
	applyWallpaper({
	'kubejs:stripped_wallpaper':  { item: 'kubejs:wallpaper',        to: 'kubejs:wallpaper1' },
	'kubejs:stripped_wallpaper2': { item: 'kubejs:wallpaper',        to: 'kubejs:wallpaper2' },
	'kubejs:stripped_wallpaper3': { item: 'kubejs:wallpaper',        to: 'kubejs:wallpaper3' },
	'kubejs:stripped_wallpaper5': { item: 'kubejs:wallpaper_white',  to: 'kubejs:wallpaper5' },
	'kubejs:stripped_wallpaper6': { item: 'kubejs:wallpaper',        to: 'kubejs:wallpaper6' },
	'kubejs:stripped_wallpaper7': { item: 'kubejs:wallpaper_pink',   to: 'kubejs:wallpaper7' },
	'kubejs:stripped_wallpaper8': { item: 'kubejs:wallpaper',        to: 'kubejs:wallpaper8' }
	})
	

	if (block === 'kubejs:ceilling_lamp_off' && item === 'kubejs:fluorescent_tube') {
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run function backrooms:lamp_restoration`)
		return
	}
	/*
chip0 (0000): blank
chip1 (1000): dripstone cave: 10 coal ore, 12 iron ore, 3 gold ore, 3 emerald ore, 3 lapis ore, 58 dripstone blocks+spikes
chip2 (0100): crimson nether: 11 nether quartz ore, 2 nether gold, 38 crimson nylium, 115 netherrack
chip3 (1100): osmium cave: 6 osmium ore, 86 deepslate, 26 dripstone blocks+spikes
chip4 (0010): plains: COW, 3 dandelion, 27 grass, 49 grass block, 113 dirt
chip5 (1010): deep ores: 319 deepslate, 9 DS diamond ore, 10 DS gold ore, 5 DS uranium ore
chip6 (0110): blaze fortress: 3 blaze, 1 netherite upgrade, 2 debris, 29 netherrack, 108 nether bricks, 27 nether brick fence
chip7 (1110): desert temple: 9 tnt, 192 sandstone, 11 chiseled, 27 cut, 43 stone, 3 gold ore, 5 iron ore CHESTS: 3 enderio wooden gear, 2 dia, 12 gold, 8 emerald, 2 dune trim, 2 drill core, 1 aa bat wing, 1 botania black lotus, 2 saddle, 2 iron horse, 2 gold horse, 2 spider eye, 5 string, 29 bone, 18 gunpowder
chip8 (0001): end island: 189 endstone, 35 obsidian
chip9 (1001): living room: 1 tv, 1 mint sprout, 1 fern, 3 oak table, 4 light gray sofa, 2 light gray stools, 49 spruce planks
chip10 (0101): ruin portal: 1 gold block, 4 cobalt ore, 6 obsidian, 3 crying obsidian, 5 magma block, 65 netherrack CHEST: rib trim, 6 gold ingot, 8 gold nugget, 1 gold horse armor, 1 saddle
chip11 (0011): skyblock: dirt, one bucket
chip12 (1011): ocean: 4 trop fish, 1 salmon, 1 squid
chip13 (1101): sculk
chip14 (0111): library: 5% chance of eye, 1 moyai, brazier and weather vane
chip15 (1111): elven: two mana pools, black and pink magical flowers
chip infinity: black hole
chip error: corrupted: 1 ancient debris, 1 sand, 1 crying obsidian, 1 diamond ore, 1 iron ore, grass blocks, error blocks
available: 0011, 0111, 1011, 1101, 1111
	 */
	const chipFunctions = {
		'kubejs:data_chip1': 'reality_cave',
		'kubejs:data_chip2': 'reality_nether',
		'kubejs:data_chip3': 'reality_osmium',
		'kubejs:data_chip4': 'reality_grass',
		'kubejs:data_chip5': 'reality_diamonds',
		'kubejs:data_chip6': 'reality_blaze',
		'kubejs:data_chip7': 'reality_pyramid',
		'kubejs:data_chip8': 'reality_end',
		'kubejs:data_chip9': 'reality_room',
		'kubejs:data_chip10': 'reality_portal',
		'kubejs:data_chip11': 'reality_skyblock',
		'kubejs:data_chip12': 'reality_ocean',
		'kubejs:data_chip13': 'reality_sculk',
		'kubejs:data_chip14': 'reality_library',
		'kubejs:data_chip15': 'reality_elven',
		'kubejs:data_chip_error': 'reality_error',
		'kubejs:data_chip_infinity': 'reality_black_hole',
		'kubejs:data_chip_infinity_error': 'reality_error_modified'
	}
	if (block === 'kubejs:reality_controller' && chipFunctions[item]) {
			server.runCommandSilent(
				`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~1 ~ kubejs:reality_charge run function backrooms:${chipFunctions[item]}`
			)
			return
	}

	const sculkBlocks = [
		'minecraft:sculk',
		'minecraft:sculk_catalyst',
		'minecraft:sculk_vein',
		'minecraft:sculk_sensor',
		'minecraft:sculk_shrieker',
		'kubejs:sculk_tendrils',
		'kubejs:sculk_shroom',
		'kubejs:sculk_slab',
		'kubejs:sculk_stairs',
		'minecraft:spawner'
	];

	if (block === 'minecraft:spawner' && item === 'tiab:time_in_a_bottle') {
		return;
	}
	if (sculkBlocks.includes(block) && item === 'kubejs:sculk_scrubber') {
		if (!creative) {
			sendActionbar("Try to place the Sculk Scrubber on the side of a non-sculk block to get it in the right position.");
			event.cancel();
		}
		return;
	}

	if (sculkBlocks.includes(block) && item !== 'tiab:time_in_a_bottle') {
		if (!creative) {
			sendActionbar("You can't place blocks on sculk!");
			event.cancel();
		}
		return;
	}

	if (block === 'kubejs:sculk_scrubber') {
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~-1 ~ minecraft:sculk run summon marker ~ ~ ~ {Tags:["scrubber"]}`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} unless block ~ ~-1 ~ minecraft:sculk run title @a[distance=..5] actionbar {"text":"You need to place the scrubber on sculk!","color":"white"}`)
		return
	}

	if (block === 'eidolon:shadow_gem_block' && item === 'create:chromatic_compound') {
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run function backrooms:shadow_conversion`)
		if (!creative) event.item.count--
		event.player.give('create:shadow_steel')
		return
	}

	if (block === 'kubejs:hyper_experience_block' && item === 'minecraft:enchanted_book') {
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run function backrooms:cryptic_levelup`)
		if (!creative) {
			event.item.count--
			event.player.give('minecraft:book')
		} else {
			event.cancel()
		}
		return
	}

	if (block === 'supplementaries:cannon') {
		event.cancel()
		return
	} //disabled due to wall xray exploit

	if (block === 'kubejs:wall_destroyer') {
		// north/south
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=north] unless block ~ ~ ~1 minecraft:air if block ~ ~ ~2 kubejs:wall_destroyer[facing=south] positioned ~ ~ ~1 run function backrooms:wall_break`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=south] unless block ~ ~ ~-1 minecraft:air if block ~ ~ ~-2 kubejs:wall_destroyer[facing=north] positioned ~ ~ ~-1 run function backrooms:wall_break`)
		// west/east
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=west] unless block ~1 ~ ~ minecraft:air if block ~2 ~ ~ kubejs:wall_destroyer[facing=east] positioned ~1 ~ ~ run function backrooms:wall_break`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=east] unless block ~-1 ~ ~ minecraft:air if block ~-2 ~ ~ kubejs:wall_destroyer[facing=west] positioned ~-1 ~ ~ run function backrooms:wall_break`)
		// up/down
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=down] unless block ~ ~1 ~ minecraft:air if block ~ ~2 ~ kubejs:wall_destroyer[facing=up] positioned ~ ~1 ~ run function backrooms:wall_break`)
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} if block ~ ~ ~ kubejs:wall_destroyer[facing=up] unless block ~ ~-1 ~ minecraft:air if block ~ ~-2 ~ kubejs:wall_destroyer[facing=down] positioned ~ ~-1 ~ run function backrooms:wall_break`)
		return
	}

	if (block === 'kubejs:carpet_grass' && item === 'minecraft:bone_meal') {
		if (!creative) event.item.count--
		server.runCommandSilent(`execute in ${dim} positioned ${x} ${y} ${z} run summon item ~ ~ ~ {Item:{id:"kubejs:carpet_grass",Count:1b}, Motion:[0.0,0.3,0.0]}`)
		return
	}

	const coolers = Ingredient.of('#refurbished_furniture:coolers');
	if (coolers.test(block) && item === 'supplementaries:soap') {
		event.cancel();
		return;
	} //Fix cooler dupe (issue #182)

	const insulators = [
		'immersiveengineering:connector_hv',
		'immersiveengineering:connector_lv',
		'immersiveengineering:connector_mv'
	];
	if (insulators.includes(block) && item === 'immersiveengineering:hammer') {
		event.cancel();
	} //Feedthrough insulator intentionally disabled due to exploit

})