package dev.crucifixbeta.mod.modules.impl.exploit;

import dev.crucifixbeta.api.events.eventbus.EventListener;
import dev.crucifixbeta.api.events.impl.PacketEvent;
import dev.crucifixbeta.api.events.impl.UpdateEvent;
import dev.crucifixbeta.mod.modules.Module;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket;
import net.minecraft.network.packet.c2s.common.ResourcePackStatusC2SPacket.Status;
import net.minecraft.network.packet.s2c.common.ResourcePackSendS2CPacket;

@Environment(EnvType.CLIENT)
public class NoResourcePack extends Module {
   public static NoResourcePack INSTANCE;
   private boolean confirm;
   private boolean accepted;
   private int delay;

   public NoResourcePack() {
      super("NoResourcePack", Module.Category.Exploit);
      this.setChinese("\u53cd\u8d44\u6e90\u5305");
      INSTANCE = this;
   }

   @EventListener
   public void onPacketReceive(PacketEvent.Receive e) {
      if (e.getPacket() instanceof ResourcePackSendS2CPacket) {
         this.confirm = true;
         this.accepted = false;
         this.delay = 0;
         e.cancel();
      }
   }

   @EventListener
   public void onUpdate(UpdateEvent event) {
      if (this.confirm) {
         this.delay++;
         if (this.delay > 15 && !this.accepted) {
            mc.getNetworkHandler().sendPacket(new ResourcePackStatusC2SPacket(mc.player.getUuid(), Status.ACCEPTED));
            this.accepted = true;
         }

         if (this.delay > 40) {
            mc.getNetworkHandler().sendPacket(new ResourcePackStatusC2SPacket(mc.player.getUuid(), Status.SUCCESSFULLY_LOADED));
            this.confirm = false;
         }
      }
   }
}
