Merge branch '1.21' into new-container-api

This commit is contained in:
DBotThePony 2025-03-26 22:14:39 +07:00
commit c2f20a76f6
Signed by: DBot
GPG Key ID: DCC23B5715498507
53 changed files with 569 additions and 444 deletions

View File

@ -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")

View File

@ -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", "Генерирует энергию сжигая твёрдое топливо")

View File

@ -104,6 +104,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)
@ -134,9 +136,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)

View File

@ -24,6 +24,7 @@ import ru.dbotthepony.mc.otm.config.MachinesConfig
import ru.dbotthepony.mc.otm.container.slotted.ContainerSlot
import ru.dbotthepony.mc.otm.container.slotted.SlottedContainer
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) : Ma
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) : Ma
matter.extractMatter(received, false)
}
}
companion object {
private val BASE_RECEIVE = Decimal("0.4")
}
}

View File

@ -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
@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -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) {

View File

@ -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<String>,
val minimum: Decimal? = null,
val maximum: Decimal? = null,
) : ObservedConfigValue<Decimal>(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<String, Decimal> {
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<String>, defaultValue: Decimal, minimum: Decimal? = null, maximum: Decimal? = null): DecimalConfigValue {
commentRange(minimum, maximum)
comment("Default: $defaultValue")
return DecimalConfigValue(define(path, defaultValue.toString()), minimum, maximum)
}

View File

@ -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

View File

@ -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") {

View File

@ -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") {

View File

@ -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 {

View File

@ -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)

View File

@ -13,7 +13,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
@ -32,7 +31,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
@ -69,14 +67,11 @@ 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.enums.EnumEntries
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)
@ -194,43 +189,6 @@ fun <T : Enum<T>> T.prev(values: Array<out T>): 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 : IntList> 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 <T, L : MutableList<T>> L.shuffle(random: RandomSource): L {
Util.shuffle(this, random)
return this
}
fun <T> List<T>.random(random: RandomGenerator): T {
if (isEmpty())
throw NoSuchElementException("This list is empty")
return get(random.nextInt(size))
}
fun <T> List<T>.random(random: RandomSource): T {
if (isEmpty())
throw NoSuchElementException("This list is empty")
return get(random.nextInt(size))
}
inline fun <T : Any> immutableList(size: Int, initializer: (index: Int) -> T): ImmutableList<T> {
require(size >= 0) { "Invalid list size $size" }
@ -644,44 +602,6 @@ 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 <K : Any, V> SimpleCache(size: Long, freshness: Duration): Cache<K, V> {
return Caffeine.newBuilder()
.maximumSize(size)
@ -690,21 +610,3 @@ fun <K : Any, V> SimpleCache(size: Long, freshness: Duration): Cache<K, V> {
.expireAfterWrite(freshness)
.build()
}
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)
}

View File

@ -0,0 +1,221 @@
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.math.BigInteger
import java.util.*
import java.util.random.RandomGenerator
import kotlin.experimental.and
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 : IntList> 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 <T, L : MutableList<T>> L.shuffle(random: RandomSource): L {
Util.shuffle(this, random)
return this
}
fun <T> List<T>.random(random: RandomGenerator): T {
if (isEmpty())
throw NoSuchElementException("This list is empty")
return get(random.nextInt(size))
}
fun <T> List<T>.random(random: RandomSource): T {
if (isEmpty())
throw NoSuchElementException("This list is empty")
return get(random.nextInt(size))
}
class RandomByteSource(private val source: RandomSource) {
private val bytes = ByteArray(8)
private var i = 7
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 {
return nextDecimal(-value / 2, value / 2, round)
}

View File

@ -1,17 +1,9 @@
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
@ -47,6 +39,8 @@ sealed class Decimal : Number(), Comparable<Decimal> {
*/
abstract val fractional: BigInteger
abstract internal val mag: BigInteger
/**
* *Signed* normalized (-1,1) fractional part of this Decimal, as [Float]
*/
@ -189,7 +183,7 @@ sealed class Decimal : Number(), Comparable<Decimal> {
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()))
@ -711,6 +705,9 @@ sealed class Decimal : Number(), Comparable<Decimal> {
}
private object PositiveInfinity : Decimal() {
override val mag: BigInteger
get() = throw UnsupportedOperationException()
private fun readResolve(): Any = PositiveInfinity
override val isInfinite: Boolean
@ -960,6 +957,9 @@ sealed class Decimal : Number(), Comparable<Decimal> {
}
private object NegativeInfinity : Decimal() {
override val mag: BigInteger
get() = throw UnsupportedOperationException()
private fun readResolve(): Any = NegativeInfinity
override val isInfinite: Boolean
@ -1203,6 +1203,9 @@ sealed class Decimal : Number(), Comparable<Decimal> {
}
private object Zero : Decimal() {
override val mag: BigInteger
get() = BigInteger.ZERO
private fun readResolve(): Any = Zero
override val isInfinite: Boolean
@ -1603,28 +1606,6 @@ sealed class Decimal : Number(), Comparable<Decimal> {
}
}
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,70 +1614,3 @@ fun Short.toDecimal() = Decimal(this)
fun Long.toDecimal() = Decimal(this)
fun Decimal.toDecimal() = this
class DecimalConfigValue(
parent: ModConfigSpec.ConfigValue<String>,
val minimum: Decimal? = null,
val maximum: Decimal? = null,
) : ObservedConfigValue<Decimal>(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<String, Decimal> {
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<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 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)
}

View File

@ -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
@ -125,3 +126,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)

View File

@ -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)
}

