From 63944456c61625a6559d02512f67c8b0e83c246e Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sat, 1 Apr 2023 16:20:17 +0700 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=86=D0=B5=D0=BF=D1=82=D1=8B=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BF=D0=B5=D1=80=D1=8C=20=D0=B7=D0=B0=D0=B2=D1=91?= =?UTF-8?q?=D1=80=D0=BD=D1=83=D1=82=D1=8B=20=D0=B2=20RegistryObject?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dbotthepony/kstarbound/RecipeRegistry.kt | 40 ++++++++++--------- .../ru/dbotthepony/kstarbound/Starbound.kt | 6 ++- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/RecipeRegistry.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/RecipeRegistry.kt index 4e048442..b479847f 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/RecipeRegistry.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/RecipeRegistry.kt @@ -1,44 +1,48 @@ package ru.dbotthepony.kstarbound +import com.google.gson.Gson +import com.google.gson.JsonObject import it.unimi.dsi.fastutil.objects.Object2ObjectFunction import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap +import ru.dbotthepony.kstarbound.api.IStarboundFile import ru.dbotthepony.kstarbound.defs.player.RecipeDefinition import java.util.Collections class RecipeRegistry { - private val recipesInternal = ArrayList() - private val group2recipesInternal = Object2ObjectOpenHashMap>() - private val group2recipesBacking = Object2ObjectOpenHashMap>() - private val output2recipesInternal = Object2ObjectOpenHashMap>() - private val output2recipesBacking = Object2ObjectOpenHashMap>() - private val input2recipesInternal = Object2ObjectOpenHashMap>() - private val input2recipesBacking = Object2ObjectOpenHashMap>() + private val recipesInternal = ArrayList>() + private val group2recipesInternal = Object2ObjectOpenHashMap>>() + private val group2recipesBacking = Object2ObjectOpenHashMap>>() + private val output2recipesInternal = Object2ObjectOpenHashMap>>() + private val output2recipesBacking = Object2ObjectOpenHashMap>>() + private val input2recipesInternal = Object2ObjectOpenHashMap>>() + private val input2recipesBacking = Object2ObjectOpenHashMap>>() - val recipes: List = Collections.unmodifiableList(recipesInternal) - val group2recipes: Map> = Collections.unmodifiableMap(group2recipesBacking) - val output2recipes: Map> = Collections.unmodifiableMap(output2recipesBacking) - val input2recipes: Map> = Collections.unmodifiableMap(input2recipesBacking) + val recipes: List> = Collections.unmodifiableList(recipesInternal) + val group2recipes: Map>> = Collections.unmodifiableMap(group2recipesBacking) + val output2recipes: Map>> = Collections.unmodifiableMap(output2recipesBacking) + val input2recipes: Map>> = Collections.unmodifiableMap(input2recipesBacking) - fun add(recipe: RecipeDefinition) { + fun add(recipe: RegistryObject) { + val value = recipe.value recipesInternal.add(recipe) - for (group in recipe.groups) { + for (group in value.groups) { group2recipesInternal.computeIfAbsent(group, Object2ObjectFunction { p -> - ArrayList().also { + ArrayList>().also { group2recipesBacking[p as String] = Collections.unmodifiableList(it) } }).add(recipe) } - output2recipesInternal.computeIfAbsent(recipe.output.item.name, Object2ObjectFunction { p -> - ArrayList().also { + output2recipesInternal.computeIfAbsent(value.output.item.name, Object2ObjectFunction { p -> + ArrayList>().also { output2recipesBacking[p as String] = Collections.unmodifiableList(it) } }).add(recipe) - for (input in recipe.input) { + for (input in value.input) { input2recipesInternal.computeIfAbsent(input.item.name, Object2ObjectFunction { p -> - ArrayList().also { + ArrayList>().also { input2recipesBacking[p as String] = Collections.unmodifiableList(it) } }).add(recipe) diff --git a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt index a40720b3..a9309d5d 100644 --- a/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt +++ b/src/main/kotlin/ru/dbotthepony/kstarbound/Starbound.kt @@ -467,7 +467,7 @@ class Starbound : ISBFileLocator { state.setTableFunction("recipesForItem", this) { args -> args.lua.push(JsonArray().also { a -> - recipeRegistry.output2recipes[args.getString()]?.stream()?.map { gson.toJsonTree(it) }?.forEach { + recipeRegistry.output2recipes[args.getString()]?.stream()?.map { it.toJson() }?.forEach { a.add(it) } }) @@ -986,7 +986,9 @@ class Starbound : ISBFileLocator { for (listedFile in files) { try { callback("Loading $listedFile") - recipeRegistry.add(gson.fromJson(listedFile.reader(), RecipeDefinition::class.java)) + val json = gson.fromJson(listedFile.reader(), JsonElement::class.java) + val value = gson.fromJson(JsonTreeReader(json), RecipeDefinition::class.java) + recipeRegistry.add(RegistryObject(value, json, listedFile, gson, pathStack)) } catch (err: Throwable) { logger.error("Loading recipe definition file $listedFile", err) }