|
| 1 | +package org.embeddedt.modernfix.forge.mixin.bugfix.model_data_manager_cme; |
| 2 | + |
| 3 | +import net.minecraft.client.Minecraft; |
| 4 | +import net.minecraft.core.BlockPos; |
| 5 | +import net.minecraft.world.level.ChunkPos; |
| 6 | +import net.minecraftforge.client.model.data.ModelDataManager; |
| 7 | +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; |
| 8 | +import org.spongepowered.asm.mixin.Mixin; |
| 9 | +import org.spongepowered.asm.mixin.Shadow; |
| 10 | +import org.spongepowered.asm.mixin.injection.At; |
| 11 | +import org.spongepowered.asm.mixin.injection.ModifyArg; |
| 12 | +import org.spongepowered.asm.mixin.injection.Redirect; |
| 13 | + |
| 14 | +import java.util.Collections; |
| 15 | +import java.util.Set; |
| 16 | +import java.util.concurrent.ConcurrentHashMap; |
| 17 | +import java.util.function.Function; |
| 18 | + |
| 19 | +/** |
| 20 | + * Fix several concurrency issues in the default ModelDataManager. |
| 21 | + */ |
| 22 | +@Mixin(ModelDataManager.class) |
| 23 | +@ClientOnlyMixin |
| 24 | +public abstract class ModelDataManagerMixin { |
| 25 | + @Shadow protected abstract void refreshAt(ChunkPos chunk); |
| 26 | + |
| 27 | + /** |
| 28 | + * Make the set of positions to refresh a real concurrent hash set rather than relying on synchronizedSet, |
| 29 | + * because the returned iterator won't be thread-safe otherwise. See https://github.com/AppliedEnergistics/Applied-Energistics-2/issues/7511 |
| 30 | + */ |
| 31 | + @ModifyArg(method = "requestRefresh", at = @At(value = "INVOKE", target = "Ljava/util/Map;computeIfAbsent(Ljava/lang/Object;Ljava/util/function/Function;)Ljava/lang/Object;", ordinal = 0), index = 1, remap = false) |
| 32 | + private static Function<ChunkPos, Set<BlockPos>> changeTypeOfSetUsed(Function<ChunkPos, Set<BlockPos>> mappingFunction) { |
| 33 | + return pos -> Collections.newSetFromMap(new ConcurrentHashMap<>()); |
| 34 | + } |
| 35 | + |
| 36 | + @Redirect(method = "getAt(Lnet/minecraft/world/level/ChunkPos;)Ljava/util/Map;", at = @At(value = "INVOKE", target = "Lnet/minecraftforge/client/model/data/ModelDataManager;refreshAt(Lnet/minecraft/world/level/ChunkPos;)V"), remap = false) |
| 37 | + private void onlyRefreshOnMainThread(ModelDataManager instance, ChunkPos pos) { |
| 38 | + // Only refresh model data on the main thread. This prevents calling getBlockEntity from worker threads |
| 39 | + // which could cause weird CMEs or other behavior. |
| 40 | + if(Minecraft.getInstance().isSameThread()) { |
| 41 | + // Refresh the given chunk, and all its neighbors. This is less efficient than the default code |
| 42 | + // but we have no choice since we need to not do refreshing on workers, and blocks might |
| 43 | + // try to access model data in neighboring chunks. |
| 44 | + for(int x = -1; x <= 1; x++) { |
| 45 | + for(int z = -1; z <= 1; z++) { |
| 46 | + refreshAt(new ChunkPos(pos.x + x, pos.z + z)); |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
0 commit comments