Skip to content

Commit

Permalink
Disable be sync opto
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jun 26, 2024
1 parent 1f60ccd commit e048e9e
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 82 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ yarn_mappings=1.21+build.2
loader_version=0.15.11

# Mod Properties
mod_version=0.5.1
mod_version=0.5.2
maven_group=net.modfest
archives_base_name=fireblanket

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,88 +33,89 @@

@Mixin(ChunkHolder.class)
public abstract class MixinChunkHolder extends AbstractChunkHolder {
private static final List<BEUpdate> BATCHED_UPDATES = new ArrayList<>();

// TODO: mod conflicts, no-sync opto should be opt-in
public MixinChunkHolder(ChunkPos pos) {
super(pos);
}

@Shadow protected abstract void sendBlockEntityUpdatePacket(List<ServerPlayerEntity> players, World world, BlockPos pos);

@Shadow protected abstract void sendPacketToPlayers(List<ServerPlayerEntity> players, Packet<?> packet);

@Shadow @Final private ChunkHolder.PlayersWatchingChunkProvider playersWatchingChunkProvider;

/**
* @author Jasmine
*
* @reason Don't send BE update packets until the observable state of the BE has *actually* changed.
*/
@Overwrite
private void tryUpdateBlockEntityAt(List<ServerPlayerEntity> players, World world, BlockPos pos, BlockState state) {
if (state.hasBlockEntity()) {
// TODO: this code here is duplicated because mods mixin to sendBlockEntityUpdatePacket, which makes it less ideal for mixing into
BlockEntity blockEntity = world.getBlockEntity(pos);

if (blockEntity != null) {
CachedCompoundBE cbe = (CachedCompoundBE) blockEntity;
Packet<?> packet = blockEntity.toUpdatePacket();
if (packet instanceof BlockEntityUpdateS2CPacket bes2c) {
NbtCompound cached = cbe.fireblanket$getCachedCompound();
NbtCompound nbt = bes2c.getNbt();

// Don't update if we're the same as before.

// TODO: do this over the network thread, so it doesn't block the main thread
if (Objects.equals(cached, nbt)) {
return;
}

cbe.fireblanket$setCachedCompound(nbt);
}

this.sendBlockEntityUpdatePacket(players, world, pos);
}
}
}

@Redirect(method = "sendBlockEntityUpdatePacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ChunkHolder;sendPacketToPlayers(Ljava/util/List;Lnet/minecraft/network/packet/Packet;)V"))
private void fireblanket$dontSendUpdate(ChunkHolder instance, List<ServerPlayerEntity> players, Packet<?> packet) {
if (packet instanceof BlockEntityUpdateS2CPacket bes2c) {
// We're a chunk update- let's batch per chunk
BATCHED_UPDATES.add(new BEUpdate(bes2c.getPos(), bes2c.getBlockEntityType(), bes2c.getNbt()));
} else {
// No idea what we are- need to fall back to worst case
this.sendPacketToPlayers(players, packet);
}
}

@Inject(method = "flushUpdates", at = @At("HEAD"))
private void fireblanket$flushUpdates$head(WorldChunk chunk, CallbackInfo ci) {
// Should probably assert here for the list being clear

BATCHED_UPDATES.clear();
}

