optimize reflection usafe

KUtil 1.26 got some fancy new reflection utilities. Now used. Still depends on KUtil 1.26.1 (for field::type)
This commit is contained in:
Moritz Zwerger 2024-02-02 20:21:36 +01:00
parent 1be37cd9ba
commit a80ced4cf1
No known key found for this signature in database
GPG Key ID: 5CAD791931B09AC4
6 changed files with 33 additions and 22 deletions

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -21,6 +21,7 @@ import de.bixilon.kutil.json.JsonUtil.asJsonObject
import de.bixilon.kutil.primitive.BooleanUtil.toBoolean
import de.bixilon.kutil.primitive.FloatUtil.toFloat
import de.bixilon.kutil.primitive.IntUtil.toInt
import de.bixilon.kutil.reflection.ReflectionUtil.field
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactories
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactory
@ -128,7 +129,7 @@ open class PixLyzerBlock(
companion object : IdentifierCodec<Block>, PixLyzerBlockFactory<Block>, MultiClassFactory<Block> {
private val NULL_OFFSET_XYZ = Vec3i(0, 0, 0).getWorldOffset(RandomOffsetTypes.XYZ)
private val NULL_OFFSET_XZ = Vec3i(0, 0, 0).getWorldOffset(RandomOffsetTypes.XZ)
private val ITEM_FIELD = PixLyzerBlock::item.jvmField
private val ITEM_FIELD = PixLyzerBlock::item.jvmField.field
override val ALIASES: Set<String> = setOf("Block")
override fun deserialize(registries: Registries?, identifier: ResourceLocation, data: Map<String, Any>): Block {

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.registries.blocks.types.pixlyzer.entity
import de.bixilon.kutil.cast.CastUtil.unsafeNull
import de.bixilon.kutil.reflection.ReflectionUtil.field
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
import de.bixilon.minosoft.data.entities.block.BlockEntity
import de.bixilon.minosoft.data.registries.blocks.entites.BlockEntityType
@ -35,6 +36,6 @@ abstract class PixLyzerBlockWithEntity<T : BlockEntity>(resourceLocation: Resour
override fun createBlockEntity(connection: PlayConnection) = blockEntity?.factory?.build(connection)
private companion object {
val FACTORY_FIELD = PixLyzerBlockWithEntity<*>::blockEntity.jvmField
val FACTORY_FIELD = PixLyzerBlockWithEntity<*>::blockEntity.jvmField.field
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -44,6 +44,6 @@ abstract class BlockItem<T : Block>(identifier: ResourceLocation) : Item(identif
private companion object {
private val BLOCK_FIELD = BlockItem<*>::block.jvmField
private val BLOCK_FIELD = BlockItem<*>::block.jvmField.field
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -14,6 +14,7 @@
package de.bixilon.minosoft.data.registries.item.items.block.legacy
import de.bixilon.kutil.cast.CastUtil.unsafeNull
import de.bixilon.kutil.reflection.ReflectionUtil.field
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
import de.bixilon.minosoft.camera.target.targets.BlockTarget
import de.bixilon.minosoft.data.container.stack.ItemStack
@ -52,7 +53,7 @@ open class PixLyzerBlockItem(
companion object : PixLyzerItemFactory<PixLyzerBlockItem>, MultiClassFactory<PixLyzerBlockItem> {
override val ALIASES = setOf("BlockItem", "AliasedBlockItem")
private val BLOCK_FIELD = PixLyzerBlockItem::block.jvmField
private val BLOCK_FIELD = PixLyzerBlockItem::block.jvmField.field
override fun build(resourceLocation: ResourceLocation, registries: Registries, data: Map<String, Any>): PixLyzerBlockItem {
return PixLyzerBlockItem(resourceLocation, registries, data)

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -14,8 +14,9 @@
package de.bixilon.minosoft.data.registries.registries.registry
import de.bixilon.kutil.cast.CastUtil.unsafeNull
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
import de.bixilon.kutil.reflection.ReflectionUtil.field
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
import de.bixilon.kutil.reflection.wrapper.ObjectField
import de.bixilon.minosoft.data.registries.identified.Identified
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
import de.bixilon.minosoft.data.registries.registries.Registries
@ -24,14 +25,16 @@ import kotlin.reflect.KProperty
abstract class RegistryItem : Identified {
open val injectable: Boolean get() = true
private val injects: MutableMap<Field, List<Any>> = if (injectable) hashMapOf() else unsafeNull()
private val injects: MutableMap<ObjectField, List<Any>> = if (injectable) hashMapOf() else unsafeNull()
fun <T : RegistryItem> KProperty<T?>.inject(vararg keys: Any?): T {
return this.jvmField.inject(*keys)
}
fun <T : RegistryItem> Field.inject(vararg keys: Any?): T {
fun <T : RegistryItem> ObjectField.inject(vararg keys: Any?): T {
if (!injectable) throw IllegalStateException("Not injectable")
if (keys.isEmpty()) return unsafeNull()
val list: MutableList<Any> = ArrayList(keys.size)
for (key in keys) {
if (key == null) continue
@ -40,21 +43,26 @@ abstract class RegistryItem : Identified {
if (list.isEmpty()) return unsafeNull()
injects[this] = list
return unsafeNull()
}
fun <T : RegistryItem> Field.inject(vararg keys: Any?): T {
return this.field.inject(*keys)
}
fun inject(registries: Registries) {
if (!injectable) {
return
}
if (!injectable || injects.isEmpty()) return
for ((field, keys) in injects) {
var value: Any? = null
for (key in keys) {
value = registries[field.type as Class<out RegistryItem>]?.get(key) ?: continue
val registry = registries[field.type as Class<out RegistryItem>] ?: continue
value = registry[key] ?: continue
break
}
value ?: continue
if (value == null) continue
field.forceSet(this, value)
field.set(this, value)
}
INJECTS_FIELD.set(this, null)
@ -79,6 +87,6 @@ abstract class RegistryItem : Identified {
}
companion object {
private val INJECTS_FIELD = RegistryItem::injects.jvmField
private val INJECTS_FIELD = RegistryItem::injects.jvmField.field
}
}

View File

@ -1,6 +1,6 @@
/*
* Minosoft
* Copyright (C) 2020-2023 Moritz Zwerger
* Copyright (C) 2020-2024 Moritz Zwerger
*
* This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
*
@ -14,13 +14,13 @@
package de.bixilon.minosoft.gui.rendering.tint.tints.grass
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
import de.bixilon.kutil.reflection.ReflectionUtil.getFieldOrNull
import de.bixilon.kutil.reflection.ReflectionUtil.getUnsafeField
import de.bixilon.minosoft.gui.rendering.tint.TintManager
import de.bixilon.minosoft.gui.rendering.tint.TintedBlock
interface GrassTinted : TintedBlock {
override fun initTint(manager: TintManager) {
this::class.java.getFieldOrNull("tintProvider")!!.forceSet(this, manager.grass)
this::class.java.getUnsafeField("tintProvider").forceSet(this, manager.grass)
}
}