Skip to content

Commit

Permalink
Fix issues in the server connection protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
Aeltumn committed Apr 27, 2024
1 parent 4d9654b commit fc2e125
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
55 changes: 40 additions & 15 deletions fabric/src/main/java/com/noxcrew/noxesium/NoxesiumMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.fabricmc.fabric.api.resource.ResourceManagerHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.resources.ResourceLocation;
Expand Down Expand Up @@ -45,6 +46,11 @@ public class NoxesiumMod implements ClientModInitializer {
*/
private int currentMaxProtocol = ProtocolVersion.VERSION;

/**
* Whether the server connection has been initialized correctly.
*/
private boolean initialized = false;

/**
* The configuration file used by the mod.
*/
Expand Down Expand Up @@ -104,28 +110,21 @@ public void onInitializeClient() {
registerModule(new MccIslandTracker());
registerModule(new NoxesiumPacketHandling());

// Every time the client joins a server we send over information on the version being used
// Every time the client joins a server we send over information on the version being used,
// we initialize when both packets are known ad we are in the PLAY phase, whenever both have
// happened.
C2SPlayChannelEvents.REGISTER.register((ignored1, ignored2, ignored3, channels) -> {
// Find the packet that includes the server asking for the information packet
if (!channels.contains(NoxesiumPackets.CLIENT_INFO.id())) return;

// Check if the connection has been established first, just in case
if (Minecraft.getInstance().getConnection() != null) {
// Send a packet containing information about the client version of Noxesium
new ServerboundClientInformationPacket(ProtocolVersion.VERSION).send();

// Inform the player about the GUI scale of the client
syncGuiScale();

// Call connection hooks
modules.values().forEach(NoxesiumModule::onJoinServer);
}
initialize();
});
ClientPlayConnectionEvents.JOIN.register((ignored1, ignored2, ignored3) -> {
initialize();
});

// Call disconnection hooks
ClientPlayConnectionEvents.DISCONNECT.register((ignored1, ignored2) -> {
// Reset the current max protocol version
currentMaxProtocol = ProtocolVersion.VERSION;
initialized = false;

// Handle quitting the server
modules.values().forEach(NoxesiumModule::onQuitServer);
Expand All @@ -141,6 +140,32 @@ public void onInitializeClient() {
ResourceManagerHelper.get(PackType.CLIENT_RESOURCES).registerReloadListener(new NoxesiumReloadListener());
}

/**
* Initializes the connection with the current server if the connection has been established.
*/
private void initialize() {
// Ignore if already initialized
if (initialized) return;

// Don't allow if the server doesn't support Noxesium
if (!ClientPlayNetworking.canSend(NoxesiumPackets.CLIENT_INFO.id())) return;

// Check if the connection has been established first, just in case
if (Minecraft.getInstance().getConnection() != null) {
// Mark down that we are initializing the connection
initialized = true;

// Send a packet containing information about the client version of Noxesium
new ServerboundClientInformationPacket(ProtocolVersion.VERSION).send();

// Inform the player about the GUI scale of the client
syncGuiScale();

// Call connection hooks
modules.values().forEach(NoxesiumModule::onJoinServer);
}
}

/**
* Sends a packet to the server containing the GUI scale of the client which
* allows servers to more accurately adapt their UI to clients.
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ loader_version=0.15.10
#Fabric api
fabric_version=0.97.5+1.20.5
# Mod Properties
mod_version=2.0.3
mod_version=2.0.4

# Mod dependencies
sodium = mc1.20.5-0.5.8
Expand Down

0 comments on commit fc2e125

Please sign in to comment.