Don't trigger full blockstate cache rebuilds when requesting fluid

This causes mods to prematurely rebuild the whole cache, which
is not what we want
This commit is contained in:
embeddedt 2023-08-05 19:38:20 -04:00
parent 7b18bcafd6
commit ec5b92dd7a
No known key found for this signature in database
GPG Key ID: A69433EC199B5613

View File

@ -1,7 +1,14 @@
package org.embeddedt.modernfix.common.mixin.perf.reduce_blockstate_cache_rebuilds;
import com.google.common.collect.ImmutableMap;
import com.mojang.serialization.MapCodec;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.block.state.StateHolder;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.material.FluidState;
import net.minecraft.world.level.material.Fluids;
import org.embeddedt.modernfix.duck.IBlockState;
import org.objectweb.asm.Opcodes;
import org.spongepowered.asm.mixin.Dynamic;
@ -14,13 +21,21 @@ import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;
@Mixin(BlockBehaviour.BlockStateBase.class)
public abstract class BlockStateBaseMixin implements IBlockState {
public abstract class BlockStateBaseMixin extends StateHolder<Block, BlockState> implements IBlockState {
protected BlockStateBaseMixin(Block object, ImmutableMap<Property<?>, Comparable<?>> immutableMap, MapCodec<BlockState> mapCodec) {
super(object, immutableMap, mapCodec);
}
private static final FluidState MFIX$VANILLA_DEFAULT_FLUID = Fluids.EMPTY.defaultFluidState();
@Shadow public abstract void initCache();
@Shadow private BlockBehaviour.BlockStateBase.Cache cache;
@Shadow private FluidState fluidState;
@Shadow private boolean isRandomlyTicking;
@Shadow protected abstract BlockState asState();
private volatile boolean cacheInvalid = false;
private static boolean buildingCache = false;
@Override
@ -72,7 +87,11 @@ public abstract class BlockStateBaseMixin implements IBlockState {
ordinal = 0
), require = 0)
private FluidState genCacheBeforeGettingFluid(BlockBehaviour.BlockStateBase base) {
generateCache(base);
// don't generate the full cache here as mods will iterate for the fluid state a lot
// assume blockstates will not change their contained fluidstate at runtime more than once
// this is how Lithium's implementation used to work, so it should be fine
if(this.cacheInvalid && this.fluidState == MFIX$VANILLA_DEFAULT_FLUID)
this.fluidState = this.owner.getFluidState(this.asState());
return this.fluidState;
}
@ -83,7 +102,8 @@ public abstract class BlockStateBaseMixin implements IBlockState {
ordinal = 0
))
private boolean genCacheBeforeGettingTicking(BlockBehaviour.BlockStateBase base) {
generateCache(base);
if(this.cacheInvalid)
return this.owner.isRandomlyTicking(this.asState());
return this.isRandomlyTicking;
}