From 902190a7a9c20103f647063880f382abedda2ceb Mon Sep 17 00:00:00 2001 From: fzzyhmstrs <72876796+fzzyhmstrs@users.noreply.github.com> Date: Sun, 9 Jun 2024 17:01:12 -0400 Subject: [PATCH] Optimize tickAsh method in MixinServerWorld (#161) - Optimize tickAsh method in MixinServerWorld (fzzyhmstrs) * Update common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java Co-authored-by: haykam821 <24855774+haykam821@users.noreply.github.com> --- .../cinderscapes/mixin/MixinServerWorld.java | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java b/common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java index 706e12d..a0a6fed 100644 --- a/common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java +++ b/common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java @@ -16,7 +16,9 @@ import net.minecraft.world.World; import net.minecraft.world.biome.Biome; import net.minecraft.world.dimension.DimensionType; +import net.minecraft.world.dimension.DimensionTypes; import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; @@ -25,8 +27,8 @@ import java.util.function.Supplier; @Mixin(ServerWorld.class) -public abstract class MixinServerWorld extends World { - protected MixinServerWorld(MutableWorldProperties properties, RegistryKey registryRef, DynamicRegistryManager registryManager, RegistryEntry dimensionEntry, Supplier profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) { +abstract class MixinServerWorld extends World { + private MixinServerWorld(MutableWorldProperties properties, RegistryKey registryRef, DynamicRegistryManager registryManager, RegistryEntry dimensionEntry, Supplier profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) { super(properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess, maxChainedNeighborUpdates); } @@ -36,33 +38,38 @@ protected MixinServerWorld(MutableWorldProperties properties, RegistryKey */ @Inject(method="tickIceAndSnow", at = @At(value = "HEAD"), locals = LocalCapture.NO_CAPTURE) private void cinderscapes$tickAsh(BlockPos tickPos, CallbackInfo ci) { + // Ashy shoals only exists in the nether, why do this iteration on any other dimension + if (!getDimensionEntry().matchesKey(DimensionTypes.THE_NETHER)) return; if (CinderscapesConfig.INSTANCE.enableAshFall) { - BlockPos pos = tickPos.mutableCopy(); - BlockState state = getBlockState(pos); - RegistryEntry biome = this.getBiome(pos); + BlockPos.Mutable pos = tickPos.mutableCopy(); - while (pos.getY() < 127 && !( - biome.matchesKey(CinderscapesBiomes.ASHY_SHOALS) && - state.isSideSolidFullSquare(this, pos, Direction.UP) && - blockAbove(pos).isIn(CinderscapesBlockTags.ASH_PERMEABLE) && - this.getBlockState(pos.up()).isAir() && - CinderscapesBlocks.ASH.getDefaultState().canPlaceAt(this, pos.up()))) { - pos = pos.up(); - state = getBlockState(pos); - biome = this.getBiome(pos); - } - - if (pos.getY() < 127) { - this.setBlockState(pos.up(), CinderscapesBlocks.ASH.getDefaultState()); + for (; pos.getY() < 127; pos.setY(pos.getY() + 1)) { + BlockPos up = pos.up(); + if (!canPlace(up)) { + continue; + } else if (!this.getBiome(up).matchesKey(CinderscapesBiomes.ASHY_SHOALS)) { + continue; + } + this.setBlockState(up, CinderscapesBlocks.ASH.getDefaultState()); + break; } } } + @Unique + private boolean canPlace(BlockPos pos){ + return this.getBlockState(pos).isAir() && + blockAbove(pos).isIn(CinderscapesBlockTags.ASH_PERMEABLE) && + CinderscapesBlocks.ASH.getDefaultState().canPlaceAt(this, pos); + } + + @Unique private BlockState blockAbove(BlockPos pos) { - BlockPos iPos = pos.mutableCopy().up(); + BlockPos iPos = pos.mutableCopy(); + //up() makes new immutable blockpos per call. This uses the Mutable as a Mutable. //noinspection StatementWithEmptyBody - for (; isAir(iPos) && iPos.getY() < 127; iPos = iPos.up()); + for (; isAir(iPos) && iPos.getY() < 127; iPos.setY(iPos.getY() + 1)); return getBlockState(iPos); }