Skip to content

Commit

Permalink
feat: ✨ add disconnection info (health when disconnected, cooldown)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdt1806 committed Jul 4, 2024
1 parent 91c19ce commit 10bf246
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 24 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ fabric_version=0.100.4+1.21

# Mod Properties
# Join the version of the mod with the version of the game with "+"
mod_version = 1.1.1+1.21
mod_version = 1.2.0+1.21
maven_group = net.pdteggman
archives_base_name = autodisconnect
53 changes: 30 additions & 23 deletions src/main/java/net/pdteggman/autodisconnect/AutoDisconnect.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.minecraft.network.DisconnectionInfo;
import net.minecraft.text.Text;

@Environment(EnvType.CLIENT)
Expand Down Expand Up @@ -61,8 +62,8 @@ private void createConfigFileIfNotExists() {
private void writeFile() {
try (FileWriter writer = new FileWriter(configFile)) {
writer.write("%s %s %s".formatted(toggle, healthToLeave, cooldownInSeconds));
} catch (IOException ex) {
ex.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

Expand Down Expand Up @@ -109,27 +110,27 @@ private void registerCommands() {
+ " second" + (cooldownInSeconds == 1 ? "" : "s")));
return 1;
})).then(ClientCommandManager.literal("default").executes(context -> {
toggle = true;
healthToLeave = 8;
cooldownInSeconds = 15;
context.getSource()
.sendFeedback(Text.of("AutoDisconnect settings reset to default!"));
writeFile();
return 1;
})).then(ClientCommandManager.literal("help").executes(context -> {
context.getSource().sendFeedback(Text.of("AutoDisconnect Commands:"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect cooldown <cooldown> - Sets the cooldown (in seconds) after reconnecting to disconnect again (1-60)"));
context.getSource().sendFeedback(
Text.of("/autodisconnect default - Resets AutoDisconnect settings to default"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect health <health> - Sets the health to disconnect at (1-19)"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect status - Shows the current status and settings of AutoDisconnect"));
context.getSource().sendFeedback(
Text.of("/autodisconnect toggle - Toggles AutoDisconnect"));
return 1;
})));
toggle = true;
healthToLeave = 8;
cooldownInSeconds = 15;
context.getSource()
.sendFeedback(Text.of("AutoDisconnect settings reset to default!"));
writeFile();
return 1;
})).then(ClientCommandManager.literal("help").executes(context -> {
context.getSource().sendFeedback(Text.of("AutoDisconnect Commands:"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect cooldown <cooldown> - Sets the cooldown (in seconds) after reconnecting to disconnect again (1-60)"));
context.getSource().sendFeedback(
Text.of("/autodisconnect default - Resets AutoDisconnect settings to default"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect health <health> - Sets the health to disconnect at (1-19)"));
context.getSource().sendFeedback(Text.of(
"/autodisconnect status - Shows the current status and settings of AutoDisconnect"));
context.getSource().sendFeedback(
Text.of("/autodisconnect toggle - Toggles AutoDisconnect"));
return 1;
})));
});
}

Expand All @@ -153,7 +154,13 @@ private void registerDisconnectEvent() {
|| (healthWhenDisconnected > 0 && health < healthWhenDisconnected)) {
healthWhenDisconnected = health;
cooldown = cooldownInSeconds * 20;

client.player.getWorld().disconnect();
client.player.networkHandler
.onDisconnected(new DisconnectionInfo(Text.of(
"Disconnected by AutoDisconnect\n\nHealth when disconnected: %s\nCooldown (in seconds): %s"
.formatted(healthWhenDisconnected, cooldownInSeconds))));

} else {
cooldown--;
}
Expand Down

0 comments on commit 10bf246

Please sign in to comment.