Skip to content

Commit

Permalink
Properly reselect modpack
Browse files Browse the repository at this point in the history
#258
Fixes related to server join
  • Loading branch information
Skidamek committed Jul 29, 2024
1 parent 975f1aa commit bfe0fad
Show file tree
Hide file tree
Showing 14 changed files with 40 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -321,9 +321,7 @@ public void ModpackUpdaterMain(String link, Path modpackDir, Path modpackContent
Path cwd = Path.of(System.getProperty("user.dir"));
CustomFileUtils.deleteDummyFiles(cwd, serverModpackContent.list);

String modpackName = modpackDir.getFileName().toString();
ModpackUtils.addModpackToList(modpackName, link);
ModpackUtils.selectModpack(modpackDir);
ModpackUtils.selectModpack(modpackDir, link);

if (preload) {
LOGGER.info("Update completed! Took: {}ms", System.currentTimeMillis() - start);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,8 @@ public static Path renameModpackDir(Jsons.ModpackContentFields serverModpackCont
try {
Files.move(modpackDir, newModpackDir, StandardCopyOption.REPLACE_EXISTING);

addModpackToList(newModpackDir.getFileName().toString(), installedModpackLink);
removeModpackFromList(installedModpackName);
selectModpack(newModpackDir);
selectModpack(newModpackDir, installedModpackLink);

LOGGER.info("Changed modpack name of {} to {}", modpackDir.getFileName().toString(), serverModpackName);

Expand All @@ -261,14 +260,14 @@ public static Path renameModpackDir(Jsons.ModpackContentFields serverModpackCont
}

// Returns true if value changed
public static boolean selectModpack(Path modpackDir) {
String modpackToSelect = modpackDir.getFileName().toString();
public static boolean selectModpack(Path modpackDirToSelect, String modpackLinkToSelect) {
String modpackToSelect = modpackDirToSelect.getFileName().toString();
String selectedModpack = clientConfig.selectedModpack;
String modpackToSelectLink = clientConfig.installedModpacks.get(modpackToSelect);
String selectedModpackLink = clientConfig.installedModpacks.get(selectedModpack);
clientConfig.selectedModpack = modpackToSelect;
ConfigTools.save(clientConfigFile, clientConfig);
return !Objects.equals(modpackToSelect, selectedModpack) || !Objects.equals(modpackToSelectLink, selectedModpackLink);
ModpackUtils.addModpackToList(modpackToSelect, modpackLinkToSelect);
return !Objects.equals(modpackToSelect, selectedModpack) || !Objects.equals(modpackLinkToSelect, selectedModpackLink);
}

public static void removeModpackFromList(String modpackName) {
Expand All @@ -289,15 +288,9 @@ public static void addModpackToList(String modpackName, String link) {
return;
}

if (clientConfig.installedModpacks == null) {
Map<String, String> modpacks = new HashMap<>();
modpacks.put(modpackName, link);
clientConfig.installedModpacks = modpacks;
} else if (!clientConfig.installedModpacks.containsKey(modpackName)) {
Map<String, String> modpacks = new HashMap<>(clientConfig.installedModpacks);
modpacks.put(modpackName, link);
clientConfig.installedModpacks = modpacks;
}
Map<String, String> modpacks = new HashMap<>(clientConfig.installedModpacks);
modpacks.put(modpackName, link);
clientConfig.installedModpacks = modpacks;

ConfigTools.save(clientConfigFile, clientConfig);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ private void updateChangelogs() {
}

// remove method is only available in 1.17+
//#if MC >= 1170
/*? if >=1.17 {*/
this.remove(this.listEntryWidget);
this.remove(this.backButton);
this.remove(this.openMainPageButton);
//#endif
/*?}*/

this.listEntryWidget = new ListEntryWidget(formattedChanges, this.client, this.width, this.height, 48, this.height - 50, 20); // 38

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public ListEntryWidget(Map<String, String> changelogs, MinecraftClient client, i
}

/*? if <=1.20.2 {*/
/*public void render(/^? if <1.20 {^/ MatrixStack /^?} else {^/ /^DrawContext ^//^?}^/ matrices, int mouseX, int mouseY, float delta) {
/*public void render(/^? if <1.20 {^/ /^MatrixStack ^//^?} else {^/ DrawContext /^?}^/ matrices, int mouseX, int mouseY, float delta) {
super.render(matrices, mouseX, mouseY, delta);
}
*//*?}*/
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/pl/skidam/automodpack/init/NeoForgeInit.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

/*? if neoforge {*/
/*/^? if >1.20.5 {^/
/^import net.neoforged.fml.common.EventBusSubscriber;
^//^?}^/
import net.neoforged.fml.common.EventBusSubscriber;
/^?}^/
import pl.skidam.automodpack.client.ScreenImpl;
import pl.skidam.automodpack.client.audio.AudioManager;
import pl.skidam.automodpack.modpack.Commands;
Expand Down Expand Up @@ -41,10 +41,10 @@ public NeoForgeInit(IEventBus eventBus) {
}
/^? if >1.20.5 {^/
/^@EventBusSubscriber(modid = MOD_ID)
^//^?} else {^/
@Mod.EventBusSubscriber(modid = MOD_ID)
/^?}^/
@EventBusSubscriber(modid = MOD_ID)
/^?} else {^/
/^@Mod.EventBusSubscriber(modid = MOD_ID)
^//^?}^/
public static class events {
@SubscribeEvent
public static void onCommandsRegister(RegisterCommandsEvent event) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static void onReady(ServerLoginNetworkHandler handler, MinecraftServer se
HandshakePacket handshakePacket = new HandshakePacket(serverConfig.acceptedLoaders, AM_VERSION, MC_VERSION);
String jsonHandshakePacket = handshakePacket.toJson();

buf.writeString(jsonHandshakePacket, 32767);
buf.writeString(jsonHandshakePacket, Short.MAX_VALUE);
sender.sendPacket(HANDSHAKE, buf);
}));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package pl.skidam.automodpack.networking;

//#if MC >= 1202
/*? if >=1.20.2 {*/
import io.netty.buffer.Unpooled;
import net.minecraft.network.PacketByteBuf;

Expand All @@ -24,4 +24,4 @@ private static void assertSize(PacketByteBuf buf, int maxSize) {
}
}
}
//#endif
/*?} else {*/
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ private boolean handlePacket(int queryId, Identifier channelName, PacketByteBuf
future.thenAccept(resultBuf -> {
LoginQueryResponseC2SPacket packet = new LoginQueryResponseC2SPacket(queryId, /*? if <1.20.2 {*/ /*resultBuf *//*?} else {*/ new LoginResponsePayload(channelName, resultBuf) /*?}*/);
((ClientLoginNetworkHandlerAccessor) this.handler).getConnection().send(packet);
((ClientLoginNetworkHandlerAccessor) this.handler).getConnection().send(packet);
});
} catch (Throwable e) {
LOGGER.error("Encountered exception while handling in channel with name \"{}\"", channelName, e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

public class DataC2SPacket {
public static CompletableFuture<PacketByteBuf> receive(MinecraftClient minecraftClient, ClientLoginNetworkHandler handler, PacketByteBuf buf) {
String serverResponse = buf.readString(32767);
String serverResponse = buf.readString(Short.MAX_VALUE);

DataPacket dataPacket = DataPacket.fromJson(serverResponse);
String link = dataPacket.link;
Expand Down Expand Up @@ -50,12 +50,13 @@ public static CompletableFuture<PacketByteBuf> receive(MinecraftClient minecraft
LOGGER.info("Received link packet from server! {}", link);
}

// TODO: dont require/hardcode this
link = link + "/automodpack/";

Path modpackDir = ModpackUtils.getModpackPath(link, dataPacket.modpackName);
boolean selectedModpackChanged = ModpackUtils.selectModpack(modpackDir);
boolean selectedModpackChanged = ModpackUtils.selectModpack(modpackDir, link);

Boolean reply = null;
Boolean needsDisconnecting = null;

var optionalServerModpackContent = ModpackUtils.requestServerModpackContent(link);

Expand All @@ -65,24 +66,19 @@ public static CompletableFuture<PacketByteBuf> receive(MinecraftClient minecraft
if (update) {
disconnectImmediately(handler);
new ModpackUpdater().startModpackUpdate(optionalServerModpackContent.get(), link, modpackDir);
reply = true;
needsDisconnecting = true;
} else if (selectedModpackChanged) {
disconnectImmediately(handler);

// select modpack and restart
String modpackName = modpackDir.getFileName().toString();
ModpackUtils.addModpackToList(modpackName, link);
ModpackUtils.selectModpack(modpackDir);

// Its needed since newly selected modpack may not be loaded
new ReLauncher(modpackDir, UpdateType.SELECT).restart(false);
reply = true;
needsDisconnecting = true;
} else {
reply = false;
needsDisconnecting = false;
}
}

PacketByteBuf response = new PacketByteBuf(Unpooled.buffer());
response.writeString(String.valueOf(reply), 32767);
response.writeString(String.valueOf(needsDisconnecting), Short.MAX_VALUE);

return CompletableFuture.completedFuture(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class DataS2CPacket {
public static void receive(MinecraftServer server, ServerLoginNetworkHandler handler, boolean understood, PacketByteBuf buf, ServerLoginNetworking.LoginSynchronizer loginSynchronizer, PacketSender sender) {
GameProfile profile = ((ServerLoginNetworkHandlerAccessor) handler).getGameProfile();

String clientHasUpdate = buf.readString(32767);
String clientHasUpdate = buf.readString(Short.MAX_VALUE);

if ("true".equals(clientHasUpdate)) { // disconnect
LOGGER.warn("{} has not installed modpack", profile.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
public class HandshakeC2SPacket {

public static CompletableFuture<PacketByteBuf> receive(MinecraftClient client, ClientLoginNetworkHandler handler, PacketByteBuf buf) {
String serverResponse = buf.readString(32767);
String serverResponse = buf.readString(Short.MAX_VALUE);

HandshakePacket serverHandshakePacket = HandshakePacket.fromJson(serverResponse);

String loader = new LoaderManager().getPlatformType().toString().toLowerCase();

PacketByteBuf outBuf = new PacketByteBuf(Unpooled.buffer());
HandshakePacket clientHandshakePacket = new HandshakePacket(List.of(loader), AM_VERSION, MC_VERSION);
outBuf.writeString(clientHandshakePacket.toJson(), 32767);
outBuf.writeString(clientHandshakePacket.toJson(), Short.MAX_VALUE);

if (serverHandshakePacket.equals(clientHandshakePacket) || (serverHandshakePacket.loaders.contains(loader) && serverHandshakePacket.amVersion.equals(AM_VERSION))) {
LOGGER.info("Versions match " + serverHandshakePacket.amVersion);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void receive(MinecraftServer server, ServerLoginNetworkHandler han
public static void handleHandshake(ClientConnection connection, String playerName, PacketByteBuf buf, PacketSender packetSender) {
LOGGER.info("{} has installed AutoModpack.", playerName);

String clientResponse = buf.readString(32767);
String clientResponse = buf.readString(Short.MAX_VALUE);
HandshakePacket clientHandshakePacket = HandshakePacket.fromJson(clientResponse);

boolean isAcceptedLoader = false;
Expand Down Expand Up @@ -125,7 +125,7 @@ public static void handleHandshake(ClientConnection connection, String playerNam
String packetContentJson = dataPacket.toJson();

PacketByteBuf outBuf = new PacketByteBuf(Unpooled.buffer());
outBuf.writeString(packetContentJson, 32767);
outBuf.writeString(packetContentJson, Short.MAX_VALUE);
packetSender.sendPacket(DATA, outBuf);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import net.minecraft.network.PacketByteBuf;
import net.minecraft.util.Identifier;

///*? if <1.20.2 {*//*
//public record LoginRequestPayload(Identifier id, PacketByteBuf data) { }
//*//*}*/
/*? if <1.20.2 {*/
/*public record LoginRequestPayload(Identifier id, PacketByteBuf data) { }
*//*?}*/

/*? if >=1.20.2 {*/
/*? if >1.20.1 {*/
import net.minecraft.network.packet.s2c.login.LoginQueryRequestPayload;

public record LoginRequestPayload(Identifier id, PacketByteBuf data) implements LoginQueryRequestPayload {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public interface LoginSynchronizer {
* return;
* }
*
* String checkMessage = buf.readString(32767);
* String checkMessage = buf.readString(Short.MAX_VALUE);
*
* // Just send the CompletableFuture returned by the server's submit method
* synchronizer.waitFor(server.submit(() -&gt; {
Expand Down

0 comments on commit bfe0fad

Please sign in to comment.