forked from FallingColors/HexMod
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChunkScanning.kt
More file actions
70 lines (62 loc) · 2.34 KB
/
ChunkScanning.kt
File metadata and controls
70 lines (62 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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
*
* TODO: MAKE DOC THIS BETTER
*/
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)
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)
}
// maybe not required, but still not a bad idea to have a Clear method
fun clearCache(){
chunks.clear()
}
// TODO: Might not need this
fun containsChunk(chunk: ChunkPos): Boolean{
return chunks.contains(chunk.toLong())
}
}