From bad0ff82c93c81f372889b3236f07b255514721c Mon Sep 17 00:00:00 2001 From: alemi Date: Thu, 16 Feb 2023 02:53:28 +0100 Subject: feat: rudimentary autodisconnect --- src/main/java/ftbsc/bscv/BoSCoVicino.java | 1 + .../ftbsc/bscv/modules/self/AutoDisconnect.java | 43 ++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 src/main/java/ftbsc/bscv/modules/self/AutoDisconnect.java (limited to 'src/main/java/ftbsc') diff --git a/src/main/java/ftbsc/bscv/BoSCoVicino.java b/src/main/java/ftbsc/bscv/BoSCoVicino.java index 8479ed8..60735c3 100644 --- a/src/main/java/ftbsc/bscv/BoSCoVicino.java +++ b/src/main/java/ftbsc/bscv/BoSCoVicino.java @@ -60,6 +60,7 @@ public class BoSCoVicino implements ICommons { ForgeConfigSpec.Builder cfg = new ForgeConfigSpec.Builder(); CommandDispatcher dp = this.dispatcher; + BoSCoVicino.mods.add(new AutoDisconnect(cfg, dp).done(cfg)); BoSCoVicino.mods.add(new ActiveModules(cfg, dp).done(cfg)); BoSCoVicino.mods.add(new VanillaFlight(cfg, dp).done(cfg)); BoSCoVicino.mods.add(new FastInteract(cfg, dp).done(cfg)); diff --git a/src/main/java/ftbsc/bscv/modules/self/AutoDisconnect.java b/src/main/java/ftbsc/bscv/modules/self/AutoDisconnect.java new file mode 100644 index 0000000..a192581 --- /dev/null +++ b/src/main/java/ftbsc/bscv/modules/self/AutoDisconnect.java @@ -0,0 +1,43 @@ +package ftbsc.bscv.modules.self; + +import com.mojang.brigadier.CommandDispatcher; +import com.mojang.brigadier.arguments.DoubleArgumentType; + +import ftbsc.bscv.ICommons; +import ftbsc.bscv.events.PacketEvent; +import ftbsc.bscv.modules.Module; +import net.minecraft.command.CommandSource; +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; + +public class AutoDisconnect extends Module implements ICommons { + + public final ForgeConfigSpec.ConfigValue threshold; + + public AutoDisconnect(ForgeConfigSpec.Builder builder, CommandDispatcher dispatcher) { + super("AutoDisconnect", Group.SELF, builder, dispatcher); + + this.threshold = this.option( + "threshold", "hp below which connection should be closed", 10., + DoubleArgumentType.doubleArg(0., 20.), Double.class, + builder, dispatcher + ); + } + + @SubscribeEvent + public void onPacket(PacketEvent event) { + if (event.outgoing) return; + 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(); + } + } + } +} -- cgit v1.2.3-56-ga3b1