Skip to content

Commit

Permalink
Avoid refresh loop if no model data needs refreshing
Browse files Browse the repository at this point in the history
  • Loading branch information
embeddedt committed Jan 5, 2024
1 parent 7174ae1 commit c678ebb
Showing 1 changed file with 6 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
import net.minecraft.world.level.Level;
import net.minecraftforge.client.model.ModelDataManager;
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyArg;
import org.spongepowered.asm.mixin.injection.Redirect;

import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
Expand All @@ -27,6 +29,8 @@ public abstract class ModelDataManagerMixin {
throw new AssertionError();
}

@Shadow @Final private static Map<ChunkPos, Set<BlockPos>> needModelDataRefresh;

/**
* Make the set of positions to refresh a real concurrent hash set rather than relying on synchronizedSet,
* because the returned iterator won't be thread-safe otherwise. See https://github.com/AppliedEnergistics/Applied-Energistics-2/issues/7511
Expand All @@ -40,7 +44,8 @@ private static Function<ChunkPos, Set<BlockPos>> changeTypeOfSetUsed(Function<Ch
private static void onlyRefreshOnMainThread(Level toUpdate, ChunkPos pos) {
// Only refresh model data on the main thread. This prevents calling getBlockEntity from worker threads
// which could cause weird CMEs or other behavior.
if(Minecraft.getInstance().isSameThread()) {
// Avoid the loop if no model data needs to be refreshed, to prevent unnecessary allocation.
if(Minecraft.getInstance().isSameThread() && !needModelDataRefresh.isEmpty()) {
// Refresh the given chunk, and all its neighbors. This is less efficient than the default code
// but we have no choice since we need to not do refreshing on workers, and blocks might
// try to access model data in neighboring chunks.
Expand Down

0 comments on commit c678ebb

Please sign in to comment.