Skip to content

Commit

Permalink
switch to published model version
Browse files Browse the repository at this point in the history
  • Loading branch information
PssbleTrngle committed Oct 17, 2024
1 parent cbf0f41 commit 8af62cb
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ travelers_titles_version=oUSK4sOQ
yungs_api_version=PJOYAmAs
dynmap_version=RtI5TFAi
graphql_client_version=8.1.0
atheneum_version=0.0.0-dev
atheneum_version=0.0.7-alpha

repository=voidshake/atheneumconnector
modrinth_project_id=WIPHNZss
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.possible_triangle.atheneum_connector

import com.mojang.datafixers.util.Either
import com.possible_triangle.atheneum_connector.generated.AreasQuery
import com.possible_triangle.atheneum_connector.generated.PlacesQuery
import com.possible_triangle.atheneum_connector.generated.areasquery.Area
import com.possible_triangle.atheneum_connector.generated.areasquery.FlatPoint
import com.possible_triangle.atheneum_connector.generated.placesquery.Place
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import net.minecraft.core.BlockPos
import net.minecraft.core.SectionPos.blockToSectionCoord
Expand All @@ -14,29 +16,55 @@ import net.minecraft.resources.ResourceLocation
import net.minecraft.world.level.ChunkPos
import net.minecraft.world.level.Level

class LocationCache<T> {
class LocationCache<T>(private val getId: (T) -> Int) {

private val lookup = hashMapOf<ResourceKey<Level>, MutableMap<ChunkPos, T>>()
private val reverseLookup = hashMapOf<Int, Pair<ResourceKey<Level>, List<ChunkPos>>>()

fun get(level: ResourceKey<Level>, chunk: ChunkPos): T? {
return lookup[level]?.get(chunk)
}

fun set(level: ResourceKey<Level>, value: T, chunks: List<ChunkPos>) {
reverseLookup[getId(value)] = level to chunks

lookup.getOrPut(level, ::hashMapOf).apply {
chunks.forEach { put(it, value) }
}
}

companion object {
private val AREAS = LocationCache<Area>()
private val AREAS = LocationCache<Area> { it.id }
private val PLACES = LocationCache<Place> { it.id }

fun reload() {
AREAS.clear()
PLACES.clear()
initialize()
}

private fun String.levelKey() = ResourceKey.create(Registries.DIMENSION, ResourceLocation(this))

private fun chunkAt(x: Int, z: Int) = ChunkPos(
blockToSectionCoord(x),
blockToSectionCoord(z),
)

fun initialize() = runBlocking {
val areas = GraphQL.query(AreasQuery()).areas
areas.nodes.forEach { area ->
val level = ResourceKey.create(Registries.DIMENSION, ResourceLocation(area.world))
val areasQuery = async { GraphQL.query(AreasQuery()) }
val placesQuery = async { GraphQL.query(PlacesQuery()) }

areasQuery.await().areas.nodes.forEach { area ->
val level = area.world.levelKey()
val chunks = area.points.containingChunks()
AREAS.reverseLookup[area.id] = level to chunks
AREAS.set(level, area, chunks)
}

AREAS.lookup.getOrPut(level, ::hashMapOf).apply {
chunks.forEach { put(it, area) }
}
placesQuery.await().places.nodes.forEach { place ->
val level = place.pos.world.levelKey()
val chunks = listOf(chunkAt(place.pos.x, place.pos.z))

PLACES.set(level, place, chunks)
}
}

Expand Down Expand Up @@ -69,20 +97,17 @@ class LocationCache<T> {
}

odd
}.map {
ChunkPos(
blockToSectionCoord(it.x),
blockToSectionCoord(it.z),
)
}
}.map { chunkAt(it.x, it.z) }
}

fun containing(level: ResourceKey<Level>, pos: BlockPos): Either<Area, Place>? {
val chunk = ChunkPos(pos)

val area = AREAS.lookup[level]?.get(chunk)

return area?.let { Either.left(area) }
return PLACES.get(level, chunk)?.let {
Either.right(it)
} ?: AREAS.get(level, chunk)?.let {
Either.left(it)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ object PlayerEvents {
val location = LocationCache.containing(level.dimension(), player.onPos) ?: return

location.ifLeft {
Network.sendTo(player, DisplayTitlePaket(Component.literal("You entered ${it.name}"), "area-${it.id}"))
Network.sendTo(player, DisplayTitlePaket(Component.translatable("travelerstitles.entered_area", it.name), "area-${it.id}"))
}

location.ifRight {
Network.sendTo(
player,
DisplayTitlePaket(Component.literal("You are close to ${it.name}"), "place-${it.id}")
DisplayTitlePaket(Component.translatable("travelerstitles.at_place", it.name), "place-${it.id}")
)
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/assets/travelerstitles/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"travelerstitles.at_place": "%s",
"travelerstitles.entered_area": "%s"
}

0 comments on commit 8af62cb

Please sign in to comment.