Skip to content

Commit

Permalink
Additional performance improvements for anomalies
Browse files Browse the repository at this point in the history
  • Loading branch information
Adubbz committed Jan 21, 2024
1 parent 3079ced commit 55342bf
Showing 1 changed file with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import biomesoplenty.api.block.BOPBlockEntities;
import biomesoplenty.block.AnomalyBlock;
import com.google.common.base.Suppliers;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Registry;
import net.minecraft.core.registries.Registries;
Expand All @@ -18,29 +19,41 @@
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;

import java.util.LinkedHashSet;
import java.util.Map;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public class AnomalyBlockEntity extends BlockEntity
{
private long lastTime = -1;
private BlockState lastState = null;

private final Supplier<LinkedHashSet<BlockState>> renderStates = Suppliers.memoize(() -> {
Registry<Block> blockRegistry = level.registryAccess().registryOrThrow(Registries.BLOCK);
return blockRegistry.entrySet().stream().map(e -> e.getValue().defaultBlockState()).filter(state -> state.getRenderShape() == RenderShape.MODEL).collect(Collectors.toCollection(LinkedHashSet::new));
});

public AnomalyBlockEntity(BlockPos pos, BlockState state) {
super(BOPBlockEntities.ANOMALY, pos, state);
}

public BlockState getRenderState()
{
Level level = this.getLevel();
final long time = level.getGameTime();

if (level == null)
return Blocks.AIR.defaultBlockState();

final long time = level.getGameTime();
if (lastTime == time && lastState != null)
return lastState;

RandomSource random = RandomSource.create(Mth.getSeed(this.getBlockPos()));
BlockState state = this.getBlockState();
Registry<Block> blockRegistry = level.registryAccess().registryOrThrow(Registries.BLOCK);
int index = random.nextInt(blockRegistry.size());

final var renderStates = this.renderStates.get();
int index = random.nextInt(renderStates.size());

switch (state.getValue(AnomalyBlock.ANOMALY_TYPE))
{
Expand All @@ -55,15 +68,8 @@ public BlockState getRenderState()
}
}

index %= blockRegistry.size();
BlockState renderState = Blocks.AIR.defaultBlockState();

while (renderState.getRenderShape() != RenderShape.MODEL)
{
Block renderBlock = blockRegistry.entrySet().stream().skip(index).map(Map.Entry::getValue).findFirst().orElseThrow();
renderState = renderBlock.defaultBlockState();
index = (index + 1) % blockRegistry.size();
}
index %= renderStates.size();
BlockState renderState = renderStates.stream().skip(index).findFirst().orElseThrow();

lastState = renderState;
lastTime = time;
Expand Down

0 comments on commit 55342bf

Please sign in to comment.