Skip to content

Commit

Permalink
🦀 custom vec3 stream codec
Browse files Browse the repository at this point in the history
  • Loading branch information
maximumpower55 committed Jan 12, 2025
1 parent 57d2974 commit 4eec8f6
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import io.github.fusionflux.portalcubed.framework.util.PortalCubedStreamCodecs;
import io.netty.buffer.ByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
Expand All @@ -23,7 +22,7 @@ public record PortalData(Vec3 origin, Quaternionf rotation, PortalSettings setti
).apply(instance, PortalData::new));

public static final StreamCodec<ByteBuf, PortalData> STREAM_CODEC = StreamCodec.composite(
PortalCubedStreamCodecs.VEC3, PortalData::origin,
Vec3.STREAM_CODEC, PortalData::origin,
ByteBufCodecs.QUATERNIONF, PortalData::rotation,
PortalSettings.STREAM_CODEC, PortalData::settings,
PortalData::new
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,14 @@
public interface PortalCubedStreamCodecs {
StreamCodec<ByteBuf, Unit> UNIT = StreamCodec.unit(Unit.INSTANCE);

StreamCodec<ByteBuf, Vec3> VEC3 = new StreamCodec<>() {
@Override
public @NotNull Vec3 decode(ByteBuf buffer) {
return new Vec3(buffer.readDouble(), buffer.readDouble(), buffer.readDouble());
}

@Override
public void encode(ByteBuf buffer, Vec3 value) {
buffer.writeDouble(value.x);
buffer.writeDouble(value.y);
buffer.writeDouble(value.z);
}
};

StreamCodec<ByteBuf, InteractionHand> HAND = ByteBufCodecs.BOOL.map(
value -> value ? InteractionHand.MAIN_HAND : InteractionHand.OFF_HAND,
hand -> hand == InteractionHand.MAIN_HAND
);

StreamCodec<ByteBuf, BlockHitResult> BLOCK_HIT_RESULT = StreamCodec.composite(
ByteBufCodecs.BOOL, i -> i.getType() == HitResult.Type.MISS,
VEC3, HitResult::getLocation,
Vec3.STREAM_CODEC, HitResult::getLocation,
Direction.STREAM_CODEC, BlockHitResult::getDirection,
BlockPos.STREAM_CODEC, BlockHitResult::getBlockPos,
ByteBufCodecs.BOOL, BlockHitResult::isInside,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.github.fusionflux.portalcubed.content.portal.PortalTeleportHandler;
import io.github.fusionflux.portalcubed.content.portal.PortalTeleportInfo;
import io.github.fusionflux.portalcubed.content.portal.manager.PortalManager;
import io.github.fusionflux.portalcubed.framework.util.PortalCubedStreamCodecs;
import io.github.fusionflux.portalcubed.packet.PortalCubedPackets;
import io.github.fusionflux.portalcubed.packet.ServerboundPacket;
import io.netty.buffer.ByteBuf;
Expand All @@ -22,7 +21,7 @@
public record ClientTeleportedPacket(PortalTeleportInfo info, Vec3 pos, float xRot, float yRot) implements ServerboundPacket {
public static final StreamCodec<ByteBuf, ClientTeleportedPacket> CODEC = StreamCodec.composite(
PortalTeleportInfo.STREAM_CODEC, ClientTeleportedPacket::info,
PortalCubedStreamCodecs.VEC3, ClientTeleportedPacket::pos,
Vec3.STREAM_CODEC, ClientTeleportedPacket::pos,
ByteBufCodecs.FLOAT, ClientTeleportedPacket::xRot,
ByteBufCodecs.FLOAT, ClientTeleportedPacket::yRot,
ClientTeleportedPacket::new
Expand Down

1 comment on commit 4eec8f6

@TropheusJ
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

skill issue

Please sign in to comment.