Skip to content

Commit

Permalink
Backport playtest to 1.18.2
Browse files Browse the repository at this point in the history
  • Loading branch information
The-Fireplace committed Jul 9, 2024
1 parent b7e43c6 commit 46fcbff
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.the_fireplace.lib.api.command.injectables.Requirements;
import dev.the_fireplace.lib.api.command.interfaces.RegisterableCommand;
import dev.the_fireplace.lib.api.network.injectables.PacketSender;
import dev.the_fireplace.lib.api.uuid.injectables.EmptyUUID;
import dev.the_fireplace.playtest.network.ClientboundPackets;
import dev.the_fireplace.playtest.network.SimplePacketBuilder;
import dev.the_fireplace.playtest.network.clientbound.PingResponse;
Expand All @@ -16,6 +17,7 @@
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.level.ServerPlayer;

@Singleton
Expand All @@ -25,18 +27,21 @@ public final class PingCommand implements RegisterableCommand
private final PacketSender packetSender;
private final ClientboundPackets clientboundPackets;
private final SimplePacketBuilder simplePacketBuilder;
private final EmptyUUID emptyUUID;

@Inject
public PingCommand(
Requirements requirements,
PacketSender packetSender,
ClientboundPackets clientboundPackets,
SimplePacketBuilder simplePacketBuilder
SimplePacketBuilder simplePacketBuilder,
EmptyUUID emptyUUID
) {
this.requirements = requirements;
this.packetSender = packetSender;
this.clientboundPackets = clientboundPackets;
this.simplePacketBuilder = simplePacketBuilder;
this.emptyUUID = emptyUUID;
}


Expand All @@ -50,7 +55,7 @@ public CommandNode<CommandSourceStack> register(CommandDispatcher<CommandSourceS

private int execute(CommandContext<CommandSourceStack> command) throws CommandSyntaxException {
ServerPlayer serverPlayer = command.getSource().getPlayerOrException();
serverPlayer.sendSystemMessage(Component.literal("Ping command received, sending response."));
serverPlayer.sendMessage(new TextComponent("Ping command received, sending response."), emptyUUID.get());
packetSender.sendToClient(
serverPlayer.connection,
clientboundPackets.getPingResponseSpec(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;

@Singleton
public final class PingResponseReceiver implements ClientboundPacketReceiver
Expand All @@ -38,7 +39,7 @@ public void receive(Minecraft client, ClientPacketListener handler, FriendlyByte
}
client.submit(() -> {
assert client.player != null;
client.player.displayClientMessage(Component.literal("Received first ping response! Pinging again via FL packet."), false);
client.player.displayClientMessage(new TextComponent("Received first ping response! Pinging again via FL packet."), false);
this.packetSender.sendToServer(this.serverboundPackets.getSecondPingSpec(), simplePacketBuilder.build(SecondPing.PAYLOAD));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;

@Singleton
public final class SecondPingResponseReceiver implements ClientboundPacketReceiver
Expand All @@ -38,7 +39,7 @@ public void receive(Minecraft client, ClientPacketListener handler, FriendlyByte
}
client.submit(() -> {
assert client.player != null;
client.player.displayClientMessage(Component.literal("Received second ping response! Network is likely functional, firing one more serverbound packet to verify."), false);
client.player.displayClientMessage(new TextComponent("Received second ping response! Network is likely functional, firing one more serverbound packet to verify."), false);
this.packetSender.sendToServer(this.serverboundPackets.getThirdPingSpec(), simplePacketBuilder.build(ThirdPing.PAYLOAD));
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.the_fireplace.lib.api.network.injectables.PacketSender;
import dev.the_fireplace.lib.api.network.interfaces.ServerboundPacketReceiver;
import dev.the_fireplace.lib.api.uuid.injectables.EmptyUUID;
import dev.the_fireplace.playtest.network.ClientboundPackets;
import dev.the_fireplace.playtest.network.SimplePacketBuilder;
import dev.the_fireplace.playtest.network.clientbound.SecondPingResponse;
Expand All @@ -10,6 +11,7 @@
import jakarta.inject.Singleton;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;
Expand All @@ -20,16 +22,18 @@ public final class SecondPingReceiver implements ServerboundPacketReceiver
private final PacketSender packetSender;
private final ClientboundPackets clientboundPackets;
private final SimplePacketBuilder simplePacketBuilder;
private final EmptyUUID emptyUUID;

@Inject
public SecondPingReceiver(
PacketSender packetSender,
ClientboundPackets clientboundPackets,
SimplePacketBuilder simplePacketBuilder
SimplePacketBuilder simplePacketBuilder, EmptyUUID emptyUUID
) {
this.packetSender = packetSender;
this.clientboundPackets = clientboundPackets;
this.simplePacketBuilder = simplePacketBuilder;
this.emptyUUID = emptyUUID;
}

@Override
Expand All @@ -38,7 +42,7 @@ public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacke
throw new Error("Received unexpected payload from second ping!");
}
server.execute(() -> {
player.sendSystemMessage(Component.literal("Second ping received. Issuing clientbound response now."));
player.sendMessage(new TextComponent("Second ping received. Issuing clientbound response now."), emptyUUID.get());
this.packetSender.sendToClient(
player.connection,
this.clientboundPackets.getSecondPingResponseSpec(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
package dev.the_fireplace.playtest.network.serverbound.reciever;

import dev.the_fireplace.lib.api.network.interfaces.ServerboundPacketReceiver;
import dev.the_fireplace.lib.api.uuid.injectables.EmptyUUID;
import dev.the_fireplace.playtest.network.serverbound.ThirdPing;
import jakarta.inject.Inject;
import jakarta.inject.Singleton;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.TextComponent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.server.network.ServerGamePacketListenerImpl;

@Singleton
public final class ThirdPingReceiver implements ServerboundPacketReceiver
{
private final EmptyUUID emptyUUID;

@Inject
public ThirdPingReceiver(EmptyUUID emptyUUID) {
this.emptyUUID = emptyUUID;
}

@Override
public void receive(MinecraftServer server, ServerPlayer player, ServerGamePacketListenerImpl handler, FriendlyByteBuf buf) {
if (!buf.readUtf().equals(ThirdPing.PAYLOAD)) {
throw new Error("Received unexpected payload from third ping!");
}
server.execute(() -> {
player.sendSystemMessage(Component.literal("Third ping received. Networking is fully functional."));
player.sendMessage(new TextComponent("Third ping received. Networking is fully functional."), emptyUUID.get());
});
}
}

0 comments on commit 46fcbff

Please sign in to comment.