Skip to content

Commit a170f07

Browse files
committed
Invalidate memoization for non-category creative tabs when categories are rebuilt
1 parent e7542af commit a170f07

File tree

1 file changed

+28
-4
lines changed

1 file changed

+28
-4
lines changed

common/src/main/java/org/embeddedt/modernfix/common/mixin/perf/memoize_creative_tab_build/CreativeModeTabMixin.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22

33
import com.llamalad7.mixinextras.injector.wrapmethod.WrapMethod;
44
import com.llamalad7.mixinextras.injector.wrapoperation.Operation;
5+
import net.minecraft.core.registries.BuiltInRegistries;
56
import net.minecraft.world.item.CreativeModeTab;
67
import org.spongepowered.asm.mixin.Mixin;
8+
import org.spongepowered.asm.mixin.Shadow;
79
import org.spongepowered.asm.mixin.Unique;
810

911
@Mixin(CreativeModeTab.class)
10-
public class CreativeModeTabMixin {
12+
public abstract class CreativeModeTabMixin {
13+
@Shadow public abstract CreativeModeTab.Type getType();
14+
1115
@Unique
1216
private CreativeModeTab.ItemDisplayParameters mfix$oldParameters;
1317

18+
@Unique
19+
private static boolean MFIX$REBUILT_NON_CATEGORY = false;
20+
1421
/**
1522
* @author embeddedt
1623
* @reason Vanilla already does similar memoization in the CreativeModeTabs class, but mods often have to bypass
@@ -20,9 +27,26 @@ public class CreativeModeTabMixin {
2027
*/
2128
@WrapMethod(method = "buildContents")
2229
private synchronized void buildContentsIfChanged(CreativeModeTab.ItemDisplayParameters parameters, Operation<Void> original) {
23-
if (mfix$oldParameters == null || mfix$oldParameters.needsUpdate(parameters.enabledFeatures(), parameters.hasPermissions(), parameters.holders())) {
24-
original.call(parameters);
30+
synchronized (CreativeModeTab.class) {
31+
if (mfix$oldParameters == null || mfix$oldParameters.needsUpdate(parameters.enabledFeatures(), parameters.hasPermissions(), parameters.holders())) {
32+
original.call(parameters);
33+
if (this.getType() == CreativeModeTab.Type.CATEGORY) {
34+
if (MFIX$REBUILT_NON_CATEGORY) {
35+
// We must mark every other tab that's not a category as needing rebuild. Not doing this causes mods
36+
// that build a search tab early on without having built the dependencies to permanently leave it
37+
// in a broken state.
38+
for (CreativeModeTab tab : BuiltInRegistries.CREATIVE_MODE_TAB) {
39+
if (tab.getType() != CreativeModeTab.Type.CATEGORY) {
40+
((CreativeModeTabMixin)(Object)tab).mfix$oldParameters = null;
41+
}
42+
}
43+
MFIX$REBUILT_NON_CATEGORY = false;
44+
}
45+
} else {
46+
MFIX$REBUILT_NON_CATEGORY = true;
47+
}
48+
}
49+
mfix$oldParameters = parameters;
2550
}
26-
mfix$oldParameters = parameters;
2751
}
2852
}

0 commit comments

Comments
 (0)