Skip to content

Commit eeb8423

Browse files
committed
Merge remote-tracking branch 'origin/1.20' into 1.21.1
2 parents 6531b69 + 64a427f commit eeb8423

File tree

6 files changed

+202
-1
lines changed

6 files changed

+202
-1
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package org.embeddedt.modernfix.common.mixin.bugfix.missing_block_entities;
2+
3+
import net.minecraft.client.Minecraft;
4+
import net.minecraft.core.BlockPos;
5+
import net.minecraft.core.Registry;
6+
import net.minecraft.world.level.ChunkPos;
7+
import net.minecraft.world.level.Level;
8+
import net.minecraft.world.level.LevelHeightAccessor;
9+
import net.minecraft.world.level.biome.Biome;
10+
import net.minecraft.world.level.block.entity.BlockEntity;
11+
import net.minecraft.world.level.block.state.BlockBehaviour;
12+
import net.minecraft.world.level.block.state.BlockState;
13+
import net.minecraft.world.level.chunk.ChunkAccess;
14+
import net.minecraft.world.level.chunk.LevelChunk;
15+
import net.minecraft.world.level.chunk.LevelChunkSection;
16+
import net.minecraft.world.level.chunk.UpgradeData;
17+
import net.minecraft.world.level.levelgen.blending.BlendingData;
18+
import org.embeddedt.modernfix.ModernFix;
19+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
20+
import org.jetbrains.annotations.Nullable;
21+
import org.spongepowered.asm.mixin.Final;
22+
import org.spongepowered.asm.mixin.Mixin;
23+
import org.spongepowered.asm.mixin.Shadow;
24+
import org.spongepowered.asm.mixin.Unique;
25+
import org.spongepowered.asm.mixin.injection.At;
26+
import org.spongepowered.asm.mixin.injection.Inject;
27+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
28+
29+
/**
30+
* Hypixel (and possibly other buggy servers) send chunks to the client that are missing some block entity data, which
31+
* causes these entities to be invisible. We "fix" this by recreating the block entity on the client with default data,
32+
* which is hopefully what the legacy server also expects.
33+
*/
34+
@Mixin(LevelChunk.class)
35+
@ClientOnlyMixin
36+
public abstract class LevelChunkMixin extends ChunkAccess {
37+
@Shadow @Final private Level level;
38+
39+
@Shadow @Nullable public abstract BlockEntity getBlockEntity(BlockPos pos, LevelChunk.EntityCreationType creationType);
40+
41+
public LevelChunkMixin(ChunkPos chunkPos, UpgradeData upgradeData, LevelHeightAccessor levelHeightAccessor, Registry<Biome> biomeRegistry, long inhabitedTime, @Nullable LevelChunkSection[] sections, @Nullable BlendingData blendingData) {
42+
super(chunkPos, upgradeData, levelHeightAccessor, biomeRegistry, inhabitedTime, sections, blendingData);
43+
}
44+
45+
@Inject(method = "replaceWithPacketData", at = @At("RETURN"))
46+
private void validateBlockEntitiesInChunk(CallbackInfo ci) {
47+
// No reason to check in singleplayer or on the integrated server
48+
if (this.level.isClientSide && !Minecraft.getInstance().isLocalServer()) {
49+
for (int i = 0; i < this.sections.length; i++) {
50+
var section = this.sections[i];
51+
try {
52+
if (!section.hasOnlyAir() && section.maybeHas(BlockBehaviour.BlockStateBase::hasBlockEntity)) {
53+
scanSectionForBlockEntities(section, i);
54+
}
55+
} catch(Exception e) {
56+
ModernFix.LOGGER.error("Exception validating data in chunk", e);
57+
return;
58+
}
59+
}
60+
}
61+
}
62+
63+
@Unique
64+
private void scanSectionForBlockEntities(LevelChunkSection section, int i) {
65+
int chunkXOff = this.chunkPos.x * 16;
66+
int chunkZOff = this.chunkPos.z * 16;
67+
BlockPos.MutableBlockPos cursor = new BlockPos.MutableBlockPos();
68+
int sectionYOff = this.getSectionYFromSectionIndex(i) * 16;
69+
for (int y = 0; y < 16; y++) {
70+
for (int z = 0; z < 16; z++) {
71+
for (int x = 0; x < 16; x++) {
72+
var state = section.getBlockState(x, y, z);
73+
if (state.hasBlockEntity()) {
74+
cursor.set(chunkXOff + x, sectionYOff + y, chunkZOff + z);
75+
makeBlockEntityIfNotExists(state, cursor);
76+
}
77+
}
78+
}
79+
}
80+
}
81+
82+
@Unique
83+
private void makeBlockEntityIfNotExists(BlockState state, BlockPos.MutableBlockPos pos) {
84+
if (this.blockEntities.containsKey(pos) || this.pendingBlockEntities.containsKey(pos)) {
85+
return;
86+
}
87+
88+
BlockEntity blockEntity = this.getBlockEntity(pos.immutable(), LevelChunk.EntityCreationType.IMMEDIATE);
89+
String blockName = state.getBlock().toString();
90+
if (blockEntity != null) {
91+
ModernFix.LOGGER.warn("Created missing block entity for {} at {}", blockName, pos.toShortString());
92+
} else {
93+
ModernFix.LOGGER.error("Block entity is missing for {} at {}, but could not be created", blockName, pos.toShortString());
94+
}
95+
}
96+
}
97+

