
package net.mcreator.watermelon_play_simulator.network;

import net.minecraftforge.network.NetworkEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.eventbus.api.SubscribeEvent;

import net.minecraft.world.level.Level;
import net.minecraft.world.entity.player.Player;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.core.BlockPos;

import net.mcreator.watermelon_play_simulator.world.inventory.DgchooselevelMenu;
import net.mcreator.watermelon_play_simulator.procedures.Dg14anniuProcedure;
import net.mcreator.watermelon_play_simulator.procedures.Dg13anniuProcedure;
import net.mcreator.watermelon_play_simulator.procedures.Dg12anniuProcedure;
import net.mcreator.watermelon_play_simulator.procedures.Dg11anniuProcedure;
import net.mcreator.watermelon_play_simulator.WatermelonPlaySimulator2Mod;

import java.util.function.Supplier;
import java.util.HashMap;

@Mod.EventBusSubscriber(bus = Mod.EventBusSubscriber.Bus.MOD)
public class DgchooselevelButtonMessage {
	private final int buttonID, x, y, z;

	public DgchooselevelButtonMessage(FriendlyByteBuf buffer) {
		this.buttonID = buffer.readInt();
		this.x = buffer.readInt();
		this.y = buffer.readInt();
		this.z = buffer.readInt();
	}

	public DgchooselevelButtonMessage(int buttonID, int x, int y, int z) {
		this.buttonID = buttonID;
		this.x = x;
		this.y = y;
		this.z = z;
	}

	public static void buffer(DgchooselevelButtonMessage message, FriendlyByteBuf buffer) {
		buffer.writeInt(message.buttonID);
		buffer.writeInt(message.x);
		buffer.writeInt(message.y);
		buffer.writeInt(message.z);
	}

	public static void handler(DgchooselevelButtonMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
		NetworkEvent.Context context = contextSupplier.get();
		context.enqueueWork(() -> {
			Player entity = context.getSender();
			int buttonID = message.buttonID;
			int x = message.x;
			int y = message.y;
			int z = message.z;
			handleButtonAction(entity, buttonID, x, y, z);
		});
		context.setPacketHandled(true);
	}

	public static void handleButtonAction(Player entity, int buttonID, int x, int y, int z) {
		Level world = entity.level;
		HashMap guistate = DgchooselevelMenu.guistate;
		// security measure to prevent arbitrary chunk generation
		if (!world.hasChunkAt(new BlockPos(x, y, z)))
			return;
		if (buttonID == 0) {

			Dg11anniuProcedure.execute(world, entity);
		}
		if (buttonID == 1) {

			Dg12anniuProcedure.execute(world, entity);
		}
		if (buttonID == 2) {

			Dg13anniuProcedure.execute(world, entity);
		}
		if (buttonID == 3) {

			Dg14anniuProcedure.execute(world, entity);
		}
	}

	@SubscribeEvent
	public static void registerMessage(FMLCommonSetupEvent event) {
		WatermelonPlaySimulator2Mod.addNetworkMessage(DgchooselevelButtonMessage.class, DgchooselevelButtonMessage::buffer, DgchooselevelButtonMessage::new, DgchooselevelButtonMessage::handler);
	}
}
