From fdfc406ca67290bdce2d2412911ab1218029cc77 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Sun, 23 Mar 2025 01:48:33 +0700 Subject: [PATCH 01/12] Make exopack slot upgrades be consumed in stacks, to make bulk use way less annoying --- .../exopack/AbstractExopackSlotUpgradeItem.kt | 42 ++++++++++++------- .../mc/otm/item/exopack/ExopackUpgradeItem.kt | 4 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/AbstractExopackSlotUpgradeItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/AbstractExopackSlotUpgradeItem.kt index e9750cab0..dd15524c2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/AbstractExopackSlotUpgradeItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/AbstractExopackSlotUpgradeItem.kt @@ -20,6 +20,9 @@ import ru.dbotthepony.mc.otm.client.minecraft import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.isExplosion import ru.dbotthepony.mc.otm.core.isFire +import ru.dbotthepony.mc.otm.core.isNotEmpty +import ru.dbotthepony.mc.otm.core.nextUUID +import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.registry.game.MItems import ru.dbotthepony.mc.otm.runIfClient import ru.dbotthepony.mc.otm.triggers.ExopackSlotsExpandedTrigger @@ -35,7 +38,7 @@ abstract class AbstractExopackSlotUpgradeItem(properties: Properties = defaultPr } override fun getUseDuration(p_41454_: ItemStack, p_344979_: LivingEntity): Int { - return 30 + return 20 } override fun appendHoverText( @@ -76,7 +79,7 @@ abstract class AbstractExopackSlotUpgradeItem(properties: Properties = defaultPr } override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder { - val matteryPlayer = player.matteryPlayer ?: return super.use(p_41432_, player, hand) + val matteryPlayer = player.matteryPlayer val uuid = uuid(player.getItemInHand(hand)) @@ -98,25 +101,32 @@ abstract class AbstractExopackSlotUpgradeItem(properties: Properties = defaultPr return super.finishUsingItem(itemStack, level, player) } - if (!player.abilities.instabuild) - itemStack.shrink(1) + var allowedUses = itemStack.count - if (player is ServerPlayer) { - if (uuid != null) { - if (ServerConfig.INFINITE_EXOSUIT_UPGRADES && uuid in matteryPlayer.exopackSlotModifier) { - matteryPlayer.exopackSlotModifier[UUID.randomUUID()] = slotCount + while (allowedUses > 0) { + if (!player.abilities.instabuild) + itemStack.shrink(1) + + allowedUses-- + + if (player is ServerPlayer) { + if (uuid != null) { + if (ServerConfig.INFINITE_EXOSUIT_UPGRADES && uuid in matteryPlayer.exopackSlotModifier) { + matteryPlayer.exopackSlotModifier[level.otmRandom.nextUUID()] = slotCount + } else { + matteryPlayer.exopackSlotModifier[uuid] = slotCount + allowedUses = 0 + } } else { - matteryPlayer.exopackSlotModifier[uuid] = slotCount + matteryPlayer.exopackSlotModifier[level.otmRandom.nextUUID()] = slotCount } - } else { - matteryPlayer.exopackSlotModifier[UUID.randomUUID()] = slotCount - } - ExopackSlotsExpandedTrigger.trigger(player, slotCount, matteryPlayer.exopackContainer.containerSize) - player.displayClientMessage(TranslatableComponent("otm.exopack_upgrades.slots_upgraded", slotCount).withStyle(ChatFormatting.DARK_GREEN), false) + ExopackSlotsExpandedTrigger.trigger(player, slotCount, matteryPlayer.exopackContainer.containerSize) + player.displayClientMessage(TranslatableComponent("otm.exopack_upgrades.slots_upgraded", slotCount).withStyle(ChatFormatting.DARK_GREEN), false) - if (this === MItems.ExopackUpgrades.INVENTORY_UPGRADE_ENDER_DRAGON) { - MItems.ExopackUpgrades.ENDER_UPGRADE.finishUsingItem(ItemStack(MItems.ExopackUpgrades.INVENTORY_UPGRADE_ENDER_DRAGON), level, player) + if (this === MItems.ExopackUpgrades.INVENTORY_UPGRADE_ENDER_DRAGON) { + MItems.ExopackUpgrades.ENDER_UPGRADE.finishUsingItem(ItemStack(MItems.ExopackUpgrades.INVENTORY_UPGRADE_ENDER_DRAGON), level, player) + } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/ExopackUpgradeItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/ExopackUpgradeItem.kt index faf2b52ee..1db813729 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/ExopackUpgradeItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/exopack/ExopackUpgradeItem.kt @@ -60,7 +60,7 @@ class ExopackUpgradeItem( } override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder { - if (player.matteryPlayer?.hasExopack == true && !type.prop.get(player.matteryPlayer!!)) { + if (player.matteryPlayer.hasExopack && !type.prop.get(player.matteryPlayer)) { player.startUsingItem(hand) return InteractionResultHolder.consume(player.getItemInHand(hand)) } @@ -70,7 +70,7 @@ class ExopackUpgradeItem( override fun finishUsingItem(itemStack: ItemStack, level: Level, player: LivingEntity): ItemStack { if (player !is Player) return super.finishUsingItem(itemStack, level, player) - val mattery = player.matteryPlayer ?: return super.finishUsingItem(itemStack, level, player) + val mattery = player.matteryPlayer if (!mattery.hasExopack || type.prop.get(mattery)) { return super.finishUsingItem(itemStack, level, player) From 588b9c1b7c6af8af6f627fe19e15817d642b1bf4 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 24 Mar 2025 13:55:36 +0700 Subject: [PATCH 02/12] Put all otm registries in one place --- .../mc/otm/OverdriveThatMatters.kt | 4 +- .../mc/otm/data/world/DecimalProvider.kt | 24 +++----- .../mc/otm/matter/AbstractRegistryAction.kt | 13 ++--- .../mc/otm/matter/IMatterFunction.kt | 10 +--- .../mc/otm/matter/MatterManager.kt | 26 ++------- .../android/AndroidResearchDescription.kt | 16 ++---- .../player/android/AndroidResearchResult.kt | 19 ++----- .../mc/otm/registry/MBuiltInRegistries.kt | 57 +++++++++++++++++++ .../mc/otm/registry/MRegistries.kt | 29 ++++++++++ .../dbotthepony/mc/otm/registry/MRegistry.kt | 6 -- .../mc/otm/registry/RegistryDelegate.kt | 45 --------------- .../mc/otm/registry/game/AndroidFeatures.kt | 3 +- .../mc/otm/storage/StorageStack.kt | 10 +--- 13 files changed, 121 insertions(+), 141 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/registry/MBuiltInRegistries.kt create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistries.kt delete mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/registry/RegistryDelegate.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt index caddadd36..58603f67f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/OverdriveThatMatters.kt @@ -103,6 +103,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) @@ -133,9 +135,7 @@ object OverdriveThatMatters { MOD_BUS.addListener(::registerNetworkPackets) DecimalProvider.register(MOD_BUS) - AndroidResearchDescription.register(MOD_BUS) AndroidResearchDescriptions.register(MOD_BUS) - AndroidResearchResult.register(MOD_BUS) AndroidResearchResults.register(MOD_BUS) AbstractRegistryAction.register(MOD_BUS) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt index bdda35e24..67a993e4e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt @@ -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>("decimal_provider_type") { - defaultKey(ResourceLocation(OverdriveThatMatters.MOD_ID, "zero")) - } - val CODEC: Codec 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) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/AbstractRegistryAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/AbstractRegistryAction.kt index a72d7c944..227dd5ab6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/AbstractRegistryAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/AbstractRegistryAction.kt @@ -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>("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 by lazy { - registry.byNameCodec().dispatch({ it.type }, { it.codec }) + MBuiltInRegistries.MATTER_REGISTRY_ACTION.byNameCodec().dispatch({ it.type }, { it.codec }) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterFunction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterFunction.kt index b7e64a2d0..69a634136 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterFunction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterFunction.kt @@ -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("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 { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt index 828868a1f..32ae1bd51 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/MatterManager.kt @@ -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("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>() @JvmStatic @@ -1533,7 +1518,6 @@ object MatterManager { } internal fun initialize(bus: IEventBus) { - bus.addListener(Resolver.delegate::build) Resolver.registrar.register(bus) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchDescription.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchDescription.kt index b1774f198..4b9e51cce 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchDescription.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchDescription.kt @@ -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>("android_research_description") {} - - val registry by delegate - val registryKey get() = delegate.key - val CODEC: Codec 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 { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchResult.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchResult.kt index 6e908a33f..0ff3eaae1 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchResult.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidResearchResult.kt @@ -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>("android_research_result") {} - - val registry by delegate - val registryKey get() = delegate.key val CODEC: Codec 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 }) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MBuiltInRegistries.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MBuiltInRegistries.kt new file mode 100644 index 000000000..462770f23 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MBuiltInRegistries.kt @@ -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>() + + private class Delegate(private val key: ResourceKey>, private val configurator: RegistryBuilder.() -> Unit = {}) { + init { + delegates.add(this) + } + + private var _value: Registry? = null + + operator fun getValue(thisRef: Any, property: KProperty<*>): Registry { + 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) } + } +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistries.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistries.kt new file mode 100644 index 000000000..5533b8ead --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistries.kt @@ -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 k(name: String): ResourceKey> { + return ResourceKey.createRegistryKey(ResourceLocation(OverdriveThatMatters.MOD_ID, name)) + } + + val DECIMAL_PROVIDER_TYPE = k>("decimal_provider_type") + val MATTER_REGISTRY_ACTION = k>("matter_registry_action") + val MATTER_FUNCTION = k("matter_function") + val RECIPE_FINDER = k("recipe_finder") + val ANDROID_RESEARCH_DESCRIPTION = k>("android_research_description") + val ANDROID_RESEARCH_RESULT = k>("android_research_result") + val ANDROID_FEATURE = k>("android_feature") + val STACK_TYPE = k>("stack_type") +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt index 719d54efe..c15342aa9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/MRegistry.kt @@ -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>("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 = 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) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/RegistryDelegate.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/RegistryDelegate.kt deleted file mode 100644 index fc412a415..000000000 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/RegistryDelegate.kt +++ /dev/null @@ -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(key: String, private val configurator: RegistryBuilder.() -> Unit = {}) : ReadOnlyProperty>, Supplier>, Lazy> { - private var _value: Registry? = null - - override val value: Registry - get() = get() - - override fun isInitialized(): Boolean { - return _value != null - } - - val location = ResourceLocation(OverdriveThatMatters.MOD_ID, key) - val key: ResourceKey> = ResourceKey.createRegistryKey(location) - - override fun get(): Registry { - val value = _value ?: throw IllegalStateException("Tried to access uninitialized registry $location") - return value - } - - override fun getValue(thisRef: Any, property: KProperty<*>): Registry { - 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) - } - } -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/game/AndroidFeatures.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/game/AndroidFeatures.kt index 9e1c1d460..b548a39ff 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/registry/game/AndroidFeatures.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/registry/game/AndroidFeatures.kt @@ -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) } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/storage/StorageStack.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/storage/StorageStack.kt index 0dfd65e2d..ddf49249b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/storage/StorageStack.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/storage/StorageStack.kt @@ -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>(val count: BigInteger) { @@ -110,11 +109,7 @@ abstract class StorageStack>(val count: BigInteger) { return o?.hashCodeWithoutCount() ?: 0 } - private val delegate = RegistryDelegate>("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 by registrar.register("items") { SimpleType( @@ -131,7 +126,6 @@ abstract class StorageStack>(val count: BigInteger) { } internal fun register(bus: IEventBus) { - bus.addListener(delegate::build) registrar.register(bus) } } From 1bb612b07c5772ef4ebdac5e4540641d1762ec22 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 24 Mar 2025 18:14:47 +0700 Subject: [PATCH 03/12] Fixes for last commit --- .../dbotthepony/mc/otm/matter/ComputeAction.kt | 5 +++-- .../dbotthepony/mc/otm/matter/UpdateAction.kt | 7 ++++--- .../mc/otm/network/AndroidPackets.kt | 17 +++++++++-------- .../dbotthepony/mc/otm/player/MatteryPlayer.kt | 3 ++- .../mc/otm/player/android/AndroidFeatureType.kt | 3 ++- .../mc/otm/triggers/KillAsAndroidTrigger.kt | 3 ++- 6 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt index 75a6560a1..1571b96bf 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt @@ -15,6 +15,7 @@ import net.minecraft.world.item.Item import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.data.codec.DecimalCodec import ru.dbotthepony.mc.otm.data.codec.PredicatedCodecList +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import java.util.Optional import java.util.function.Predicate @@ -29,8 +30,8 @@ class ComputeAction( it.group( DecimalCodec.fieldOf("matter").forGetter(Constant::matter), Codec.DOUBLE.fieldOf("complexity").forGetter(Constant::complexity), - IMatterFunction.registry.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), - IMatterFunction.registry.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), ).apply(it, ::Constant) } to Predicate { true } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt index 7c3f4c099..9849df8aa 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/UpdateAction.kt @@ -11,6 +11,7 @@ import net.minecraft.world.item.Item import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.data.codec.DecimalCodec import ru.dbotthepony.mc.otm.data.codec.simpleCodec +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries class UpdateAction( id: Either>, @@ -22,7 +23,7 @@ class UpdateAction( data class MatterFunction(val function: IMatterFunction, val value: Decimal) { companion object { val CODEC: Codec by lazy { - simpleCodec(::MatterFunction, MatterFunction::function, IMatterFunction.registry.byNameCodec(), MatterFunction::value, DecimalCodec) + simpleCodec(::MatterFunction, MatterFunction::function, MBuiltInRegistries.MATTER_FUNCTION.byNameCodec(), MatterFunction::value, DecimalCodec) } } } @@ -30,7 +31,7 @@ class UpdateAction( data class ComplexityFunction(val function: IMatterFunction, val value: Double) { companion object { val CODEC: Codec by lazy { - simpleCodec(::ComplexityFunction, ComplexityFunction::function, IMatterFunction.registry.byNameCodec(), ComplexityFunction::value, Codec.DOUBLE) + simpleCodec(::ComplexityFunction, ComplexityFunction::function, MBuiltInRegistries.MATTER_FUNCTION.byNameCodec(), ComplexityFunction::value, Codec.DOUBLE) } } } @@ -38,7 +39,7 @@ class UpdateAction( data class PriorityFunction(val function: IMatterFunction, val value: Int) { companion object { val CODEC: Codec by lazy { - simpleCodec(::PriorityFunction, PriorityFunction::function, IMatterFunction.registry.byNameCodec(), PriorityFunction::value, Codec.INT) + simpleCodec(::PriorityFunction, PriorityFunction::function, MBuiltInRegistries.MATTER_FUNCTION.byNameCodec(), PriorityFunction::value, Codec.INT) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/AndroidPackets.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/AndroidPackets.kt index b05ef757b..d008ccdae 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/AndroidPackets.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/AndroidPackets.kt @@ -26,13 +26,14 @@ import ru.dbotthepony.mc.otm.core.readComponent import ru.dbotthepony.mc.otm.core.writeComponent import ru.dbotthepony.mc.otm.menu.tech.AndroidStationMenu import ru.dbotthepony.mc.otm.onceServer +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures import ru.dbotthepony.mc.otm.registry.MRegistry import ru.dbotthepony.mc.otm.registry.game.MSoundEvents class AndroidFeatureSyncPacket(val type: AndroidFeatureType<*>, val data: ByteArrayList) : CustomPacketPayload { fun write(buff: RegistryFriendlyByteBuf) { - buff.writeInt(MRegistry.ANDROID_FEATURES.getId(type)) + buff.writeInt(MBuiltInRegistries.ANDROID_FEATURE.getId(type)) buff.writeBytes(data.elements(), 0, data.size) } @@ -58,7 +59,7 @@ class AndroidFeatureSyncPacket(val type: AndroidFeatureType<*>, val data: ByteAr fun read(buff: RegistryFriendlyByteBuf): AndroidFeatureSyncPacket { return AndroidFeatureSyncPacket( - MRegistry.ANDROID_FEATURES.byIdOrThrow(buff.readInt()), + MBuiltInRegistries.ANDROID_FEATURE.byIdOrThrow(buff.readInt()), ByteArrayList.wrap(ByteArray(buff.readableBytes()).also { buff.readBytes(it) }) ) } @@ -140,7 +141,7 @@ class AndroidResearchRequestPacket(val type: AndroidResearchType) : CustomPacket class AndroidFeatureRemovePacket(val type: AndroidFeatureType<*>) : CustomPacketPayload { fun write(buff: FriendlyByteBuf) { - buff.writeInt(MRegistry.ANDROID_FEATURES.getId(type)) + buff.writeInt(MBuiltInRegistries.ANDROID_FEATURE.getId(type)) } fun play(context: IPayloadContext) { @@ -164,7 +165,7 @@ class AndroidFeatureRemovePacket(val type: AndroidFeatureType<*>) : CustomPacket StreamCodec.ofMember(AndroidFeatureRemovePacket::write, ::read) fun read(buff: FriendlyByteBuf): AndroidFeatureRemovePacket { - return AndroidFeatureRemovePacket(MRegistry.ANDROID_FEATURES.byIdOrThrow(buff.readInt())) + return AndroidFeatureRemovePacket(MBuiltInRegistries.ANDROID_FEATURE.byIdOrThrow(buff.readInt())) } } } @@ -219,7 +220,7 @@ class PlayerIterationPacket(val iteration: Int, val deathLog: List, val newState: Boolean) : CustomPacketPayload { fun write(buff: FriendlyByteBuf) { - buff.writeInt(MRegistry.ANDROID_FEATURES.getId(type)) + buff.writeInt(MBuiltInRegistries.ANDROID_FEATURE.getId(type)) buff.writeBoolean(newState) } @@ -259,14 +260,14 @@ class SwitchAndroidFeaturePacket(val type: AndroidFeatureType<*>, val newState: StreamCodec.ofMember(SwitchAndroidFeaturePacket::write, ::read) fun read(buff: FriendlyByteBuf): SwitchAndroidFeaturePacket { - return SwitchAndroidFeaturePacket(MRegistry.ANDROID_FEATURES.byIdOrThrow(buff.readInt()), buff.readBoolean()) + return SwitchAndroidFeaturePacket(MBuiltInRegistries.ANDROID_FEATURE.byIdOrThrow(buff.readInt()), buff.readBoolean()) } } } class ActivateAndroidFeaturePacket(val type: AndroidFeatureType<*>) : CustomPacketPayload { fun write(buff: FriendlyByteBuf) { - buff.writeInt(MRegistry.ANDROID_FEATURES.getId(type)) + buff.writeInt(MBuiltInRegistries.ANDROID_FEATURE.getId(type)) } fun play(context: IPayloadContext) { @@ -298,7 +299,7 @@ class ActivateAndroidFeaturePacket(val type: AndroidFeatureType<*>) : CustomPack StreamCodec.ofMember(ActivateAndroidFeaturePacket::write, ::read) fun read(buff: FriendlyByteBuf): ActivateAndroidFeaturePacket { - return ActivateAndroidFeaturePacket(MRegistry.ANDROID_FEATURES.byIdOrThrow(buff.readInt())) + return ActivateAndroidFeaturePacket(MBuiltInRegistries.ANDROID_FEATURE.byIdOrThrow(buff.readInt())) } } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryPlayer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryPlayer.kt index 366b86900..ef4dbae2c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryPlayer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryPlayer.kt @@ -97,6 +97,7 @@ import ru.dbotthepony.mc.otm.menu.ExopackInventoryMenu import ru.dbotthepony.mc.otm.menu.IItemStackSortingSettings import ru.dbotthepony.mc.otm.network.* import ru.dbotthepony.mc.otm.network.SmokeParticlesPacket.Companion.makeSmoke +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import ru.dbotthepony.mc.otm.registry.game.AndroidFeatures import ru.dbotthepony.mc.otm.registry.MDamageTypes import ru.dbotthepony.mc.otm.registry.game.MItems @@ -1004,7 +1005,7 @@ class MatteryPlayer(val ply: Player) { research.clear() for (featureTag in tag.getCompoundList("features")) { - val feature = MRegistry.ANDROID_FEATURES.get(ResourceLocation.parse(featureTag.getString("id"))) + val feature = MBuiltInRegistries.ANDROID_FEATURE.get(ResourceLocation.parse(featureTag.getString("id"))) if (feature?.isApplicable(this) == true) { val instance = feature.create(this) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidFeatureType.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidFeatureType.kt index 9fcc9be5c..1b32d9d96 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidFeatureType.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/player/android/AndroidFeatureType.kt @@ -6,6 +6,7 @@ import net.minecraft.network.chat.MutableComponent import net.minecraft.network.chat.contents.TranslatableContents import ru.dbotthepony.mc.otm.player.MatteryPlayer import ru.dbotthepony.mc.otm.core.getKeyNullable +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import ru.dbotthepony.mc.otm.registry.MRegistry open class AndroidFeatureType { @@ -26,7 +27,7 @@ open class AndroidFeatureType { open fun isApplicable(android: MatteryPlayer) = true val registryName by lazy { - MRegistry.ANDROID_FEATURES.getKeyNullable(this) + MBuiltInRegistries.ANDROID_FEATURE.getKeyNullable(this) } open val displayContents: ComponentContents by lazy { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/KillAsAndroidTrigger.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/KillAsAndroidTrigger.kt index 23fe57ee2..ca973c252 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/KillAsAndroidTrigger.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/triggers/KillAsAndroidTrigger.kt @@ -15,6 +15,7 @@ import ru.dbotthepony.mc.otm.player.MatteryPlayer import ru.dbotthepony.mc.otm.player.matteryPlayer import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.data.codec.SingletonCodec +import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import ru.dbotthepony.mc.otm.registry.MRegistry import java.util.Optional import java.util.function.Predicate @@ -76,7 +77,7 @@ object KillAsAndroidTrigger : MCriterionTrigger(R } class Has(val name: ResourceLocation) : FeaturePredicate() { - private val resolved by lazy { MRegistry.ANDROID_FEATURES.get(name) } + private val resolved by lazy { MBuiltInRegistries.ANDROID_FEATURE.get(name) } override val type: PredicateType get() = PredicateType.HAS From 6226621b956f8d8e0c11d85788f57dbe4e45f1d1 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 24 Mar 2025 18:15:53 +0700 Subject: [PATCH 04/12] god damn it --- .../mc/otm/matter/ComputeAction.kt | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt index 1571b96bf..9bbe9b673 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/ComputeAction.kt @@ -41,7 +41,7 @@ class ComputeAction( it.group( DecimalCodec.fieldOf("matter").forGetter(Constant::matter), Codec.DOUBLE.fieldOf("complexity").forGetter(Constant::complexity), - IMatterFunction.registry.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), ).apply(it) { a, b, c -> Constant(a, b, c, c) } } to Predicate { it.matterFunction == it.complexityFunction } } @@ -50,8 +50,8 @@ class ComputeAction( RecordCodecBuilder.create { it.group( DecimalCodec.fieldOf("value").forGetter(Constant::matter), - IMatterFunction.registry.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), - IMatterFunction.registry.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), ).apply(it) { a, b, c -> Constant(a, a.toDouble(), b, c) } } to Predicate { it.matter == Decimal(it.complexity.toString()) } } @@ -60,7 +60,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( DecimalCodec.fieldOf("value").forGetter(Constant::matter), - IMatterFunction.registry.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), ).apply(it) { a, b -> Constant(a, a.toDouble(), b, b) } } to Predicate { it.matter == Decimal(it.complexity.toString()) && it.matterFunction == it.complexityFunction } } @@ -69,7 +69,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( Codec.DOUBLE.fieldOf("complexity").forGetter(Constant::complexity), - IMatterFunction.registry.byNameCodec().fieldOf("function").forGetter(Constant::complexityFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("function").forGetter(Constant::complexityFunction), ).apply(it) { a, b -> Constant(Decimal.ZERO, a.toDouble(), IMatterFunction.NOOP, b) } } to Predicate { it.matterFunction == IMatterFunction.NOOP } } @@ -78,7 +78,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( Codec.DOUBLE.fieldOf("complexity").forGetter(Constant::complexity), - IMatterFunction.registry.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("complexityFunction").forGetter(Constant::complexityFunction), ).apply(it) { a, b -> Constant(Decimal.ZERO, a.toDouble(), IMatterFunction.NOOP, b) } } to Predicate { it.matterFunction == IMatterFunction.NOOP } } @@ -87,7 +87,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( DecimalCodec.fieldOf("matter").forGetter(Constant::matter), - IMatterFunction.registry.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("function").forGetter(Constant::matterFunction), ).apply(it) { a, b -> Constant(a, 0.0, b, IMatterFunction.NOOP) } } to Predicate { it.complexityFunction == IMatterFunction.NOOP } } @@ -96,7 +96,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( DecimalCodec.fieldOf("matter").forGetter(Constant::matter), - IMatterFunction.registry.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("matterFunction").forGetter(Constant::matterFunction), ).apply(it) { a, b -> Constant(a, 0.0, b, IMatterFunction.NOOP) } } to Predicate { it.complexityFunction == IMatterFunction.NOOP } } @@ -183,7 +183,7 @@ class ComputeAction( RecordCodecBuilder.create { it.group( TargetCodec.fieldOf("id").forGetter(Value::id), - IMatterFunction.registry.byNameCodec().fieldOf("function").forGetter(Value::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("function").forGetter(Value::matterFunction), ).apply(it, ::Value) } } @@ -192,8 +192,8 @@ class ComputeAction( RecordCodecBuilder.create { it.group( TargetCodec.fieldOf("id").forGetter(Value::id), - IMatterFunction.registry.byNameCodec().fieldOf("matterFunction").forGetter(Value::matterFunction), - IMatterFunction.registry.byNameCodec().fieldOf("complexityFunction").forGetter(Value::complexityFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("matterFunction").forGetter(Value::matterFunction), + MBuiltInRegistries.MATTER_FUNCTION.byNameCodec().fieldOf("complexityFunction").forGetter(Value::complexityFunction), ).apply(it, ::Value) } } From ddcfe11780f4230ae453bb541ad5654c45adef90 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Mon, 24 Mar 2025 23:33:52 +0700 Subject: [PATCH 05/12] Caused by: java.lang.NoClassDefFoundError: ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature --- src/main/resources/coremods/limb_brush_overclock.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/resources/coremods/limb_brush_overclock.js b/src/main/resources/coremods/limb_brush_overclock.js index 88e51b8e4..5a2034769 100644 --- a/src/main/resources/coremods/limb_brush_overclock.js +++ b/src/main/resources/coremods/limb_brush_overclock.js @@ -18,7 +18,7 @@ function initializeCoreMod() { if (insn.getOpcode() == Opcodes.BIPUSH) { node.instructions.insert(insn, new MethodInsnNode( Opcodes.INVOKESTATIC, - 'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature', + 'ru/dbotthepony/mc/otm/player/android/feature/LimbOverclockingFeature', 'getBrushCooldown', '(Lnet/minecraft/world/entity/LivingEntity;)I' )) @@ -31,7 +31,7 @@ function initializeCoreMod() { if (insn.getOpcode() == Opcodes.ICONST_5) { node.instructions.insert(insn, new MethodInsnNode( Opcodes.INVOKESTATIC, - 'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature', + 'ru/dbotthepony/mc/otm/player/android/feature/LimbOverclockingFeature', 'getBrushTick', '(Lnet/minecraft/world/entity/LivingEntity;)I' )) @@ -59,7 +59,7 @@ function initializeCoreMod() { if (insn.getOpcode() == Opcodes.LDC && insn.cst == 10) { node.instructions.insert(insn, new MethodInsnNode( Opcodes.INVOKESTATIC, - 'ru/dbotthepony/mc/otm/android/feature/LimbOverclockingFeature', + 'ru/dbotthepony/mc/otm/player/android/feature/LimbOverclockingFeature', 'getBrushableBlockCooldown', '(Lnet/minecraft/world/entity/player/Player;)J' )) From 4fc646a13a23ea025d61ce217f9ab26fe2c5cfae Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 18:52:26 +0700 Subject: [PATCH 06/12] Energy counter "pull" mode --- .../ru/dbotthepony/mc/otm/datagen/lang/English.kt | 2 ++ .../ru/dbotthepony/mc/otm/datagen/lang/Russian.kt | 2 ++ .../block/entity/tech/EnergyCounterBlockEntity.kt | 13 ++++++++++--- .../otm/client/screen/tech/EnergyCounterScreen.kt | 14 +++++++++++++- .../mc/otm/menu/tech/EnergyCounterMenu.kt | 3 +++ 5 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt index 67ead1708..33c067834 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/English.kt @@ -631,6 +631,8 @@ private fun blocks(provider: MatteryLanguageProvider) { add(MBlocks.ENERGY_COUNTER[null]!!, "switch", "Switch input facing") add(MBlocks.ENERGY_COUNTER[null]!!, "limit", "I/O Limit. -1 means no limit") add(MBlocks.ENERGY_COUNTER[null]!!, "display_this", "Display this information on block's screen") + add(MBlocks.ENERGY_COUNTER[null]!!, "do_pull", "Pull energy from input side and push to output") + add(MBlocks.ENERGY_COUNTER[null]!!, "dont_pull", "Don't pull energy") addBlock(MBlocks.CHEMICAL_GENERATOR.values, "Chemical Generator") addBlock(MBlocks.CHEMICAL_GENERATOR.values, "desc", "Generates power by burning solid fuels") diff --git a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt index cc3b3a7d4..a79597b28 100644 --- a/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt +++ b/src/data/kotlin/ru/dbotthepony/mc/otm/datagen/lang/Russian.kt @@ -635,6 +635,8 @@ private fun blocks(provider: MatteryLanguageProvider) { add(MBlocks.ENERGY_COUNTER[null]!!, "switch", "Сменить сторону входа") add(MBlocks.ENERGY_COUNTER[null]!!, "limit", "Лимит ввода/вывода. -1 для отключения лимитов") add(MBlocks.ENERGY_COUNTER[null]!!, "display_this", "Отображать эти данные на экране блока") + add(MBlocks.ENERGY_COUNTER[null]!!, "do_pull", "Забирать энергию на входе и выталкивать её на выходе") + add(MBlocks.ENERGY_COUNTER[null]!!, "dont_pull", "Не забирать энергию на входе") addBlock(MBlocks.CHEMICAL_GENERATOR.values, "Химический генератор") addBlock(MBlocks.CHEMICAL_GENERATOR.values, "desc", "Генерирует энергию сжигая твёрдое топливо") diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt index 80b12d46f..790067c07 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt @@ -31,6 +31,7 @@ import ru.dbotthepony.mc.otm.registry.game.MBlockEntities class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : MatteryDeviceBlockEntity(MBlockEntities.ENERGY_COUNTER, p_155229_, p_155230_) { var passed by syncher.decimal() + var pullEnergyFromInput by syncher.boolean() override val blockRotation: BlockRotation by countingLazy(blockStateChangesCounter) { BlockRotation.of(blockState[EnergyCounterBlock.INPUT_DIRECTION]) @@ -86,10 +87,8 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat savetables.stateful(::history1h) savetables.stateful(::history6h) savetables.stateful(::history24h) - } - override fun saveLevel(nbt: CompoundTag, registry: HolderLookup.Provider) { - super.saveLevel(nbt, registry) + savetablesConfig.bool(::pullEnergyFromInput) } override fun loadAdditional(nbt: CompoundTag, registry: HolderLookup.Provider) { @@ -267,6 +266,14 @@ class EnergyCounterBlockEntity(p_155229_: BlockPos, p_155230_: BlockState) : Mat override fun tick() { super.tick() + if (pullEnergyFromInput) { + val input = inputCapability + val output = outputCapability + + if (input != null && output != null) + thisTick += moveEnergy(source = input, destination = output, simulate = false) + } + lastTick = thisTick charts.forEach { it.add(thisTick) } thisTick = Decimal.ZERO diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/EnergyCounterScreen.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/EnergyCounterScreen.kt index b552b93a1..8ddd297a5 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/EnergyCounterScreen.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/client/screen/tech/EnergyCounterScreen.kt @@ -111,7 +111,19 @@ class EnergyCounterScreen(menu: EnergyCounterMenu, inventory: Inventory, title: } } - makeDeviceControls(this, frame, redstoneConfig = menu.redstone) + val controls = makeDeviceControls(this, frame, redstoneConfig = menu.redstone) + + controls.addButton( + BooleanButtonPanel.square18( + this, + controls, + menu.pullEnergyFromInput, + iconActive = Widgets18.LEFT_CONTROLS.push, + iconInactive = Widgets18.LEFT_CONTROLS.output, + tooltipActive = TranslatableComponent("block.overdrive_that_matters.energy_counter.do_pull"), + tooltipInactive = TranslatableComponent("block.overdrive_that_matters.energy_counter.dont_pull") + ) + ) return frame } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt index 0f917784e..5849e82e4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt @@ -12,6 +12,7 @@ import ru.dbotthepony.mc.otm.core.chart.DecimalHistoryChart import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.toDecimal import ru.dbotthepony.mc.otm.menu.MatteryMenu +import ru.dbotthepony.mc.otm.menu.input.BooleanInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.IntInputWithFeedback import ru.dbotthepony.mc.otm.registry.game.MMenus @@ -34,6 +35,8 @@ class EnergyCounterMenu( val history6h = tile?.history6h ?: DecimalHistoryChart(720 * 6, 100) val history24h = tile?.history24h ?: DecimalHistoryChart(720 * 24, 100) + val pullEnergyFromInput = BooleanInputWithFeedback(this, tile?.let { it::pullEnergyFromInput }) + init { mSynchronizer.add(history5s) mSynchronizer.add(history15s) From 85dde24e59e98e3b197a3b94c16c3375df98f866 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 19:06:39 +0700 Subject: [PATCH 07/12] Update energy counter menu code --- .../mc/otm/menu/tech/EnergyCounterMenu.kt | 25 ++++--------------- .../mc/otm/network/syncher/SynchableGroup.kt | 4 +++ 2 files changed, 9 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt index 5849e82e4..e95e7e6e2 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/menu/tech/EnergyCounterMenu.kt @@ -17,15 +17,16 @@ import ru.dbotthepony.mc.otm.menu.input.EnumInputWithFeedback import ru.dbotthepony.mc.otm.menu.input.IntInputWithFeedback import ru.dbotthepony.mc.otm.registry.game.MMenus import java.math.BigDecimal +import java.util.function.Supplier class EnergyCounterMenu( p_38852_: Int, inventory: Inventory, tile: EnergyCounterBlockEntity? = null ) : MatteryMenu(MMenus.ENERGY_COUNTER, p_38852_, inventory, tile) { - var passed by mSynchronizer.decimal() - var lastTick by mSynchronizer.decimal() - var maxIO by mSynchronizer.decimal() + var passed by mSynchronizer.computedDecimal { tile?.passed ?: Decimal.ZERO } + var lastTick by mSynchronizer.computedDecimal { tile?.lastTick ?: Decimal.ZERO } + var maxIO by mSynchronizer.computedDecimal { tile?.ioLimit ?: Decimal.ZERO } val history5s = tile?.history5s ?: DecimalHistoryChart(1, 100) val history15s = tile?.history15s ?: DecimalHistoryChart(3, 100) @@ -62,7 +63,7 @@ class EnergyCounterMenu( } } - var inputDirection: Direction by mSynchronizer.enum(Direction.UP) + val inputDirection: Direction by mSynchronizer.computedEnum { tile?.blockState?.getValue(EnergyCounterBlock.INPUT_DIRECTION) ?: Direction.UP} val maxIOInput = decimalInput { if (tile is EnergyCounterBlockEntity) { @@ -73,20 +74,4 @@ class EnergyCounterMenu( } } } - - override fun beforeBroadcast() { - super.beforeBroadcast() - - if (tile is EnergyCounterBlockEntity) { - passed = tile.passed - lastTick = tile.lastTick - inputDirection = tile.blockState.getValue(EnergyCounterBlock.INPUT_DIRECTION) - - maxIO = tile.ioLimit?.toDecimal() ?: -Decimal.ONE - } - } - - companion object { - private val MINUS_ONE = -BigDecimal.ONE - } } diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt index 88813c500..9e7f0749c 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/syncher/SynchableGroup.kt @@ -270,6 +270,10 @@ class SynchableGroup : Observer, ISynchable, Iterable { return add(ListenableDelegate.maskSmart(value, getter, setter), MatteryStreamCodec.Enum(value::class.java)) } + inline fun > computedEnum(value: Supplier): SynchableDelegate { + return computed(value, MatteryStreamCodec.Enum(E::class.java)) + } + fun decimal(value: Decimal = Decimal.ZERO, setter: DelegateSetter = DelegateSetter.passthrough(), getter: DelegateGetter = DelegateGetter.passthrough()): SynchableDelegate { return add(ListenableDelegate.maskSmart(value, getter, setter), StreamCodecs.DECIMAL) } From e93c766edb30f7b6f749de6cc1ac968cc04b0394 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 19:16:30 +0700 Subject: [PATCH 08/12] Move stuff from "Decimal" to appropriate places --- .../entity/tech/EnergyCounterBlockEntity.kt | 2 +- .../energy/BatteryBackedEnergyStorage.kt | 2 +- .../jade/providers/MatterStorageProvider.kt | 4 +- .../jade/providers/MatteryEnergyProvider.kt | 4 +- .../mc/otm/config/AbstractConfig.kt | 1 - .../dbotthepony/mc/otm/config/CablesConfig.kt | 1 - .../mc/otm/config/DecimalConfigValue.kt | 58 +++++++++++++ .../mc/otm/config/ExopackConfig.kt | 1 - .../dbotthepony/mc/otm/config/ItemsConfig.kt | 1 - .../mc/otm/config/MachinesConfig.kt | 1 - .../dbotthepony/mc/otm/config/PlayerConfig.kt | 1 - .../dbotthepony/mc/otm/config/ServerConfig.kt | 1 - .../dbotthepony/mc/otm/core/math/Decimal.kt | 83 ------------------- .../mc/otm/core/nbt/CompoundTagExt.kt | 5 ++ .../mc/otm/core/util/FriendlyStreams.kt | 19 +++++ .../mc/otm/data/FlywheelMaterials.kt | 4 +- .../mc/otm/item/QuantumBatteryItem.kt | 7 +- .../mc/otm/item/weapon/EnergySwordItem.kt | 4 +- .../mc/otm/item/weapon/FallingSunItem.kt | 4 +- .../dbotthepony/mc/otm/matter/IMatterValue.kt | 4 +- .../mc/otm/network/StreamCodecs.kt | 4 +- .../mc/otm/player/MatteryFoodData.kt | 3 +- 22 files changed, 102 insertions(+), 112 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/config/DecimalConfigValue.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt index 790067c07..2b1bb16e0 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/tech/EnergyCounterBlockEntity.kt @@ -22,7 +22,7 @@ import ru.dbotthepony.mc.otm.core.chart.DecimalHistoryChart import ru.dbotthepony.mc.otm.core.math.BlockRotation import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.math.RelativeSide -import ru.dbotthepony.mc.otm.core.math.getDecimal +import ru.dbotthepony.mc.otm.core.nbt.getDecimal import ru.dbotthepony.mc.otm.core.nbt.mapPresent import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.core.util.countingLazy diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BatteryBackedEnergyStorage.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BatteryBackedEnergyStorage.kt index 256a66c80..b24020479 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BatteryBackedEnergyStorage.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/capability/energy/BatteryBackedEnergyStorage.kt @@ -15,7 +15,7 @@ import ru.dbotthepony.mc.otm.capability.FlowDirection import ru.dbotthepony.mc.otm.capability.extractEnergy import ru.dbotthepony.mc.otm.capability.receiveEnergy import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.getDecimal +import ru.dbotthepony.mc.otm.core.nbt.getDecimal import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.registry.StatNames import ru.dbotthepony.mc.otm.triggers.AndroidBatteryTrigger diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatterStorageProvider.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatterStorageProvider.kt index 3c3b15906..5c2769876 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatterStorageProvider.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatterStorageProvider.kt @@ -13,8 +13,8 @@ import ru.dbotthepony.mc.otm.compat.jade.JadeUids import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.getCapability import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.getDecimal -import ru.dbotthepony.mc.otm.core.math.putDecimal +import ru.dbotthepony.mc.otm.core.nbt.getDecimal +import ru.dbotthepony.mc.otm.core.nbt.putDecimal import ru.dbotthepony.mc.otm.core.util.formatMatter import snownee.jade.api.BlockAccessor import snownee.jade.api.IBlockComponentProvider diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatteryEnergyProvider.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatteryEnergyProvider.kt index 56727d58a..d4291dbb6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatteryEnergyProvider.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/compat/jade/providers/MatteryEnergyProvider.kt @@ -13,8 +13,8 @@ import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.kommons.math.RGBAColor import ru.dbotthepony.mc.otm.capability.IProfiledStorage import ru.dbotthepony.mc.otm.core.getCapability -import ru.dbotthepony.mc.otm.core.math.getDecimal -import ru.dbotthepony.mc.otm.core.math.putDecimal +import ru.dbotthepony.mc.otm.core.nbt.getDecimal +import ru.dbotthepony.mc.otm.core.nbt.putDecimal import ru.dbotthepony.mc.otm.core.util.formatPower import snownee.jade.api.* import snownee.jade.api.config.IPluginConfig diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/AbstractConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/AbstractConfig.kt index b76938fb9..a74463904 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/AbstractConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/AbstractConfig.kt @@ -7,7 +7,6 @@ import ru.dbotthepony.kommons.util.Delegate import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal import ru.dbotthepony.mc.otm.core.util.WriteOnce abstract class AbstractConfig(private val configName: String, private val type: ModConfig.Type = ModConfig.Type.SERVER) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt index fe5e269e3..ad00a469f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/CablesConfig.kt @@ -3,7 +3,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.kommons.util.setValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal object CablesConfig : AbstractConfig("cables") { enum class E(throughput: Decimal) { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/DecimalConfigValue.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/DecimalConfigValue.kt new file mode 100644 index 000000000..457e734c9 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/DecimalConfigValue.kt @@ -0,0 +1,58 @@ +package ru.dbotthepony.mc.otm.config + +import net.neoforged.neoforge.common.ModConfigSpec +import ru.dbotthepony.mc.otm.core.math.Decimal + +class DecimalConfigValue( + parent: ModConfigSpec.ConfigValue, + val minimum: Decimal? = null, + val maximum: Decimal? = null, +) : ObservedConfigValue(parent) { + override fun fromString(value: String): Decimal? { + try { + val parsed = Decimal(value) + + if (minimum != null && minimum > parsed) { + return minimum + } else if (maximum != null && maximum < parsed) { + return maximum + } + + return parsed + } catch (err: java.lang.NumberFormatException) { + return null + } + } + + override fun toString(value: Decimal): Pair { + if (minimum != null && minimum > value) { + return minimum.toString() to minimum + } else if (maximum != null && maximum < value) { + return maximum.toString() to maximum + } + + return value.toString() to value + } +} + +private fun ModConfigSpec.Builder.commentRange(minimum: Decimal?, maximum: Decimal?) { + if (minimum != null && maximum != null) { + comment("Range: $minimum ~ $maximum") + } else if (minimum != null) { + comment("Range: >= $minimum") + } else if (maximum != null) { + comment("Range: <= $maximum") + } +} + +fun ModConfigSpec.Builder.defineDecimal(path: String, defaultValue: Decimal, minimum: Decimal? = null, maximum: Decimal? = null): DecimalConfigValue { + commentRange(minimum, maximum) + comment("Default: $defaultValue") + return DecimalConfigValue(define(path, defaultValue.toString()), minimum, maximum) +} + +fun ModConfigSpec.Builder.defineDecimal(path: List, defaultValue: Decimal, minimum: Decimal? = null, maximum: Decimal? = null): DecimalConfigValue { + commentRange(minimum, maximum) + comment("Default: $defaultValue") + return DecimalConfigValue(define(path, defaultValue.toString()), minimum, maximum) +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ExopackConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ExopackConfig.kt index afe73a16a..99f6fc655 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ExopackConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ExopackConfig.kt @@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal object ExopackConfig : AbstractConfig("exopack") { val ENERGY_CAPACITY by builder diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ItemsConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ItemsConfig.kt index efe4c77ee..84e8bdb68 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ItemsConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ItemsConfig.kt @@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal import ru.dbotthepony.mc.otm.registry.MNames object ItemsConfig : AbstractConfig("items") { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/MachinesConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/MachinesConfig.kt index 7b312c5ae..573c1d70f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/MachinesConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/MachinesConfig.kt @@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal import ru.dbotthepony.mc.otm.registry.MNames object MachinesConfig : AbstractConfig("machines") { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/PlayerConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/PlayerConfig.kt index c9f907244..d903cad0e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/PlayerConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/PlayerConfig.kt @@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal object PlayerConfig : AbstractConfig("player") { init { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ServerConfig.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ServerConfig.kt index 0687260bb..df5193062 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/config/ServerConfig.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/config/ServerConfig.kt @@ -2,7 +2,6 @@ package ru.dbotthepony.mc.otm.config import ru.dbotthepony.kommons.util.getValue import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.defineDecimal object ServerConfig : AbstractConfig("misc") { val LABORATORY_LAMP_LIGHT_LENGTH: Int by builder.comment("In blocks").defineInRange("LABORATORY_LAMP_LIGHT_LENGTH", 6, 1, 128) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt index 4c4289757..18f0bb4e4 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt @@ -1,17 +1,10 @@ package ru.dbotthepony.mc.otm.core.math import net.minecraft.nbt.ByteArrayTag -import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag import net.minecraft.network.FriendlyByteBuf import net.minecraft.util.RandomSource -import net.neoforged.neoforge.common.ModConfigSpec -import ru.dbotthepony.mc.otm.config.ObservedConfigValue -import ru.dbotthepony.mc.otm.core.util.readVarIntLE -import ru.dbotthepony.mc.otm.core.util.writeVarIntLE -import java.io.InputStream -import java.io.OutputStream import java.math.BigDecimal import java.math.BigInteger import java.math.MathContext @@ -1603,28 +1596,6 @@ sealed class Decimal : Number(), Comparable { } } -fun FriendlyByteBuf.readDecimal() = Decimal.read(this) -fun FriendlyByteBuf.writeDecimal(value: Decimal) = value.write(this) - -fun InputStream.readDecimal(): Decimal { - val size = readVarIntLE() - require(size >= 0) { "Negative payload size: $size" } - val bytes = ByteArray(size) - read(bytes) - return Decimal.fromByteArray(bytes) -} - -fun OutputStream.writeDecimal(value: Decimal) { - val bytes = value.toByteArray() - writeVarIntLE(bytes.size) - write(bytes) -} - -fun CompoundTag.getDecimal(key: String) = Decimal.deserializeNBT(this[key]) -fun CompoundTag.putDecimal(key: String, value: Decimal) = put(key, value.serializeNBT()) - -operator fun CompoundTag.set(key: String, value: Decimal) = putDecimal(key, value) - fun Float.toDecimal() = Decimal(this) fun Double.toDecimal() = Decimal(this) fun Int.toDecimal() = Decimal(this) @@ -1633,60 +1604,6 @@ fun Short.toDecimal() = Decimal(this) fun Long.toDecimal() = Decimal(this) fun Decimal.toDecimal() = this -class DecimalConfigValue( - parent: ModConfigSpec.ConfigValue, - val minimum: Decimal? = null, - val maximum: Decimal? = null, -) : ObservedConfigValue(parent) { - override fun fromString(value: String): Decimal? { - try { - val parsed = Decimal(value) - - if (minimum != null && minimum > parsed) { - return minimum - } else if (maximum != null && maximum < parsed) { - return maximum - } - - return parsed - } catch (err: java.lang.NumberFormatException) { - return null - } - } - - override fun toString(value: Decimal): Pair { - if (minimum != null && minimum > value) { - return minimum.toString() to minimum - } else if (maximum != null && maximum < value) { - return maximum.toString() to maximum - } - - return value.toString() to value - } -} - -private fun ModConfigSpec.Builder.commentRange(minimum: Decimal?, maximum: Decimal?) { - if (minimum != null && maximum != null) { - comment("Range: $minimum ~ $maximum") - } else if (minimum != null) { - comment("Range: >= $minimum") - } else if (maximum != null) { - comment("Range: <= $maximum") - } -} - -fun ModConfigSpec.Builder.defineDecimal(path: String, defaultValue: Decimal, minimum: Decimal? = null, maximum: Decimal? = null): DecimalConfigValue { - commentRange(minimum, maximum) - comment("Default: $defaultValue") - return DecimalConfigValue(define(path, defaultValue.toString()), minimum, maximum) -} - -fun ModConfigSpec.Builder.defineDecimal(path: List, defaultValue: Decimal, minimum: Decimal? = null, maximum: Decimal? = null): DecimalConfigValue { - commentRange(minimum, maximum) - comment("Default: $defaultValue") - return DecimalConfigValue(define(path, defaultValue.toString()), minimum, maximum) -} - fun RandomSource.nextDecimal(min: Decimal, max: Decimal, round: Boolean = false): Decimal { val value = nextDouble() diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt index 9387c88e2..6521c0cda 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/nbt/CompoundTagExt.kt @@ -20,6 +20,7 @@ import net.minecraft.nbt.ShortTag import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag import net.minecraft.world.item.ItemStack +import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.core.util.readBinaryJson import ru.dbotthepony.mc.otm.core.util.writeBinaryJson import java.util.UUID @@ -115,3 +116,7 @@ fun CompoundTag.getJson(key: String, sizeLimit: NbtAccounter = NbtAccounter(1 sh fun CompoundTag.getBoolean(index: String, orElse: Boolean): Boolean { return (this[index] as? NumericTag)?.asInt?.let { it > 0 } ?: orElse } + +fun CompoundTag.getDecimal(key: String) = Decimal.deserializeNBT(this[key]) +fun CompoundTag.putDecimal(key: String, value: Decimal) = put(key, value.serializeNBT()) +operator fun CompoundTag.set(key: String, value: Decimal) = putDecimal(key, value) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt index c1aa44fc4..a97d9f517 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/util/FriendlyStreams.kt @@ -5,12 +5,14 @@ import net.minecraft.core.registries.BuiltInRegistries import net.minecraft.nbt.CompoundTag import net.minecraft.nbt.NbtAccounter import net.minecraft.nbt.NbtIo +import net.minecraft.network.FriendlyByteBuf import net.minecraft.network.chat.Component import net.minecraft.resources.ResourceLocation import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack import net.minecraft.world.level.material.Fluid import net.neoforged.neoforge.fluids.FluidStack +import ru.dbotthepony.mc.otm.core.math.Decimal import java.io.* import java.math.BigDecimal import java.math.BigInteger @@ -238,3 +240,20 @@ fun OutputStream.writeBinaryString(input: String) { writeVarIntLE(bytes.size) write(bytes) } + +fun FriendlyByteBuf.readDecimal() = Decimal.read(this) +fun FriendlyByteBuf.writeDecimal(value: Decimal) = value.write(this) + +fun InputStream.readDecimal(): Decimal { + val size = readVarIntLE() + require(size >= 0) { "Negative payload size: $size" } + val bytes = ByteArray(size) + read(bytes) + return Decimal.fromByteArray(bytes) +} + +fun OutputStream.writeDecimal(value: Decimal) { + val bytes = value.toByteArray() + writeVarIntLE(bytes.size) + write(bytes) +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/FlywheelMaterials.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/FlywheelMaterials.kt index c2cbf6141..50388d03a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/FlywheelMaterials.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/FlywheelMaterials.kt @@ -38,8 +38,8 @@ import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.core.TextComponent import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.readDecimal -import ru.dbotthepony.mc.otm.core.math.writeDecimal +import ru.dbotthepony.mc.otm.core.util.readDecimal +import ru.dbotthepony.mc.otm.core.util.writeDecimal import ru.dbotthepony.mc.otm.core.readBlockType import ru.dbotthepony.mc.otm.core.registryName import ru.dbotthepony.mc.otm.core.util.formatPower diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt index 5e2ba1f97..270b8ab3f 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/QuantumBatteryItem.kt @@ -38,11 +38,10 @@ import ru.dbotthepony.mc.otm.core.TranslatableComponent import ru.dbotthepony.mc.otm.core.collect.filter import ru.dbotthepony.mc.otm.core.isNotEmpty import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.getDecimal -import ru.dbotthepony.mc.otm.core.math.readDecimal -import ru.dbotthepony.mc.otm.core.math.set -import ru.dbotthepony.mc.otm.core.math.writeDecimal +import ru.dbotthepony.mc.otm.core.nbt.getDecimal +import ru.dbotthepony.mc.otm.core.util.readDecimal import ru.dbotthepony.mc.otm.core.nbt.set +import ru.dbotthepony.mc.otm.core.util.writeDecimal import ru.dbotthepony.mc.otm.core.nextUUID import ru.dbotthepony.mc.otm.core.util.formatPower import ru.dbotthepony.mc.otm.isClientThread diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt index 666ffbac2..d9a00fd6b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt @@ -29,8 +29,8 @@ import ru.dbotthepony.mc.otm.player.matteryPlayer import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.DecimalConfigValue -import ru.dbotthepony.mc.otm.core.math.defineDecimal +import ru.dbotthepony.mc.otm.config.DecimalConfigValue +import ru.dbotthepony.mc.otm.config.defineDecimal import ru.dbotthepony.mc.otm.core.math.nextVariance import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.core.util.WriteOnce diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt index d4f636c9e..0f3351298 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt @@ -29,8 +29,8 @@ import ru.dbotthepony.mc.otm.player.matteryPlayer import ru.dbotthepony.mc.otm.core.ResourceLocation import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.DecimalConfigValue -import ru.dbotthepony.mc.otm.core.math.defineDecimal +import ru.dbotthepony.mc.otm.config.DecimalConfigValue +import ru.dbotthepony.mc.otm.config.defineDecimal import ru.dbotthepony.mc.otm.core.math.nextVariance import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.core.util.WriteOnce diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterValue.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterValue.kt index 17fa4e5c2..5c87223a9 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterValue.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/matter/IMatterValue.kt @@ -2,8 +2,8 @@ package ru.dbotthepony.mc.otm.matter import net.minecraft.network.FriendlyByteBuf import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.readDecimal -import ru.dbotthepony.mc.otm.core.math.writeDecimal +import ru.dbotthepony.mc.otm.core.util.readDecimal +import ru.dbotthepony.mc.otm.core.util.writeDecimal import ru.dbotthepony.mc.otm.core.util.readDouble import ru.dbotthepony.mc.otm.core.util.writeDouble import java.io.InputStream diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/network/StreamCodecs.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/network/StreamCodecs.kt index 1e0b59ff9..8fffdd713 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/network/StreamCodecs.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/network/StreamCodecs.kt @@ -9,8 +9,8 @@ import net.minecraft.resources.ResourceLocation import net.minecraft.world.level.block.Block import net.minecraft.world.level.block.state.BlockState import ru.dbotthepony.kommons.math.RGBAColor -import ru.dbotthepony.mc.otm.core.math.readDecimal -import ru.dbotthepony.mc.otm.core.math.writeDecimal +import ru.dbotthepony.mc.otm.core.util.readDecimal +import ru.dbotthepony.mc.otm.core.util.writeDecimal import ru.dbotthepony.mc.otm.core.readBlockType import ru.dbotthepony.mc.otm.core.readItemType import ru.dbotthepony.mc.otm.core.writeBlockType diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryFoodData.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryFoodData.kt index 9f442d2df..cc547f8e6 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryFoodData.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/player/MatteryFoodData.kt @@ -14,8 +14,7 @@ import ru.dbotthepony.mc.otm.config.IFoodRegenerationValues import ru.dbotthepony.mc.otm.config.PlayerConfig import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.getDecimal -import ru.dbotthepony.mc.otm.core.math.set +import ru.dbotthepony.mc.otm.core.nbt.getDecimal import ru.dbotthepony.mc.otm.core.nbt.set import ru.dbotthepony.mc.otm.registry.MDamageTypes import kotlin.math.max From 5663b262ebebb2b69064fd025922867464593550 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 19:34:22 +0700 Subject: [PATCH 09/12] Move random extensions to RandomUtils file --- .../kotlin/ru/dbotthepony/mc/otm/core/Ext.kt | 97 ----------------- .../ru/dbotthepony/mc/otm/core/RandomUtils.kt | 103 ++++++++++++++++++ 2 files changed, 103 insertions(+), 97 deletions(-) create mode 100644 src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt index e1e0b3427..3b1c6b88b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/Ext.kt @@ -10,7 +10,6 @@ import com.google.common.collect.ImmutableSet import com.google.gson.JsonElement import com.google.gson.JsonObject import com.google.gson.JsonPrimitive -import it.unimi.dsi.fastutil.ints.IntList import it.unimi.dsi.fastutil.objects.ObjectComparators import net.minecraft.Util import net.minecraft.core.BlockPos @@ -29,7 +28,6 @@ import net.minecraft.network.chat.contents.TranslatableContents import net.minecraft.resources.ResourceKey import net.minecraft.resources.ResourceLocation import net.minecraft.tags.TagKey -import net.minecraft.util.RandomSource import net.minecraft.world.entity.Entity import net.minecraft.world.item.Item import net.minecraft.world.item.ItemStack @@ -65,13 +63,10 @@ import java.util.concurrent.Callable import java.util.concurrent.Future import java.util.function.Consumer import java.util.function.Supplier -import java.util.random.RandomGenerator import java.util.stream.Stream import java.util.stream.StreamSupport import kotlin.NoSuchElementException import kotlin.jvm.optionals.getOrNull -import kotlin.math.ln -import kotlin.math.sqrt import kotlin.reflect.KProperty operator fun RecipeInput.get(index: Int): ItemStack = getItem(index) @@ -189,43 +184,6 @@ fun > T.prev(values: Array): T { return values[next] } -fun IntArray.shuffle(random: RandomSource): IntArray { - for (i in lastIndex downTo 1) { - val j = random.nextInt(i + 1) - val copy = this[i] - this[i] = this[j] - this[j] = copy - } - - return this -} - -fun L.shuffle(random: RandomSource): L { - for (i in lastIndex downTo 1) { - val j = random.nextInt(i + 1) - set(j, set(i, getInt(j))) - } - - return this -} - -fun > L.shuffle(random: RandomSource): L { - Util.shuffle(this, random) - return this -} - -fun List.random(random: RandomGenerator): T { - if (isEmpty()) - throw NoSuchElementException("This list is empty") - return get(random.nextInt(size)) -} - -fun List.random(random: RandomSource): T { - if (isEmpty()) - throw NoSuchElementException("This list is empty") - return get(random.nextInt(size)) -} - inline fun immutableList(size: Int, initializer: (index: Int) -> T): ImmutableList { require(size >= 0) { "Invalid list size $size" } @@ -635,58 +593,3 @@ infix fun FluidStack.isNotSameAs(other: FluidStack): Boolean { return !FluidStack.isSameFluidSameComponents(this, other) && amount == other.amount } -data class DoublePair(val first: Double, val second: Double) - -fun RandomSource.nextUUID(): UUID { - return UUID(nextLong(), nextLong()) -} - -// normal distribution via Marsaglia polar method -fun RandomGenerator.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { - var rand1: Double - var rand2: Double - var distSqr: Double - - do { - rand1 = 2.0 * nextDouble() - 1.0 - rand2 = 2.0 * nextDouble() - 1.0 - distSqr = rand1 * rand1 + rand2 * rand2 - } while (distSqr >= 1.0 || distSqr == 0.0) - - val mapping = sqrt(-2.0 * ln(distSqr) / distSqr) - - return DoublePair( - rand1 * mapping * stddev + mean, - rand2 * mapping * stddev + mean - ) -} - -// All Mojang's random sources use MarsagliaPolarGaussian for generating normal distributed doubles -fun RandomSource.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { - return DoublePair( - nextGaussian() * stddev + mean, - nextGaussian() * stddev + mean - ) -} - -fun RandomSource.nextNormalDouble(stddev: Double, mean: Double): Double { - return nextGaussian() * stddev + mean -} - -fun RandomSource.nextFloat(min: Float, max: Float): Float { - if (this is RandomGenerator) - return nextFloat(min, max) - - require(max >= min) { "Min is bigger than max: $min vs $max" } - if (min == max) return min - return min + nextFloat() * (max - min) -} - -fun RandomSource.nextDouble(min: Double, max: Double): Double { - if (this is RandomGenerator) - return nextDouble(min, max) - - require(max >= min) { "Min is bigger than max: $min vs $max" } - if (min == max) return min - return min + nextDouble() * (max - min) -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt new file mode 100644 index 000000000..a042c8d08 --- /dev/null +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt @@ -0,0 +1,103 @@ +package ru.dbotthepony.mc.otm.core + +import it.unimi.dsi.fastutil.ints.IntList +import net.minecraft.Util +import net.minecraft.util.RandomSource +import java.util.* +import java.util.random.RandomGenerator +import kotlin.NoSuchElementException +import kotlin.math.ln +import kotlin.math.sqrt + +data class DoublePair(val first: Double, val second: Double) + +fun RandomSource.nextUUID(): UUID { + return UUID(nextLong(), nextLong()) +} + +// normal distribution via Marsaglia polar method +fun RandomGenerator.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { + var rand1: Double + var rand2: Double + var distSqr: Double + + do { + rand1 = 2.0 * nextDouble() - 1.0 + rand2 = 2.0 * nextDouble() - 1.0 + distSqr = rand1 * rand1 + rand2 * rand2 + } while (distSqr >= 1.0 || distSqr == 0.0) + + val mapping = sqrt(-2.0 * ln(distSqr) / distSqr) + + return DoublePair( + rand1 * mapping * stddev + mean, + rand2 * mapping * stddev + mean + ) +} + +// All Mojang's random sources use MarsagliaPolarGaussian for generating normal distributed doubles +fun RandomSource.nextNormalDoubles(stddev: Double, mean: Double): DoublePair { + return DoublePair( + nextGaussian() * stddev + mean, + nextGaussian() * stddev + mean + ) +} + +fun RandomSource.nextNormalDouble(stddev: Double, mean: Double): Double { + return nextGaussian() * stddev + mean +} + +fun RandomSource.nextFloat(min: Float, max: Float): Float { + if (this is RandomGenerator) + return nextFloat(min, max) + + require(max >= min) { "Min is bigger than max: $min vs $max" } + if (min == max) return min + return min + nextFloat() * (max - min) +} + +fun RandomSource.nextDouble(min: Double, max: Double): Double { + if (this is RandomGenerator) + return nextDouble(min, max) + + require(max >= min) { "Min is bigger than max: $min vs $max" } + if (min == max) return min + return min + nextDouble() * (max - min) +} + +fun IntArray.shuffle(random: RandomSource): IntArray { + for (i in lastIndex downTo 1) { + val j = random.nextInt(i + 1) + val copy = this[i] + this[i] = this[j] + this[j] = copy + } + + return this +} + +fun L.shuffle(random: RandomSource): L { + for (i in lastIndex downTo 1) { + val j = random.nextInt(i + 1) + set(j, set(i, getInt(j))) + } + + return this +} + +fun > L.shuffle(random: RandomSource): L { + Util.shuffle(this, random) + return this +} + +fun List.random(random: RandomGenerator): T { + if (isEmpty()) + throw NoSuchElementException("This list is empty") + return get(random.nextInt(size)) +} + +fun List.random(random: RandomSource): T { + if (isEmpty()) + throw NoSuchElementException("This list is empty") + return get(random.nextInt(size)) +} From c965bcdc8254169f50919fd9d44a2a1ce1dd910f Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 19:36:52 +0700 Subject: [PATCH 10/12] Move decimal sampling to random utils --- .../ru/dbotthepony/mc/otm/core/RandomUtils.kt | 15 +++++++++++++++ .../ru/dbotthepony/mc/otm/core/math/Decimal.kt | 14 -------------- .../mc/otm/data/world/DecimalProvider.kt | 2 +- .../mc/otm/item/weapon/EnergySwordItem.kt | 2 +- .../mc/otm/item/weapon/FallingSunItem.kt | 2 +- .../mc/otm/worldgen/feature/BlackHolePlacer.kt | 3 +-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt index a042c8d08..62214303e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt @@ -3,6 +3,7 @@ package ru.dbotthepony.mc.otm.core import it.unimi.dsi.fastutil.ints.IntList import net.minecraft.Util import net.minecraft.util.RandomSource +import ru.dbotthepony.mc.otm.core.math.Decimal import java.util.* import java.util.random.RandomGenerator import kotlin.NoSuchElementException @@ -101,3 +102,17 @@ fun List.random(random: RandomSource): T { throw NoSuchElementException("This list is empty") return get(random.nextInt(size)) } + +fun RandomSource.nextDecimal(min: Decimal, max: Decimal, round: Boolean = false): Decimal { + val value = nextDouble() + + return if (round) { + Decimal((min + (max - min) * value).whole) + } else { + min + (max - min) * value + } +} + +fun RandomSource.nextVariance(value: Decimal, round: Boolean = false): Decimal { + return nextDecimal(-value / 2, value / 2, round) +} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt index 18f0bb4e4..0a85c870a 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt @@ -4,7 +4,6 @@ import net.minecraft.nbt.ByteArrayTag import net.minecraft.nbt.StringTag import net.minecraft.nbt.Tag import net.minecraft.network.FriendlyByteBuf -import net.minecraft.util.RandomSource import java.math.BigDecimal import java.math.BigInteger import java.math.MathContext @@ -1604,16 +1603,3 @@ fun Short.toDecimal() = Decimal(this) fun Long.toDecimal() = Decimal(this) fun Decimal.toDecimal() = this -fun RandomSource.nextDecimal(min: Decimal, max: Decimal, round: Boolean = false): Decimal { - val value = nextDouble() - - return if (round) { - Decimal((min + (max - min) * value).whole) - } else { - min + (max - min) * value - } -} - -fun RandomSource.nextVariance(value: Decimal, round: Boolean = false): Decimal { - return nextDecimal(-value / 2, value / 2, round) -} diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt index 67a993e4e..fc733d3f3 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/data/world/DecimalProvider.kt @@ -8,7 +8,7 @@ import net.minecraft.util.RandomSource import net.neoforged.bus.api.IEventBus import ru.dbotthepony.mc.otm.OverdriveThatMatters import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.nextDecimal +import ru.dbotthepony.mc.otm.core.nextDecimal import ru.dbotthepony.mc.otm.data.codec.DecimalCodec import ru.dbotthepony.mc.otm.registry.MBuiltInRegistries import ru.dbotthepony.mc.otm.registry.MDeferredRegister diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt index d9a00fd6b..bf4d16672 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/EnergySwordItem.kt @@ -31,7 +31,7 @@ import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.config.DecimalConfigValue import ru.dbotthepony.mc.otm.config.defineDecimal -import ru.dbotthepony.mc.otm.core.math.nextVariance +import ru.dbotthepony.mc.otm.core.nextVariance import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.core.util.WriteOnce import ru.dbotthepony.mc.otm.item.MatteryItem diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt index 0f3351298..0b7042e3b 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/item/weapon/FallingSunItem.kt @@ -31,7 +31,7 @@ import ru.dbotthepony.mc.otm.core.damageType import ru.dbotthepony.mc.otm.core.math.Decimal import ru.dbotthepony.mc.otm.config.DecimalConfigValue import ru.dbotthepony.mc.otm.config.defineDecimal -import ru.dbotthepony.mc.otm.core.math.nextVariance +import ru.dbotthepony.mc.otm.core.nextVariance import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.core.util.WriteOnce import ru.dbotthepony.mc.otm.item.MatteryItem diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/feature/BlackHolePlacer.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/feature/BlackHolePlacer.kt index 102d3eccf..9a687d100 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/feature/BlackHolePlacer.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/worldgen/feature/BlackHolePlacer.kt @@ -1,6 +1,5 @@ package ru.dbotthepony.mc.otm.worldgen.feature -import com.mojang.serialization.Codec import com.mojang.serialization.codecs.RecordCodecBuilder import net.minecraft.world.level.levelgen.feature.Feature import net.minecraft.world.level.levelgen.feature.FeaturePlaceContext @@ -8,7 +7,7 @@ import net.minecraft.world.level.levelgen.feature.configurations.FeatureConfigur import ru.dbotthepony.mc.otm.block.entity.blackhole.BlackHoleBlockEntity import ru.dbotthepony.mc.otm.config.ServerConfig import ru.dbotthepony.mc.otm.core.math.Decimal -import ru.dbotthepony.mc.otm.core.math.nextDecimal +import ru.dbotthepony.mc.otm.core.nextDecimal import ru.dbotthepony.mc.otm.data.codec.DecimalCodec import ru.dbotthepony.mc.otm.registry.game.MBlocks From 1087e755ff46a5ba7ce66f1db6808afde18594b4 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 22:06:39 +0700 Subject: [PATCH 11/12] And now, proper random decimals implementation --- .../ru/dbotthepony/mc/otm/core/RandomUtils.kt | 117 ++++++++++++++++-- .../dbotthepony/mc/otm/core/math/Decimal.kt | 13 +- 2 files changed, 122 insertions(+), 8 deletions(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt index 62214303e..38dd8b17e 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/RandomUtils.kt @@ -4,9 +4,10 @@ import it.unimi.dsi.fastutil.ints.IntList import net.minecraft.Util import net.minecraft.util.RandomSource import ru.dbotthepony.mc.otm.core.math.Decimal +import java.math.BigInteger import java.util.* import java.util.random.RandomGenerator -import kotlin.NoSuchElementException +import kotlin.experimental.and import kotlin.math.ln import kotlin.math.sqrt @@ -103,14 +104,116 @@ fun List.random(random: RandomSource): T { return get(random.nextInt(size)) } -fun RandomSource.nextDecimal(min: Decimal, max: Decimal, round: Boolean = false): Decimal { - val value = nextDouble() +class RandomByteSource(private val source: RandomSource) { + private val bytes = ByteArray(8) + private var i = 7 - return if (round) { - Decimal((min + (max - min) * value).whole) - } else { - min + (max - min) * value + fun next(): Byte { + if (++i == 8) { + i = 0 + + var generate = source.nextLong() + + for (i in 0 .. 7) { + bytes[i] = generate.toByte() + generate = generate ushr 8 + } + } + + return bytes[i] } + + fun next(bound: Int): Byte { + require(bound > 0) { "Bound must be positive" } + val m = bound - 1 + var r = next().toInt().and(0xFF) + + if (bound and m == 0) { + r = r and m + } else { + var u = r ushr 1 + + while (true) { + r = u % bound + + if (u + m - r < 0) { + u = next().toInt().and(0xFF).ushr(1) + } else { + break + } + } + } + + return r.toByte() + } + + fun next(bytes: ByteArray) { + for (i in bytes.indices) { + bytes[i] = next() + } + } +} + +/** + * Uniformely distributed [Decimal] value on [0,[bound]) range + * + * If [round] is `true`, will only return integers + */ +fun RandomSource.nextDecimal(bound: Decimal, round: Boolean = false): Decimal { + if (round) + require(bound > Decimal.ZERO) { "Bound must be positive, $bound given" } + else + require(bound >= Decimal.ONE) { "Bound must be 1 or bigger, $bound given" } + + require(bound.isFinite) { "Bound must be finite" } + + val bytes = RandomByteSource(this) + + if (round) { + val thisBytes = bound.whole.toByteArray() + val generateBytes = ByteArray(thisBytes.size) + bytes.next(generateBytes) + + generateBytes[0] = generateBytes[0].and(127) + + if (generateBytes[0] >= thisBytes[0]) { + generateBytes[0] = bytes.next(thisBytes[0].toInt().and(127)) + } + + return Decimal(BigInteger(generateBytes)) + } else { + val thisBytes = bound.mag.toByteArray() + val generateBytes = ByteArray(thisBytes.size) + bytes.next(generateBytes) + + generateBytes[0] = generateBytes[0].and(127) + + if (generateBytes[0] >= thisBytes[0]) { + generateBytes[0] = bytes.next(thisBytes[0].toInt().and(127)) + } + + return Decimal.raw(BigInteger(generateBytes)) + } +} + +/** + * Uniformely distributed [Decimal] value on [[origin],[bound]) range + * + * If [round] is `true`, will only return integers + */ +fun RandomSource.nextDecimal(origin: Decimal, bound: Decimal, round: Boolean = false): Decimal { + require(origin < bound) { "Origin must be less than bound: $origin < $bound" } + require(origin.isFinite) { "Origin must be finite" } + require(bound.isFinite) { "Bound must be finite" } + + return origin + nextDecimal(bound - origin, round) +} + +/** + * Uniformely distributed [Decimal] value on [0,1) range + */ +fun RandomSource.nextDecimal(): Decimal { + return nextDecimal(Decimal.ZERO, Decimal.ONE) } fun RandomSource.nextVariance(value: Decimal, round: Boolean = false): Decimal { diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt index 0a85c870a..284cb53be 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/core/math/Decimal.kt @@ -39,6 +39,8 @@ sealed class Decimal : Number(), Comparable { */ abstract val fractional: BigInteger + abstract internal val mag: BigInteger + /** * *Signed* normalized (-1,1) fractional part of this Decimal, as [Float] */ @@ -181,7 +183,7 @@ sealed class Decimal : Number(), Comparable { return toBigDecmial().divide(divisor.toBigDecmial(), PERCENTAGE_CONTEXT).toFloat() } - private class Regular(val mag: BigInteger, marker: Nothing?) : Decimal() { + private class Regular(override val mag: BigInteger, marker: Nothing?) : Decimal() { constructor(value: BigInteger) : this(value * PRECISION_POW_BI, null) constructor(value: BigDecimal) : this(value.setScale(PRECISION, RoundingMode.HALF_UP).unscaledValue(), null) constructor(value: Float) : this(BigDecimal.valueOf(value.toDouble())) @@ -703,6 +705,9 @@ sealed class Decimal : Number(), Comparable { } private object PositiveInfinity : Decimal() { + override val mag: BigInteger + get() = throw UnsupportedOperationException() + private fun readResolve(): Any = PositiveInfinity override val isInfinite: Boolean @@ -952,6 +957,9 @@ sealed class Decimal : Number(), Comparable { } private object NegativeInfinity : Decimal() { + override val mag: BigInteger + get() = throw UnsupportedOperationException() + private fun readResolve(): Any = NegativeInfinity override val isInfinite: Boolean @@ -1195,6 +1203,9 @@ sealed class Decimal : Number(), Comparable { } private object Zero : Decimal() { + override val mag: BigInteger + get() = BigInteger.ZERO + private fun readResolve(): Any = Zero override val isInfinite: Boolean From a3ed8869009118ac10e9f697ce8f2cb295aa0ad7 Mon Sep 17 00:00:00 2001 From: DBotThePony Date: Wed, 26 Mar 2025 22:11:37 +0700 Subject: [PATCH 12/12] Fix wrong matter recycler receive values --- .../otm/block/entity/matter/MatterRecyclerBlockEntity.kt | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterRecyclerBlockEntity.kt b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterRecyclerBlockEntity.kt index ce4f69727..f87ee9707 100644 --- a/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterRecyclerBlockEntity.kt +++ b/src/main/kotlin/ru/dbotthepony/mc/otm/block/entity/matter/MatterRecyclerBlockEntity.kt @@ -24,6 +24,7 @@ import ru.dbotthepony.mc.otm.config.MachinesConfig import ru.dbotthepony.mc.otm.container.MatteryContainer import ru.dbotthepony.mc.otm.container.HandlerFilter import ru.dbotthepony.mc.otm.core.math.Decimal +import ru.dbotthepony.mc.otm.core.nextDecimal import ru.dbotthepony.mc.otm.core.otmRandom import ru.dbotthepony.mc.otm.graph.matter.MatterGraph import ru.dbotthepony.mc.otm.item.matter.MatterDustItem @@ -137,7 +138,7 @@ class MatterRecyclerBlockEntity(blockPos: BlockPos, blockState: BlockState) else if (matter.receiveMatter(toReceive, true) != toReceive) return status.noMatter() - matter.receiveMatter(toReceive * 0.4 + level!!.otmRandom.nextDouble() * 0.6, false) + matter.receiveMatter(toReceive * level!!.otmRandom.nextDecimal(BASE_RECEIVE, Decimal.ONE), false) job.totalMatter -= toReceive } @@ -151,4 +152,8 @@ class MatterRecyclerBlockEntity(blockPos: BlockPos, blockState: BlockState) matter.extractMatter(received, false) } } + + companion object { + private val BASE_RECEIVE = Decimal("0.4") + } }