Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jun 25, 2024
1 parent 78fbd81 commit 28b26d1
Show file tree
Hide file tree
Showing 2 changed files with 38 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 @@ -78,7 +78,7 @@ public void onBlockEntityRequest(ServerPlayerEntity player, BlockPos pos)

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

public void onEntityRequest(ServerPlayerEntity player, int entityId)
Expand All @@ -87,6 +87,6 @@ public void onEntityRequest(ServerPlayerEntity player, int entityId)

Entity entity = player.getWorld().getEntityById(entityId);
NbtCompound nbt = entity != null ? entity.writeNbt(new NbtCompound()) : new NbtCompound();
HANDLER.encodeServerData(player, ServuxEntitiesPacket.EntityResponse(entityId, nbt));
HANDLER.encodeServerData(player, ServuxEntitiesPacket.SimpleEntityResponse(entityId, nbt));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,57 +23,51 @@ public class ServuxEntitiesPacket implements IServerPayloadData

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

// 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);
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;
}

public static ServuxEntitiesPacket EntityResponse(int entityId, NbtCompound nbt)
// 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;
packet.nbt = nbt.copy();
packet.entityId = entityId;
return packet;
}

public static ServuxEntitiesPacket BlockEntityResponse(BlockPos pos, NbtCompound nbt)
public static ServuxEntitiesPacket SimpleBlockResponse(BlockPos pos, NbtCompound nbt)
{
var packet = new ServuxEntitiesPacket(Type.PACKET_S2C_BLOCK_NBT_RESPONSE_SIMPLE);
packet.nbt = nbt;
packet.pos = pos;
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;
}

Expand Down Expand Up @@ -287,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 @@ -299,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 @@ -335,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 @@ -344,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

0 comments on commit 28b26d1

Please sign in to comment.