root.tenantConfig и root.getMatchingTenants
This commit is contained in:
parent
90040aec3a
commit
31c539948e
@ -14,6 +14,7 @@ import it.unimi.dsi.fastutil.objects.ObjectIterator
|
||||
import it.unimi.dsi.fastutil.objects.ObjectSet
|
||||
import org.apache.logging.log4j.LogManager
|
||||
import ru.dbotthepony.kstarbound.api.IStarboundFile
|
||||
import ru.dbotthepony.kstarbound.lua.LuaState
|
||||
import ru.dbotthepony.kstarbound.util.PathStack
|
||||
import java.util.*
|
||||
import kotlin.reflect.KClass
|
||||
@ -27,6 +28,14 @@ class RegistryObject<T : Any>(val value: T, private val json: JsonObject, val fi
|
||||
return json.deepCopy()
|
||||
}
|
||||
|
||||
fun push(lua: LuaState) {
|
||||
lua.push(json)
|
||||
}
|
||||
|
||||
fun push(lua: LuaState.ArgStack) {
|
||||
lua.push(json)
|
||||
}
|
||||
|
||||
override fun equals(other: Any?): Boolean {
|
||||
return other === this || other is RegistryObject<*> && other.value == value && other.json == json
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ import com.github.benmanes.caffeine.cache.Interner
|
||||
import com.google.gson.*
|
||||
import com.google.gson.internal.bind.JsonTreeReader
|
||||
import it.unimi.dsi.fastutil.Hash
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectFunction
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap
|
||||
@ -33,6 +34,7 @@ import ru.dbotthepony.kstarbound.defs.item.impl.LegsArmorItemPrototype
|
||||
import ru.dbotthepony.kstarbound.defs.item.impl.LiquidItemPrototype
|
||||
import ru.dbotthepony.kstarbound.defs.item.impl.MaterialItemPrototype
|
||||
import ru.dbotthepony.kstarbound.defs.npc.NpcTypeDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.npc.TenantDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.tile.LiquidDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.particle.ParticleDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.player.BlueprintLearnList
|
||||
@ -43,6 +45,7 @@ import ru.dbotthepony.kstarbound.defs.projectile.ProjectileDefinition
|
||||
import ru.dbotthepony.kstarbound.defs.quest.QuestTemplate
|
||||
import ru.dbotthepony.kstarbound.defs.tile.MaterialModifier
|
||||
import ru.dbotthepony.kstarbound.defs.tile.TileDefinition
|
||||
import ru.dbotthepony.kstarbound.util.JsonArrayCollector
|
||||
import ru.dbotthepony.kstarbound.io.*
|
||||
import ru.dbotthepony.kstarbound.io.json.AABBTypeAdapter
|
||||
import ru.dbotthepony.kstarbound.io.json.AABBiTypeAdapter
|
||||
@ -133,6 +136,9 @@ class Starbound : ISBFileLocator {
|
||||
private val _projectiles = ObjectRegistry("projectiles", ProjectileDefinition::projectileName)
|
||||
val projectiles = _projectiles.view
|
||||
|
||||
private val _tenants = ObjectRegistry("tenants", TenantDefinition::name)
|
||||
val tenants = _tenants.view
|
||||
|
||||
val recipeRegistry = RecipeRegistry()
|
||||
|
||||
val gson: Gson = with(GsonBuilder()) {
|
||||
@ -506,6 +512,34 @@ class Starbound : ISBFileLocator {
|
||||
1
|
||||
}
|
||||
|
||||
state.setTableFunction("tenantConfig", this) { args ->
|
||||
// Json root.tenantConfig(String tenantName)
|
||||
val name = args.getString()
|
||||
tenants[name]?.push(args) ?: throw NoSuchElementException("No such tenant $name")
|
||||
1
|
||||
}
|
||||
|
||||
state.setTableFunction("getMatchingTenants", this) { args ->
|
||||
// Json root.tenantConfig(String tenantName)
|
||||
val tags = args.getTable()
|
||||
val actualTags = Object2IntOpenHashMap<String>()
|
||||
|
||||
for ((k, v) in tags.entrySet()) {
|
||||
if (v is JsonPrimitive && v.isNumber) {
|
||||
actualTags[k] = v.asInt
|
||||
}
|
||||
}
|
||||
|
||||
args.push(tenants.values
|
||||
.stream()
|
||||
.filter { it.value.test(actualTags) }
|
||||
.sorted { a, b -> b.value.compareTo(a.value) }
|
||||
.map { it.copy() }
|
||||
.collect(JsonArrayCollector))
|
||||
|
||||
1
|
||||
}
|
||||
|
||||
state.pop()
|
||||
|
||||
state.load(polyfill, "@starbound.jar!/scripts/polyfill.lua")
|
||||
@ -714,6 +748,7 @@ class Starbound : ISBFileLocator {
|
||||
loadStage(callback, _techs, ext2files["tech"] ?: listOf())
|
||||
loadStage(callback, _npcTypes, ext2files["npctype"] ?: listOf())
|
||||
loadStage(callback, _projectiles, ext2files["projectile"] ?: listOf())
|
||||
loadStage(callback, _tenants, ext2files["tenant"] ?: listOf())
|
||||
|
||||
pathStack.block("/") {
|
||||
//playerDefinition = gson.fromJson(locate("/player.config").reader(), PlayerDefinition::class.java)
|
||||
|
@ -0,0 +1,21 @@
|
||||
package ru.dbotthepony.kstarbound.defs.npc
|
||||
|
||||
import com.google.common.collect.ImmutableMap
|
||||
import it.unimi.dsi.fastutil.objects.Object2IntMap
|
||||
import ru.dbotthepony.kstarbound.io.json.builder.JsonFactory
|
||||
import java.util.function.Predicate
|
||||
|
||||
@JsonFactory
|
||||
data class TenantDefinition(
|
||||
val name: String,
|
||||
val colonyTagCriteria: ImmutableMap<String, Int>,
|
||||
val priority: Double = 1.0
|
||||
) : Predicate<Object2IntMap<String>>, Comparable<TenantDefinition> {
|
||||
override fun test(t: Object2IntMap<String>): Boolean {
|
||||
return colonyTagCriteria.all { t.getInt(it.key) >= it.value }
|
||||
}
|
||||
|
||||
override fun compareTo(other: TenantDefinition): Int {
|
||||
return priority.compareTo(other.priority)
|
||||
}
|
||||
}
|
@ -0,0 +1,31 @@
|
||||
package ru.dbotthepony.kstarbound.util
|
||||
|
||||
import com.google.gson.JsonArray
|
||||
import com.google.gson.JsonElement
|
||||
import java.util.function.BiConsumer
|
||||
import java.util.function.BinaryOperator
|
||||
import java.util.function.Function
|
||||
import java.util.function.Supplier
|
||||
import java.util.stream.Collector
|
||||
|
||||
object JsonArrayCollector : Collector<JsonElement, JsonArray, JsonArray> {
|
||||
override fun supplier(): Supplier<JsonArray> {
|
||||
return Supplier { JsonArray() }
|
||||
}
|
||||
|
||||
override fun accumulator(): BiConsumer<JsonArray, JsonElement> {
|
||||
return BiConsumer { t, u -> t.add(u) }
|
||||
}
|
||||
|
||||
override fun combiner(): BinaryOperator<JsonArray> {
|
||||
return BinaryOperator { t, u -> t.addAll(u); t }
|
||||
}
|
||||
|
||||
override fun finisher(): Function<JsonArray, JsonArray> {
|
||||
return Function.identity()
|
||||
}
|
||||
|
||||
override fun characteristics(): Set<Collector.Characteristics> {
|
||||
return setOf(Collector.Characteristics.IDENTITY_FINISH)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user