common/src/main/java/org/embeddedt/modernfix/core/config/ModernFixEarlyConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public DefaultSettingMapBuilder put(String key, Boolean value) {
166166
.put("mixin.bugfix.restore_old_dragon_movement", false)
167167
.put("mixin.perf.worldgen_allocation", false) // experimental
168168
.put("mixin.feature.cause_lag_by_disabling_threads", false)
169+
.put("mixin.bugfix.missing_block_entities", false)
169170
.put("mixin.perf.clear_mixin_classinfo", false)
170171
.put("mixin.perf.deduplicate_climate_parameters", false)
171172
.put("mixin.bugfix.packet_leak", false)

common/src/main/resources/assets/modernfix/lang/en_us.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,5 +134,6 @@
134134
"modernfix.option.mixin.perf.fix_loop_spin_waiting": "Fixes Minecraft's built-in wait function consuming excessive amounts of CPU resources.",
135135
"modernfix.option.mixin.perf.forge_cap_retrieval": "Small micro-optimization that makes retrieving custom entity data slightly more efficient on Forge.",
136136
"modernfix.option.mixin.perf.forge_registry_lambda": "Fixes oversights in Forge that lead to excessive allocation in hot registry methods.",
137-
"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."
137+
"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.",
138+
"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."
138139
}

gradle.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ refined_storage_version=4392788
1414
jei_version=19.0.0.9
1515
rei_version=13.0.678
1616
ctm_version=1.21-1.2.0+2
17+
ldlib_version=5782845
1718
kubejs_version=1902.6.0-build.142
1819
rhino_version=1902.2.2-build.268
1920
supported_minecraft_versions=1.21.1

