Merge branch '1.21' into worldgen-placement-providers

This commit is contained in:
DBotThePony 2025-03-24 13:58:58 +07:00
commit 9ee561d0fd
Signed by: DBot
GPG Key ID: DCC23B5715498507
13 changed files with 121 additions and 141 deletions

View File

@ -105,6 +105,8 @@ object OverdriveThatMatters {
fun loc(path: String): ResourceLocation = ResourceLocation.fromNamespaceAndPath(MOD_ID, path)
init {
MBuiltInRegistries.register(MOD_BUS)
MBlocks.register(MOD_BUS)
MFluids.register(MOD_BUS)
MBlockEntities.register(MOD_BUS)
@ -137,9 +139,7 @@ object OverdriveThatMatters {
DecimalProvider.register(MOD_BUS)
BooleanProvider.register(MOD_BUS)
AndroidResearchDescription.register(MOD_BUS)
AndroidResearchDescriptions.register(MOD_BUS)
AndroidResearchResult.register(MOD_BUS)
AndroidResearchResults.register(MOD_BUS)
AbstractRegistryAction.register(MOD_BUS)

View File

@ -7,12 +7,12 @@ import com.mojang.serialization.codecs.RecordCodecBuilder
import net.minecraft.util.RandomSource
import net.neoforged.bus.api.IEventBus
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.math.nextDecimal
import ru.dbotthepony.mc.otm.data.codec.DecimalCodec
import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
fun interface SampledDecimal {
fun sample(source: RandomSource): Decimal
@ -28,33 +28,25 @@ abstract class DecimalProvider : SampledDecimal {
abstract val type: Type<*>
companion object {
private val registryHolder = RegistryDelegate<Type<*>>("decimal_provider_type") {
defaultKey(ResourceLocation(OverdriveThatMatters.MOD_ID, "zero"))
}
val CODEC: Codec<DecimalProvider> by lazy {
Codec
.either(DecimalCodec, registry.byNameCodec().dispatch({ it.type }, { it.codec }))
.either(DecimalCodec, MBuiltInRegistries.DECIMAL_PROVIDER_TYPE.byNameCodec().dispatch({ it.type }, { it.codec }))
.xmap(
{ c -> c.map(::ConstantDecimal, { it }) },
{ if (it.type === ConstantDecimal) Either.left(it.minValue) else Either.right(it) }
)
}
val registry by registryHolder
val registryKey get() = registryHolder.key
private val registror = MDeferredRegister(registryKey, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.DECIMAL_PROVIDER_TYPE, OverdriveThatMatters.MOD_ID)
init {
registror.register("zero") { ConstantDecimal.Zero }
registror.register("constant") { ConstantDecimal }
registror.register("uniform") { UniformDecimal }
registrar.register("zero") { ConstantDecimal.Zero }
registrar.register("constant") { ConstantDecimal }
registrar.register("uniform") { UniformDecimal }
}
internal fun register(bus: IEventBus) {
bus.addListener(registryHolder::build)
registror.register(bus)
registrar.register(bus)
}
}
}

View File

@ -13,8 +13,9 @@ import net.minecraft.tags.TagKey
import net.minecraft.world.item.Item
import net.neoforged.bus.api.IEventBus
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
import java.util.*
abstract class AbstractRegistryAction(
@ -99,12 +100,7 @@ abstract class AbstractRegistryAction(
}
companion object {
private val registryDelegate = RegistryDelegate<Type<*>>("matter_registry_action") { sync(false) }
val registryKey get() = registryDelegate.key
val registry by registryDelegate
private val registrar = MDeferredRegister(registryKey, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.MATTER_REGISTRY_ACTION, OverdriveThatMatters.MOD_ID)
init {
registrar.register("insert") { InsertAction.Companion }
@ -116,11 +112,10 @@ abstract class AbstractRegistryAction(
internal fun register(bus: IEventBus) {
registrar.register(bus)
bus.addListener(registryDelegate::build)
}
val CODEC: Codec<AbstractRegistryAction> by lazy {
registry.byNameCodec().dispatch({ it.type }, { it.codec })
MBuiltInRegistries.MATTER_REGISTRY_ACTION.byNameCodec().dispatch({ it.type }, { it.codec })
}
}
}

View File

@ -7,7 +7,7 @@ import ru.dbotthepony.mc.otm.matter.SimpleMatterFunction.DecimalFunction
import ru.dbotthepony.mc.otm.matter.SimpleMatterFunction.DoubleFunction
import ru.dbotthepony.mc.otm.matter.SimpleMatterFunction.IntFunction
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
interface IMatterFunction {
fun updateValue(self: Int, other: Int): Int
@ -15,12 +15,7 @@ interface IMatterFunction {
fun updateValue(self: Double, other: Double): Double
companion object : IMatterFunction {
private val registryDelegate = RegistryDelegate<IMatterFunction>("matter_function") { sync(false) }
val registryKey get() = registryDelegate.key
val registry by registryDelegate
private val registrar = MDeferredRegister(registryKey, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.MATTER_FUNCTION, OverdriveThatMatters.MOD_ID)
init {
registrar.register("noop") { this }
@ -38,7 +33,6 @@ interface IMatterFunction {
internal fun register(bus: IEventBus) {
registrar.register(bus)
bus.addListener(registryDelegate::build)
}
override fun updateValue(self: Int, other: Int): Int {

View File

@ -102,8 +102,9 @@ import ru.dbotthepony.mc.otm.core.writeItemType
import ru.dbotthepony.mc.otm.matter.MatterManager.Finder
import ru.dbotthepony.mc.otm.milliTime
import ru.dbotthepony.mc.otm.onceServer
import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
import ru.dbotthepony.mc.otm.secondTime
import ru.dbotthepony.mc.otm.storage.ItemStorageStack
import ru.dbotthepony.mc.otm.storage.StorageStack
@ -397,12 +398,10 @@ object MatterManager {
}
private object Resolver : SimpleJsonResourceReloadListener(GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(), FINDER_DIRECTORY) {
val delegate = RegistryDelegate<Finder>("recipe_finder") { sync(false) }
var ready = false
private set
val registrar = MDeferredRegister(delegate.key, OverdriveThatMatters.MOD_ID)
val registrar = MDeferredRegister(MRegistries.RECIPE_FINDER, OverdriveThatMatters.MOD_ID)
init {
registrar.register("simple") {
@ -639,11 +638,11 @@ object MatterManager {
val location = (json["type"] ?: throw JsonSyntaxException("Missing resolver type")).let { ResourceLocation.tryParse(it.asString) } ?: throw JsonSyntaxException("Invalid resolver type: ${json["type"]}")
if (!recipeFinders.containsKey(location)) {
if (!MBuiltInRegistries.RECIPE_FINDER.containsKey(location)) {
throw JsonParseException("Resolver type $location does not exist (in $key)")
}
val resolver = recipeFinders.get(location) ?: throw ConcurrentModificationException()
val resolver = MBuiltInRegistries.RECIPE_FINDER.get(location) ?: throw ConcurrentModificationException()
builder.put(key, resolver to json)
}
@ -1495,20 +1494,6 @@ object MatterManager {
return Registry.direct(value)
}
/**
* Access recipe finders registry
*
* @throws IllegalStateException if calling too early
*/
@JvmStatic val recipeFinders get() = Resolver.delegate.get()
/**
* Access recipe finders registry key
*
* Use this with your [DeferredRegister]
*/
@JvmStatic val recipeFindersKey get() = Resolver.delegate.key
private val commentary = Reference2ObjectOpenHashMap<Item, ArrayList<Component>>()
@JvmStatic
@ -1533,7 +1518,6 @@ object MatterManager {
}
internal fun initialize(bus: IEventBus) {
bus.addListener(Resolver.delegate::build)
Resolver.registrar.register(bus)
}

View File

@ -13,11 +13,12 @@ import ru.dbotthepony.mc.otm.config.PlayerConfig
import ru.dbotthepony.mc.otm.core.TextComponent
import ru.dbotthepony.mc.otm.core.TranslatableComponent
import ru.dbotthepony.mc.otm.core.util.formatPower
import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
object AndroidResearchDescriptions {
private val registrar = MDeferredRegister(AndroidResearchDescription.registryKey, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.ANDROID_RESEARCH_DESCRIPTION, OverdriveThatMatters.MOD_ID)
init {
registrar.register("plain") { PlainAndroidResearchDescription }
@ -140,17 +141,8 @@ interface AndroidResearchDescription {
val type: Type<*>
companion object {
private val delegate = RegistryDelegate<Type<*>>("android_research_description") {}
val registry by delegate
val registryKey get() = delegate.key
val CODEC: Codec<AndroidResearchDescription> by lazy {
registry.byNameCodec().dispatch({ it.type }, { it.codec })
}
internal fun register(bus: IEventBus) {
bus.addListener(delegate::build)
MBuiltInRegistries.ANDROID_RESEARCH_DESCRIPTION.byNameCodec().dispatch({ it.type }, { it.codec })
}
fun singleton(callback: (research: AndroidResearch) -> Component): Singleton {

View File

@ -7,13 +7,14 @@ import net.minecraft.resources.ResourceLocation
import net.neoforged.bus.api.IEventBus
import org.apache.logging.log4j.LogManager
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries
import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.MRegistries
import ru.dbotthepony.mc.otm.registry.MRegistry
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
object AndroidResearchResults {
private val registrar = MDeferredRegister(AndroidResearchResult.registryKey, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.ANDROID_RESEARCH_RESULT, OverdriveThatMatters.MOD_ID)
init {
registrar.register("feature") { AndroidResearchResult.Feature }
@ -71,7 +72,7 @@ interface AndroidResearchResult {
* Adds specific android feature [id] to target, does nothing if target already has specified feature
*/
class Feature(val id: ResourceLocation, val optional: Boolean = false) : AndroidResearchResult {
val feature = MRegistry.ANDROID_FEATURES.get(id) ?: if (optional) null else throw NoSuchElementException("Unknown android feature $id")
val feature = MBuiltInRegistries.ANDROID_FEATURE.get(id) ?: if (optional) null else throw NoSuchElementException("Unknown android feature $id")
override val type: Type<*>
get() = Companion
@ -101,7 +102,7 @@ interface AndroidResearchResult {
*/
class FeatureLevel(val id: ResourceLocation, val optional: Boolean = false, val levels: Int = 1) :
AndroidResearchResult {
val feature = MRegistry.ANDROID_FEATURES.get(id) ?: if (optional) null else throw NoSuchElementException("Unknown android feature $id")
val feature = MBuiltInRegistries.ANDROID_FEATURE.get(id) ?: if (optional) null else throw NoSuchElementException("Unknown android feature $id")
override val type: Type<*>
get() = Companion
@ -153,17 +154,9 @@ interface AndroidResearchResult {
companion object {
private val LOGGER = LogManager.getLogger()
private val delegate = RegistryDelegate<Type<*>>("android_research_result") {}
val registry by delegate
val registryKey get() = delegate.key
val CODEC: Codec<AndroidResearchResult> by lazy {
registry.byNameCodec().dispatch({ it.type }, { it.codec })
}
internal fun register(bus: IEventBus) {
bus.addListener(delegate::build)
MBuiltInRegistries.ANDROID_RESEARCH_RESULT.byNameCodec().dispatch({ it.type }, { it.codec })
}
}
}

View File

@ -0,0 +1,57 @@
package ru.dbotthepony.mc.otm.registry
import net.minecraft.core.Registry
import net.minecraft.resources.ResourceKey
import net.neoforged.bus.api.IEventBus
import net.neoforged.neoforge.registries.NewRegistryEvent
import net.neoforged.neoforge.registries.RegistryBuilder
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import kotlin.reflect.KProperty
object MBuiltInRegistries {
private val delegates = ArrayList<Delegate<*>>()
private class Delegate<T : Any>(private val key: ResourceKey<Registry<T>>, private val configurator: RegistryBuilder<T>.() -> Unit = {}) {
init {
delegates.add(this)
}
private var _value: Registry<T>? = null
operator fun getValue(thisRef: Any, property: KProperty<*>): Registry<T> {
return _value ?: throw IllegalStateException("Tried to access uninitialized registry ${key.location()}")
}
fun build(event: NewRegistryEvent) {
if (_value != null) {
throw IllegalStateException("Already created registry ${key.location()}!")
}
_value = RegistryBuilder(key).let {
configurator.invoke(it)
event.create(it)
}
}
}
val DECIMAL_PROVIDER_TYPE by Delegate(MRegistries.DECIMAL_PROVIDER_TYPE) {
defaultKey(ResourceLocation(OverdriveThatMatters.MOD_ID, "zero"))
}
val MATTER_REGISTRY_ACTION by Delegate(MRegistries.MATTER_REGISTRY_ACTION)
val MATTER_FUNCTION by Delegate(MRegistries.MATTER_FUNCTION) {
defaultKey(ResourceLocation(OverdriveThatMatters.MOD_ID, "noop"))
}
val RECIPE_FINDER by Delegate(MRegistries.RECIPE_FINDER)
val ANDROID_RESEARCH_DESCRIPTION by Delegate(MRegistries.ANDROID_RESEARCH_DESCRIPTION)
val ANDROID_RESEARCH_RESULT by Delegate(MRegistries.ANDROID_RESEARCH_RESULT)
val ANDROID_FEATURE by Delegate(MRegistries.ANDROID_FEATURE) { sync(true) }
val STACK_TYPE by Delegate(MRegistries.STACK_TYPE)
internal fun register(bus: IEventBus) {
delegates.forEach { bus.addListener(it::build) }
}
}

View File

@ -0,0 +1,29 @@
package ru.dbotthepony.mc.otm.registry
import net.minecraft.core.Registry
import net.minecraft.resources.ResourceKey
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import ru.dbotthepony.mc.otm.data.world.DecimalProvider
import ru.dbotthepony.mc.otm.matter.AbstractRegistryAction
import ru.dbotthepony.mc.otm.matter.IMatterFunction
import ru.dbotthepony.mc.otm.matter.MatterManager
import ru.dbotthepony.mc.otm.player.android.AndroidFeatureType
import ru.dbotthepony.mc.otm.player.android.AndroidResearchDescription
import ru.dbotthepony.mc.otm.player.android.AndroidResearchResult
import ru.dbotthepony.mc.otm.storage.StorageStack
object MRegistries {
private fun <T> k(name: String): ResourceKey<Registry<T>> {
return ResourceKey.createRegistryKey(ResourceLocation(OverdriveThatMatters.MOD_ID, name))
}
val DECIMAL_PROVIDER_TYPE = k<DecimalProvider.Type<*>>("decimal_provider_type")
val MATTER_REGISTRY_ACTION = k<AbstractRegistryAction.Type<*>>("matter_registry_action")
val MATTER_FUNCTION = k<IMatterFunction>("matter_function")
val RECIPE_FINDER = k<MatterManager.Finder>("recipe_finder")
val ANDROID_RESEARCH_DESCRIPTION = k<AndroidResearchDescription.Type<*>>("android_research_description")
val ANDROID_RESEARCH_RESULT = k<AndroidResearchResult.Type<*>>("android_research_result")
val ANDROID_FEATURE = k<AndroidFeatureType<*>>("android_feature")
val STACK_TYPE = k<StorageStack.Type<*>>("stack_type")
}

View File

@ -53,11 +53,6 @@ import ru.dbotthepony.mc.otm.registry.objects.IBlockItemRegistryAcceptor
import ru.dbotthepony.mc.otm.registry.objects.StripedColoredDecorativeBlock
object MRegistry : IBlockItemRegistryAcceptor {
private val features = RegistryDelegate<AndroidFeatureType<*>>("android_features") { sync(true) }
val ANDROID_FEATURES by features
val ANDROID_FEATURES_LOCATION get() = features.location
val ANDROID_FEATURES_KEY get() = features.key
val DYE_ORDER: ImmutableList<DyeColor> = ImmutableList.of(
DyeColor.BLACK,
DyeColor.BLUE,
@ -251,7 +246,6 @@ object MRegistry : IBlockItemRegistryAcceptor {
}
internal fun initialize(bus: IEventBus) {
bus.addListener(features::build)
bus.addListener(this::initializeClient)
bus.addListener(this::initializeCommon)
bus.addListener(MStats::registerVanilla)

View File

@ -1,45 +0,0 @@
package ru.dbotthepony.mc.otm.registry
import net.minecraft.core.Registry
import net.minecraft.resources.ResourceKey
import net.neoforged.neoforge.registries.NewRegistryEvent
import net.neoforged.neoforge.registries.RegistryBuilder
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.ResourceLocation
import java.util.function.Supplier
import kotlin.properties.ReadOnlyProperty
import kotlin.reflect.KProperty
class RegistryDelegate<T>(key: String, private val configurator: RegistryBuilder<T>.() -> Unit = {}) : ReadOnlyProperty<Any, Registry<T>>, Supplier<Registry<T>>, Lazy<Registry<T>> {
private var _value: Registry<T>? = null
override val value: Registry<T>
get() = get()
override fun isInitialized(): Boolean {
return _value != null
}
val location = ResourceLocation(OverdriveThatMatters.MOD_ID, key)
val key: ResourceKey<Registry<T>> = ResourceKey.createRegistryKey(location)
override fun get(): Registry<T> {
val value = _value ?: throw IllegalStateException("Tried to access uninitialized registry $location")
return value
}
override fun getValue(thisRef: Any, property: KProperty<*>): Registry<T> {
return get()
}
fun build(event: NewRegistryEvent) {
if (_value != null) {
throw IllegalStateException("Already built registry $location!")
}
_value = RegistryBuilder(key).let {
configurator.invoke(it)
event.create(it)
}
}
}

View File

@ -18,10 +18,11 @@ import ru.dbotthepony.mc.otm.player.android.feature.StepAssistFeature
import ru.dbotthepony.mc.otm.player.android.feature.SwimBoostersFeature
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.MNames
import ru.dbotthepony.mc.otm.registry.MRegistries
import ru.dbotthepony.mc.otm.registry.MRegistry
object AndroidFeatures {
private val registry = MDeferredRegister(MRegistry.ANDROID_FEATURES_KEY)
private val registry = MDeferredRegister(MRegistries.ANDROID_FEATURE)
val AIR_BAGS by registry.register(MNames.AIR_BAGS) { AndroidFeatureType(::DummyAndroidFeature) }
val STEP_ASSIST by registry.register(MNames.STEP_ASSIST) { AndroidFeatureType(::StepAssistFeature) }

View File

@ -5,14 +5,13 @@ import net.minecraft.network.RegistryFriendlyByteBuf
import net.minecraft.world.item.ItemStack
import net.neoforged.bus.api.IEventBus
import ru.dbotthepony.mc.otm.OverdriveThatMatters
import ru.dbotthepony.mc.otm.core.getValue
import ru.dbotthepony.mc.otm.core.math.Decimal
import ru.dbotthepony.mc.otm.core.readBigInteger
import ru.dbotthepony.mc.otm.core.readItem
import ru.dbotthepony.mc.otm.core.writeBigInteger
import ru.dbotthepony.mc.otm.core.writeItem
import ru.dbotthepony.mc.otm.registry.MDeferredRegister
import ru.dbotthepony.mc.otm.registry.RegistryDelegate
import ru.dbotthepony.mc.otm.registry.MRegistries
import java.math.BigInteger
abstract class StorageStack<S : StorageStack<S>>(val count: BigInteger) {
@ -110,11 +109,7 @@ abstract class StorageStack<S : StorageStack<S>>(val count: BigInteger) {
return o?.hashCodeWithoutCount() ?: 0
}
private val delegate = RegistryDelegate<Type<*>>("stack_type") {}
val REGISTRY by delegate
val REGISTRY_KEY by delegate::key
private val registrar = MDeferredRegister(REGISTRY_KEY, OverdriveThatMatters.MOD_ID)
private val registrar = MDeferredRegister(MRegistries.STACK_TYPE, OverdriveThatMatters.MOD_ID)
val ITEMS: Type<ItemStorageStack> by registrar.register("items") {
SimpleType(
@ -131,7 +126,6 @@ abstract class StorageStack<S : StorageStack<S>>(val count: BigInteger) {
}
internal fun register(bus: IEventBus) {
bus.addListener(delegate::build)
registrar.register(bus)
}
}