package com.thebrokenrail.twine.mixin;

import com.thebrokenrail.twine.util.boat.BoatChestModes;
import com.thebrokenrail.twine.util.boat.BoatUtil;
import net.minecraft.entity.vehicle.BoatEntity;
import net.minecraft.network.packet.c2s.play.ClientCommandC2SPacket;
import net.minecraft.server.network.ServerPlayNetworkHandler;
import net.minecraft.server.network.ServerPlayerEntity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(ServerPlayNetworkHandler.class)
public class MixinServerPlayNetworkHandler {
    @Shadow
    public ServerPlayerEntity player;

    @Inject(at = @At("TAIL"), method = "onClientCommand")
    public void onClientCommand(ClientCommandC2SPacket packet, CallbackInfo info) {
        if (packet.getMode() == ClientCommandC2SPacket.Mode.OPEN_INVENTORY && player.hasVehicle() && player.getVehicle() instanceof BoatEntity && ((BoatUtil) player.getVehicle()).getChestMode() != BoatChestModes.NONE) {
            ((BoatUtil) player.getVehicle()).openInventory(player);
        }
    }
}
