refactor(Interop): use of string builder and okio buffer (#5925)

This commit is contained in:
木葉 Scarlet
2025-03-29 00:24:51 +08:00
committed by GitHub
parent 956cdc9421
commit ef57d65f67
3 changed files with 25 additions and 35 deletions

View File

@@ -42,8 +42,8 @@ import net.minecraft.util.Identifier
import net.minecraft.util.math.BlockPos
import kotlin.jvm.optionals.getOrNull
val ACCEPTED_ITEM_TAGS
get() = arrayOf(
private val ACCEPTED_ITEM_TAGS =
arrayOf(
ItemTags.WOOL,
ItemTags.PLANKS,
ItemTags.STONE_BRICKS,
@@ -92,8 +92,8 @@ val ACCEPTED_ITEM_TAGS
ItemTags.SHOVELS,
)
val ACCEPTED_BLOCK_TAGS
get() = arrayOf(
private val ACCEPTED_BLOCK_TAGS =
arrayOf(
BlockTags.WOOL,
BlockTags.PLANKS,
BlockTags.STONE_BRICKS,
@@ -142,7 +142,7 @@ val ACCEPTED_BLOCK_TAGS
BlockTags.SNOW,
)
fun <T> constructMap(registry: DefaultedRegistry<T>, tagKeys: Array<TagKey<T>>): Map<Identifier, Identifier> {
private fun <T> constructMap(registry: DefaultedRegistry<T>, tagKeys: Array<TagKey<T>>): Map<Identifier, Identifier> {
val map = hashMapOf<Identifier, Identifier>()
for (acceptedTag in tagKeys) {
@@ -151,13 +151,12 @@ fun <T> constructMap(registry: DefaultedRegistry<T>, tagKeys: Array<TagKey<T>>):
get.forEach {
val itemId = registry.getId(it.value())
if (map.containsKey(itemId)) {
println("Duplicate $itemId in ${acceptedTag.id} in ${map[itemId]}")
val prev = map.putIfAbsent(itemId, acceptedTag.id)
if (prev != null) {
logger.warn("Duplicate $itemId in ${acceptedTag.id} in $prev")
return@forEach
}
map[itemId] = acceptedTag.id
}
}
@@ -207,7 +206,7 @@ fun getRegistries(requestObject: RequestObject) = httpOk(JsonObject().apply {
}
})
add("itemGroups", JsonObject().apply {
for ((k, v) in constructMap(Registries.ITEM, ACCEPTED_ITEM_TAGS).entries) {
for ((k, v) in constructMap(Registries.ITEM, ACCEPTED_ITEM_TAGS)) {
add(
k.toString(),
JsonObject().apply {
@@ -226,12 +225,14 @@ fun getRegistries(requestObject: RequestObject) = httpOk(JsonObject().apply {
val obj = when (id) {
in parentMap -> JsonObject().apply {
addProperty("relation", "parent")
addProperty("relative", parentMap[id].toString())
addProperty("relative", parentMap[id]!!.toString())
}
in constructedMap -> JsonObject().apply {
addProperty("relation", "group")
addProperty("relative", constructedMap[id].toString())
addProperty("relative", constructedMap[id]!!.toString())
}
else -> return@forEach
}

View File

@@ -34,10 +34,6 @@ import net.minecraft.registry.Registries
import net.minecraft.registry.RegistryKey
import net.minecraft.registry.RegistryKeys
import net.minecraft.util.Identifier
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
import java.nio.channels.Channels
import java.nio.channels.WritableByteChannel
import java.util.*
import javax.imageio.ImageIO
import kotlin.jvm.optionals.getOrNull
@@ -71,14 +67,12 @@ fun getItemTexture(requestObject: RequestObject) = run {
val of = RegistryKey.of(RegistryKeys.ITEM, alternativeIdentifier)
val resource = Registries.ITEM.get(of)
?: return@run httpBadRequest("Item not found")
val image = Registries.ITEM.get(of)?.let(ItemImageAtlas::getItemImage)
?: return@run httpBadRequest("Item image not found")
val writer = ByteArrayOutputStream(2048)
ImageIO.write(ItemImageAtlas.getItemImage(resource), "PNG", writer)
httpFileStream(ByteArrayInputStream(writer.toByteArray()))
val buffer = okio.Buffer()
ImageIO.write(image, "PNG", buffer.outputStream())
httpFileStream(buffer.inputStream())
}
// GET /api/v1/client/skin
@@ -90,15 +84,9 @@ fun getSkin(requestObject: RequestObject) = run {
val texture = mc.textureManager.getTexture(skinTextures.texture)
if (texture is NativeImageBackedTexture) {
val outputStream = ByteArrayOutputStream()
val channel: WritableByteChannel = Channels.newChannel(outputStream)
texture.image?.write(channel) ?: return@run httpInternalServerError("Texture is not cached yet")
channel.close()
ByteArrayInputStream(outputStream.toByteArray()).use {
httpFileStream(it)
}
val buffer = okio.Buffer()
texture.image?.write(buffer) ?: return@run httpInternalServerError("Texture is not cached yet")
httpFileStream(buffer.inputStream())
} else {
val resource = mc.resourceManager.getResource(skinTextures.texture)
.getOrNull() ?: return@run httpInternalServerError("Texture not found")

View File

@@ -34,8 +34,6 @@ fun String.stripMinecraftColorCodes(): String {
return COLOR_PATTERN.matcher(this).replaceAll("")
}
fun text(): MutableText = Text.literal("")
fun String.asText(): MutableText = Text.literal(this)
fun Text.asNbt(world: World? = null): NbtString =
@@ -43,7 +41,10 @@ fun Text.asNbt(world: World? = null): NbtString =
Text.Serialization.toJsonString(this, world?.registryManager ?: DynamicRegistryManager.EMPTY)
)
fun Text.convertToString(): String = "${string}${siblings.joinToString(separator = "") { it.convertToString() }}"
fun Text.convertToString(): String = buildString {
append(string)
siblings.forEach { append(it.convertToString()) }
}
fun OrderedText.toText(): Text {
val textSnippets = mutableListOf<Pair<String, Style>>()