Skip to content

Commit

Permalink
Fix PCASyncer
Browse files Browse the repository at this point in the history
Signed-off-by: Hendrix-Shen <[email protected]>
  • Loading branch information
Hendrix-Shen committed Jul 20, 2024
1 parent adf997b commit 6a1c277
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 147 deletions.

This file was deleted.

This file was deleted.

114 changes: 93 additions & 21 deletions src/main/java/com/plusls/MasaGadget/util/PcaSyncProtocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,18 @@ public class PcaSyncProtocol {

public static void init() {
//#if MC > 12004
//$$ PayloadTypeRegistry.playC2S().register(ServerboundCancelSyncBlockEntityPacket.TYPE, ServerboundCancelSyncBlockEntityPacket.CODEC);
//$$ PayloadTypeRegistry.playC2S().register(ServerboundCancelSyncEntityPacket.TYPE, ServerboundCancelSyncEntityPacket.CODEC);
//$$ PayloadTypeRegistry.playC2S().register(ServerboundSyncBlockEntityPacket.TYPE, ServerboundSyncBlockEntityPacket.CODEC);
//$$ PayloadTypeRegistry.playC2S().register(ServerboundSyncEntityPacket.TYPE, ServerboundSyncEntityPacket.CODEC);
//$$ PayloadTypeRegistry.playS2C().register(ClientboundDisablePcaSyncProtocolPacket.TYPE, ClientboundDisablePcaSyncProtocolPacket.CODEC);
//$$ PayloadTypeRegistry.playS2C().register(ClientboundEnablePcaSyncProtocolPacket.TYPE, ClientboundEnablePcaSyncProtocolPacket.CODEC);
//$$ PayloadTypeRegistry.playS2C().register(ClientboundUpdateBlockEntityPacket.TYPE, ClientboundUpdateBlockEntityPacket.CODEC);
//$$ PayloadTypeRegistry.playS2C().register(ClientboundUpdateEntityPacket.TYPE, ClientboundUpdateEntityPacket.CODEC);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundDisablePcaSyncProtocolPacket.TYPE, ClientboundDisablePcaSyncProtocolPacket::handle);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundEnablePcaSyncProtocolPacket.TYPE, ClientboundEnablePcaSyncProtocolPacket::handle);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundUpdateBlockEntityPacket.TYPE, ClientboundUpdateBlockEntityPacket::handle);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundUpdateEntityPacket.TYPE, ClientboundUpdateEntityPacket::handle);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundDisablePcaSyncProtocolPacket.TYPE, PcaSyncProtocol::disablePcaSyncProtocolHandler);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundEnablePcaSyncProtocolPacket.TYPE, PcaSyncProtocol::enablePcaSyncProtocolHandler);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundUpdateBlockEntityPacket.TYPE, PcaSyncProtocol::updateBlockEntityHandler);
//$$ ClientPlayNetworking.registerGlobalReceiver(ClientboundUpdateEntityPacket.TYPE, PcaSyncProtocol::updateEntityHandler);
//#else
ClientPlayNetworking.registerGlobalReceiver(ENABLE_PCA_SYNC_PROTOCOL, PcaSyncProtocol::enablePcaSyncProtocolHandler);
ClientPlayNetworking.registerGlobalReceiver(DISABLE_PCA_SYNC_PROTOCOL, PcaSyncProtocol::disablePcaSyncProtocolHandler);
Expand All @@ -94,25 +98,55 @@ private static void onDisconnect() {
enable = false;
}

public static void enablePcaSyncProtocolHandler(Minecraft client, ClientPacketListener handler,
FriendlyByteBuf buf, PacketSender responseSender) {
if (!client.hasSingleplayerServer()) {
private static void enablePcaSyncProtocolHandler(
//#if MC > 12004
//$$ ClientboundEnablePcaSyncProtocolPacket packet,
//$$ ClientPlayNetworking.Context context
//#else
Minecraft client,
ClientPacketListener handler,
FriendlyByteBuf buf,
PacketSender responseSender
//#endif
) {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
SharedConstants.getLogger().info("pcaSyncProtocol enable.");
enable = true;
}
}

public static void disablePcaSyncProtocolHandler(Minecraft client, ClientPacketListener handler,
FriendlyByteBuf buf, PacketSender responseSender) {
if (!client.hasSingleplayerServer()) {
public static void disablePcaSyncProtocolHandler(
//#if MC > 12004
//$$ ClientboundDisablePcaSyncProtocolPacket packet,
//$$ ClientPlayNetworking.Context context
//#else
Minecraft client,
ClientPacketListener handler,
FriendlyByteBuf buf,
PacketSender responseSender
//#endif
) {
if (!Minecraft.getInstance().hasSingleplayerServer()) {
SharedConstants.getLogger().info("pcaSyncProtocol disable.");
enable = false;
}
}

// 反序列化实体数据
public static void updateEntityHandler(Minecraft client, ClientPacketListener handler,
FriendlyByteBuf buf, PacketSender responseSender) {
public static void updateEntityHandler(
//#if MC > 12004
//$$ ClientboundUpdateEntityPacket packet,
//$$ ClientPlayNetworking.Context context
//#else
Minecraft client,
ClientPacketListener handler,
FriendlyByteBuf buf,
PacketSender responseSender
//#endif
) {
//#if MC > 12004
//$$ Minecraft client = context.client();
//#endif
LocalPlayer player = client.player;

if (player == null) {
Expand All @@ -123,12 +157,23 @@ public static void updateEntityHandler(Minecraft client, ClientPacketListener ha
LevelCompat levelCompat = playerCompat.getLevel();
Level level = levelCompat.get();

if (!levelCompat.getDimensionLocation().equals(buf.readResourceLocation())) {
if (!levelCompat.getDimensionLocation().equals(
//#if MC > 12004
//$$ packet.dimension()
//#else
buf.readResourceLocation()
//#endif
)) {
return;
}

//#if MC > 12004
//$$ int entityId = packet.entityId();
//$$ CompoundTag tag = packet.tag();
//#else
int entityId = buf.readInt();
CompoundTag tag = NetworkUtil.readNbt(buf);
//#endif
Entity entity = level.getEntity(entityId);

if (entity != null) {
Expand Down Expand Up @@ -166,7 +211,7 @@ public static void updateEntityHandler(Minecraft client, ClientPacketListener ha
//$$ if (tag.contains("Offers")) {
//$$ MerchantOffers.CODEC
//$$ .parse(client.level.registryAccess().createSerializationContext(NbtOps.INSTANCE),
//$$ tag.getCompound("Offers").get("Offers"))
//$$ tag.get("Offers"))
//$$ .resultOrPartial(Util.prefix("Failed to load offers: ", MagicLib.getLogger()::warn))
//$$ .ifPresent(merchantOffers -> ((AccessorAbstractVillager) entity).setOffers(merchantOffers));
//$$ }
Expand Down Expand Up @@ -209,8 +254,20 @@ public static void updateEntityHandler(Minecraft client, ClientPacketListener ha
}

// 反序列化 blockEntity 数据
public static void updateBlockEntityHandler(Minecraft client, ClientPacketListener handler,
FriendlyByteBuf buf, PacketSender responseSender) {
public static void updateBlockEntityHandler(
//#if MC > 12004
//$$ ClientboundUpdateBlockEntityPacket packet,
//$$ ClientPlayNetworking.Context context
//#else
Minecraft client,
ClientPacketListener handler,
FriendlyByteBuf buf,
PacketSender responseSender
//#endif
) {
//#if MC > 12004
//$$ Minecraft client = context.client();
//#endif
LocalPlayer player = client.player;

if (player == null) {
Expand All @@ -220,12 +277,23 @@ public static void updateBlockEntityHandler(Minecraft client, ClientPacketListen
LevelCompat levelCompat = PlayerCompat.of(player).getLevel();
Level level = levelCompat.get();

if (!levelCompat.getDimensionLocation().equals(buf.readResourceLocation())) {
if (!levelCompat.getDimensionLocation().equals(
//#if MC > 12004
//$$ packet.dimension()
//#else
buf.readResourceLocation()
//#endif
)) {
return;
}

//#if MC > 12004
//$$ BlockPos pos = packet.blockPos();
//$$ CompoundTag tag = packet.tag();
//#else
BlockPos pos = buf.readBlockPos();
CompoundTag tag = buf.readNbt();
//#endif
BlockEntity blockEntity = level.getBlockEntity(pos);

if (Configs.saveInventoryToSchematicInServer.getBooleanValue() && pos.equals(PcaSyncUtil.lastUpdatePos)) {
Expand All @@ -252,11 +320,13 @@ public static void syncBlockEntity(BlockPos pos) {
SharedConstants.getLogger().debug("syncBlockEntity: {}", pos);
lastBlockPos = pos;
lastEntityId = -1;
//#if MC < 12005
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeBlockPos(pos);
//#endif
ClientPlayNetworking.send(
//#if MC > 12004
//$$ new ServerboundSyncBlockEntityPacket(buf)
//$$ new ServerboundSyncBlockEntityPacket(pos)
//#else
SYNC_BLOCK_ENTITY,
buf
Expand All @@ -272,11 +342,13 @@ public static void syncEntity(int entityId) {
SharedConstants.getLogger().debug("syncEntity: {}", entityId);
lastEntityId = entityId;
lastBlockPos = null;
//#if MC < 12005
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
buf.writeInt(entityId);
//#endif
ClientPlayNetworking.send(
//#if MC > 12004
//$$ new ServerboundSyncEntityPacket(buf)
//$$ new ServerboundSyncEntityPacket(entityId)
//#else
SYNC_ENTITY,
buf
Expand All @@ -294,7 +366,7 @@ public static void cancelSyncBlockEntity() {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
ClientPlayNetworking.send(
//#if MC > 12004
//$$ new ServerboundCancelSyncBlockEntityPacket(buf)
//$$ new ServerboundCancelSyncBlockEntityPacket()
//#else
CANCEL_SYNC_REQUEST_BLOCK_ENTITY,
buf
Expand All @@ -312,7 +384,7 @@ public static void cancelSyncEntity() {
FriendlyByteBuf buf = new FriendlyByteBuf(Unpooled.buffer());
ClientPlayNetworking.send(
//#if MC > 12004
//$$ new ServerboundCancelSyncEntityPacket(buf)
//$$ new ServerboundCancelSyncEntityPacket()
//#else
CANCEL_SYNC_ENTITY,
buf
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package com.plusls.MasaGadget.impl.network.packet;

import com.plusls.MasaGadget.api.network.packet.PCAPacket;
import com.plusls.MasaGadget.impl.network.NoopCodec;
import com.plusls.MasaGadget.util.PcaSyncProtocol;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

public class ClientboundDisablePcaSyncProtocolPacket extends PCAPacket {
public static final Type<PCAPacket> TYPE = new Type<>(PcaSyncProtocol.DISABLE_PCA_SYNC_PROTOCOL);
public static final StreamCodec<FriendlyByteBuf, PCAPacket> CODEC = NoopCodec.create(ClientboundDisablePcaSyncProtocolPacket.transform());
public record ClientboundDisablePcaSyncProtocolPacket() implements CustomPacketPayload {
public static final Type<ClientboundDisablePcaSyncProtocolPacket> TYPE = new Type<>(PcaSyncProtocol.DISABLE_PCA_SYNC_PROTOCOL);
public static final StreamCodec<FriendlyByteBuf, ClientboundDisablePcaSyncProtocolPacket> CODEC = CustomPacketPayload.codec(ClientboundDisablePcaSyncProtocolPacket::write, ClientboundDisablePcaSyncProtocolPacket::new);

public ClientboundDisablePcaSyncProtocolPacket(FriendlyByteBuf byteBuf) {
super(byteBuf);
}

public static void handle(PCAPacket packet, ClientPlayNetworking.Context context) {
PcaSyncProtocol.disablePcaSyncProtocolHandler(context.client(), context.client().getConnection(), packet.getByteBuf(), context.responseSender());
}

private static Function<FriendlyByteBuf, PCAPacket> transform() {
return ClientboundDisablePcaSyncProtocolPacket::new;
this();
}

@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return ClientboundDisablePcaSyncProtocolPacket.TYPE;
}

private void write(FriendlyByteBuf byteBuf) {
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,24 @@
package com.plusls.MasaGadget.impl.network.packet;

import com.plusls.MasaGadget.api.network.packet.PCAPacket;
import com.plusls.MasaGadget.impl.network.NoopCodec;
import com.plusls.MasaGadget.util.PcaSyncProtocol;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;

public class ClientboundEnablePcaSyncProtocolPacket extends PCAPacket {
public static final Type<PCAPacket> TYPE = new Type<>(PcaSyncProtocol.ENABLE_PCA_SYNC_PROTOCOL);
public static final StreamCodec<FriendlyByteBuf, PCAPacket> CODEC = NoopCodec.create(ClientboundEnablePcaSyncProtocolPacket.transform());
public record ClientboundEnablePcaSyncProtocolPacket() implements CustomPacketPayload {
public static final Type<ClientboundEnablePcaSyncProtocolPacket> TYPE = new Type<>(PcaSyncProtocol.ENABLE_PCA_SYNC_PROTOCOL);
public static final StreamCodec<FriendlyByteBuf, ClientboundEnablePcaSyncProtocolPacket> CODEC = CustomPacketPayload.codec(ClientboundEnablePcaSyncProtocolPacket::write, ClientboundEnablePcaSyncProtocolPacket::new);

public ClientboundEnablePcaSyncProtocolPacket(FriendlyByteBuf byteBuf) {
super(byteBuf);
}

public static void handle(PCAPacket packet, ClientPlayNetworking.Context context) {
PcaSyncProtocol.enablePcaSyncProtocolHandler(context.client(), context.client().getConnection(), packet.getByteBuf(), context.responseSender());
}

private static Function<FriendlyByteBuf, PCAPacket> transform() {
return ClientboundEnablePcaSyncProtocolPacket::new;
this();
}

@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return ClientboundEnablePcaSyncProtocolPacket.TYPE;
}

private void write(FriendlyByteBuf byteBuf) {
}
}
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
package com.plusls.MasaGadget.impl.network.packet;

import com.plusls.MasaGadget.api.network.packet.PCAPacket;
import com.plusls.MasaGadget.impl.network.NoopCodec;
import com.plusls.MasaGadget.util.PcaSyncProtocol;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking;
import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
import net.minecraft.resources.ResourceLocation;
import org.jetbrains.annotations.NotNull;
import top.hendrixshen.magiclib.util.minecraft.NetworkUtil;

import java.util.function.Function;
public record ClientboundUpdateBlockEntityPacket(ResourceLocation dimension, BlockPos blockPos, CompoundTag tag) implements CustomPacketPayload {
public static final Type<ClientboundUpdateBlockEntityPacket> TYPE = new Type<>(PcaSyncProtocol.UPDATE_BLOCK_ENTITY);
public static final StreamCodec<FriendlyByteBuf, ClientboundUpdateBlockEntityPacket> CODEC = CustomPacketPayload.codec(ClientboundUpdateBlockEntityPacket::write, ClientboundUpdateBlockEntityPacket::new);

public class ClientboundUpdateBlockEntityPacket extends PCAPacket {
public static final Type<PCAPacket> TYPE = new Type<>(PcaSyncProtocol.UPDATE_BLOCK_ENTITY);
public static final StreamCodec<FriendlyByteBuf, PCAPacket> CODEC = NoopCodec.create(ClientboundUpdateBlockEntityPacket.transform());

public ClientboundUpdateBlockEntityPacket(FriendlyByteBuf byteBuf) {
super(byteBuf);
}

public static void handle(PCAPacket packet, ClientPlayNetworking.Context context) {
PcaSyncProtocol.updateBlockEntityHandler(context.client(), context.client().getConnection(), packet.getByteBuf(), context.responseSender());
}

private static Function<FriendlyByteBuf, PCAPacket> transform() {
return ClientboundUpdateBlockEntityPacket::new;
public ClientboundUpdateBlockEntityPacket(@NotNull FriendlyByteBuf byteBuf) {
this(byteBuf.readResourceLocation(), byteBuf.readBlockPos(), NetworkUtil.readNbt(byteBuf));
}

@Override
public @NotNull Type<? extends CustomPacketPayload> type() {
return ClientboundUpdateBlockEntityPacket.TYPE;
}

private void write(@NotNull FriendlyByteBuf byteBuf) {
byteBuf.writeResourceLocation(this.dimension);
byteBuf.writeBlockPos(this.blockPos);
byteBuf.writeNbt(this.tag);
}
}
Loading

0 comments on commit 6a1c277

Please sign in to comment.