Avoid sending duplicate visual updates to the client

This commit is contained in:
thedarkcolour 2024-02-07 14:10:14 -08:00
parent 14a05d561c
commit da98acc3cd

View File

@ -23,26 +23,24 @@ import net.minecraft.world.level.chunk.LevelChunk;
import net.minecraftforge.network.PacketDistributor; import net.minecraftforge.network.PacketDistributor;
import thedarkcolour.exdeorum.blockentity.EBlockEntity; import thedarkcolour.exdeorum.blockentity.EBlockEntity;
import java.util.ArrayList; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.WeakHashMap;
// Syncs certain block entity data to the client for visual purposes // Syncs certain block entity data to the client for visual purposes
// Since some block entities might change their data multiple times a tick, this class keeps track of // Since some block entities might change their data multiple times a tick, this class keeps track of
// whether a block entity has updated and then pushes out the changes once at the end of each tick. // whether a block entity has updated and then pushes out the changes once at the end of each tick.
public class VisualUpdateTracker { public class VisualUpdateTracker {
// WeakHashMap is faster than Guava mapmaker // WeakHashMap is faster than Guava mapmaker
private static final Map<LevelChunk, List<BlockPos>> UPDATES = new WeakHashMap<>(); // Use sets to avoid duplicate updates
private static final Map<LevelChunk, Set<BlockPos>> UPDATES = new WeakHashMap<>();
public static void sendVisualUpdate(EBlockEntity blockEntity) { public static void sendVisualUpdate(EBlockEntity blockEntity) {
var level = blockEntity.getLevel(); var level = blockEntity.getLevel();
if (level != null && !level.isClientSide) { if (level != null && !level.isClientSide) {
var dimension = level.getChunkAt(blockEntity.getBlockPos()); var dimension = level.getChunkAt(blockEntity.getBlockPos());
List<BlockPos> updatesList; Set<BlockPos> updatesList;
if (!UPDATES.containsKey(dimension)) { if (!UPDATES.containsKey(dimension)) {
UPDATES.put(dimension, updatesList = new ArrayList<>()); UPDATES.put(dimension, updatesList = new HashSet<>());
} else { } else {
updatesList = UPDATES.get(dimension); updatesList = UPDATES.get(dimension);
} }