Skip to content

Commit

Permalink
do not use transaction id
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jun 25, 2024
1 parent 31c517e commit cd40941
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.BlockEntityResponse(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.EntityResponse(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,6 +21,10 @@ public class ServuxEntitiesPacket implements IServerPayloadData
private PacketByteBuf buffer;
public static final int PROTOCOL_VERSION = 1;

private ServuxEntitiesPacket(Type type) {
this.packetType = type;
}

// Metadata/Request Packet
public ServuxEntitiesPacket(NbtCompound nbt, boolean request)
{
Expand Down Expand Up @@ -57,6 +61,22 @@ public ServuxEntitiesPacket(int transactionId, int entityId)
this.clearPacket();
}

public static ServuxEntitiesPacket EntityResponse(int entityId, NbtCompound nbt)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_S2C_ENTITY_NBT_RESPONSE_SIMPLE);
packet.nbt = nbt;
packet.entityId = entityId;
return packet;
}

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

// Response Nbt Packet, set splitter to true to use Packet Splitter
public ServuxEntitiesPacket(int transactionId, NbtCompound nbt, boolean splitter)
{
Expand Down Expand Up @@ -351,7 +371,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 cd40941

Please sign in to comment.