/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.llamalad7.mixinextras.sugar.Local
 *  com.llamalad7.mixinextras.sugar.Share
 *  com.llamalad7.mixinextras.sugar.ref.LocalBooleanRef
 *  com.llamalad7.mixinextras.sugar.ref.LocalFloatRef
 *  com.llamalad7.mixinextras.sugar.ref.LocalRef
 *  com.mojang.brigadier.exceptions.CommandSyntaxException
 *  it.unimi.dsi.fastutil.ints.IntListIterator
 *  meteordevelopment.orbit.ICancellable
 *  net.minecraft.class_1297
 *  net.minecraft.class_1542
 *  net.minecraft.class_243
 *  net.minecraft.class_2535
 *  net.minecraft.class_2604
 *  net.minecraft.class_2649
 *  net.minecraft.class_2653
 *  net.minecraft.class_2664
 *  net.minecraft.class_2672
 *  net.minecraft.class_2678
 *  net.minecraft.class_2708
 *  net.minecraft.class_2716
 *  net.minecraft.class_2767
 *  net.minecraft.class_2775
 *  net.minecraft.class_2818
 *  net.minecraft.class_310
 *  net.minecraft.class_634
 *  net.minecraft.class_638
 *  net.minecraft.class_8588
 *  net.minecraft.class_8673
 *  net.minecraft.class_8675
 *  org.spongepowered.asm.mixin.Mixin
 *  org.spongepowered.asm.mixin.Shadow
 *  org.spongepowered.asm.mixin.injection.At
 *  org.spongepowered.asm.mixin.injection.At$Shift
 *  org.spongepowered.asm.mixin.injection.Inject
 *  org.spongepowered.asm.mixin.injection.callback.CallbackInfo
 */
package meteordevelopment.meteorclient.mixin;