neoforge/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ dependencies {
5555
modCompileOnly("dev.latvian.mods:kubejs-forge:${kubejs_version}")
5656
//modRuntimeOnly("curse.maven:ferritecore-429235:4441949")
5757
modCompileOnly("team.chisel.ctm:CTM:${ctm_version}")
58+
modCompileOnly("curse.maven:ldlib-626676:${ldlib_version}")
5859

5960
modCompileOnly("curse.maven:supermartijncore-454372:4455391")
6061
modCompileOnly("vazkii.patchouli:Patchouli:1.19.2-77")
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package org.embeddedt.modernfix.neoforge.mixin.perf.dynamic_resources.ldlib;
2+
3+
import com.lowdragmc.lowdraglib.LDLib;
4+
import com.lowdragmc.lowdraglib.client.ClientProxy;
5+
import com.lowdragmc.lowdraglib.client.model.custommodel.CustomBakedModel;
6+
import com.lowdragmc.lowdraglib.client.model.custommodel.LDLMetadataSection;
7+
import com.lowdragmc.lowdraglib.client.model.forge.LDLRendererModel;
8+
import net.minecraft.client.resources.model.BakedModel;
9+
import net.minecraft.client.resources.model.Material;
10+
import net.minecraft.client.resources.model.ModelBakery;
11+
import net.minecraft.client.resources.model.ModelResourceLocation;
12+
import net.minecraft.client.resources.model.ModelState;
13+
import net.minecraft.client.resources.model.UnbakedModel;
14+
import net.minecraft.resources.ResourceLocation;
15+
import org.embeddedt.modernfix.ModernFixClient;
16+
import org.embeddedt.modernfix.annotation.ClientOnlyMixin;
17+
import org.embeddedt.modernfix.annotation.RequiresMod;
18+
import org.embeddedt.modernfix.api.entrypoint.ModernFixClientIntegration;
19+
import org.spongepowered.asm.mixin.Mixin;
20+
import org.spongepowered.asm.mixin.injection.At;
21+
import org.spongepowered.asm.mixin.injection.Inject;
22+
import org.spongepowered.asm.mixin.injection.Redirect;
23+
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
24+
25+
import java.util.ArrayDeque;
26+
import java.util.Deque;
27+
import java.util.HashSet;
28+
import java.util.Map;
29+
import java.util.Set;
30+
31+
@Mixin(ClientProxy.class)
32+
@ClientOnlyMixin
33+
@RequiresMod("ldlib")
34+
public abstract class ClientProxyImplMixin implements ModernFixClientIntegration {
35+
@Inject(method = "<init>", at = @At("RETURN"))
36+
private void registerIntegration(CallbackInfo ci) {
37+
ModernFixClient.CLIENT_INTEGRATIONS.add(this);
38+
}
39+
40+
@Redirect(method = "modelBake", at = @At(value = "INVOKE", target = "Ljava/util/Map;entrySet()Ljava/util/Set;", ordinal = 0), remap = false)
41+
private Set<?> disableLoop(Map<?, ?> map) {
42+
return Set.of();
43+
}
44+
45+
@Override
46+
public BakedModel onBakedModelLoad(ModelResourceLocation mrl, UnbakedModel rootModel, BakedModel baked, ModelState state, ModelBakery bakery, ModelBakery.TextureGetter textureGetter) {
47+
if (baked == null) {
48+
return null;
49+
}
50+
if (rootModel != null) {
51+
if (baked instanceof LDLRendererModel) {
52+
return baked;
53+
}
54+
if (baked.isCustomRenderer()) { // Nothing we can add to builtin models
55+
return baked;
56+
}
57+
Deque<ResourceLocation> dependencies = new ArrayDeque<>();
58+
Set<ResourceLocation> seenModels = new HashSet<>();
59+
ResourceLocation rl = mrl.id();
60+
dependencies.push(rl);
61+
seenModels.add(rl);
62+
boolean shouldWrap = ClientProxy.WRAPPED_MODELS.getOrDefault(mrl, false);
63+
// Breadth-first loop through dependencies, exiting as soon as a CTM texture is found, and skipping duplicates/cycles
64+
while (!shouldWrap && !dependencies.isEmpty()) {
65+
ResourceLocation dep = dependencies.pop();
66+
UnbakedModel model;
67+
try {
68+
model = dep == rl ? rootModel : bakery.getModel(dep);
69+
} catch (Exception e) {
70+
continue;
71+
}
72+
try {
73+
Set<Material> textures = new HashSet<>(ClientProxy.SCRAPED_TEXTURES.get(dep));
74+
for (Material tex : textures) {
75+
// Cache all dependent texture metadata
76+
// At least one texture has CTM metadata, so we should wrap this baked
77+
if (!LDLMetadataSection.getMetadata(LDLMetadataSection.spriteToAbsolute(tex.texture())).isMissing()) { // TODO lazy
78+
shouldWrap = true;
79+
break;
80+
}
81+
}
82+
if (!shouldWrap) {
83+
for (ResourceLocation newDep : model.getDependencies()) {
84+
if (seenModels.add(newDep)) {
85+
dependencies.push(newDep);
86+
}
87+
}
88+
}
89+
} catch (Exception e) {
90+
LDLib.LOGGER.error("Error loading baked dependency {} for baked {}. Skipping...", dep, rl, e);
91+
}
92+
}
93+
ClientProxy.WRAPPED_MODELS.put(mrl, shouldWrap);
94+
if (shouldWrap) {
95+
return new CustomBakedModel<>(baked);
96+
}
97+
}
98+
return baked;
99+
}
100+
}

0 commit comments

Comments
 (0)