Don't computeIfAbsent so we can recursively compute stuff in future

This commit is contained in:
DBotThePony 2022-11-09 23:37:58 +07:00
parent 3a589f53ec
commit 00c02aab82
Signed by: DBot
GPG Key ID: DCC23B5715498507

View File

@ -6,7 +6,6 @@ import com.google.gson.JsonElement
import com.google.gson.JsonObject
import com.google.gson.JsonParseException
import com.mojang.blaze3d.platform.InputConstants
import it.unimi.dsi.fastutil.objects.Reference2ObjectFunction
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap
import net.minecraft.ChatFormatting
import net.minecraft.resources.ResourceLocation
@ -114,6 +113,35 @@ object MatterManager : SimpleJsonResourceReloadListener(GsonBuilder().setPrettyP
private val computedEntries = Reference2ObjectOpenHashMap<Item, Entry>()
val computedEntriesView: Map<Item, Entry> = Collections.unmodifiableMap(computedEntries)
private fun doGetDirectInner(value: Item): Entry? {
val key = value.registryName ?: throw NullPointerException("$value has no registry name!")
val keyEntry = keyEntries[key]
if (keyEntry != null) {
return keyEntry
}
for (entry in tagEntries) {
if (entry.bound.contains(value)) {
return entry
}
}
return Entry.Companion
}
private fun getDirectInner(value: Item): Entry {
val result = doGetDirectInner(value)
if (result != null) {
computedEntries[value] = result
return result
}
return Entry.Companion
}
/**
* Returns directly defined matter value
*/
@ -128,24 +156,7 @@ object MatterManager : SimpleJsonResourceReloadListener(GsonBuilder().setPrettyP
}
synchronized(computedEntries) {
return computedEntries.computeIfAbsent(value, Reference2ObjectFunction {
it as Item
val key = it.registryName ?: throw NullPointerException("$it has no registry name!")
val keyEntry = keyEntries[key]
if (keyEntry != null) {
return@Reference2ObjectFunction keyEntry
}
for (entry in tagEntries) {
if (entry.bound.contains(it)) {
return@Reference2ObjectFunction entry
}
}
return@Reference2ObjectFunction Entry.Companion
})
return getDirectInner(value)
}
}