/FileExtraction/Java_unseen/ionar2_summit/src/main/java/com/salhack/summit/module/exploit/AntiHunger.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.network.EventClientPacket;
import com.salhack.summit.module.Module;
import com.salhack.summit.module.Value;
import com.salhack.summit.util.Timer;
import net.minecraft.network.play.client.CPacketEntityAction;
import net.minecraft.network.play.client.CPacketPlayer;

import static net.minecraft.network.play.client.CPacketEntityAction.Action.START_SPRINTING;
import static net.minecraft.network.play.client.CPacketEntityAction.Action.STOP_SPRINTING;

public final class AntiHunger extends Module
{
    public final Value<Boolean> Sprint = new Value<>("Sprint", new String[]
    { "Sprint" }, "Cancel out the sprinting packets", false);
    public final Value<Boolean> Ground = new Value<>("Ground", new String[]
    { "Ground" }, "Spoofs onground state for movement packets", true);

    public AntiHunger()
    {
        super("AntiHunger", new String[]
        { "NoHunger" }, "Prevents hunger loss by spoofing on ground state", "NONE", 0x563562, ModuleType.EXPLOIT);
    }
    
    private boolean _isAttemptingElytraFly = false;
    private Timer _elytraUpdateTimer = new Timer();
    
    @EventHandler
    private Listener<EventClientPacket> onClientPacket = new Listener<>(event ->
    {
        if (event.getStage() != MinecraftEvent.Stage.Pre)
            return;
        
        if (event.getPacket() instanceof CPacketPlayer && Ground.getValue())
        {
            if (_elytraUpdateTimer.passed(5000) && !mc.player.isElytraFlying() && _isAttemptingElytraFly)
                _isAttemptingElytraFly = false;
            
            if (!mc.player.isElytraFlying() && !_isAttemptingElytraFly)
            {
                final CPacketPlayer packet = (CPacketPlayer) event.getPacket();
                if (mc.player.fallDistance > 0 || mc.playerController.isHittingBlock)
                    packet.onGround = true;
                else
                    packet.onGround = false;
            }
        }
        
        if (event.getPacket() instanceof CPacketEntityAction && Sprint.getValue())
        {
            final CPacketEntityAction packet = (CPacketEntityAction) event.getPacket();
            if (packet.getAction() == START_SPRINTING || packet.getAction() == STOP_SPRINTING)
                event.cancel();
            
            if (packet.getAction() == CPacketEntityAction.Action.START_FALL_FLYING)
            {
                _isAttemptingElytraFly = true;
                _elytraUpdateTimer.reset();
            }
        }
    });
}
