Skip to content

Commit e42a216

Browse files
committed
Handle account switches
fix #47
1 parent 6714424 commit e42a216

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

src/client/java/com/coflnet/CoflModClient.java

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ public class CoflModClient implements ClientModInitializer {
102102
public static Pair<String, String> lastScoreboardUploaded = new Pair<>("","0");
103103

104104
private String username = "";
105+
private String lastCheckedUsername = ""; // Track last username to detect account switches
105106
private static String lastNbtRequest = "";
106107
private boolean uploadedScoreboard = false;
107108
private static boolean popupShown = false;
@@ -135,6 +136,7 @@ public String getString() {
135136
public void onInitializeClient() {
136137
instance = this;
137138
username = MinecraftClient.getInstance().getSession().getUsername();
139+
lastCheckedUsername = username; // Initialize with current username
138140
Path configDir = FabricLoader.getInstance().getConfigDir();
139141
CoflCore cofl = new CoflCore();
140142
cofl.init(configDir);
@@ -190,7 +192,15 @@ public void onInitializeClient() {
190192
if (MinecraftClient.getInstance() != null && MinecraftClient.getInstance().getCurrentServerEntry() != null
191193
&& MinecraftClient.getInstance().getCurrentServerEntry().address.contains("hypixel.net")) {
192194
System.out.println("Connected to Hypixel");
193-
username = MinecraftClient.getInstance().getSession().getUsername();
195+
196+
// Update username in case of account switch before joining
197+
String currentUsername = MinecraftClient.getInstance().getSession().getUsername();
198+
if (!currentUsername.equals(username)) {
199+
System.out.println("Account changed before joining server: " + username + " -> " + currentUsername);
200+
username = currentUsername;
201+
lastCheckedUsername = currentUsername;
202+
}
203+
194204
if (!CoflCore.Wrapper.isRunning && CoflCore.config.autoStart)
195205
CoflSkyCommand.start(username);
196206
Thread.startVirtualThread(() -> {
@@ -273,6 +283,14 @@ public void onInitializeClient() {
273283
);
274284
});
275285

286+
// General screen event to check for account switches in menus
287+
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
288+
// Only check for account switches when not connected to a server
289+
if (client.player == null) {
290+
checkAndHandleAccountSwitch();
291+
}
292+
});
293+
276294
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
277295
if (screen instanceof GenericContainerScreen gcs && CoflCore.config.purchaseOverlay != null && gcs.getTitle() != null ) {
278296
// System.out.println(gcs.getTitle().getString());
@@ -402,6 +420,10 @@ public void onInitializeClient() {
402420

403421
ScreenEvents.AFTER_INIT.register((minecraftClient, screen, i, i1) -> {
404422
if(!(MinecraftClient.getInstance().currentScreen instanceof TitleScreen)) return;
423+
424+
// Check for account switches in the title screen
425+
checkAndHandleAccountSwitch();
426+
405427
if (!popupShown && !checkVersionCompability()) {
406428
popupShown = true;
407429
Screen currentScreen = MinecraftClient.getInstance().currentScreen;
@@ -1376,4 +1398,32 @@ private static void sendChatMessage(String message) {
13761398
client.player.sendMessage(Text.literal(message), false);
13771399
}
13781400
}
1401+
1402+
/**
1403+
* Check if the Minecraft account has changed and update SkyCoflCore connection if needed.
1404+
* This handles runtime account switches from other mods.
1405+
* Can only be called in the main menu (not while connected to a server).
1406+
*/
1407+
private void checkAndHandleAccountSwitch() {
1408+
String currentUsername = MinecraftClient.getInstance().getSession().getUsername();
1409+
1410+
// Check if username has changed
1411+
if (!currentUsername.equals(lastCheckedUsername) && !lastCheckedUsername.isEmpty()) {
1412+
System.out.println("Detected account switch from " + lastCheckedUsername + " to " + currentUsername);
1413+
1414+
// If CoflCore is running, we need to restart it with the new username
1415+
if (CoflCore.Wrapper.isRunning) {
1416+
System.out.println("Restarting CoflCore connection for new account: " + currentUsername);
1417+
CoflCore.Wrapper.stop();
1418+
username = currentUsername;
1419+
CoflSkyCommand.start(username);
1420+
} else {
1421+
// Just update the username for next time
1422+
username = currentUsername;
1423+
}
1424+
}
1425+
1426+
// Update last checked username
1427+
lastCheckedUsername = currentUsername;
1428+
}
13791429
}

0 commit comments

Comments
 (0)