Skip to content
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
Expand Up @@ -5,6 +5,7 @@
import at.petrak.hexcasting.api.casting.eval.vm.CastingImage;
import at.petrak.hexcasting.api.misc.Result;
import at.petrak.hexcasting.api.pigment.FrozenPigment;
import at.petrak.hexcasting.api.utils.ChunkScanning;
import at.petrak.hexcasting.api.utils.HexUtils;
import com.mojang.datafixers.util.Pair;
import net.minecraft.ChatFormatting;
Expand Down Expand Up @@ -95,13 +96,17 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
todo.add(Pair.of(impetus.getStartDirection(), impetus.getBlockPos().relative(impetus.getStartDirection())));
var seenGoodPosSet = new HashSet<BlockPos>();
var seenGoodPositions = new ArrayList<BlockPos>();
var scanning = new ChunkScanning(level);

while (!todo.isEmpty()) {
var pair = todo.pop();
var enterDir = pair.getFirst();
var herePos = pair.getSecond();
var hereBs = scanning.getBlock(herePos);

var hereBs = level.getBlockState(herePos);
if (hereBs == null){
continue;
}
if (!(hereBs.getBlock() instanceof ICircleComponent cmp)) {
continue;
}
Expand All @@ -118,6 +123,7 @@ protected CircleExecutionState(BlockPos impetusPos, Direction impetusDir, Set<Bl
}
}
}
scanning.clearCache();

if (seenGoodPositions.isEmpty()) {
return new Result.Err<>(null);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package at.petrak.hexcasting.api.utils

import at.petrak.hexcasting.api.HexAPI
import it.unimi.dsi.fastutil.longs.Long2ObjectLinkedOpenHashMap
import net.minecraft.core.BlockPos
import net.minecraft.server.level.ServerLevel
import net.minecraft.world.level.ChunkPos
import net.minecraft.world.level.block.entity.BlockEntity
import net.minecraft.world.level.block.state.BlockState
import net.minecraft.world.level.chunk.ChunkStatus
import net.minecraft.world.level.chunk.ImposterProtoChunk

/**
* This is a helper class to efficiently scan chunks in ways Minecraft did not intend for. This is for only reading chunks, not writing
*/
class ChunkScanning(var level: ServerLevel) {
var chunks: Long2ObjectLinkedOpenHashMap<ImposterProtoChunk> = Long2ObjectLinkedOpenHashMap()

/**
* This attempts to cache a chunk to the local [chunks]
* @param ChunkPos the chunk to try to cache
* @return If the function could cache the chunk or not
*/
fun cacheChunk(chunk: ChunkPos): Boolean {
val chunkLong = chunk.toLong()
// We have the chunk already, so we can skip it
if (chunks.contains(chunkLong)){
return true
}
val future = level.chunkSource.getChunkFuture(chunk.x,chunk.z, ChunkStatus.EMPTY,true).get()
if (future.left().isPresent){
chunks.put(chunkLong, future.left().get() as ImposterProtoChunk)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this cast safe?

return true
}
HexAPI.LOGGER.warn("Failed to get chunk at {}!",chunk)
return false
}

fun cacheChunk(chunk: Long): Boolean{
return cacheChunk(ChunkPos(chunk))
}

fun getBlock(blockPos: BlockPos): BlockState? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockState(blockPos)
}

fun getBlockEntity(blockPos: BlockPos): BlockEntity? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockEntity(blockPos)
}
Comment on lines +24 to +57
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fun cacheChunk(chunk: ChunkPos): Boolean {
val chunkLong = chunk.toLong()
// We have the chunk already, so we can skip it
if (chunks.contains(chunkLong)){
return true
}
val future = level.chunkSource.getChunkFuture(chunk.x,chunk.z, ChunkStatus.EMPTY,true).get()
if (future.left().isPresent){
chunks.put(chunkLong, future.left().get() as ImposterProtoChunk)
return true
}
HexAPI.LOGGER.warn("Failed to get chunk at {}!",chunk)
return false
}
fun cacheChunk(chunk: Long): Boolean{
return cacheChunk(ChunkPos(chunk))
}
fun getBlock(blockPos: BlockPos): BlockState? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockState(blockPos)
}
fun getBlockEntity(blockPos: BlockPos): BlockEntity? {
val chunkPos = ChunkPos(blockPos).toLong()
if (!cacheChunk(chunkPos)){
return null
}
return chunks.get(chunkPos).getBlockEntity(blockPos)
}
fun cacheChunk(chunk: ChunkPos): ImposterProtoChunk? {
val chunkLong = chunk.toLong()
// If we have the chunk already, we can skip fetching it
val existing = chunks.get(chunkLong)
if (existing != null){
return existing
}
val future = level.chunkSource.getChunkFuture(chunk.x,chunk.z, ChunkStatus.EMPTY,true).get()
if (future.left().isPresent){
val next = future.left().get() as ImposterProtoChunk
chunks.put(chunkLong, next)
return next
}
HexAPI.LOGGER.warn("Failed to get chunk at {}!",chunk)
return null
}
fun cacheChunk(chunk: Long): ImposterProtoChunk? {
return cacheChunk(ChunkPos(chunk))
}
fun getBlock(blockPos: BlockPos): BlockState? {
val chunkPos = ChunkPos(blockPos).toLong()
return cacheChunk(chunkPos)?.getBlockState(blockPos)
}
fun getBlockEntity(blockPos: BlockPos): BlockEntity? {
val chunkPos = ChunkPos(blockPos).toLong()
return cacheChunk(chunkPos)?.getBlockEntity(blockPos)
}


// Maybe not required, but still not a bad idea to have a Clear method
fun clearCache(){
chunks.clear()
}

// Might not be needed
fun containsChunk(chunk: ChunkPos): Boolean{
return chunks.contains(chunk.toLong())
}
}