Skip to content

Commit

Permalink
avoid creating an update packet to send to no one
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Aug 30, 2024
1 parent 3c3774a commit 93a31fc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/main/java/mekanism/common/network/PacketUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.netty.buffer.Unpooled;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import mekanism.common.Mekanism;
import mekanism.common.config.MekanismConfig;
Expand All @@ -18,6 +19,7 @@
import net.minecraft.SharedConstants;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.SectionPos;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
Expand Down Expand Up @@ -129,6 +131,23 @@ public static <MSG extends CustomPacketPayload> void sendToAllTracking(MSG messa
PacketDistributor.sendToPlayersTrackingChunk((ServerLevel) world, new ChunkPos(pos), message);
}

/**
* Based on home {@link PacketDistributor#sendToPlayersTrackingChunk(ServerLevel, ChunkPos, CustomPacketPayload, CustomPacketPayload...)} finds players to send to,
* without the immutable list.
*/
public static boolean hasPlayersTracking(ServerLevel level, BlockPos pos) {
int chunkX = SectionPos.blockToSectionCoord(pos.getX());
int chunkZ = SectionPos.blockToSectionCoord(pos.getZ());
Set<ServerPlayer> players = level.getChunkSource().chunkMap.playerMap.getAllPlayers();

for (ServerPlayer serverplayer : players) {
if (level.getChunkSource().chunkMap.isChunkTracked(serverplayer, chunkX, chunkZ)) {
return true;
}
}
return false;
}

//TODO: Evaluate moving various network related packets over to this (and making it support non buffered networks)
public static void sendToAllTracking(DynamicBufferedNetwork<?, ?, ?, ?> network, CustomPacketPayload... packets) {
Range3D range = network.getPacketRange();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import net.minecraft.nbt.NbtOps;
import net.minecraft.network.Connection;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
Expand Down Expand Up @@ -173,7 +174,7 @@ public void sendUpdatePacket(BlockEntity tracking) {
Mekanism.logger.warn("Update packet call requested from client side", new IllegalStateException());
} else if (isRemoved()) {
Mekanism.logger.warn("Update packet call requested for removed tile", new IllegalStateException());
} else {
} else if (PacketUtils.hasPlayersTracking((ServerLevel) tracking.getLevel(), tracking.getBlockPos())) {
//Note: We use our own update packet/channel to avoid chunk trashing and minecraft attempting to rerender
// the entire chunk when most often we are just updating a TileEntityRenderer, so the chunk itself
// does not need to and should not be redrawn
Expand Down
5 changes: 4 additions & 1 deletion src/main/resources/META-INF/accesstransformer.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,7 @@ public net.minecraft.world.level.levelgen.placement.PlacedFeature placeWithConte
public net.minecraft.world.level.material.MapColor MATERIAL_COLORS
public net.minecraft.world.level.storage.loot.functions.CopyNbtFunction$Path

public net.minecraft.world.phys.AABB getDirection(Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/phys/Vec3;[DLnet/minecraft/core/Direction;DDD)Lnet/minecraft/core/Direction;
public net.minecraft.world.phys.AABB getDirection(Lnet/minecraft/world/phys/AABB;Lnet/minecraft/world/phys/Vec3;[DLnet/minecraft/core/Direction;DDD)Lnet/minecraft/core/Direction;

public net.minecraft.server.level.ChunkMap playerMap
public net.minecraft.server.level.ChunkMap isChunkTracked(Lnet/minecraft/server/level/ServerPlayer;II)Z

0 comments on commit 93a31fc

Please sign in to comment.