From 0af63721d7cd3c57ac04c638a893d930863542e6 Mon Sep 17 00:00:00 2001 From: Sam Kirby Date: Wed, 20 Dec 2023 09:52:11 +0000 Subject: [PATCH] feat(patch): Prevent iteration tile entity invalidation during chunk loading --- build.gradle | 3 +- gradle.properties | 2 +- .../sevpatches/core/mixins/ChunkTEMixin.java | 47 +++++++++++++++++++ src/main/resources/mixins.sevpatches.json | 1 + 4 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 src/main/java/tv/darkosto/sevpatches/core/mixins/ChunkTEMixin.java diff --git a/build.gradle b/build.gradle index 61e9740..4015e11 100644 --- a/build.gradle +++ b/build.gradle @@ -2,7 +2,6 @@ buildscript { repositories { maven { url = 'https://repo.spongepowered.org/maven' } maven { url = 'https://files.minecraftforge.net/maven' } - jcenter() mavenCentral() } dependencies { @@ -79,7 +78,7 @@ dependencies { } compile 'curse.maven:astralsorcery-1.0.23-241721:2920254' - compile fg.deobf('curse.maven:jaff-1.7-235261:2448283') + compile 'curse.maven:jaff-1.7-235261:2448284' compile 'slimeknights:TConstruct:1.12.2-2.13.0.184' compile 'curse.maven:realisticitemdrops_deobf_1.2.14-245620:2630385' } diff --git a/gradle.properties b/gradle.properties index 9c12398..5878a67 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -version_base = 1.10.1 +version_base = 1.11.0 # Sets default memory used for gradle commands. Can be overridden by user or command line properties. # This is required to provide enough memory for the Minecraft decompilation process. diff --git a/src/main/java/tv/darkosto/sevpatches/core/mixins/ChunkTEMixin.java b/src/main/java/tv/darkosto/sevpatches/core/mixins/ChunkTEMixin.java new file mode 100644 index 0000000..f2ce179 --- /dev/null +++ b/src/main/java/tv/darkosto/sevpatches/core/mixins/ChunkTEMixin.java @@ -0,0 +1,47 @@ +package tv.darkosto.sevpatches.core.mixins; + +import net.minecraft.block.Block; +import net.minecraft.block.state.IBlockState; +import net.minecraft.tileentity.TileEntity; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.chunk.Chunk; +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.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; +import tv.darkosto.sevpatches.SevPatches; + +@Mixin(value = Chunk.class, priority = 1500) +public abstract class ChunkTEMixin { + private boolean loadingTileEntities = false; + + @Shadow + public abstract IBlockState getBlockState(BlockPos pos); + + @Inject(method = "createNewTileEntity", at = @At(value = "HEAD"), cancellable = true, require = 1) + private void skipTileEntityIfLoading(BlockPos blockPos, CallbackInfoReturnable cir) { + IBlockState blockState = this.getBlockState(blockPos); + Block block = blockState.getBlock(); + if (this.loadingTileEntities && block.hasTileEntity(blockState)) { + SevPatches.LOGGER.warn("#########################################################################"); + SevPatches.LOGGER.warn("A mod has attempted to add a TileEntity to a chunk during chunk loading; this may result in iterator invalidation"); + SevPatches.LOGGER.warn("This attempt has been blocked, which may lead to some weirdness, e.g. pipes not connecting"); + SevPatches.LOGGER.warn("TE at: {} for block {}", blockPos, block.getRegistryName()); + SevPatches.LOGGER.warn("Stack trace:", new Throwable()); + SevPatches.LOGGER.warn("#########################################################################"); + cir.setReturnValue(null); + } + } + + @Inject(method = "onLoad", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;addTileEntities(Ljava/util/Collection;)V"), require = 1) + private void setLoadingTileEntitiesOn(CallbackInfo ci) { + this.loadingTileEntities = true; + } + + @Inject(method = "onLoad", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/World;addTileEntities(Ljava/util/Collection;)V", shift = At.Shift.AFTER), require = 1) + private void setLoadingTileEntitiesOff(CallbackInfo ci) { + this.loadingTileEntities = false; + } +} diff --git a/src/main/resources/mixins.sevpatches.json b/src/main/resources/mixins.sevpatches.json index 076a8b2..b1b8fbb 100644 --- a/src/main/resources/mixins.sevpatches.json +++ b/src/main/resources/mixins.sevpatches.json @@ -6,6 +6,7 @@ "minVersion": "0.8", "compatibilityLevel": "JAVA_8", "mixins": [ + "ChunkTEMixin", "EnchantmentHelperMixin", "TileEntityBeaconMixin" ]