/FileExtraction/Java_unseen/ionar2_summit/src/main/java/com/salhack/summit/module/exploit/EntityDesync.java
package com.salhack.summit.module.exploit;

import com.salhack.summit.events.MinecraftEvent;
import com.salhack.summit.events.bus.EventHandler;
import com.salhack.summit.events.bus.Listener;
import com.salhack.summit.events.entity.EventEntityAdded;
import com.salhack.summit.events.network.EventServerPacket;
import com.salhack.summit.events.player.EventPlayerUpdate;
import com.salhack.summit.main.SummitStatic;
import com.salhack.summit.module.Module;
import com.salhack.summit.module.Value;
import com.salhack.summit.util.MathUtil;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.network.play.client.CPacketVehicleMove;
import net.minecraft.network.play.server.*;
import net.minecraft.util.math.Vec3d;
import net.minecraftforge.event.entity.EntityJoinWorldEvent;

public final class EntityDesync extends Module
{
    public final Value<Boolean> HClip = new Value<Boolean>("HClip", true);
    public final Value<Boolean> Timer = new Value<Boolean>("Timer", true);
    public final Value<Boolean> PacketCanceller = new Value<Boolean>("PacketCanceller", true);
    
    public EntityDesync()
    {
        super("EntityDesync", new String[]
        { "ED" }, "Clientside dismount from entity", "NONE", 0xDBAB24, ModuleType.EXPLOIT);
    }

    private Entity Riding = null;
    
    @Override
    public void toggleNoSave()
    {
        
    }

    @Override
    public void onEnable()
    {
        super.onEnable();

        if (mc.player == null)
        {
            Riding = null;
            toggle();
            return;
        }

        if (!mc.player.isRiding())
        {
            SendMessage("You are not riding an entity.");
            Riding = null;
            toggle();
            return;
        }

        Riding = mc.player.getRidingEntity();

        mc.player.dismountRidingEntity();
        mc.world.removeEntity(Riding);
        SendMessage("Forced a dismount, with entityid : " + Riding.getEntityId());
        
        if (HClip.getValue())
        {
            final Vec3d l_Direction = MathUtil.direction(mc.player.rotationYaw);
            
            if (l_Direction != null)
            {
                Entity l_Entity = mc.player.isRiding() ? mc.player.getRidingEntity() : mc.player;
                
                l_Entity.setPosition(mc.player.posX + l_Direction.x*122, mc.player.posY-30, mc.player.posZ + l_Direction.z*122);
                
                SendMessage(String.format("Teleported you %s blocks forward", 122));
            }
        }
        
        if (Timer.getValue())
        {
            if (!SummitStatic.TIMER.isEnabled())
                SummitStatic.TIMER.toggle();
        }
    }

    @Override
    public void onDisable()
    {
        super.onDisable();
        
        if (mc.world == null)
            return;
        
        mc.player.capabilities.isFlying = false;
        
        if (Riding != null)
        {
            Riding.isDead = false;
            if (!mc.player.isRiding())
            {
                mc.world.spawnEntity(Riding);
                mc.player.startRiding(Riding, true);
            }
            Riding = null;
            SendMessage("Forced a remount.");
        }
        
        if (PacketCanceller.getValue())
        {
            if (SummitStatic.PACKETCANCELLER.isEnabled())
                SummitStatic.PACKETCANCELLER.toggle();
        }
        
        if (Timer.getValue())
        {
            if (SummitStatic.TIMER.isEnabled())
                SummitStatic.TIMER.toggle();
        }
    }
    
    @EventHandler
    private Listener<EventPlayerUpdate> OnUpdate = new Listener<>(event ->
    {
        /// We must be riding to send these packets.
        if (Riding == null)
            return;

        if (mc.player.isRiding())
            return;
        
        mc.player.onGround = true;
        
        if (mc.player.posY <= 3)
        {
            mc.player.setPosition(mc.player.posX, 69, mc.player.posZ);
            mc.player.posY = 69.0;
        }

        Riding.setPosition(mc.player.posX, mc.player.posY, mc.player.posZ);
        
        mc.player.connection.sendPacket(new CPacketVehicleMove(Riding));
    });
    
    @EventHandler
    private Listener<EventServerPacket> onServerPacket = new Listener<>(event ->
    {
        if (event.getStage() != MinecraftEvent.Stage.Pre)
            return;
        
        if (event.getPacket() instanceof SPacketSetPassengers)
        {
            if (Riding == null)
                return;
            
            SPacketSetPassengers l_Packet = (SPacketSetPassengers) event.getPacket();
            
            Entity en = mc.world.getEntityByID(l_Packet.getEntityId());
            
            if (en == Riding)
            {
                for (int i : l_Packet.getPassengerIds())
                {
                    Entity ent = mc.world.getEntityByID(i);
                    
                    if (ent == mc.player)
                        return;
                }
                
                SendMessage("You dismounted. RIP");
                toggle();
            }
        }
        else if (event.getPacket() instanceof SPacketDestroyEntities)
        {
            SPacketDestroyEntities l_Packet = (SPacketDestroyEntities) event.getPacket();

            for (int l_EntityId : l_Packet.getEntityIDs())
            {
                if (l_EntityId == Riding.getEntityId())
                {
                    SendMessage("Entity is now null SPacketDestroyEntities");
                    toggle();
                    return;
                }
            }
        }
        else if (event.getPacket() instanceof SPacketSpawnMob)
        {
            SPacketSpawnMob packet = (SPacketSpawnMob) event.getPacket();
            
            if (Riding != null && Riding.getEntityId() == packet.getEntityID())
            {
                event.cancel();
                SendMessage("It's the don key!");
            }
        }
        else if (event.getPacket() instanceof SPacketMoveVehicle)
        {
            SPacketMoveVehicle packet = (SPacketMoveVehicle) event.getPacket();
            
            SendMessage("SPacketMoveVehicle: " + new Vec3d(Math.floor(packet.getX()), Math.floor(packet.getY()), Math.floor(packet.getZ())).toString());
        
            event.cancel();
        }
        else if (event.getPacket() instanceof SPacketDisconnect)
        {
            toggle();
        }
    });
    
    @EventHandler
    private Listener<EntityJoinWorldEvent> OnWorldEvent = new Listener<>(event ->
    {
        if (event.getEntity() == mc.player)
        {
            SendMessage("Joined world event!");
        }
    });
    
    @EventHandler
    private Listener<EventEntityAdded> OnEntityAdded = new Listener<>(event ->
    {
        if (PacketCanceller.getValue())
        {
            if (!SummitStatic.PACKETCANCELLER.isEnabled())
                if (event.GetEntity() instanceof EntityPlayer && event.GetEntity() != mc.player)
                    SummitStatic.PACKETCANCELLER.toggle();
        }
    });
}
