Skip to content

Commit

Permalink
Merge pull request #1 from zly2006/fabric_1.21-block_entities-patch-1
Browse files Browse the repository at this point in the history
block entities sync
  • Loading branch information
sakura-ryoko authored Jun 25, 2024
2 parents 31c517e + 28b26d1 commit d514305
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ public void sendMetadata(ServerPlayerEntity player)
// Sends Metadata handshake, it doesn't succeed the first time, so using networkHandler
if (player.networkHandler != null)
{
HANDLER.sendPlayPayload(player.networkHandler, new ServuxEntitiesPacket.Payload(new ServuxEntitiesPacket(this.metadata, false)));
HANDLER.sendPlayPayload(player.networkHandler, new ServuxEntitiesPacket.Payload(ServuxEntitiesPacket.MetadataResponse(this.metadata)));
}
else
{
HANDLER.sendPlayPayload(player, new ServuxEntitiesPacket.Payload(new ServuxEntitiesPacket(this.metadata, false)));
HANDLER.sendPlayPayload(player, new ServuxEntitiesPacket.Payload(ServuxEntitiesPacket.MetadataResponse(this.metadata)));
}
}

Expand All @@ -72,21 +72,21 @@ public void onPacketFailure(ServerPlayerEntity player)
// Do something when packets fail, if required
}

public void onBlockEntityRequest(ServerPlayerEntity player, int transactionId, BlockPos pos)
public void onBlockEntityRequest(ServerPlayerEntity player, BlockPos pos)
{
Servux.logger.warn("onBlockEntityRequest(): from player {} // transactionId {}", player.getName().getLiteralString(), transactionId);
Servux.logger.warn("onBlockEntityRequest(): from player {}", player.getName().getLiteralString());

BlockEntity be = player.getEntityWorld().getBlockEntity(pos);
NbtCompound nbt = be != null ? be.createNbt(player.getRegistryManager()) : new NbtCompound();
HANDLER.encodeServerData(player, new ServuxEntitiesPacket(transactionId, nbt, true));
HANDLER.encodeServerData(player, ServuxEntitiesPacket.SimpleBlockResponse(pos, nbt));
}

public void onEntityRequest(ServerPlayerEntity player, int transactionId, int entityId)
public void onEntityRequest(ServerPlayerEntity player, int entityId)
{
Servux.logger.warn("onEntityRequest(): from player {} // transactionId {}", player.getName().getLiteralString(), transactionId);
Servux.logger.warn("onEntityRequest(): from player {}", player.getName().getLiteralString());

Entity entity = player.getWorld().getEntityById(entityId);
NbtCompound nbt = entity != null ? entity.writeNbt(new NbtCompound()) : new NbtCompound();
HANDLER.encodeServerData(player, new ServuxEntitiesPacket(transactionId, nbt, true));
HANDLER.encodeServerData(player, ServuxEntitiesPacket.SimpleEntityResponse(entityId, nbt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public <P extends IServerPayloadData> void decodeServerData(Identifier channel,
case PACKET_C2S_BLOCK_ENTITY_REQUEST ->
{
Servux.logger.warn("ServuxEntitiesHandler#decodeServerData(): received Block Entity Request from player {}", player.getName().getLiteralString());
EntitiesDataProvider.INSTANCE.onBlockEntityRequest(player, packet.getTransactionId(), packet.getPos());
EntitiesDataProvider.INSTANCE.onBlockEntityRequest(player, packet.getPos());
}
case PACKET_C2S_ENTITY_REQUEST ->
{
Servux.logger.warn("ServuxEntitiesHandler#decodeServerData(): received Entity Request from player {}", player.getName().getLiteralString());
EntitiesDataProvider.INSTANCE.onEntityRequest(player, packet.getTransactionId(), packet.getEntityId());
EntitiesDataProvider.INSTANCE.onEntityRequest(player, packet.getEntityId());
}
default -> Servux.logger.warn("decodeServerData(): Invalid packetType '{}' from player: {}, of size in bytes: {}.", packet.getPacketType(), player.getName().getLiteralString(), packet.getTotalSize());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,40 +21,54 @@ public class ServuxEntitiesPacket implements IServerPayloadData
private PacketByteBuf buffer;
public static final int PROTOCOL_VERSION = 1;

// Metadata/Request Packet
public ServuxEntitiesPacket(NbtCompound nbt, boolean request)
{
if (request)
{
this.packetType = Type.PACKET_C2S_METADATA_REQUEST;
}
else
{
this.packetType = Type.PACKET_S2C_METADATA;
}
this.nbt = new NbtCompound();
this.nbt.copyFrom(nbt);
private ServuxEntitiesPacket(Type type) {
this.packetType = type;
this.clearPacket();
}

// Block Entity Query
public ServuxEntitiesPacket(int transactionId, BlockPos pos)
public static ServuxEntitiesPacket MetadataRequest(NbtCompound nbt)
{
this.packetType = Type.PACKET_C2S_BLOCK_ENTITY_REQUEST;
this.transactionId = transactionId;
this.pos = pos;
this.nbt = new NbtCompound();
this.clearPacket();
var packet = new ServuxEntitiesPacket(Type.PACKET_C2S_METADATA_REQUEST);
packet.nbt = nbt.copy();
return packet;
}

// Entity Query
public ServuxEntitiesPacket(int transactionId, int entityId)
public static ServuxEntitiesPacket MetadataResponse(NbtCompound nbt)
{
this.packetType = Type.PACKET_C2S_ENTITY_REQUEST;
this.transactionId = transactionId;
this.entityId = entityId;
this.nbt = new NbtCompound();
this.clearPacket();
var packet = new ServuxEntitiesPacket(Type.PACKET_S2C_METADATA);
packet.nbt = nbt.copy();
return packet;
}

// Entity simple response
public static ServuxEntitiesPacket SimpleEntityResponse(int entityId, NbtCompound nbt)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_S2C_ENTITY_NBT_RESPONSE_SIMPLE);
packet.nbt = nbt.copy();
packet.entityId = entityId;
return packet;
}

public static ServuxEntitiesPacket SimpleBlockResponse(BlockPos pos, NbtCompound nbt)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE);
packet.nbt = nbt.copy();
packet.pos = pos.toImmutable();
return packet;
}

public static ServuxEntitiesPacket BlockEntityRequest(BlockPos pos)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_C2S_BLOCK_ENTITY_REQUEST);
packet.pos = pos.toImmutable();
return packet;
}

