// Script to manage refunding buckets when milk, lava, or axolotl buckets 
// are used on their respective horses
// Author: InnerTri





// Imports
// Important Global Variables
import {
    system,
	world,
	ItemStack
} from "@minecraft/server";






// Handling milk, lava, and axolotl buckets
// Since we can't run code in beforeEvents (since that's what read-only mode is),
// we use system.run() to schedule code to be run as soon as possible,
// via an anonymous callback function
// I'm LEARNING!!!
world.beforeEvents.playerInteractWithEntity.subscribe(
	event => {
		if (event.itemStack == undefined) {
			return;
		}
		switch (event.target.typeId) {
			case "cyn_hao:skeleton_horse":
				if (event.itemStack.typeId == "minecraft:milk_bucket") {
					system.run(
						() => {
							event.player.getComponent("minecraft:inventory").container.setItem(event.player.selectedSlotIndex, new ItemStack("minecraft:bucket", 1));
							event.player.runCommand("playsound random.drink @a ~ ~ ~ 0.5");
						}
					)
				}
			return;

			case "cyn_hao:kelpie_horse":
				if (event.itemStack.typeId == "minecraft:axolotl_bucket") {
					system.run(
						() => {
							event.player.getComponent("minecraft:inventory").container.setItem(event.player.selectedSlotIndex, new ItemStack("minecraft:bucket", 1));
							event.player.runCommand("playsound random.burp @a ~ ~ ~ 0.25");
						}
					)
				}
			return;
			
			case "cyn_hao:lava_horse":
				if (event.itemStack.typeId == "minecraft:lava_bucket") {
					system.run(
						() => {
							event.player.getComponent("minecraft:inventory").container.setItem(event.player.selectedSlotIndex, new ItemStack("minecraft:bucket", 1));
							event.player.runCommand("playsound random.drink @a ~ ~ ~ 0.5");
						}
					)
				}
			return;

			default:
			return;
		}
		
	}
);