Skip to content

Commit

Permalink
Fix ClientPlayNetworking.Contexnt.player() being null
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Feb 18, 2024
1 parent 548b1e1 commit 3612bbd
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientConfigurationNetworkHandler;
Expand Down Expand Up @@ -144,5 +145,8 @@ public ChannelInfoHolder getChannelInfoHolder() {
}

private record ContextImpl(PacketSender responseSender) implements ClientConfigurationNetworking.Context {
private ContextImpl {
Objects.requireNonNull(responseSender, "responseSender");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import com.mojang.logging.LogUtils;
import org.slf4j.Logger;
Expand Down Expand Up @@ -51,8 +52,8 @@ public ClientPlayNetworkAddon(ClientPlayNetworkHandler handler, MinecraftClient
super(ClientNetworkingImpl.PLAY, handler.getConnection(), "ClientPlayNetworkAddon for " + handler.getProfile().getName());
this.handler = handler;
this.client = client;
this.context = new ContextImpl(client, client.player, this);

this.context = new ContextImpl(client, this);
// Must register pending channels via lateinit
this.registerPendingChannels((ChannelInfoHolder) this.connection, NetworkPhase.PLAY);
}
Expand Down Expand Up @@ -137,6 +138,15 @@ protected boolean isReservedChannel(Identifier channelName) {
return NetworkingImpl.isReservedCommonChannel(channelName);
}

private record ContextImpl(MinecraftClient client, ClientPlayerEntity player, PacketSender responseSender) implements ClientPlayNetworking.Context {
private record ContextImpl(MinecraftClient client, PacketSender responseSender) implements ClientPlayNetworking.Context {
private ContextImpl {
Objects.requireNonNull(client, "client");
Objects.requireNonNull(responseSender, "responseSender");
}

@Override
public ClientPlayerEntity player() {
return Objects.requireNonNull(client.player, "player");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import net.minecraft.network.NetworkPhase;
import net.minecraft.network.PacketCallbacks;
Expand Down Expand Up @@ -181,5 +182,9 @@ public ChannelInfoHolder getChannelInfoHolder() {
}

private record ContextImpl(ServerConfigurationNetworkHandler networkHandler, PacketSender responseSender) implements ServerConfigurationNetworking.Context {
private ContextImpl {
Objects.requireNonNull(networkHandler, "networkHandler");
Objects.requireNonNull(responseSender, "responseSender");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

import net.minecraft.network.ClientConnection;
import net.minecraft.network.NetworkPhase;
Expand Down Expand Up @@ -129,5 +130,9 @@ protected boolean isReservedChannel(Identifier channelName) {
}

private record ContextImpl(ServerPlayerEntity player, PacketSender responseSender) implements ServerPlayNetworking.Context {
private ContextImpl {
Objects.requireNonNull(player, "player");
Objects.requireNonNull(responseSender, "responseSender");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerTickEvents;
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry;
import net.fabricmc.fabric.api.networking.v1.ServerPlayConnectionEvents;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.fabricmc.fabric.test.networking.NetworkingTestmods;
import net.fabricmc.loader.api.FabricLoader;
Expand Down Expand Up @@ -106,6 +107,8 @@ public void onInitialize() {
NetworkingPlayPacketTest.registerCommand(dispatcher);
});

ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> sender.sendPacket(new OverlayPacket(Text.literal("Fabric API"))));

ServerTickEvents.START_SERVER_TICK.register(server -> {
if (!spamUnknownPackets) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package net.fabricmc.fabric.test.networking.client.play;

import java.util.Objects;

import com.mojang.brigadier.Command;

import net.minecraft.network.PacketByteBuf;
Expand All @@ -38,7 +40,13 @@ public void onInitializeClient() {
// Register the payload only on the client.
PayloadTypeRegistry.playC2S().register(UnknownPayload.ID, UnknownPayload.CODEC);

ClientPlayConnectionEvents.INIT.register((handler, client) -> ClientPlayNetworking.registerReceiver(NetworkingPlayPacketTest.OverlayPacket.ID, (payload, context) -> context.client().inGameHud.setOverlayMessage(payload.message(), true)));
ClientPlayConnectionEvents.INIT.register((handler, client) -> ClientPlayNetworking.registerReceiver(NetworkingPlayPacketTest.OverlayPacket.ID, (payload, context) -> {
Objects.requireNonNull(context);
Objects.requireNonNull(context.client());
Objects.requireNonNull(context.player());

context.client().inGameHud.setOverlayMessage(payload.message(), true);
}));

ClientCommandRegistrationCallback.EVENT.register((dispatcher, dedicated) -> dispatcher.register(
ClientCommandManager.literal("clientnetworktestcommand")
Expand Down

0 comments on commit 3612bbd

Please sign in to comment.