Skip to content

Commit

Permalink
Fix networking (i guess)
Browse files Browse the repository at this point in the history
  • Loading branch information
MATRIX-feather committed Apr 29, 2024
1 parent 119e22d commit 539a71a
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ protected void initDataTracker(DataTracker.Builder builder)
{
super.initDataTracker(builder);

builder.add(PLAYER_MODEL_PARTS, (byte)127);
//builder.add(PLAYER_MODEL_PARTS, (byte)127);
this.dataTracker.set(PLAYER_MODEL_PARTS, (byte)127);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ public class CodecDispatcherMixin<B extends ByteBuf, V, T>
exception.printStackTrace();
System.err.println("- x - x - x - x - x - x - x - x - x - x - x -");

System.err.println("All available types at this moment:");

for (PacketCodecDispatcher.PacketType<B, V, T> type : packetTypes)
System.err.println(" - " + type.id());

System.err.println("- x - x - x - x - x - x - x - x - x - x - x -");
System.err.println();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@

public record MorphCommandPayload(String content) implements CustomPayload
{
public static final PacketCodec<PacketByteBuf, MorphCommandPayload> CODEC = PacketCodecs.STRING
.xmap(MorphCommandPayload::new, MorphCommandPayload::content)
.cast();
public static final PacketCodec<PacketByteBuf, MorphCommandPayload> CODEC = PacketCodec.of(
(value, buf) -> buf.writeBytes(MorphInitChannelPayload.writeString(value.content())),
buf -> new MorphCommandPayload(MorphInitChannelPayload.readString(buf))
);

public static final CustomPayload.Id<MorphCommandPayload> id = new Id<>(ServerHandler.commandChannelIdentifier);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,45 @@
package xiamomc.morph.client.network.payload;

import net.minecraft.network.RegistryByteBuf;
import com.google.common.io.ByteStreams;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import xiamomc.morph.client.ServerHandler;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;

public record MorphInitChannelPayload(String message) implements CustomPayload
{
public static final PacketCodec<RegistryByteBuf, MorphInitChannelPayload> CODEC = PacketCodecs.string(32768)
.xmap(MorphInitChannelPayload::new, MorphInitChannelPayload::message)
.cast();
public static final PacketCodec<PacketByteBuf, MorphInitChannelPayload> CODEC = PacketCodec.of(
(value, buf) -> buf.writeString(value.message()),
buf -> new MorphInitChannelPayload(readString(buf))
);

public static byte[] writeString(String string)
{
var out = ByteStreams.newDataOutput();
out.writeUTF(string);

return out.toByteArray();
}

public static String readString(PacketByteBuf buf)
{
//System.out.println("Buf is '" + buf.toString(StandardCharsets.UTF_8) + "' :: with hashCode" + buf.hashCode());

var directBuffer = buf.readBytes(buf.readableBytes());
var dst = new byte[directBuffer.capacity()];
directBuffer.getBytes(0, dst);

//System.out.printf("DST is '%s'%n", Arrays.toString(dst));

var input = ByteStreams.newDataInput(dst);
var asByteBuf = input.readUTF();

buf.clear();
return asByteBuf;
}

public static final CustomPayload.Id<MorphInitChannelPayload> id = new Id<>(ServerHandler.initializeChannelIdentifier);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,32 @@
import net.minecraft.network.packet.CustomPayload;
import xiamomc.morph.client.ServerHandler;

import java.nio.charset.StandardCharsets;

public record MorphVersionChannelPayload(int protocolVersion) implements CustomPayload
{
public static final PacketCodec<PacketByteBuf, MorphVersionChannelPayload> CODEC = PacketCodecs.INTEGER
.xmap(MorphVersionChannelPayload::new, MorphVersionChannelPayload::protocolVersion)
.cast();
public static final PacketCodec<PacketByteBuf, MorphVersionChannelPayload> CODEC = PacketCodec.of(
(value, buf) -> buf.writeInt(value.protocolVersion()),
buf -> new MorphVersionChannelPayload(parseInt(buf))
);

public static int parseInt(PacketByteBuf buf)
{
//System.out.println("Buf is '" + buf.toString(StandardCharsets.UTF_8) + "' :: with hashCode" + buf.hashCode());
int read = -1;
try
{
read = buf.readInt();
}
catch (Throwable t)
{
System.err.println("[FeatherMorph] Error parsing protocol version from server: " + t.getMessage());
t.printStackTrace();
}

buf.clear();
return read;
}

public static final Id<MorphVersionChannelPayload> id = new Id<>(ServerHandler.versionChannelIdentifier);

Expand Down

0 comments on commit 539a71a

Please sign in to comment.