Skip to content

Commit

Permalink
add NETWORK serialization attribute and make MinecraftEndecs#ofRegist…
Browse files Browse the repository at this point in the history
…ry use it
  • Loading branch information
BasiqueEvangelist committed Aug 27, 2024
1 parent 68b0aa6 commit b077211
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 4 deletions.
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,19 @@
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.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 @@ -105,11 +110,37 @@ 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);

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,11 @@
package io.wispforest.uwu.network;

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

public record DispatchedSubclassThree(Item item, Block block) implements DispatchedInterface {
@Override
public String getName() {
return "three";
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
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.client.option.KeyBinding;
import net.minecraft.item.Items;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import org.lwjgl.glfw.GLFW;
Expand All @@ -28,6 +29,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 +69,7 @@ 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)));

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

0 comments on commit b077211

Please sign in to comment.