Skip to content

Commit

Permalink
redesign the PacketByteBuf read to be entirely in the Netty Loop
Browse files Browse the repository at this point in the history
(cherry picked from commit 957c9be)
  • Loading branch information
fzzyhmstrs committed Apr 22, 2024
1 parent 7bb4fbd commit d279116
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 21 deletions.
20 changes: 19 additions & 1 deletion src/main/java/com/github/nyuppo/MoreMobVariantsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,25 @@ public void onInitializeClient() {
}
});

// Client event to handle response from server about mob variant
// Client event to handle response from server about basic mob variants
ClientPlayNetworking.registerGlobalReceiver(MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, ((client, handler, buf, responseSender) -> {
int id = buf.readInt();
String variantId = buf.readString();
client.execute(() -> {
if (client.world != null) {
Entity entity = client.world.getEntityById(id);
if (entity != null) {
NbtCompound nbt = new NbtCompound();
entity.writeNbt(nbt);
nbt.putString(MoreMobVariants.NBT_KEY, variantId);
entity.readNbt(nbt);
}
}
});
}));


// Client event to handle response from server about complex mob variants
ClientPlayNetworking.registerGlobalReceiver(MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, ((client, handler, buf, responseSender) -> {
int id = buf.readInt();
String variantId = buf.readString();
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/com/github/nyuppo/mixin/CatVariantsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand Down Expand Up @@ -91,6 +91,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.NBT_KEY, variant.getIdentifier().toString());
child.readCustomDataFromNbt(childNbt);
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -81,4 +81,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.NBT_KEY, variant.getIdentifier().toString());
child.readCustomDataFromNbt(childNbt);
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/github/nyuppo/mixin/CowVariantsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -80,4 +80,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.NBT_KEY, variant.getIdentifier().toString());
child.readCustomDataFromNbt(childNbt);
}
}
}
6 changes: 4 additions & 2 deletions src/main/java/com/github/nyuppo/mixin/PigVariantsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
PacketByteBuf updateBuf = PacketByteBufs.create();
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());
//all three values in the "regular" packet post update
updateBuf.writeBoolean(isMuddy);
updateBuf.writeInt(muddyTimeLeft);
updateBuf.writeVarInt(muddyTimeLeft);
updateBuf.writeString("");

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
});
Expand Down Expand Up @@ -123,4 +125,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.NBT_KEY, variant.getIdentifier().toString());
child.readCustomDataFromNbt(childNbt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
PacketByteBuf updateBuf = PacketByteBufs.create();
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());
//all three values in the "regular" packet post update
updateBuf.writeBoolean(false);
updateBuf.writeVarInt(0);
updateBuf.writeString(hornColour);

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
Expand Down Expand Up @@ -112,4 +115,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.SHEEP_HORN_COLOUR_NBT_KEY, colour);
child.readCustomDataFromNbt(childNbt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -61,4 +61,4 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
protected void onInitialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, @Nullable NbtCompound entityNbt, CallbackInfoReturnable<EntityData> ci) {
variant = Variants.getRandomVariant(EntityType.SKELETON, world.getRandom().nextLong(), world.getBiome(((SkeletonEntity)(Object)this).getBlockPos()), null, world.getMoonSize());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -61,4 +61,4 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
protected void onInitialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, @Nullable NbtCompound entityNbt, CallbackInfoReturnable<EntityData> ci) {
variant = Variants.getRandomVariant(EntityType.SPIDER, world.getRandom().nextLong(), world.getBiome(((SpiderEntity)(Object)this).getBlockPos()), null, world.getMoonSize());
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/github/nyuppo/mixin/WolfVariantsMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -81,4 +81,4 @@ private void onCreateChild(ServerWorld world, PassiveEntity entity, CallbackInfo
childNbt.putString(MoreMobVariants.NBT_KEY, variant.getIdentifier().toString());
child.readCustomDataFromNbt(childNbt);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
updateBuf.writeInt(((Entity)(Object)this).getId());
updateBuf.writeString(variant.getIdentifier().toString());

ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, updateBuf);
ServerPlayNetworking.send(player, MMVNetworkingConstants.SERVER_RESPOND_BASIC_VARIANT_ID, updateBuf);
});
}
}
Expand All @@ -61,4 +61,4 @@ protected void onReadCustomDataFromNbt(NbtCompound nbt, CallbackInfo ci) {
protected void onInitialize(ServerWorldAccess world, LocalDifficulty difficulty, SpawnReason spawnReason, @Nullable EntityData entityData, @Nullable NbtCompound entityNbt, CallbackInfoReturnable<EntityData> ci) {
variant = Variants.getRandomVariant(EntityType.ZOMBIE, world.getRandom().nextLong(), world.getBiome(((ZombieEntity)(Object)this).getBlockPos()), null, world.getMoonSize());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public class MMVNetworkingConstants {
public static final Identifier CLIENT_REQUEST_VARIANT_ID = MoreMobVariants.id("client_request");
public static final Identifier SERVER_RESPOND_VARIANT_ID = MoreMobVariants.id("server_respond");
}
public static final Identifier SERVER_RESPOND_BASIC_VARIANT_ID = MoreMobVariants.id("server_respond_basic");
}

0 comments on commit d279116

Please sign in to comment.