mirror of
https://gitlab.bixilon.de/bixilon/minosoft.git
synced 2025-09-17 11:24:56 -04:00
improve registry item post mapping
This commit is contained in:
parent
e592a02581
commit
657dcbc268
@ -21,7 +21,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
class EntityObjectType(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
companion object : ResourceLocationDeserializer<EntityObjectType> {
|
||||
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: JsonObject): EntityObjectType {
|
||||
|
@ -22,7 +22,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
class BlockEntityMetaType(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
companion object : ResourceLocationDeserializer<BlockEntityMetaType> {
|
||||
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: JsonObject): BlockEntityMetaType {
|
||||
|
@ -37,7 +37,7 @@ data class Dimension(
|
||||
val ultraWarm: Boolean = false,
|
||||
val height: Int = 256,
|
||||
val supports3DBiomes: Boolean = true,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
val lowestSection = if (minY < 0) {
|
||||
(minY + 1) / ProtocolDefinition.SECTION_HEIGHT_Y - 1
|
||||
} else {
|
||||
|
@ -21,7 +21,7 @@ data class Motive(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
val width: Int,
|
||||
val height: Int,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -21,7 +21,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
data class PluginChannel(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
val name: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -41,7 +41,7 @@ data class Biome(
|
||||
val grassColorOverride: RGBColor?,
|
||||
val descriptionId: String?,
|
||||
val grassColorModifier: GrassColorModifiers = GrassColorModifiers.NONE,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
val temperatureColorMapCoordinate = getColorMapCoordinate(temperature)
|
||||
val downfallColorMapCoordinate = getColorMapCoordinate(downfall * temperature)
|
||||
|
||||
|
@ -26,22 +26,9 @@ import de.bixilon.minosoft.protocol.network.connection.PlayConnection
|
||||
|
||||
data class BlockEntityType(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
private var blockIds: Set<Int>?,
|
||||
val blocks: Set<Block>,
|
||||
val factory: BlockEntityFactory<out BlockEntity>,
|
||||
) : RegistryItem {
|
||||
lateinit var blocks: Set<Block>
|
||||
private set
|
||||
|
||||
override fun postInit(registries: Registries) {
|
||||
val blocks: MutableSet<Block> = mutableSetOf()
|
||||
|
||||
for (blockId in blockIds!!) {
|
||||
blocks += registries.blockRegistry[blockId]
|
||||
}
|
||||
this.blockIds = null
|
||||
|
||||
this.blocks = blocks.toSet()
|
||||
}
|
||||
) : RegistryItem() {
|
||||
|
||||
fun build(connection: PlayConnection): BlockEntity {
|
||||
return DefaultBlockEntityMetaDataFactory.buildBlockEntity(factory, connection)
|
||||
@ -49,17 +36,18 @@ data class BlockEntityType(
|
||||
|
||||
companion object : ResourceLocationDeserializer<BlockEntityType> {
|
||||
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: JsonObject): BlockEntityType? {
|
||||
check(registries != null)
|
||||
val factory = DefaultBlockEntityMetaDataFactory[resourceLocation] ?: return null // ToDo
|
||||
|
||||
val blockIds: MutableSet<Int> = mutableSetOf()
|
||||
val blocks: MutableSet<Block> = mutableSetOf()
|
||||
|
||||
for (blockId in data["blocks"].asJsonArray) {
|
||||
blockIds += blockId.asInt
|
||||
for (block in data["blocks"].asJsonArray) {
|
||||
blocks += registries.blockRegistry[block]?:continue
|
||||
}
|
||||
|
||||
return BlockEntityType(
|
||||
resourceLocation = resourceLocation,
|
||||
blockIds = blockIds,
|
||||
blocks = blocks,
|
||||
factory = factory,
|
||||
)
|
||||
}
|
||||
|
@ -13,27 +13,32 @@
|
||||
|
||||
package de.bixilon.minosoft.data.registries.blocks.entites
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
import de.bixilon.minosoft.data.registries.blocks.types.Block
|
||||
import de.bixilon.minosoft.data.registries.registry.Registry
|
||||
import de.bixilon.minosoft.data.registries.registry.ResourceLocationDeserializer
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
class BlockEntityTypeRegistry(
|
||||
parentRegistry: BlockEntityTypeRegistry? = null,
|
||||
) : Registry<BlockEntityType>(parentRegistry) {
|
||||
private lateinit var blockTypeMap: MutableMap<Block, BlockEntityType>
|
||||
private val blockTypeMap: MutableMap<Block, BlockEntityType> = mutableMapOf()
|
||||
|
||||
fun getByBlock(block: Block): BlockEntityType? {
|
||||
operator fun get(block: Block): BlockEntityType? {
|
||||
val parentRegistry = super.parent as BlockEntityTypeRegistry?
|
||||
return blockTypeMap[block] ?: parentRegistry?.getByBlock(block)
|
||||
return blockTypeMap[block] ?: parentRegistry?.get(block)
|
||||
}
|
||||
|
||||
override fun postInit(registries: Registries) {
|
||||
super.postInit(registries)
|
||||
blockTypeMap = mutableMapOf()
|
||||
override fun initialize(data: Map<ResourceLocation, JsonObject>?, registries: Registries?, deserializer: ResourceLocationDeserializer<BlockEntityType>, flattened: Boolean, metaType: MetaTypes, alternative: Registry<BlockEntityType>?): Registry<BlockEntityType> {
|
||||
super.initialize(data, registries, deserializer, flattened, metaType, alternative)
|
||||
|
||||
for ((_, type) in resourceLocationMap) {
|
||||
for (block in type.blocks) {
|
||||
blockTypeMap[block] = type
|
||||
}
|
||||
}
|
||||
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
@ -44,7 +44,7 @@ open class Block(
|
||||
final override val resourceLocation: ResourceLocation,
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
open val explosionResistance: Float = data["explosion_resistance"]?.asFloat ?: 0.0f
|
||||
open val tintColor: RGBColor? = data["tint_color"]?.asInt?.let { TintColorCalculator.getJsonColor(it) }
|
||||
open val randomOffsetType: RandomOffsetTypes? = data["offset_type"]?.asString?.let { RandomOffsetTypes[it] }
|
||||
@ -53,22 +53,22 @@ open class Block(
|
||||
open var blockEntityType: BlockEntityType? = null
|
||||
protected set
|
||||
|
||||
private val itemId: Int = data["item"]?.asInt ?: 0
|
||||
|
||||
open lateinit var states: Set<BlockState>
|
||||
protected set
|
||||
open lateinit var defaultState: BlockState
|
||||
protected set
|
||||
open lateinit var item: Item
|
||||
protected set
|
||||
val item: Item? = null
|
||||
open lateinit var properties: Map<BlockProperties, List<Any>>
|
||||
open val friction = data["friction"]?.asDouble ?: 0.6
|
||||
open val velocityMultiplier = data["velocity_multiplier"]?.asDouble ?: 1.0 // ToDo: They exist since ~1.15
|
||||
open val jumpVelocityMultiplier = data["jump_velocity_multiplier"]?.asDouble ?: 1.0
|
||||
|
||||
init {
|
||||
this::item.inject(data["item"]?.asInt)
|
||||
}
|
||||
|
||||
override fun postInit(registries: Registries) {
|
||||
item = registries.itemRegistry[itemId]
|
||||
blockEntityType = registries.blockEntityTypeRegistry.getByBlock(this)
|
||||
blockEntityType = registries.blockEntityTypeRegistry[this]
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
|
@ -31,7 +31,7 @@ data class StatusEffect(
|
||||
val color: RGBColor,
|
||||
val attributes: Map<ResourceLocation, StatusEffectAttribute>,
|
||||
val uuidAttributes: Map<UUID, StatusEffectAttribute>,
|
||||
) : RegistryItem, Translatable {
|
||||
) : RegistryItem(), Translatable {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -21,7 +21,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
data class Enchantment(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
// ToDo
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -38,7 +38,7 @@ data class EntityType(
|
||||
val fireImmune: Boolean,
|
||||
val attributes: Map<ResourceLocation, Double>,
|
||||
val factory: EntityFactory<out Entity>,
|
||||
) : RegistryItem, Translatable {
|
||||
) : RegistryItem(), Translatable {
|
||||
|
||||
fun build(connection: PlayConnection, position: Vec3d, rotation: EntityRotation, entityMetaData: EntityMetaData?, versionId: Int): Entity? {
|
||||
return DefaultEntityFactories.buildEntity(factory, connection, position, rotation, entityMetaData, versionId)
|
||||
|
@ -22,7 +22,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
data class VillagerProfession(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
// ToDo
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -37,21 +37,20 @@ open class Fluid(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : RegistryItem {
|
||||
private val bucketItemId = data["bucket"]?.asInt
|
||||
val dripParticle: ParticleType? = data["drip_particle_type"]?.asInt?.let { registries.particleTypeRegistry[it] }
|
||||
) : RegistryItem() {
|
||||
open val stillTexture: ResourceLocation? = null
|
||||
var bucketItem: Item? = null
|
||||
private set
|
||||
val dripParticle: ParticleType? = null
|
||||
val bucketItem: Item? = null
|
||||
|
||||
init {
|
||||
this::bucketItem.inject(data["bucket"])
|
||||
this::dripParticle.inject(data["drip_particle_type"])
|
||||
}
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
}
|
||||
|
||||
override fun postInit(registries: Registries) {
|
||||
bucketItem = bucketItemId?.let { registries.itemRegistry[it] }
|
||||
}
|
||||
|
||||
open fun matches(other: Fluid): Boolean {
|
||||
return other == this
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import de.bixilon.minosoft.data.registries.blocks.BlockState
|
||||
import de.bixilon.minosoft.data.registries.fluid.DefaultFluids
|
||||
import de.bixilon.minosoft.data.registries.fluid.FlowableFluid
|
||||
import de.bixilon.minosoft.data.registries.fluid.Fluid
|
||||
import de.bixilon.minosoft.data.registries.particle.ParticleType
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
import de.bixilon.minosoft.gui.rendering.particle.types.render.texture.simple.lava.LavaParticle
|
||||
import de.bixilon.minosoft.gui.rendering.util.VecUtil.horizontal
|
||||
@ -39,10 +40,14 @@ class LavaFluid(
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : FlowableFluid(resourceLocation, registries, data) {
|
||||
private val lavaParticleType = registries.particleTypeRegistry[LavaParticle]
|
||||
private val lavaParticleType: ParticleType? = null
|
||||
override val stillTexture: ResourceLocation = "minecraft:block/lava_still".asResourceLocation()
|
||||
override val flowingTexture: ResourceLocation = "minecraft:block/lava_flow".asResourceLocation()
|
||||
|
||||
init {
|
||||
this::lavaParticleType.inject(LavaParticle)
|
||||
}
|
||||
|
||||
override fun getVelocityMultiplier(connection: PlayConnection, blockState: BlockState, blockPosition: Vec3i): Double {
|
||||
return (connection.world.dimension?.ultraWarm == true).decide(0.007, 0.0023333333333333335)
|
||||
}
|
||||
|
@ -20,7 +20,9 @@ import de.bixilon.minosoft.data.registries.blocks.BlockState
|
||||
import de.bixilon.minosoft.data.registries.blocks.properties.BlockProperties
|
||||
import de.bixilon.minosoft.data.registries.blocks.types.FluidFillable
|
||||
import de.bixilon.minosoft.data.registries.effects.DefaultStatusEffects
|
||||
import de.bixilon.minosoft.data.registries.effects.StatusEffect
|
||||
import de.bixilon.minosoft.data.registries.enchantment.DefaultEnchantments
|
||||
import de.bixilon.minosoft.data.registries.enchantment.Enchantment
|
||||
import de.bixilon.minosoft.data.registries.fluid.FlowableFluid
|
||||
import de.bixilon.minosoft.data.registries.fluid.Fluid
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
@ -40,11 +42,17 @@ class WaterFluid(
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : FlowableFluid(resourceLocation, registries, data) {
|
||||
private val depthStriderEnchantment = registries.enchantmentRegistry[DefaultEnchantments.DEPTH_STRIDER]
|
||||
private val dolphinsGraceStatusEffect = registries.statusEffectRegistry[DefaultStatusEffects.DOLPHINS_GRACE]
|
||||
private val depthStriderEnchantment: Enchantment? = null
|
||||
private val dolphinsGraceStatusEffect: StatusEffect? = null
|
||||
override val stillTexture: ResourceLocation = "minecraft:block/water_still".asResourceLocation()
|
||||
override val flowingTexture: ResourceLocation = "minecraft:block/water_flow".asResourceLocation()
|
||||
|
||||
|
||||
init {
|
||||
this::depthStriderEnchantment.inject(DefaultEnchantments.DEPTH_STRIDER)
|
||||
this::dolphinsGraceStatusEffect.inject(DefaultStatusEffects.DOLPHINS_GRACE)
|
||||
}
|
||||
|
||||
override fun getVelocityMultiplier(connection: PlayConnection, blockState: BlockState, blockPosition: Vec3i): Double {
|
||||
return VELOCITY_MULTIPLIER
|
||||
}
|
||||
|
@ -33,7 +33,11 @@ open class BlockItem(
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : Item(resourceLocation, registries, data) {
|
||||
val block: Block = registries.blockRegistry[data["block"].asInt]
|
||||
val block: Block? = null
|
||||
|
||||
init {
|
||||
this::block.inject(data["block"])
|
||||
}
|
||||
|
||||
override fun use(connection: PlayConnection, raycastHit: RaycastHit, hands: Hands, itemStack: ItemStack): BlockUsages {
|
||||
if (!connection.player.gamemode.canBuild) {
|
||||
@ -56,7 +60,7 @@ open class BlockItem(
|
||||
}
|
||||
|
||||
|
||||
val placeBlockState = block.getPlacementState(connection, raycastHit) ?: return BlockUsages.PASS
|
||||
val placeBlockState = block!!.getPlacementState(connection, raycastHit) ?: return BlockUsages.PASS
|
||||
|
||||
|
||||
connection.world[placePosition] = placeBlockState
|
||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.registries.items
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
import de.bixilon.minosoft.data.registries.fluid.Fluid
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
|
||||
@ -23,5 +24,9 @@ open class BucketItem(
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : Item(resourceLocation, registries, data) {
|
||||
val fluid = registries.fluidRegistry[data["bucked_fluid_type"].asInt]
|
||||
val fluid: Fluid? = null
|
||||
|
||||
init {
|
||||
this::fluid.inject(data["bucked_fluid_type"])
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ open class Item(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
registries: Registries,
|
||||
data: JsonObject,
|
||||
) : RegistryItem, Translatable {
|
||||
) : RegistryItem(), Translatable {
|
||||
val rarity: Rarities = data["rarity"]?.asInt?.let { Rarities[it] } ?: Rarities.COMMON
|
||||
val maxStackSize: Int = data["max_stack_size"]?.asInt ?: 64
|
||||
val maxDamage: Int = data["max_damage"]?.asInt ?: 1
|
||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.registries.items
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
import de.bixilon.minosoft.data.registries.sounds.SoundEvent
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
open class MusicDiscItem(
|
||||
@ -23,5 +24,9 @@ open class MusicDiscItem(
|
||||
data: JsonObject,
|
||||
) : Item(resourceLocation, registries, data) {
|
||||
val analogOutput = data["analog_output"]?.asInt ?: 0
|
||||
val sound = data["sound"]?.asInt?.let { registries.soundEventRegistry[it] }
|
||||
val sound:SoundEvent? = null
|
||||
|
||||
init {
|
||||
this::sound.inject(data["sound"])
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,7 @@ package de.bixilon.minosoft.data.registries.items
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocation
|
||||
import de.bixilon.minosoft.data.registries.entities.EntityType
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
import de.bixilon.minosoft.data.text.RGBColor.Companion.asRGBColor
|
||||
|
||||
@ -25,5 +26,9 @@ open class SpawnEggItem(
|
||||
) : Item(resourceLocation, registries, data) {
|
||||
val color1 = data["spawn_egg_color_1"]?.asInt?.asRGBColor()
|
||||
val color2 = data["spawn_egg_color_2"]?.asInt?.asRGBColor()
|
||||
val entityType = data["spawn_egg_entity_type"]?.asInt?.let { registries.entityTypeRegistry[it] }
|
||||
val entityType: EntityType? = null
|
||||
|
||||
init {
|
||||
this::entityType.inject(data["spawn_egg_entity_type"])
|
||||
}
|
||||
}
|
||||
|
@ -32,7 +32,7 @@ data class Material(
|
||||
val solidBlocking: Boolean,
|
||||
val replaceable: Boolean,
|
||||
val solid: Boolean,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -20,7 +20,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
data class ContainerType(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.toString()
|
||||
|
@ -20,7 +20,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
data class GameEvent(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.toString()
|
||||
|
@ -28,7 +28,7 @@ data class ParticleType(
|
||||
val textures: List<ResourceLocation>,
|
||||
val overrideLimiter: Boolean = false,
|
||||
val factory: ParticleFactory<out Particle>? = null,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -22,6 +22,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
import de.bixilon.minosoft.util.KUtil.asResourceLocation
|
||||
import de.bixilon.minosoft.util.KUtil.nullCast
|
||||
import de.bixilon.minosoft.util.json.ResourceLocationJsonMap.toResourceLocationMap
|
||||
import kotlin.reflect.KClass
|
||||
|
||||
open class Registry<T : RegistryItem>(
|
||||
override var parent: AbstractRegistry<T>? = null,
|
||||
@ -133,6 +134,7 @@ open class Registry<T : RegistryItem>(
|
||||
|
||||
open fun postInit(registries: Registries) {
|
||||
for ((_, value) in resourceLocationMap) {
|
||||
value.inject(registries)
|
||||
value.postInit(registries)
|
||||
}
|
||||
}
|
||||
|
@ -14,9 +14,42 @@
|
||||
package de.bixilon.minosoft.data.registries.registry
|
||||
|
||||
import de.bixilon.minosoft.data.registries.ResourceLocationAble
|
||||
import de.bixilon.minosoft.data.registries.blocks.types.Block
|
||||
import de.bixilon.minosoft.data.registries.items.BlockItem
|
||||
import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
import de.bixilon.minosoft.util.KUtil.setValue
|
||||
import kotlin.reflect.KProperty
|
||||
import kotlin.reflect.jvm.javaField
|
||||
|
||||
interface RegistryItem : ResourceLocationAble {
|
||||
abstract class RegistryItem : ResourceLocationAble {
|
||||
private val injects: MutableMap<KProperty<RegistryItem?>, List<Any>> = mutableMapOf()
|
||||
|
||||
fun postInit(registries: Registries) {}
|
||||
fun KProperty<RegistryItem?>.inject(vararg keys: Any?) {
|
||||
val keyList: MutableList<Any> = mutableListOf()
|
||||
for (key in keys) {
|
||||
key ?: continue
|
||||
keyList += key
|
||||
}
|
||||
if (keyList.isEmpty()) {
|
||||
return
|
||||
}
|
||||
injects[this] = keyList
|
||||
}
|
||||
|
||||
fun inject(registries: Registries) {
|
||||
for ((field, keys) in injects) {
|
||||
val javaField = field.javaField ?: continue
|
||||
var value: Any? = null
|
||||
for (key in keys) {
|
||||
val currentValue = registries[javaField.type as Class<out RegistryItem>]?.get(key) ?: continue
|
||||
value = currentValue
|
||||
break
|
||||
}
|
||||
value ?: continue
|
||||
|
||||
javaField.setValue(this, value)
|
||||
}
|
||||
}
|
||||
|
||||
open fun postInit(registries: Registries) { }
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ import de.bixilon.minosoft.data.registries.versions.Registries
|
||||
|
||||
data class SoundEvent(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
) : RegistryItem {
|
||||
) : RegistryItem() {
|
||||
|
||||
companion object : ResourceLocationDeserializer<SoundEvent> {
|
||||
override fun deserialize(registries: Registries?, resourceLocation: ResourceLocation, data: JsonObject): SoundEvent {
|
||||
|
@ -20,7 +20,7 @@ data class Statistic(
|
||||
override val resourceLocation: ResourceLocation,
|
||||
override val translationKey: String?,
|
||||
val subStatistics: Map<ResourceLocation, SubStatistic>,
|
||||
) : RegistryItem, Translatable {
|
||||
) : RegistryItem(), Translatable {
|
||||
|
||||
override fun toString(): String {
|
||||
return resourceLocation.full
|
||||
|
@ -50,9 +50,11 @@ import de.bixilon.minosoft.gui.rendering.chunk.models.loading.BlockModel
|
||||
import de.bixilon.minosoft.protocol.packets.c2s.play.EntityActionC2SP
|
||||
import de.bixilon.minosoft.protocol.packets.s2c.play.EntityAnimationS2CP
|
||||
import de.bixilon.minosoft.protocol.packets.s2c.play.title.TitleS2CF
|
||||
import de.bixilon.minosoft.util.KUtil.unsafeCast
|
||||
import de.bixilon.minosoft.util.collections.Clearable
|
||||
import de.bixilon.minosoft.util.json.ResourceLocationJsonMap.toResourceLocationMap
|
||||
import java.lang.reflect.Field
|
||||
import java.lang.reflect.ParameterizedType
|
||||
|
||||
|
||||
class Registries {
|
||||
@ -159,7 +161,6 @@ class Registries {
|
||||
containerTypeRegistry.initialize(pixlyzerData["container_types"]?.asJsonObject, this, ContainerType, alternative = DefaultRegistries.CONTAINER_TYPE_REGISTRY.forVersion(version))
|
||||
gameEventRegistry.initialize(pixlyzerData["game_events"]?.asJsonObject, this, GameEvent, alternative = DefaultRegistries.GAME_EVENT_REGISTRY.forVersion(version))
|
||||
|
||||
blockEntityTypeRegistry.initialize(pixlyzerData["block_entities"]?.asJsonObject, this, BlockEntityType)
|
||||
|
||||
entityTypeRegistry.initialize(pixlyzerData["entities"]?.asJsonObject, this, EntityType)
|
||||
|
||||
@ -175,6 +176,8 @@ class Registries {
|
||||
blockRegistry.initialize(pixlyzerData["blocks"]?.asJsonObject, this, Block, version.isFlattened(), Registry.MetaTypes.BITS_4)
|
||||
itemRegistry.initialize(pixlyzerData["items"]?.asJsonObject, this, Item, version.isFlattened(), Registry.MetaTypes.BITS_16)
|
||||
|
||||
blockEntityTypeRegistry.initialize(pixlyzerData["block_entities"]?.asJsonObject, this, BlockEntityType)
|
||||
|
||||
villagerProfessionRegistry.initialize(pixlyzerData["villager_professions"]?.asJsonObject, this, VillagerProfession)
|
||||
|
||||
|
||||
@ -182,10 +185,10 @@ class Registries {
|
||||
|
||||
|
||||
// post init
|
||||
biomeRegistry.postInit(this)
|
||||
fluidRegistry.postInit(this)
|
||||
blockEntityTypeRegistry.postInit(this)
|
||||
blockRegistry.postInit(this)
|
||||
for (field in TYPE_MAP.values) {
|
||||
val registry = field.get(this) as Registry<*>
|
||||
registry.postInit(this)
|
||||
}
|
||||
isFullyLoaded = true
|
||||
}
|
||||
|
||||
@ -248,9 +251,21 @@ class Registries {
|
||||
}
|
||||
}
|
||||
|
||||
operator fun <T : RegistryItem> get(type: Class<T>): Registry<T>? {
|
||||
var currentField: Field?
|
||||
var currentClass: Class<*> = type
|
||||
do {
|
||||
currentField = TYPE_MAP[currentClass]
|
||||
currentClass = currentClass.superclass
|
||||
} while (currentField == null && currentClass != Object::class.java)
|
||||
return currentField?.get(this) as Registry<T>?
|
||||
}
|
||||
|
||||
|
||||
companion object {
|
||||
private val PARENTABLE_FIELDS: List<Field>
|
||||
private val PARENTABLE_SET_PARENT_METHOD = Parentable::class.java.getDeclaredMethod("setParent", Any::class.java)
|
||||
private val TYPE_MAP: Map<Class<*>, Field>
|
||||
|
||||
init {
|
||||
val fields: MutableList<Field> = mutableListOf()
|
||||
@ -264,5 +279,37 @@ class Registries {
|
||||
|
||||
PARENTABLE_FIELDS = fields.toList()
|
||||
}
|
||||
|
||||
init {
|
||||
val types: MutableMap<Class<*>, Field> = mutableMapOf()
|
||||
|
||||
|
||||
for (field in Registries::class.java.declaredFields) {
|
||||
if (!Registry::class.java.isAssignableFrom(field.type)) {
|
||||
continue
|
||||
}
|
||||
field.isAccessible = true
|
||||
|
||||
var generic = field.genericType
|
||||
|
||||
if (field.type != Registry::class.java) {
|
||||
var type = field.type
|
||||
while (type != Object::class.java) {
|
||||
if (type.superclass == Registry::class.java) {
|
||||
generic = type.genericSuperclass
|
||||
break
|
||||
}
|
||||
type = type.superclass
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
types[generic.unsafeCast<ParameterizedType>().actualTypeArguments.first() as Class<*>] = field
|
||||
}
|
||||
|
||||
types[Item::class.java] = Registries::class.java.getDeclaredField("itemRegistry")
|
||||
|
||||
TYPE_MAP = types.toMap()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -111,9 +111,9 @@ data class Version(
|
||||
registries.load(this, pixlyzerData)
|
||||
latch.dec()
|
||||
if (pixlyzerData.size() > 0) {
|
||||
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.INFO) { "Loaded registries for $this $versionName in ${System.currentTimeMillis() - startTime}ms" }
|
||||
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.INFO) { "Loaded registries for $versionName in ${System.currentTimeMillis() - startTime}ms" }
|
||||
} else {
|
||||
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.WARN) { "Could not load registries for $this (${versionName}. Some features might not work." }
|
||||
Log.log(LogMessageType.VERSION_LOADING, level = LogLevels.WARN) { "Could not load registries for ${versionName}. Some features might not work." }
|
||||
}
|
||||
isLoaded = true
|
||||
latch.dec()
|
||||
|
@ -21,6 +21,7 @@ import de.bixilon.minosoft.protocol.protocol.ProtocolDefinition
|
||||
import de.bixilon.minosoft.util.collections.SynchronizedMap
|
||||
import de.bixilon.minosoft.util.enum.AliasableEnum
|
||||
import sun.misc.Unsafe
|
||||
import java.lang.reflect.Field
|
||||
import java.util.*
|
||||
import kotlin.Pair
|
||||
import kotlin.random.Random
|
||||
@ -208,4 +209,17 @@ object KUtil {
|
||||
else -> this.toString()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
fun Field.setValue(instance: Any, value: Any) {
|
||||
this.isAccessible = true
|
||||
|
||||
// ToDo
|
||||
// if (Modifier.isFinal(this.modifiers)) {
|
||||
// FieldUtils.removeFinalModifier(this)
|
||||
// }
|
||||
|
||||
this.set(instance, value)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user