From 1c789111e803340589150249d6ab482a65a4a7f2 Mon Sep 17 00:00:00 2001 From: embeddedt <42941056+embeddedt@users.noreply.github.com> Date: Wed, 25 Dec 2024 22:13:29 -0500 Subject: [PATCH] "Fix" Hypixel's disappearing chests problem on modern clients We really should not need to do this. Hypixel, *please* fix your protocol translation plugin. --- .../LevelChunkMixin.java | 97 +++++++++++++++++++ .../core/config/ModernFixEarlyConfig.java | 1 + .../assets/modernfix/lang/en_us.json | 3 +- 3 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/missing_block_entities/LevelChunkMixin.java diff --git a/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/missing_block_entities/LevelChunkMixin.java b/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/missing_block_entities/LevelChunkMixin.java new file mode 100644 index 00000000..e5c92256 --- /dev/null +++ b/common/src/main/java/org/embeddedt/modernfix/common/mixin/bugfix/missing_block_entities/LevelChunkMixin.java @@ -0,0 +1,97 @@ +package org.embeddedt.modernfix.common.mixin.bugfix.missing_block_entities; + +import net.minecraft.client.Minecraft; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Registry; +import net.minecraft.world.level.ChunkPos; +import net.minecraft.world.level.Level; +import net.minecraft.world.level.LevelHeightAccessor; +import net.minecraft.world.level.biome.Biome; +import net.minecraft.world.level.block.entity.BlockEntity; +import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.chunk.ChunkAccess; +import net.minecraft.world.level.chunk.LevelChunk; +import net.minecraft.world.level.chunk.LevelChunkSection; +import net.minecraft.world.level.chunk.UpgradeData; +import net.minecraft.world.level.levelgen.blending.BlendingData; +import org.embeddedt.modernfix.ModernFix; +import org.embeddedt.modernfix.annotation.ClientOnlyMixin; +import org.jetbrains.annotations.Nullable; +import org.spongepowered.asm.mixin.Final; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +/** + * Hypixel (and possibly other buggy servers) send chunks to the client that are missing some block entity data, which + * causes these entities to be invisible. We "fix" this by recreating the block entity on the client with default data, + * which is hopefully what the legacy server also expects. + */ +@Mixin(LevelChunk.class) +@ClientOnlyMixin +public abstract class LevelChunkMixin extends ChunkAccess { + @Shadow @Final private Level level; + + @Shadow @Nullable public abstract BlockEntity getBlockEntity(BlockPos pos, LevelChunk.EntityCreationType creationType); + + public LevelChunkMixin(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightAccessor levelHeightAccessor, Registry biomeRegistry, long inhabitedTime, @Nullable LevelChunkSection[] sections, @Nullable BlendingData blendingData) { + super(chunkPos, upgradeData, levelHeightAccessor, biomeRegistry, inhabitedTime, sections, blendingData); + } + + @Inject(method = "replaceWithPacketData", at = @At("RETURN")) + private void validateBlockEntitiesInChunk(CallbackInfo ci) { + // No reason to check in singleplayer or on the integrated server + if (this.level.isClientSide && !Minecraft.getInstance().isLocalServer()) { + for (int i = 0; i < this.sections.length; i++) { + var section = this.sections[i]; + try { + if (!section.hasOnlyAir() && section.maybeHas(BlockBehaviour.BlockStateBase::hasBlockEntity)) { + scanSectionForBlockEntities(section, i); + } + } catch(Exception e) { + ModernFix.LOGGER.error("Exception validating data in chunk", e); + return; + } + } + } + } + + @Unique + private void scanSectionForBlockEntities(LevelChunkSection section, int i) { + int chunkX = this.chunkPos.x; + int chunkZ = this.chunkPos.z; + BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos(); + int sectionY = this.getSectionYFromSectionIndex(i); + for (int y = 0; y < 16; y++) { + for (int z = 0; z < 16; z++) { + for (int x = 0; x < 16; x++) { + var state = section.getBlockState(x, y, z); + if (state.hasBlockEntity()) { + cursor.set(chunkX * 16 + x, sectionY * 16 + y, chunkZ * 16 + z); + makeBlockEntityIfNotExists(state, cursor); + } + } + } + } + } + + @Unique + private void makeBlockEntityIfNotExists(BlockState state, BlockPos.MutableBlockPos pos) { + if (this.blockEntities.containsKey(pos) || this.pendingBlockEntities.containsKey(pos)) { + return; + } + + BlockEntity blockEntity = this.getBlockEntity(pos.immutable(), LevelChunk.EntityCreationType.IMMEDIATE); + String blockName = state.getBlock().toString(); + if (blockEntity != null) { + ModernFix.LOGGER.warn("Created missing block entity for {} at {}", blockName, pos.toShortString()); + } else { + ModernFix.LOGGER.error("Block entity is missing for {} at {}, but could not be created", blockName, pos.toShortString()); + } + } +} + diff --git a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java index 85d4f032..c7814e3a 100644 --- a/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java +++ b/common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java @@ -167,6 +167,7 @@ public class ModernFixEarlyConfig { .put("mixin.bugfix.restore_old_dragon_movement", false) .put("mixin.perf.worldgen_allocation", false) // experimental .put("mixin.feature.cause_lag_by_disabling_threads", false) + .put("mixin.bugfix.missing_block_entities", false) .put("mixin.perf.clear_mixin_classinfo", false) .put("mixin.perf.deduplicate_climate_parameters", false) .put("mixin.bugfix.packet_leak", false) diff --git a/common/src/main/resources/assets/modernfix/lang/en_us.json b/common/src/main/resources/assets/modernfix/lang/en_us.json index 7ab5e016..e18097bc 100644 --- a/common/src/main/resources/assets/modernfix/lang/en_us.json +++ b/common/src/main/resources/assets/modernfix/lang/en_us.json @@ -134,5 +134,6 @@ "modernfix.option.mixin.perf.fix_loop_spin_waiting": "Fixes Minecraft's built-in wait function consuming excessive amounts of CPU resources.", "modernfix.option.mixin.perf.forge_cap_retrieval": "Small micro-optimization that makes retrieving custom entity data slightly more efficient on Forge.", "modernfix.option.mixin.perf.forge_registry_lambda": "Fixes oversights in Forge that lead to excessive allocation in hot registry methods.", - "modernfix.option.mixin.bugfix.restore_old_dragon_movement": "Fixes MC-272431, which tracks the ender dragon being unable to dive to the portal as it did in 1.13 and older. This causes the dragon to fly quite a bit differently from what modern players are used to and also patches out one-cycling, so it's not enabled by default. Thanks to Jukitsu for identifying the regression in the vanilla code." + "modernfix.option.mixin.bugfix.restore_old_dragon_movement": "Fixes MC-272431, which tracks the ender dragon being unable to dive to the portal as it did in 1.13 and older. This causes the dragon to fly quite a bit differently from what modern players are used to and also patches out one-cycling, so it's not enabled by default. Thanks to Jukitsu for identifying the regression in the vanilla code.", + "modernfix.option.mixin.bugfix.missing_block_entities": "Hypixel sends chunks to the client that are missing some block entity data, which makes chests etc. appear invisible. This 'fixes' the problem by creating the needed data on the client. Has no effect for properly behaved servers or in singleplayer." }