Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NETWORK serialization attribute #293

Open
wants to merge 2 commits into
base: 1.21
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/io/wispforest/owo/serialization/CodecUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ public T decode(B buf) {
? SerializationContext.attributes(RegistriesAttribute.of(registryByteBuf.getRegistryManager()))
: SerializationContext.empty();

ctx = ctx.withAttributes(MinecraftSerializationAttributes.NETWORK);

return endec.decode(ctx, ByteBufDeserializer.of(buf));
}

Expand All @@ -280,6 +282,8 @@ public void encode(B buf, T value) {
? SerializationContext.attributes(RegistriesAttribute.of(registryByteBuf.getRegistryManager()))
: SerializationContext.empty();

ctx = ctx.withAttributes(MinecraftSerializationAttributes.NETWORK);

endec.encode(ctx, ByteBufSerializer.of(buf), value);
}
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.wispforest.owo.serialization;

import io.wispforest.endec.SerializationAttribute;

public final class MinecraftSerializationAttributes {
private MinecraftSerializationAttributes() { }

/**
* This format will be sent over the network.
* <p>
* Registries and block states can be represented as integer IDs.
*/
public static final SerializationAttribute.Marker NETWORK = SerializationAttribute.marker("network");
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
package io.wispforest.owo.serialization.endec;

import com.mojang.datafixers.util.Function3;
import io.wispforest.endec.Endec;
import io.wispforest.endec.SerializationAttributes;
import io.wispforest.endec.*;
import io.wispforest.endec.impl.ReflectiveEndecBuilder;
import io.wispforest.endec.impl.StructEndecBuilder;
import io.wispforest.owo.serialization.CodecUtils;
import io.wispforest.owo.serialization.MinecraftSerializationAttributes;
import net.fabricmc.fabric.api.event.registry.RegistryAttribute;
import net.fabricmc.fabric.api.event.registry.RegistryAttributeHolder;
import net.fabricmc.fabric.api.networking.v1.PacketByteBufs;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.registry.RegistryKey;
import net.minecraft.registry.tag.TagKey;
Expand Down Expand Up @@ -87,6 +93,11 @@ private MinecraftEndecs() {}
: BlockHitResult.createMissed(pos, side, blockPos)
);

public static final Endec<BlockState> BLOCK_STATE = Endec.ifAttr(
MinecraftSerializationAttributes.NETWORK, Endec.VAR_INT.xmap(Block.STATE_IDS::get, Block.STATE_IDS::getRawId)
)
.orElse(CodecUtils.toEndec(BlockState.CODEC));

// --- Constructors for MC types ---

public static ReflectiveEndecBuilder addDefaults(ReflectiveEndecBuilder builder) {
Expand All @@ -105,11 +116,39 @@ public static ReflectiveEndecBuilder addDefaults(ReflectiveEndecBuilder builder)

builder.register(BLOCK_HIT_RESULT, BlockHitResult.class);

builder.register(ofRegistry(Registries.ITEM), Item.class)
.register(ofRegistry(Registries.BLOCK), Block.class);

builder.register(BLOCK_STATE, BlockState.class);

return builder;
}

public static <T> Endec<T> ofRegistry(Registry<T> registry) {
return IDENTIFIER.xmap(registry::get, registry::getId);
Endec<T> idEndec = IDENTIFIER.xmap(registry::get, registry::getId);
Endec<T> rawIdEndec = Endec.VAR_INT.xmap(registry::get, registry::getRawId);

return new Endec<>() {
@Override
public void encode(SerializationContext ctx, Serializer<?> serializer, T value) {
if (RegistryAttributeHolder.get(registry).hasAttribute(RegistryAttribute.SYNCED)
&& ctx.hasAttribute(MinecraftSerializationAttributes.NETWORK)) {
rawIdEndec.encode(ctx, serializer, value);
} else {
idEndec.encode(ctx, serializer, value);
}
}

@Override
public T decode(SerializationContext ctx, Deserializer<?> deserializer) {
if (RegistryAttributeHolder.get(registry).hasAttribute(RegistryAttribute.SYNCED)
&& ctx.hasAttribute(MinecraftSerializationAttributes.NETWORK)) {
return rawIdEndec.decode(ctx, deserializer);
} else {
return idEndec.decode(ctx, deserializer);
}
}
};
}

public static <T> Endec<TagKey<T>> unprefixedTagKey(RegistryKey<? extends Registry<T>> registry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.wispforest.uwu.network;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.item.Item;

public record DispatchedSubclassThree(Item item, Block block, BlockState state) implements DispatchedInterface {
@Override
public String getName() {
return "three";
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package io.wispforest.uwu.network;

import io.wispforest.endec.impl.RecordEndec;
import io.wispforest.endec.impl.ReflectiveEndecBuilder;
import io.wispforest.owo.network.OwoNetChannel;
import io.wispforest.endec.Endec;
import io.wispforest.endec.StructEndec;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper;
import net.minecraft.block.Blocks;
import net.minecraft.block.HorizontalFacingBlock;
import net.minecraft.client.option.KeyBinding;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Direction;
import org.lwjgl.glfw.GLFW;

import java.util.HashMap;
Expand All @@ -28,6 +31,7 @@ public static void init() {

REGISTRY.put("one", RecordEndec.create(CHANNEL.builder(), DispatchedSubclassOne.class));
REGISTRY.put("two", RecordEndec.create(CHANNEL.builder(), DispatchedSubclassTwo.class));
REGISTRY.put("three", RecordEndec.create(CHANNEL.builder(), DispatchedSubclassThree.class));

CHANNEL.registerClientbound(StringPacket.class, (message, access) -> {
access.player().sendMessage(Text.of(message.value()), false);
Expand Down Expand Up @@ -67,6 +71,11 @@ public static void init() {

CHANNEL.clientHandle().send(new MaldingPacket(new DispatchedSubclassOne("base")));
CHANNEL.clientHandle().send(new MaldingPacket(new DispatchedSubclassTwo(20)));
CHANNEL.clientHandle().send(new MaldingPacket(new DispatchedSubclassThree(
Items.ACACIA_BOAT,
Blocks.DRAGON_EGG,
Blocks.OAK_STAIRS.getDefaultState().with(HorizontalFacingBlock.FACING, Direction.EAST)
)));

CHANNEL.clientHandle().send(new NullablePacket(null, null));
CHANNEL.clientHandle().send(new NullablePacket("Weeee", null));
Expand Down
Loading