Skip to content

Commit

Permalink
Make AutoSyncedComponent use RegistryByteBuf
Browse files Browse the repository at this point in the history
  • Loading branch information
Pyrofab committed Apr 14, 2024
1 parent b989ccd commit 8f87e30
Show file tree
Hide file tree
Showing 9 changed files with 22 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import com.demonwav.mcdev.annotations.Env;
import net.minecraft.entity.Entity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.world.World;
import org.jetbrains.annotations.Contract;
Expand Down Expand Up @@ -75,11 +75,11 @@ default boolean shouldSyncWith(ServerPlayerEntity player) {
* nearly always provide a better implementation.
* @see ComponentKey#sync(Object)
* @see ComponentKey#sync(Object, ComponentPacketWriter)
* @see #applySyncPacket(PacketByteBuf)
* @see #applySyncPacket(RegistryByteBuf)
*/
@Contract(mutates = "param1")
@Override
default void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
default void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) {
NbtCompound tag = new NbtCompound();
this.writeToNbt(tag);
buf.writeNbt(tag);
Expand All @@ -90,12 +90,12 @@ default void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
*
* @implSpec The default implementation converts the buffer's content
* to a {@link NbtCompound} and calls {@link #readFromNbt(NbtCompound)}.
* @implNote any implementing class overriding {@link #writeSyncPacket(PacketByteBuf, ServerPlayerEntity)}
* @implNote any implementing class overriding {@link #writeSyncPacket(RegistryByteBuf, ServerPlayerEntity)}
* such that it uses a different data format must override this method.
* @see #writeSyncPacket(PacketByteBuf, ServerPlayerEntity)
* @see #writeSyncPacket(RegistryByteBuf, ServerPlayerEntity)
*/
@CheckEnv(Env.CLIENT)
default void applySyncPacket(PacketByteBuf buf) {
default void applySyncPacket(RegistryByteBuf buf) {
NbtCompound tag = buf.readNbt();
if (tag != null) {
this.readFromNbt(tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@
*/
package org.ladysnake.cca.api.v3.component.sync;

import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.Contract;

@FunctionalInterface
public interface ComponentPacketWriter {
@Contract(mutates = "param1")
void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient);
void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import net.fabricmc.fabric.api.event.lifecycle.v1.ServerBlockEntityEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import org.ladysnake.cca.api.v3.block.BlockEntitySyncAroundCallback;
Expand All @@ -39,7 +38,7 @@ public class CardinalComponentsBlock {
/**
* {@link CustomPayloadS2CPacket} channel for default block entity component synchronization.
*
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(PacketByteBuf)}
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(net.minecraft.network.RegistryByteBuf)}
* called on the game thread.
*/
public static final CustomPayload.Id<ComponentUpdatePayload<BlockEntityAddress>> PACKET_ID = CustomPayload.id("cardinal-components:block_entity_sync");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.util.Identifier;
import net.minecraft.util.Util;
Expand Down Expand Up @@ -75,7 +75,7 @@ public boolean shouldSyncWith(ServerPlayerEntity player) {
}

@Override
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
public void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) {
for (SyncedVita value : this.storage.values()) {
if (value.shouldSyncWith(recipient)) {
value.writeSyncPacket(buf, recipient);
Expand All @@ -84,7 +84,7 @@ public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
}

@Override
public void applySyncPacket(PacketByteBuf buf) {
public void applySyncPacket(RegistryByteBuf buf) {
for (SyncedVita value : this.storage.values()) {
value.applySyncPacket(buf);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerEntityEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.entity.Entity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
Expand All @@ -47,7 +46,7 @@ public final class CardinalComponentsEntity {
/**
* {@link CustomPayloadS2CPacket} channel for default entity component synchronization.
*
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(PacketByteBuf)}
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(net.minecraft.network.RegistryByteBuf)}
* called on the game thread.
*/
public static final CustomPayload.Id<ComponentUpdatePayload<Integer>> PACKET_ID = CustomPayload.id("cardinal-components:entity_sync");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.particle.ParticleTypes;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -69,7 +70,7 @@ public boolean shouldSyncWith(ServerPlayerEntity player) {
}

@Override
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient) {
public void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity recipient) {
this.writeSyncPacket(buf, recipient, 0);
}

Expand All @@ -83,7 +84,7 @@ private void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity recipient, in
}

@Override
public void applySyncPacket(PacketByteBuf buf) {
public void applySyncPacket(RegistryByteBuf buf) {
int flags = buf.readByte();
if ((flags & 1) != 0) {
this.vitality = buf.readVarInt();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@

import com.mojang.datafixers.util.Unit;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
import net.minecraft.world.WorldProperties;
Expand All @@ -39,7 +38,7 @@ public final class CardinalComponentsLevel {
/**
* {@link CustomPayloadS2CPacket} channel for default level component synchronization.
*
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(PacketByteBuf)}
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(net.minecraft.network.RegistryByteBuf)}
* called on the game thread.
*/
public static final CustomPayload.Id<ComponentUpdatePayload<Unit>> PACKET_ID = CustomPayload.id("cardinal-components:level_sync");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.mojang.datafixers.util.Unit;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.network.packet.CustomPayload;
import net.minecraft.network.packet.s2c.common.CustomPayloadS2CPacket;
Expand All @@ -42,14 +41,14 @@ public final class CardinalComponentsScoreboard {
/**
* {@link CustomPayloadS2CPacket} channel for default scoreboard component synchronization.
*
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(PacketByteBuf)}
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(net.minecraft.network.RegistryByteBuf)}
* called on the game thread.
*/
public static final CustomPayload.Id<ComponentUpdatePayload<Unit>> SCOREBOARD_PACKET_ID = CustomPayload.id("cardinal-components:scoreboard_sync");
/**
* {@link CustomPayloadS2CPacket} channel for default team component synchronization.
*
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(PacketByteBuf)}
* <p> Components synchronized through this channel will have {@linkplain AutoSyncedComponent#applySyncPacket(net.minecraft.network.RegistryByteBuf)}
* called on the game thread.
*/
public static final CustomPayload.Id<ComponentUpdatePayload<String>> TEAM_PACKET_ID = CustomPayload.id("cardinal-components:team_sync");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.hud.InGameHud;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
Expand All @@ -42,7 +42,7 @@ public abstract class AmbientVita extends BaseVita implements AutoSyncedComponen
public abstract void syncWithAll(MinecraftServer server);

@Override
public void applySyncPacket(PacketByteBuf buf) {
public void applySyncPacket(RegistryByteBuf buf) {
int vita = buf.readInt();
this.setVitality(vita);
World world = Objects.requireNonNull(MinecraftClient.getInstance().player).getWorld();
Expand All @@ -65,7 +65,7 @@ public void applySyncPacket(PacketByteBuf buf) {
* proper implementation of {@code writeToPacket}, writes a single int instead of a whole tag
*/
@Override
public void writeSyncPacket(PacketByteBuf buf, ServerPlayerEntity player) {
public void writeSyncPacket(RegistryByteBuf buf, ServerPlayerEntity player) {
buf.writeInt(this.getVitality());
}

Expand Down

0 comments on commit 8f87e30

Please sign in to comment.