Skip to content

Commit

Permalink
Optimize tickAsh method in MixinServerWorld (#161)
Browse files Browse the repository at this point in the history
- Optimize tickAsh method in MixinServerWorld (fzzyhmstrs)
* Update common/src/main/java/com/terraformersmc/cinderscapes/mixin/MixinServerWorld.java

Co-authored-by: haykam821 <[email protected]>
  • Loading branch information
fzzyhmstrs and haykam821 committed Jun 9, 2024
1 parent c935461 commit 902190a
Showing 1 changed file with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,8 +27,8 @@
import java.util.function.Supplier;

@Mixin(ServerWorld.class)
public abstract class MixinServerWorld extends World {
protected MixinServerWorld(MutableWorldProperties properties, RegistryKey<World> registryRef, DynamicRegistryManager registryManager, RegistryEntry<DimensionType> dimensionEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) {
abstract class MixinServerWorld extends World {
private MixinServerWorld(MutableWorldProperties properties, RegistryKey<World> registryRef, DynamicRegistryManager registryManager, RegistryEntry<DimensionType> dimensionEntry, Supplier<Profiler> profiler, boolean isClient, boolean debugWorld, long biomeAccess, int maxChainedNeighborUpdates) {
super(properties, registryRef, registryManager, dimensionEntry, profiler, isClient, debugWorld, biomeAccess, maxChainedNeighborUpdates);
}

Expand All @@ -36,33 +38,38 @@ protected MixinServerWorld(MutableWorldProperties properties, RegistryKey<World>
*/
@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> 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);
}
Expand Down

0 comments on commit 902190a

Please sign in to comment.