aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/ftbsc/bscv/modules/self/AutoDisconnect.java
blob: a05ca06a6bf2957a663319560061bee21d3c7413 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package ftbsc.bscv.modules.self;

import com.google.auto.service.AutoService;
import ftbsc.bscv.ICommons;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.modules.AbstractModule;
import ftbsc.bscv.patches.PacketPatch.PacketEvent;
import ftbsc.bscv.tools.Setting;
import net.minecraft.network.play.server.SDisconnectPacket;
import net.minecraft.network.play.server.SUpdateHealthPacket;
import net.minecraft.util.text.StringTextComponent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.eventbus.api.SubscribeEvent;

@AutoService(ILoadable.class)
public class AutoDisconnect extends AbstractModule implements ICommons {

   public final ForgeConfigSpec.ConfigValue<Double> threshold;

   public AutoDisconnect() {
      super();

      this.threshold = Setting.Decimal.builder()
         .fallback(10.)
         .name("threshold")
         .comment("hp below which connection should be closed")
         .build(this);
   }

   @SubscribeEvent
   public void onPacket(PacketEvent.Incoming event) {
      if (event.packet instanceof SUpdateHealthPacket) {
         SUpdateHealthPacket packet = (SUpdateHealthPacket) event.packet;
         if (packet.getHealth() < this.threshold.get()) {
            MC.player.connection.handleDisconnect( // this basically invokes disconnect() for us
               new SDisconnectPacket(new StringTextComponent("HP fell below threshold"))
            );
            this.disable();
         }
      }
   }
}