Skip to content

Commit

Permalink
Cache calculated height values
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Oct 18, 2024
1 parent 31fc890 commit 2e82a7a
Showing 1 changed file with 35 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.dimension.DimensionType;
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;
Expand All @@ -17,12 +16,23 @@
@Mixin(value = Level.class, priority = 1100)
abstract class LevelMixin implements LevelAccessor, AutoCloseable {

@Shadow
public abstract DimensionType dimensionType();
@Unique
private int height;

@Unique
private int minBuildHeight;

@Unique
private int maxBuildHeight;

@Unique
private int minSection;

@Unique
private int maxSection;

@Unique
private DimensionType dimensionType;
private int sectionsCount;

/**
* @reason Init min/max section
Expand All @@ -36,46 +46,43 @@ abstract class LevelMixin implements LevelAccessor, AutoCloseable {
)
private void init(final CallbackInfo ci,
@Local(ordinal = 0, argsOnly = true) final Holder<DimensionType> dimensionTypeHolder) {
this.dimensionType = dimensionTypeHolder.value();
final DimensionType dimType = dimensionTypeHolder.value();
this.height = dimType.height();
this.minBuildHeight = dimType.minY();
this.maxBuildHeight = this.minBuildHeight + this.height;
this.minSection = this.minBuildHeight >> 4;
this.maxSection = ((this.maxBuildHeight - 1) >> 4) + 1;
this.sectionsCount = this.maxSection - this.minSection;
}

@Override
public int getHeight() {
return this.dimensionType.height();
return this.height;
}

@Override
public int getMinBuildHeight() {
return this.dimensionType.minY();
return this.minBuildHeight;
}

@Override
public int getMaxBuildHeight() {
final DimensionType dimensionType = this.dimensionType;

return dimensionType.minY() + dimensionType.height();
return this.maxBuildHeight;
}

@Override
public int getMinSection() {
return this.dimensionType.minY() >> 4;
return this.minSection;
}

@Override
public int getMaxSection() {
final DimensionType dimensionType = this.dimensionType;

return (((dimensionType.minY() + dimensionType.height()) - 1) >> 4) + 1;
return this.maxSection;
}

@Override
public boolean isOutsideBuildHeight(final int y) {
final DimensionType dimensionType = this.dimensionType;

final int minBuildHeight = dimensionType.minY();
final int maxBuildHeight = minBuildHeight + dimensionType.height();

return y < minBuildHeight || y >= maxBuildHeight;
return y < this.minBuildHeight || y >= this.maxBuildHeight;
}

@Override
Expand All @@ -85,16 +92,21 @@ public boolean isOutsideBuildHeight(final BlockPos blockPos) {

@Override
public int getSectionIndex(final int blockY) {
return (blockY >> 4) - (this.dimensionType.minY() >> 4);
return (blockY >> 4) - this.minSection;
}

@Override
public int getSectionIndexFromSectionY(final int sectionY) {
return sectionY - (this.dimensionType.minY() >> 4);
return sectionY - this.minSection;
}

@Override
public int getSectionYFromSectionIndex(final int sectionIdx) {
return sectionIdx + (this.dimensionType.minY() >> 4);
return sectionIdx + this.minSection;
}

@Override
public int getSectionsCount() {
return this.sectionsCount;
}
}

0 comments on commit 2e82a7a

Please sign in to comment.