View File

@ -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

View File

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

View File

@ -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

View File

@ -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<ItemStack> {
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)
}
}
}

View File

@ -60,7 +60,7 @@ class ExopackUpgradeItem(
}
override fun use(p_41432_: Level, player: Player, hand: InteractionHand): InteractionResultHolder<ItemStack> {
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)

View File

@ -29,9 +29,9 @@ 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.core.math.nextVariance
import ru.dbotthepony.mc.otm.config.DecimalConfigValue
import ru.dbotthepony.mc.otm.config.defineDecimal
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

View File

@ -29,9 +29,9 @@ 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.core.math.nextVariance
import ru.dbotthepony.mc.otm.config.DecimalConfigValue
import ru.dbotthepony.mc.otm.config.defineDecimal
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

View File

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

View File

@ -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 }
}
@ -40,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 }
}
@ -49,8 +50,8 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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()) }
}
@ -59,7 +60,7 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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 }
}
@ -68,7 +69,7 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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 }
}
@ -77,7 +78,7 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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 }
}
@ -86,7 +87,7 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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 }
}
@ -95,7 +96,7 @@ class ComputeAction(
RecordCodecBuilder.create<Constant> {
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 }
}
@ -182,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)
}
}
@ -191,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)
}
}

View File

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

View File

@ -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

View File

@ -103,8 +103,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
@ -398,12 +399,10 @@ object MatterManager {
}
private object Resolver : SimpleJsonResourceReloadListener(GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(), FINDER_DIRECTORY) {
val delegate = RegistryDelegate<Finder>("recipe_finder") { sync(false) }
var ready = false
private set
val registrar = MDeferredRegister(delegate.key, OverdriveThatMatters.MOD_ID)
val registrar = MDeferredRegister(MRegistries.RECIPE_FINDER, OverdriveThatMatters.MOD_ID)
init {
registrar.register("simple") {
@ -640,11 +639,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)
}
@ -1496,20 +1495,6 @@ object MatterManager {
return Registry.direct(value)
}
/**
* Access recipe finders registry
*
* @throws IllegalStateException if calling too early
*/
@JvmStatic val recipeFinders get() = Resolver.delegate.get()
/**
* Access recipe finders registry key
*
* Use this with your [DeferredRegister]
*/
@JvmStatic val recipeFindersKey get() = Resolver.delegate.key
private val commentary = Reference2ObjectOpenHashMap<Item, ArrayList<Component>>()
@JvmStatic
@ -1534,7 +1519,6 @@ object MatterManager {
}
internal fun initialize(bus: IEventBus) {
bus.addListener(Resolver.delegate::build)
Resolver.registrar.register(bus)
}

View File

@ -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<ResourceLocation, TagKey<Item>>,
@ -22,7 +23,7 @@ class UpdateAction(
data class MatterFunction(val function: IMatterFunction, val value: Decimal) {
companion object {
val CODEC: Codec<MatterFunction> 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<ComplexityFunction> 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<PriorityFunction> 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)
}
}
}

View File

@ -12,19 +12,21 @@ 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
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)
@ -34,6 +36,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)
@ -59,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) {
@ -70,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
}
}

View File

@ -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<Pair<Int, Com
class SwitchAndroidFeaturePacket(val type: AndroidFeatureType<*>, 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()))
}
}
}

View File

@ -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

View File

@ -270,6 +270,10 @@ class SynchableGroup : Observer, ISynchable, Iterable<ISynchable> {
return add(ListenableDelegate.maskSmart(value, getter, setter), MatteryStreamCodec.Enum(value::class.java))
}
inline fun <reified E : Enum<E>> computedEnum(value: Supplier<E>): SynchableDelegate<E> {
return computed(value, MatteryStreamCodec.Enum(E::class.java))
}
fun decimal(value: Decimal = Decimal.ZERO, setter: DelegateSetter<Decimal> = DelegateSetter.passthrough(), getter: DelegateGetter<Decimal> = DelegateGetter.passthrough()): SynchableDelegate<Decimal> {
return add(ListenableDelegate.maskSmart(value, getter, setter), StreamCodecs.DECIMAL)
}

View File

@ -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

View File

@ -104,6 +104,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
@ -971,7 +972,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)

View File

@ -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<T : AndroidFeature> {
@ -26,7 +27,7 @@ open class AndroidFeatureType<T : AndroidFeature> {
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 {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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<KillAsAndroidTrigger.Instance>(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

View File

@ -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

View File

@ -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'
))