Skip to content

Commit

Permalink
Merge pull request #32 from fzzyhmstrs/1.3.0+1.20.1
Browse files Browse the repository at this point in the history
(1.20.1) Put Networking impls back onto-thread
  • Loading branch information
nyuppo committed Apr 23, 2024
2 parents 54c774a + d279116 commit 66f4608
Show file tree
Hide file tree
Showing 12 changed files with 127 additions and 91 deletions.
82 changes: 46 additions & 36 deletions src/main/java/com/github/nyuppo/MoreMobVariants.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,50 +70,60 @@ public void onInitialize() {
// Server event to respond to client request for a variant
ServerPlayNetworking.registerGlobalReceiver(MMVNetworkingConstants.CLIENT_REQUEST_VARIANT_ID, ((server, player, handler, buf, responseSender) -> {
UUID uuid = buf.readUuid();
Entity entity = server.getOverworld().getEntity(uuid);

// If we couldn't find the mob in the overworld, start checking all other worlds
if (entity == null) {
for (ServerWorld serverWorld : server.getWorlds()) {
Entity entity2 = serverWorld.getEntity(uuid);
if (entity2 != null) {
entity = entity2;
server.execute( () -> {
Entity entity = server.getOverworld().getEntity(uuid);

// If we couldn't find the mob in the overworld, start checking all other worlds
if (entity == null) {
for (ServerWorld serverWorld : server.getWorlds()) {
Entity entity2 = serverWorld.getEntity(uuid);
if (entity2 != null) {
entity = entity2;
}
}
}
}

if (entity != null) {
NbtCompound nbt = new NbtCompound();
entity.writeNbt(nbt);

if (nbt.contains(NBT_KEY)) {
PacketByteBuf responseBuf = PacketByteBufs.create();
responseBuf.writeInt(entity.getId());
responseBuf.writeString(nbt.getString(NBT_KEY));

// For some reason, "Sitting" syncing breaks, so send that too I guess
if (entity instanceof TameableEntity) {
responseBuf.writeBoolean(nbt.getBoolean("Sitting"));
}

// Muddy pigs
if (entity instanceof PigEntity) {
responseBuf.writeBoolean(nbt.getBoolean(MUDDY_NBT_KEY));
responseBuf.writeInt(nbt.getInt(MUDDY_TIMEOUT_NBT_KEY));
if (entity != null) {
NbtCompound nbt = new NbtCompound();
entity.writeNbt(nbt);

if (nbt.contains(NBT_KEY)) {
PacketByteBuf responseBuf = PacketByteBufs.create();
responseBuf.writeInt(entity.getId());
responseBuf.writeString(nbt.getString(NBT_KEY));

//going to pass all three of these regardless, so buf structure is constant. More cases can be added and hook into these as needed.
boolean bl = false;
int i = 0;
String str = "";

// For some reason, "Sitting" syncing breaks, so send that too I guess
if (entity instanceof TameableEntity) {
bl = nbt.getBoolean("Sitting");
}

// Muddy pigs
if (entity instanceof PigEntity) {
bl = nbt.getBoolean(MUDDY_NBT_KEY);
i = nbt.getInt(MUDDY_TIMEOUT_NBT_KEY);
}

// Sheep horns
if (entity instanceof SheepEntity) {
str = nbt.getString(SHEEP_HORN_COLOUR_NBT_KEY);
}
responseBuf.writeBoolean(bl);
responseBuf.writeVarInt(i);
responseBuf.writeString(str);

ServerPlayNetworking.send(handler.getPlayer(), MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, responseBuf);
}

// Sheep horns
if (entity instanceof SheepEntity) {
responseBuf.writeString(nbt.getString(SHEEP_HORN_COLOUR_NBT_KEY));
}

ServerPlayNetworking.send(handler.getPlayer(), MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, responseBuf);
}
}
});
}));
}

public static Identifier id(String path) {
return new Identifier(MOD_ID, path);
}
}
}
92 changes: 57 additions & 35 deletions src/main/java/com/github/nyuppo/MoreMobVariantsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,48 +54,70 @@ public void onInitializeClient() {
}
});

// Client event to handle response from server about mob variant
ClientPlayNetworking.registerGlobalReceiver(MMVNetworkingConstants.SERVER_RESPOND_VARIANT_ID, ((client, handler, buf, responseSender) -> {
// 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();

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

// For some reason, "Sitting" syncing breaks, so get that too I guess
if (entity instanceof TameableEntity) {
nbt.putBoolean("Sitting", buf.readBoolean());
}

// Muddy pigs
boolean isMuddy;
int muddyTimeLeft;
if (entity instanceof PigEntity) {
isMuddy = buf.readBoolean();
muddyTimeLeft = buf.readInt();

nbt.putBoolean(MoreMobVariants.MUDDY_NBT_KEY, isMuddy);
nbt.putInt(MoreMobVariants.MUDDY_TIMEOUT_NBT_KEY, muddyTimeLeft);
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);
}
}
});
}));

// Sheep horns
String hornColour;
if (entity instanceof SheepEntity) {
hornColour = buf.readString();

nbt.putString(MoreMobVariants.SHEEP_HORN_COLOUR_NBT_KEY, hornColour);
// 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();
//read all three buffer values regardless, in the Netty loop. use as needed in the client.
boolean bl = buf.readBoolean();
int i = buf.readVarInt();
String str = 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);

// For some reason, "Sitting" syncing breaks, so get that too I guess
if (entity instanceof TameableEntity) {
nbt.putBoolean("Sitting",bl);
}

// Muddy pigs
boolean isMuddy;
int muddyTimeLeft;
if (entity instanceof PigEntity) {
isMuddy = bl;
muddyTimeLeft = i;

nbt.putBoolean(MoreMobVariants.MUDDY_NBT_KEY, isMuddy);
nbt.putInt(MoreMobVariants.MUDDY_TIMEOUT_NBT_KEY, muddyTimeLeft);
}

// Sheep horns
String hornColour;
if (entity instanceof SheepEntity) {
hornColour = str;

nbt.putString(MoreMobVariants.SHEEP_HORN_COLOUR_NBT_KEY, hornColour);
}

entity.readNbt(nbt);
}

entity.readNbt(nbt);
}

}
});
}));
}

}
}
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 66f4608

Please sign in to comment.