Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Count fluids by their type to avoid unnecessary reads in Entity#updateFluidHeightAndDoFluidPushing #40

Open
wants to merge 1 commit into
base: mc/1.21.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ca.spottedleaf.moonrise.fabric.mixin.collisions;

import ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection;
import ca.spottedleaf.moonrise.patches.getblock.GetBlockLevel;
import it.unimi.dsi.fastutil.objects.Object2DoubleMap;
import net.minecraft.core.BlockPos;
import net.minecraft.tags.FluidTags;
import net.minecraft.tags.TagKey;
import net.minecraft.util.Mth;
import net.minecraft.world.entity.Entity;
Expand Down Expand Up @@ -107,6 +109,21 @@ public boolean updateFluidHeightAndDoFluidPushing(final TagKey<Fluid> fluid, fin
continue;
}

final BlockCountingChunkSection blockCountingSection = (BlockCountingChunkSection)section;
final boolean hasFluids;
if (fluid == FluidTags.WATER) {
hasFluids = blockCountingSection.moonrise$hasWaterFluids();
} else if (fluid == FluidTags.LAVA) {
hasFluids = blockCountingSection.moonrise$hasLavaFluids();
} else {
hasFluids = blockCountingSection.moonrise$hasFluids();
}

if (!hasFluids) {
// also empty
continue;
}

final PalettedContainer<BlockState> blocks = section.states;

final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) : 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ca.spottedleaf.moonrise.neoforge.mixin.collisions;

import ca.spottedleaf.moonrise.neoforge.patches.collisions.FluidPushCalculation;
import ca.spottedleaf.moonrise.patches.block_counting.BlockCountingChunkSection;
import ca.spottedleaf.moonrise.patches.getblock.GetBlockLevel;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceArrayMap;
import it.unimi.dsi.fastutil.objects.Reference2ReferenceMap;
Expand Down Expand Up @@ -101,6 +102,11 @@ public void updateFluidHeightAndDoFluidPushing() {
continue;
}

if (!((BlockCountingChunkSection)section).moonrise$hasFluids()) {
// also empty
continue;
}

final PalettedContainer<BlockState> blocks = section.states;

final int minXIterate = currChunkX == minChunkX ? (minBlockX & 15) : 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.shorts.ShortArrayList;
import net.minecraft.util.BitStorage;
import net.minecraft.tags.FluidTags;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.chunk.Palette;
Expand Down Expand Up @@ -64,6 +65,15 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
@Unique
private short specialCollidingBlocks;

@Unique
private short fluids;

@Unique
private short lavaFluids;

@Unique
private short waterFluids;

@Unique
private final ShortList tickingBlocks = new ShortList();

Expand All @@ -77,6 +87,21 @@ abstract class LevelChunkSectionMixin implements BlockCountingChunkSection {
return this.tickingBlocks;
}

@Override
public final boolean moonrise$hasFluids() {
return this.fluids != 0;
}

@Override
public final boolean moonrise$hasLavaFluids() {
return this.lavaFluids != 0;
}

@Override
public final boolean moonrise$hasWaterFluids() {
return this.waterFluids != 0;
}

/**
* @reason Callback used to update block counts on block change.
* @author Spottedleaf
Expand Down Expand Up @@ -122,6 +147,45 @@ private void updateBlockCallback(final int x, final int y, final int z, final Bl
tickingBlocks.add(position);
}
}

final FluidState oldFluid = oldState.getFluidState();
final FluidState newFluid = newState.getFluidState();
final boolean hasOldFluid = !oldFluid.isEmpty();
final boolean hasNewFluid = !newFluid.isEmpty();
if (hasOldFluid != hasNewFluid) {
if (hasOldFluid) {
--this.fluids;
} else {
++this.fluids;
}
}
if (hasOldFluid || hasNewFluid) {
boolean fluidChecked = false;
final boolean isOldWater = oldFluid.is(FluidTags.WATER);
final boolean isNewWater = newFluid.is(FluidTags.WATER);

if (isOldWater != isNewWater) {
if (isOldWater) {
--this.waterFluids;
} else {
++this.waterFluids;
}
fluidChecked = true;
}

if (!fluidChecked) {
final boolean isOldLava = oldFluid.is(FluidTags.LAVA);
final boolean isNewLava = newFluid.is(FluidTags.LAVA);

if (isOldLava != isNewLava) {
if (isOldLava) {
--this.lavaFluids;
} else {
++this.lavaFluids;
}
}
}
}
}

/**
Expand Down Expand Up @@ -150,6 +214,9 @@ public void recalcBlockCounts() {
this.tickingBlockCount = (short)0;
this.tickingFluidCount = (short)0;
this.specialCollidingBlocks = (short)0;
this.fluids = (short)0;
this.lavaFluids = (short)0;
this.waterFluids = (short)0;
this.tickingBlocks.clear();

if (this.maybeHas((final BlockState state) -> !state.isAir())) {
Expand Down Expand Up @@ -204,6 +271,13 @@ public void recalcBlockCounts() {
if (fluid.isRandomlyTicking()) {
this.tickingFluidCount += (short)paletteCount;
}
this.fluids += (short)paletteCount;

if (fluid.is(FluidTags.WATER)) {
this.waterFluids += (short)paletteCount;
} else if (fluid.is(FluidTags.LAVA)) {
this.lavaFluids += (short)paletteCount;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ public interface BlockCountingChunkSection {

public ShortList moonrise$getTickingBlockList();

public boolean moonrise$hasFluids();

public boolean moonrise$hasLavaFluids();

public boolean moonrise$hasWaterFluids();

}