mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-09 23:42:35 -04:00
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:
parent
1be37cd9ba
commit
a80ced4cf1
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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.BooleanUtil.toBoolean
|
||||||
import de.bixilon.kutil.primitive.FloatUtil.toFloat
|
import de.bixilon.kutil.primitive.FloatUtil.toFloat
|
||||||
import de.bixilon.kutil.primitive.IntUtil.toInt
|
import de.bixilon.kutil.primitive.IntUtil.toInt
|
||||||
|
import de.bixilon.kutil.reflection.ReflectionUtil.field
|
||||||
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
||||||
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactories
|
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactories
|
||||||
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactory
|
import de.bixilon.minosoft.data.registries.blocks.factory.PixLyzerBlockFactory
|
||||||
@ -128,7 +129,7 @@ open class PixLyzerBlock(
|
|||||||
companion object : IdentifierCodec<Block>, PixLyzerBlockFactory<Block>, MultiClassFactory<Block> {
|
companion object : IdentifierCodec<Block>, PixLyzerBlockFactory<Block>, MultiClassFactory<Block> {
|
||||||
private val NULL_OFFSET_XYZ = Vec3i(0, 0, 0).getWorldOffset(RandomOffsetTypes.XYZ)
|
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 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 val ALIASES: Set<String> = setOf("Block")
|
||||||
|
|
||||||
override fun deserialize(registries: Registries?, identifier: ResourceLocation, data: Map<String, Any>): Block {
|
override fun deserialize(registries: Registries?, identifier: ResourceLocation, data: Map<String, Any>): Block {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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
|
package de.bixilon.minosoft.data.registries.blocks.types.pixlyzer.entity
|
||||||
|
|
||||||
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
||||||
|
import de.bixilon.kutil.reflection.ReflectionUtil.field
|
||||||
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
||||||
import de.bixilon.minosoft.data.entities.block.BlockEntity
|
import de.bixilon.minosoft.data.entities.block.BlockEntity
|
||||||
import de.bixilon.minosoft.data.registries.blocks.entites.BlockEntityType
|
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)
|
override fun createBlockEntity(connection: PlayConnection) = blockEntity?.factory?.build(connection)
|
||||||
|
|
||||||
private companion object {
|
private companion object {
|
||||||
val FACTORY_FIELD = PixLyzerBlockWithEntity<*>::blockEntity.jvmField
|
val FACTORY_FIELD = PixLyzerBlockWithEntity<*>::blockEntity.jvmField.field
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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 companion object {
|
||||||
private val BLOCK_FIELD = BlockItem<*>::block.jvmField
|
private val BLOCK_FIELD = BlockItem<*>::block.jvmField.field
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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
|
package de.bixilon.minosoft.data.registries.item.items.block.legacy
|
||||||
|
|
||||||
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
||||||
|
import de.bixilon.kutil.reflection.ReflectionUtil.field
|
||||||
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
import de.bixilon.kutil.reflection.ReflectionUtil.jvmField
|
||||||
import de.bixilon.minosoft.camera.target.targets.BlockTarget
|
import de.bixilon.minosoft.camera.target.targets.BlockTarget
|
||||||
import de.bixilon.minosoft.data.container.stack.ItemStack
|
import de.bixilon.minosoft.data.container.stack.ItemStack
|
||||||
@ -52,7 +53,7 @@ open class PixLyzerBlockItem(
|
|||||||
|
|
||||||
companion object : PixLyzerItemFactory<PixLyzerBlockItem>, MultiClassFactory<PixLyzerBlockItem> {
|
companion object : PixLyzerItemFactory<PixLyzerBlockItem>, MultiClassFactory<PixLyzerBlockItem> {
|
||||||
override val ALIASES = setOf("BlockItem", "AliasedBlockItem")
|
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 {
|
override fun build(resourceLocation: ResourceLocation, registries: Registries, data: Map<String, Any>): PixLyzerBlockItem {
|
||||||
return PixLyzerBlockItem(resourceLocation, registries, data)
|
return PixLyzerBlockItem(resourceLocation, registries, data)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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
|
package de.bixilon.minosoft.data.registries.registries.registry
|
||||||
|
|
||||||
import de.bixilon.kutil.cast.CastUtil.unsafeNull
|
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.ReflectionUtil.jvmField
|
||||||
|
import de.bixilon.kutil.reflection.wrapper.ObjectField
|
||||||
import de.bixilon.minosoft.data.registries.identified.Identified
|
import de.bixilon.minosoft.data.registries.identified.Identified
|
||||||
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
import de.bixilon.minosoft.data.registries.identified.ResourceLocation
|
||||||
import de.bixilon.minosoft.data.registries.registries.Registries
|
import de.bixilon.minosoft.data.registries.registries.Registries
|
||||||
@ -24,14 +25,16 @@ import kotlin.reflect.KProperty
|
|||||||
|
|
||||||
abstract class RegistryItem : Identified {
|
abstract class RegistryItem : Identified {
|
||||||
open val injectable: Boolean get() = true
|
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 {
|
fun <T : RegistryItem> KProperty<T?>.inject(vararg keys: Any?): T {
|
||||||
return this.jvmField.inject(*keys)
|
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 (!injectable) throw IllegalStateException("Not injectable")
|
||||||
|
if (keys.isEmpty()) return unsafeNull()
|
||||||
|
|
||||||
val list: MutableList<Any> = ArrayList(keys.size)
|
val list: MutableList<Any> = ArrayList(keys.size)
|
||||||
for (key in keys) {
|
for (key in keys) {
|
||||||
if (key == null) continue
|
if (key == null) continue
|
||||||
@ -40,21 +43,26 @@ abstract class RegistryItem : Identified {
|
|||||||
if (list.isEmpty()) return unsafeNull()
|
if (list.isEmpty()) return unsafeNull()
|
||||||
injects[this] = list
|
injects[this] = list
|
||||||
return unsafeNull()
|
return unsafeNull()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T : RegistryItem> Field.inject(vararg keys: Any?): T {
|
||||||
|
return this.field.inject(*keys)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun inject(registries: Registries) {
|
fun inject(registries: Registries) {
|
||||||
if (!injectable) {
|
if (!injectable || injects.isEmpty()) return
|
||||||
return
|
|
||||||
}
|
|
||||||
for ((field, keys) in injects) {
|
for ((field, keys) in injects) {
|
||||||
var value: Any? = null
|
var value: Any? = null
|
||||||
for (key in keys) {
|
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
|
break
|
||||||
}
|
}
|
||||||
value ?: continue
|
if (value == null) continue
|
||||||
|
|
||||||
field.forceSet(this, value)
|
field.set(this, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
INJECTS_FIELD.set(this, null)
|
INJECTS_FIELD.set(this, null)
|
||||||
@ -79,6 +87,6 @@ abstract class RegistryItem : Identified {
|
|||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private val INJECTS_FIELD = RegistryItem::injects.jvmField
|
private val INJECTS_FIELD = RegistryItem::injects.jvmField.field
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
/*
|
/*
|
||||||
* Minosoft
|
* 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.
|
* 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
|
package de.bixilon.minosoft.gui.rendering.tint.tints.grass
|
||||||
|
|
||||||
import de.bixilon.kutil.reflection.ReflectionUtil.forceSet
|
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.TintManager
|
||||||
import de.bixilon.minosoft.gui.rendering.tint.TintedBlock
|
import de.bixilon.minosoft.gui.rendering.tint.TintedBlock
|
||||||
|
|
||||||
interface GrassTinted : TintedBlock {
|
interface GrassTinted : TintedBlock {
|
||||||
|
|
||||||
override fun initTint(manager: TintManager) {
|
override fun initTint(manager: TintManager) {
|
||||||
this::class.java.getFieldOrNull("tintProvider")!!.forceSet(this, manager.grass)
|
this::class.java.getUnsafeField("tintProvider").forceSet(this, manager.grass)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user