public static ServuxEntitiesPacket EntityRequest(int entityId)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_C2S_ENTITY_REQUEST);
packet.entityId = entityId;
return packet;
}

// Response Nbt Packet, set splitter to true to use Packet Splitter
Expand Down Expand Up @@ -209,6 +223,30 @@ public void toPacket(PacketByteBuf output)
Servux.logger.error("ServuxEntitiesPacket#toPacket: error writing buffer data to packet: [{}]", e.getLocalizedMessage());
}
}
case PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE ->
{
try
{
output.writeBlockPos(this.pos);
output.writeNbt(this.nbt);
}
catch (Exception e)
{
Servux.logger.error("ServuxEntitiesPacket#toPacket: error writing Block Entity Response to packet: [{}]", e.getLocalizedMessage());
}
}
case PACKET_S2C_ENTITY_NBT_RESPONSE_SIMPLE ->
{
try
{
output.writeVarInt(this.entityId);
output.writeNbt(this.nbt);
}
catch (Exception e)
{
Servux.logger.error("ServuxEntitiesPacket#toPacket: error writing Entity Response to packet: [{}]", e.getLocalizedMessage());
}
}
default ->
{
// Write NBT
Expand Down Expand Up @@ -243,7 +281,8 @@ public static ServuxEntitiesPacket fromPacket(PacketByteBuf input)
// Read Packet Buffer
try
{
return new ServuxEntitiesPacket(input.readVarInt(), input.readBlockPos());
input.readVarInt(); // todo: old code compat
return ServuxEntitiesPacket.BlockEntityRequest(input.readBlockPos());
}
catch (Exception e)
{
Expand All @@ -255,7 +294,8 @@ public static ServuxEntitiesPacket fromPacket(PacketByteBuf input)
// Read Packet Buffer
try
{
return new ServuxEntitiesPacket(input.readVarInt(), input.readVarInt());
input.readVarInt(); // todo: old code compat
return ServuxEntitiesPacket.EntityRequest(input.readVarInt());
}
catch (Exception e)
{
Expand Down Expand Up @@ -291,7 +331,7 @@ public static ServuxEntitiesPacket fromPacket(PacketByteBuf input)
// Read Nbt
try
{
return new ServuxEntitiesPacket(input.readNbt(), true);
return ServuxEntitiesPacket.MetadataRequest(input.readNbt());
}
catch (Exception e)
{
Expand All @@ -300,15 +340,7 @@ public static ServuxEntitiesPacket fromPacket(PacketByteBuf input)
}
default ->
{
// Read Nbt
try
{
return new ServuxEntitiesPacket(input.readNbt(), false);
}
catch (Exception e)
{
Servux.logger.error("ServuxEntitiesPacket#fromPacket: error reading NBT from packet: [{}]", e.getLocalizedMessage());
}
Servux.logger.error("ServuxEntitiesPacket#fromPacket: Unknown packet type!");
}
}

Expand Down Expand Up @@ -351,7 +383,9 @@ public enum Type
PACKET_C2S_ENTITY_REQUEST(4),
PACKET_S2C_NBT_RESPONSE_START(5),
PACKET_S2C_NBT_RESPONSE_SIMPLE(6),
PACKET_S2C_NBT_RESPONSE_DATA(7);
PACKET_S2C_NBT_RESPONSE_DATA(7),
PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE(8),
PACKET_S2C_ENTITY_NBT_RESPONSE_SIMPLE(9);

private final int type;

Expand Down

0 comments on commit d514305

Please sign in to comment.