Skip to content

Commit

Permalink
Create PayloadHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Aug 5, 2023
1 parent bb8a2ce commit bf7ffb0
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import net.fabricmc.fabric.impl.networking.GenericFutureListenerHolder;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
import net.fabricmc.fabric.mixin.networking.client.accessor.ClientLoginNetworkHandlerAccessor;

public final class ClientLoginNetworkAddon extends AbstractNetworkAddon<ClientLoginNetworking.LoginQueryRequestHandler> {
Expand All @@ -57,7 +58,7 @@ public ClientLoginNetworkAddon(ClientLoginNetworkHandler handler, MinecraftClien

public boolean handlePacket(LoginQueryRequestS2CPacket packet) {
PacketByteBufLoginQueryRequestPayload payload = (PacketByteBufLoginQueryRequestPayload) packet.payload();
return handlePacket(packet.queryId(), packet.payload().id(), payload.data());
return handlePacket(packet.queryId(), packet.payload().id(), PayloadHelper.reset(payload.data()));
}

private boolean handlePacket(int queryId, Identifier channelName, PacketByteBuf buf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

public final class ClientPlayNetworkAddon extends AbstractChanneledNetworkAddon<ClientPlayNetworking.PlayChannelHandler> {
private final ClientPlayNetworkHandler handler;
Expand Down Expand Up @@ -85,7 +86,7 @@ public void onServerReady() {
*/
public boolean handle(PacketByteBufPayload payload) {
try {
return this.handle(payload.id(), payload.data());
return this.handle(payload.id(), PayloadHelper.reset(payload.data()));
} finally {
payload.data().release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,15 @@
import net.minecraft.network.packet.s2c.login.LoginQueryRequestS2CPacket;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

public class LoginQueryRequestS2CPacketFactory {
public static LoginQueryRequestS2CPacket create(PacketByteBuf buf) {
return new LoginQueryRequestS2CPacket(buf.readVarInt(), readPayload(buf.readIdentifier(), buf));
}

private static LoginQueryRequestPayload readPayload(Identifier id, PacketByteBuf buf) {
PacketByteBuf copy = PacketByteBufs.copy(buf);
buf.skipBytes(buf.readableBytes());
return new PacketByteBufLoginQueryRequestPayload(id, copy);
return new PacketByteBufLoginQueryRequestPayload(id, PayloadHelper.read(buf));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
public record PacketByteBufLoginQueryRequestPayload(Identifier id, PacketByteBuf data) implements LoginQueryRequestPayload {
@Override
public void write(PacketByteBuf buf) {
buf.writeBytes(this.data());
PayloadHelper.write(buf, data());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@
public record PacketByteBufLoginQueryResponse(PacketByteBuf data) implements LoginQueryResponse {
@Override
public void write(PacketByteBuf buf) {
buf.writeBytes(this.data());
PayloadHelper.write(buf, data());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
public record PacketByteBufPayload(Identifier id, PacketByteBuf data) implements CustomPayload {
@Override
public void write(PacketByteBuf buf) {
buf.writeBytes(this.data());
PayloadHelper.write(buf, data());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package net.fabricmc.fabric.impl.networking.payload;

import net.minecraft.network.PacketByteBuf;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;

public class PayloadHelper {
public static void write(PacketByteBuf byteBuf, PacketByteBuf data) {
byteBuf.writeBytes(data.copy());
}

public static PacketByteBuf read(PacketByteBuf byteBuf) {
PacketByteBuf newBuf = PacketByteBufs.create();
newBuf.writeBytes(byteBuf.copy());
return newBuf;
}

public static PacketByteBuf reset(PacketByteBuf byteBuf) {
// byteBuf.resetReaderIndex();
//// byteBuf.release();
return byteBuf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import net.fabricmc.fabric.impl.networking.GenericFutureListenerHolder;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryRequestPayload;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
import net.fabricmc.fabric.mixin.networking.accessor.ServerLoginNetworkHandlerAccessor;

public final class ServerLoginNetworkAddon extends AbstractNetworkAddon<ServerLoginNetworking.LoginQueryResponseHandler> implements PacketSender {
Expand Down Expand Up @@ -130,7 +131,7 @@ private void sendCompressionPacket() {
*/
public boolean handle(LoginQueryResponseC2SPacket packet) {
PacketByteBufLoginQueryResponse response = (PacketByteBufLoginQueryResponse) packet.response();
return handle(packet.queryId(), response.data());
return handle(packet.queryId(), response == null ? null : PayloadHelper.reset(response.data()));
}

private boolean handle(int queryId, @Nullable PacketByteBuf originalBuf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import net.fabricmc.fabric.impl.networking.ChannelInfoHolder;
import net.fabricmc.fabric.impl.networking.NetworkingImpl;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;
import net.fabricmc.fabric.mixin.networking.accessor.ServerCommonNetworkHandlerAccessor;

public final class ServerPlayNetworkAddon extends AbstractChanneledNetworkAddon<ServerPlayNetworking.PlayChannelHandler> {
Expand Down Expand Up @@ -75,7 +76,7 @@ public void onClientReady() {
* @return true if the packet has been handled
*/
public boolean handle(PacketByteBufPayload payload) {
return this.handle(payload.id(), payload.data());
return this.handle(payload.id(), PayloadHelper.reset(payload.data()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import net.minecraft.network.packet.c2s.common.CustomPayloadC2SPacket;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

@Mixin(CustomPayloadC2SPacket.class)
public class CustomPayloadC2SPacketMixin {
Expand All @@ -37,8 +37,6 @@ public class CustomPayloadC2SPacketMixin {
cancellable = true
)
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
PacketByteBuf copy = PacketByteBufs.copy(buf);
buf.skipBytes(buf.readableBytes());
cir.setReturnValue(new PacketByteBufPayload(id, copy));
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.util.Identifier;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufPayload;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

@Mixin(CustomPayloadS2CPacket.class)
public class CustomPayloadS2CPacketMixin {
Expand All @@ -37,8 +37,6 @@ public class CustomPayloadS2CPacketMixin {
cancellable = true
)
private static void readPayload(Identifier id, PacketByteBuf buf, CallbackInfoReturnable<CustomPayload> cir) {
PacketByteBuf copy = PacketByteBufs.copy(buf);
buf.skipBytes(buf.readableBytes());
cir.setReturnValue(new PacketByteBufPayload(id, copy));
cir.setReturnValue(new PacketByteBufPayload(id, PayloadHelper.read(buf)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@
import net.minecraft.network.packet.c2s.login.LoginQueryResponse;
import net.minecraft.network.packet.c2s.login.LoginQueryResponseC2SPacket;

import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.fabricmc.fabric.impl.networking.payload.PacketByteBufLoginQueryResponse;
import net.fabricmc.fabric.impl.networking.payload.PayloadHelper;

@Mixin(LoginQueryResponseC2SPacket.class)
public class LoginQueryResponseC2SPacketMixin {
@Inject(method = "readResponse", at = @At("HEAD"), cancellable = true)
private static void readResponse(int queryId, PacketByteBuf buf, CallbackInfoReturnable<LoginQueryResponse> cir) {
PacketByteBuf copy = PacketByteBufs.copy(buf);
buf.skipBytes(buf.readableBytes());
cir.setReturnValue(new PacketByteBufLoginQueryResponse(copy));
cir.setReturnValue(new PacketByteBufLoginQueryResponse(PayloadHelper.read(buf)));
}
}

0 comments on commit bf7ffb0

Please sign in to comment.