import com.llamalad7.mixinextras.sugar.Local;
import com.llamalad7.mixinextras.sugar.Share;
import com.llamalad7.mixinextras.sugar.ref.LocalBooleanRef;
import com.llamalad7.mixinextras.sugar.ref.LocalFloatRef;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import it.unimi.dsi.fastutil.ints.IntListIterator;
import meteordevelopment.meteorclient.MeteorClient;
import meteordevelopment.meteorclient.commands.Commands;
import meteordevelopment.meteorclient.events.entity.EntityDestroyEvent;
import meteordevelopment.meteorclient.events.entity.player.PickItemsEvent;
import meteordevelopment.meteorclient.events.game.GameJoinedEvent;
import meteordevelopment.meteorclient.events.game.GameLeftEvent;
import meteordevelopment.meteorclient.events.game.SendMessageEvent;
import meteordevelopment.meteorclient.events.packets.ContainerSlotUpdateEvent;
import meteordevelopment.meteorclient.events.packets.InventoryEvent;
import meteordevelopment.meteorclient.events.packets.PlaySoundPacketEvent;
import meteordevelopment.meteorclient.events.world.ChunkDataEvent;
import meteordevelopment.meteorclient.mixininterface.IExplosionS2CPacket;
import meteordevelopment.meteorclient.pathing.BaritoneUtils;
import meteordevelopment.meteorclient.systems.config.Config;
import meteordevelopment.meteorclient.systems.modules.Modules;
import meteordevelopment.meteorclient.systems.modules.movement.Velocity;
import meteordevelopment.meteorclient.systems.modules.player.NoRotate;
import meteordevelopment.meteorclient.systems.modules.render.NoRender;
import meteordevelopment.meteorclient.utils.player.ChatUtils;
import meteordevelopment.orbit.ICancellable;
import net.minecraft.class_1297;
import net.minecraft.class_1542;
import net.minecraft.class_243;
import net.minecraft.class_2535;
import net.minecraft.class_2604;
import net.minecraft.class_2649;
import net.minecraft.class_2653;
import net.minecraft.class_2664;
import net.minecraft.class_2672;
import net.minecraft.class_2678;
import net.minecraft.class_2708;
import net.minecraft.class_2716;
import net.minecraft.class_2767;
import net.minecraft.class_2775;
import net.minecraft.class_2818;
import net.minecraft.class_310;
import net.minecraft.class_634;
import net.minecraft.class_638;
import net.minecraft.class_8588;
import net.minecraft.class_8673;
import net.minecraft.class_8675;
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(value={class_634.class})
public abstract class ClientPlayNetworkHandlerMixin
extends class_8673 {
    @Shadow
    private class_638 field_3699;

    protected ClientPlayNetworkHandlerMixin(class_310 client, class_2535 connection, class_8675 connectionState) {
        super(client, connection, connectionState);
    }

    @Inject(method={"method_11112"}, at={@At(value="HEAD")}, cancellable=true)
    private void onEntitySpawn(class_2604 packet, CallbackInfo info) {
        if (packet != null && packet.method_11169() != null && Modules.get().get(NoRender.class).noEntity(packet.method_11169()) && Modules.get().get(NoRender.class).getDropSpawnPacket()) {
            info.cancel();
        }
    }

    @Inject(method={"method_11120"}, at={@At(value="HEAD")})
    private void onGameJoinHead(class_2678 packet, CallbackInfo info, @Share(value="worldNotNull") LocalBooleanRef worldNotNull) {
        worldNotNull.set(this.field_3699 != null);
    }

    @Inject(method={"method_11120"}, at={@At(value="TAIL")})
    private void onGameJoinTail(class_2678 packet, CallbackInfo info, @Share(value="worldNotNull") LocalBooleanRef worldNotNull) {
        if (worldNotNull.get()) {
            MeteorClient.EVENT_BUS.post((Object)GameLeftEvent.get());
        }
        MeteorClient.EVENT_BUS.post((Object)GameJoinedEvent.get());
    }

    @Inject(method={"method_52798"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_2600;method_11074(Lnet/minecraft/class_2596;Lnet/minecraft/class_2547;Lnet/minecraft/class_11980;)V", shift=At.Shift.AFTER)})
    private void onEnterReconfiguration(class_8588 packet, CallbackInfo info) {
        MeteorClient.EVENT_BUS.post((Object)GameLeftEvent.get());
    }

    @Inject(method={"method_11146"}, at={@At(value="HEAD")})
    private void onPlaySound(class_2767 packet, CallbackInfo info) {
        MeteorClient.EVENT_BUS.post((Object)PlaySoundPacketEvent.get(packet));
    }

    @Inject(method={"method_11128"}, at={@At(value="TAIL")})
    private void onChunkData(class_2672 packet, CallbackInfo info) {
        class_2818 chunk = this.field_45588.field_1687.method_8497(packet.method_11523(), packet.method_11524());
        MeteorClient.EVENT_BUS.post((Object)new ChunkDataEvent(chunk));
    }

    @Inject(method={"method_11109"}, at={@At(value="TAIL")})
    private void onContainerSlotUpdate(class_2653 packet, CallbackInfo info) {
        MeteorClient.EVENT_BUS.post((Object)ContainerSlotUpdateEvent.get(packet));
    }

    @Inject(method={"method_11153"}, at={@At(value="TAIL")})
    private void onInventory(class_2649 packet, CallbackInfo info) {
        MeteorClient.EVENT_BUS.post((Object)InventoryEvent.get(packet));
    }

    @Inject(method={"method_11095"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_2716;method_36548()Lit/unimi/dsi/fastutil/ints/IntList;")})
    private void onEntitiesDestroy(class_2716 packet, CallbackInfo ci) {
        IntListIterator intListIterator = packet.method_36548().iterator();
        while (intListIterator.hasNext()) {
            int id = (Integer)intListIterator.next();
            MeteorClient.EVENT_BUS.post((Object)EntityDestroyEvent.get(this.field_45588.field_1687.method_8469(id)));
        }
    }

    @Inject(method={"method_11124"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_2600;method_11074(Lnet/minecraft/class_2596;Lnet/minecraft/class_2547;Lnet/minecraft/class_11980;)V", shift=At.Shift.AFTER)})
    private void onExplosionVelocity(class_2664 packet, CallbackInfo ci) {
        Velocity velocity = Modules.get().get(Velocity.class);
        if (!velocity.explosions.get().booleanValue()) {
            return;
        }
        IExplosionS2CPacket explosionPacket = (IExplosionS2CPacket)packet;
        explosionPacket.meteor$setVelocityX((float)(packet.comp_2884().orElse(class_243.field_1353).field_1352 * velocity.getHorizontal(velocity.explosionsHorizontal)));
        explosionPacket.meteor$setVelocityY((float)(packet.comp_2884().orElse(class_243.field_1353).field_1351 * velocity.getVertical(velocity.explosionsVertical)));
        explosionPacket.meteor$setVelocityZ((float)(packet.comp_2884().orElse(class_243.field_1353).field_1350 * velocity.getHorizontal(velocity.explosionsHorizontal)));
    }

    @Inject(method={"method_11150"}, at={@At(value="INVOKE", target="Lnet/minecraft/class_638;method_8469(I)Lnet/minecraft/class_1297;", ordinal=0)})
    private void onItemPickupAnimation(class_2775 packet, CallbackInfo info) {
        class_1297 itemEntity = this.field_45588.field_1687.method_8469(packet.method_11915());
        class_1297 entity = this.field_45588.field_1687.method_8469(packet.method_11912());
        if (itemEntity instanceof class_1542 && entity == this.field_45588.field_1724) {
            MeteorClient.EVENT_BUS.post((Object)PickItemsEvent.get(((class_1542)itemEntity).method_6983(), packet.method_11913()));
        }
    }

    @Inject(method={"method_11157"}, at={@At(value="HEAD")})
    private void onPlayerPositionLookHead(class_2708 packet, CallbackInfo ci, @Share(value="noRotateYaw") LocalFloatRef yawRef, @Share(value="noRotatePitch") LocalFloatRef pitchRef) {
        NoRotate noRotate = Modules.get().get(NoRotate.class);
        if (!noRotate.isActive() || this.field_45588.field_1724 == null) {
            return;
        }
        yawRef.set(this.field_45588.field_1724.method_36454());
        pitchRef.set(this.field_45588.field_1724.method_36455());
    }

    @Inject(method={"method_11157"}, at={@At(value="RETURN")})
    private void onPlayerPositionLookReturn(class_2708 packet, CallbackInfo ci, @Share(value="noRotateYaw") LocalFloatRef yawRef, @Share(value="noRotatePitch") LocalFloatRef pitchRef) {
        NoRotate noRotate = Modules.get().get(NoRotate.class);
        if (!noRotate.isActive() || this.field_45588.field_1724 == null) {
            return;
        }
        float savedYaw = yawRef.get();
        float savedPitch = pitchRef.get();
        this.field_45588.field_1724.method_36456(savedYaw + 1.0E-6f);
        this.field_45588.field_1724.method_36457(savedPitch + 1.0E-6f);
        this.field_45588.field_1724.field_6241 = savedYaw;
        this.field_45588.field_1724.field_6283 = savedYaw;
    }

    @Inject(method={"method_45729"}, at={@At(value="HEAD")}, cancellable=true)
    private void onSendChatMessage(String message, CallbackInfo ci, @Local(argsOnly=true) LocalRef<String> messageRef) {
        if (!(message.startsWith(Config.get().prefix.get()) || BaritoneUtils.IS_AVAILABLE && message.startsWith(BaritoneUtils.getPrefix()))) {
            SendMessageEvent event = (SendMessageEvent)MeteorClient.EVENT_BUS.post((ICancellable)SendMessageEvent.get(message));
            if (!event.isCancelled()) {
                messageRef.set((Object)event.message);
            } else {
                ci.cancel();
            }
            return;
        }
        if (message.startsWith(Config.get().prefix.get())) {
            try {
                Commands.dispatch(message.substring(Config.get().prefix.get().length()));
            }
            catch (CommandSyntaxException e) {
                ChatUtils.error(e.getMessage(), new Object[0]);
            }
            this.field_45588.field_1705.method_1743().method_1803(message);
            ci.cancel();
        }
    }
}