@Inject(method = "flushUpdates", at = @At("TAIL"))
private void fireblanket$flushUpdates$tail(WorldChunk chunk, CallbackInfo ci) {
if (!BATCHED_UPDATES.isEmpty()) {
List<ServerPlayerEntity> list = this.playersWatchingChunkProvider.getPlayersWatchingChunk(this.pos, false);

int size = BATCHED_UPDATES.size();
if (list.isEmpty()) {
BATCHED_UPDATES.clear();
return;
}

// RegistryByteBuf buf = RegistryByteBuf.makeFactory(chunk.getWorld().getRegistryManager()).apply(Unpooled.buffer());
// BatchedBEUpdatePayload.CODEC.encode(buf, new BatchedBEUpdatePayload(BATCHED_UPDATES));

for (ServerPlayerEntity p : list) {
ServerPlayNetworking.send(p, new BatchedBEUpdatePayload(BATCHED_UPDATES));
}

BATCHED_UPDATES.clear();
}
}
// private static final List<BEUpdate> BATCHED_UPDATES = new ArrayList<>();
//
// public MixinChunkHolder(ChunkPos pos) {
// super(pos);
// }
//
// @Shadow protected abstract void sendBlockEntityUpdatePacket(List<ServerPlayerEntity> players, World world, BlockPos pos);
//
// @Shadow protected abstract void sendPacketToPlayers(List<ServerPlayerEntity> players, Packet<?> packet);
//
// @Shadow @Final private ChunkHolder.PlayersWatchingChunkProvider playersWatchingChunkProvider;
//
// /**
// * @author Jasmine
// *
// * @reason Don't send BE update packets until the observable state of the BE has *actually* changed.
// */
// @Overwrite
// private void tryUpdateBlockEntityAt(List<ServerPlayerEntity> players, World world, BlockPos pos, BlockState state) {
// if (state.hasBlockEntity()) {
// // TODO: this code here is duplicated because mods mixin to sendBlockEntityUpdatePacket, which makes it less ideal for mixing into
// BlockEntity blockEntity = world.getBlockEntity(pos);
//
// if (blockEntity != null) {
// CachedCompoundBE cbe = (CachedCompoundBE) blockEntity;
// Packet<?> packet = blockEntity.toUpdatePacket();
// if (packet instanceof BlockEntityUpdateS2CPacket bes2c) {
// NbtCompound cached = cbe.fireblanket$getCachedCompound();
// NbtCompound nbt = bes2c.getNbt();
//
// // Don't update if we're the same as before.
//
// // TODO: do this over the network thread, so it doesn't block the main thread
// if (Objects.equals(cached, nbt)) {
// return;
// }
//
// cbe.fireblanket$setCachedCompound(nbt);
// }
//
// this.sendBlockEntityUpdatePacket(players, world, pos);
// }
// }
// }
//
// @Redirect(method = "sendBlockEntityUpdatePacket", at = @At(value = "INVOKE", target = "Lnet/minecraft/server/world/ChunkHolder;sendPacketToPlayers(Ljava/util/List;Lnet/minecraft/network/packet/Packet;)V"))
// private void fireblanket$dontSendUpdate(ChunkHolder instance, List<ServerPlayerEntity> players, Packet<?> packet) {
// if (packet instanceof BlockEntityUpdateS2CPacket bes2c) {
// // We're a chunk update- let's batch per chunk
// BATCHED_UPDATES.add(new BEUpdate(bes2c.getPos(), bes2c.getBlockEntityType(), bes2c.getNbt()));
// } else {
// // No idea what we are- need to fall back to worst case
// this.sendPacketToPlayers(players, packet);
// }
// }
//
// @Inject(method = "flushUpdates", at = @At("HEAD"))
// private void fireblanket$flushUpdates$head(WorldChunk chunk, CallbackInfo ci) {
// // Should probably assert here for the list being clear
//
// BATCHED_UPDATES.clear();
// }
//
// @Inject(method = "flushUpdates", at = @At("TAIL"))
// private void fireblanket$flushUpdates$tail(WorldChunk chunk, CallbackInfo ci) {
// if (!BATCHED_UPDATES.isEmpty()) {
// List<ServerPlayerEntity> list = this.playersWatchingChunkProvider.getPlayersWatchingChunk(this.pos, false);
//
// int size = BATCHED_UPDATES.size();
// if (list.isEmpty()) {
// BATCHED_UPDATES.clear();
// return;
// }
//
// for (ServerPlayerEntity p : list) {
// ServerPlayNetworking.send(p, new BatchedBEUpdatePayload(BATCHED_UPDATES));
// }
//
// BATCHED_UPDATES.clear();
// }
// }
}

0 comments on commit e048e9e

Please sign in to comment.