Use thread local non-cryptographic random for Quantum Battery UUIDs
since they dont need to be cryptographically secure
This commit is contained in:
parent
9b6fa89850
commit
29edf383cd
@ -7,6 +7,7 @@ import com.google.common.util.concurrent.ThreadFactoryBuilder
|
|||||||
import net.minecraft.client.server.IntegratedServer
|
import net.minecraft.client.server.IntegratedServer
|
||||||
import net.minecraft.core.HolderLookup
|
import net.minecraft.core.HolderLookup
|
||||||
import net.minecraft.server.MinecraftServer
|
import net.minecraft.server.MinecraftServer
|
||||||
|
import net.minecraft.util.RandomSource
|
||||||
import net.minecraft.world.level.Level
|
import net.minecraft.world.level.Level
|
||||||
import net.neoforged.api.distmarker.Dist
|
import net.neoforged.api.distmarker.Dist
|
||||||
import net.neoforged.fml.loading.FMLLoader
|
import net.neoforged.fml.loading.FMLLoader
|
||||||
@ -22,12 +23,17 @@ import ru.dbotthepony.mc.otm.core.collect.WeakHashSet
|
|||||||
import ru.dbotthepony.mc.otm.core.util.AtomicallyInvalidatedLazy
|
import ru.dbotthepony.mc.otm.core.util.AtomicallyInvalidatedLazy
|
||||||
import ru.dbotthepony.mc.otm.core.util.IConditionalTickable
|
import ru.dbotthepony.mc.otm.core.util.IConditionalTickable
|
||||||
import ru.dbotthepony.mc.otm.core.util.ITickable
|
import ru.dbotthepony.mc.otm.core.util.ITickable
|
||||||
|
import ru.dbotthepony.mc.otm.core.util.PCG32RandomSource
|
||||||
import ru.dbotthepony.mc.otm.core.util.TickList
|
import ru.dbotthepony.mc.otm.core.util.TickList
|
||||||
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
import ru.dbotthepony.mc.otm.graph.GraphNodeList
|
||||||
import java.lang.ref.Cleaner
|
import java.lang.ref.Cleaner
|
||||||
import java.util.*
|
import java.util.*
|
||||||
import java.util.concurrent.atomic.AtomicInteger
|
import java.util.concurrent.atomic.AtomicInteger
|
||||||
|
|
||||||
|
private val threadLocalRandom = ThreadLocal.withInitial { PCG32RandomSource() }
|
||||||
|
internal val THREAD_LOCAL_RANDOM: RandomSource
|
||||||
|
get() = threadLocalRandom.get()
|
||||||
|
|
||||||
private val preServerTick = TickList()
|
private val preServerTick = TickList()
|
||||||
private val postServerTick = TickList()
|
private val postServerTick = TickList()
|
||||||
private val preWorldTick = WeakHashMap<Level, TickList>()
|
private val preWorldTick = WeakHashMap<Level, TickList>()
|
||||||
|
@ -627,6 +627,10 @@ infix fun FluidStack.isNotSameAs(other: FluidStack): Boolean {
|
|||||||
|
|
||||||
data class DoublePair(val first: Double, val second: Double)
|
data class DoublePair(val first: Double, val second: Double)
|
||||||
|
|
||||||
|
fun RandomSource.nextUUID(): UUID {
|
||||||
|
return UUID(nextLong(), nextLong())
|
||||||
|
}
|
||||||
|
|
||||||
// normal distribution via Marsaglia polar method
|
// normal distribution via Marsaglia polar method
|
||||||
fun RandomGenerator.nextNormalDoubles(stddev: Double, mean: Double): DoublePair {
|
fun RandomGenerator.nextNormalDoubles(stddev: Double, mean: Double): DoublePair {
|
||||||
var rand1: Double
|
var rand1: Double
|
||||||
|
@ -2,11 +2,9 @@ package ru.dbotthepony.mc.otm.core.util
|
|||||||
|
|
||||||
import net.minecraft.util.Mth
|
import net.minecraft.util.Mth
|
||||||
import net.minecraft.util.RandomSource
|
import net.minecraft.util.RandomSource
|
||||||
import net.minecraft.world.level.levelgen.LegacyRandomSource
|
|
||||||
import net.minecraft.world.level.levelgen.MarsagliaPolarGaussian
|
import net.minecraft.world.level.levelgen.MarsagliaPolarGaussian
|
||||||
import net.minecraft.world.level.levelgen.PositionalRandomFactory
|
import net.minecraft.world.level.levelgen.PositionalRandomFactory
|
||||||
import net.minecraft.world.level.levelgen.RandomSupport
|
import net.minecraft.world.level.levelgen.RandomSupport
|
||||||
import ru.dbotthepony.kommons.random.LCG64Random
|
|
||||||
import ru.dbotthepony.kommons.random.PCG32Random
|
import ru.dbotthepony.kommons.random.PCG32Random
|
||||||
import java.lang.StringBuilder
|
import java.lang.StringBuilder
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ import net.neoforged.neoforge.event.tick.ServerTickEvent
|
|||||||
import net.neoforged.neoforge.network.PacketDistributor
|
import net.neoforged.neoforge.network.PacketDistributor
|
||||||
import net.neoforged.neoforge.network.handling.IPayloadContext
|
import net.neoforged.neoforge.network.handling.IPayloadContext
|
||||||
import ru.dbotthepony.mc.otm.OverdriveThatMatters
|
import ru.dbotthepony.mc.otm.OverdriveThatMatters
|
||||||
|
import ru.dbotthepony.mc.otm.THREAD_LOCAL_RANDOM
|
||||||
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
import ru.dbotthepony.mc.otm.capability.FlowDirection
|
||||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
import ru.dbotthepony.mc.otm.capability.MatteryCapability
|
||||||
import ru.dbotthepony.mc.otm.capability.trackedItems
|
import ru.dbotthepony.mc.otm.capability.trackedItems
|
||||||
@ -42,6 +43,8 @@ import ru.dbotthepony.mc.otm.core.math.readDecimal
|
|||||||
import ru.dbotthepony.mc.otm.core.math.set
|
import ru.dbotthepony.mc.otm.core.math.set
|
||||||
import ru.dbotthepony.mc.otm.core.math.writeDecimal
|
import ru.dbotthepony.mc.otm.core.math.writeDecimal
|
||||||
import ru.dbotthepony.mc.otm.core.nbt.set
|
import ru.dbotthepony.mc.otm.core.nbt.set
|
||||||
|
import ru.dbotthepony.mc.otm.core.nextUUID
|
||||||
|
import ru.dbotthepony.mc.otm.core.util.PCG32RandomSource
|
||||||
import ru.dbotthepony.mc.otm.core.util.formatPower
|
import ru.dbotthepony.mc.otm.core.util.formatPower
|
||||||
import ru.dbotthepony.mc.otm.isClientThread
|
import ru.dbotthepony.mc.otm.isClientThread
|
||||||
import ru.dbotthepony.mc.otm.isServerThread
|
import ru.dbotthepony.mc.otm.isServerThread
|
||||||
@ -82,7 +85,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class UnboundValues(override val uuid: UUID = UUID.randomUUID()) : IValues {
|
class UnboundValues(override val uuid: UUID = THREAD_LOCAL_RANDOM.nextUUID()) : IValues {
|
||||||
override var energy: Decimal = Decimal.ZERO
|
override var energy: Decimal = Decimal.ZERO
|
||||||
override var passed: Decimal = Decimal.ZERO
|
override var passed: Decimal = Decimal.ZERO
|
||||||
override var received: Decimal = Decimal.ZERO
|
override var received: Decimal = Decimal.ZERO
|
||||||
@ -125,7 +128,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc
|
|||||||
}
|
}
|
||||||
|
|
||||||
fun values(): Values {
|
fun values(): Values {
|
||||||
return values(UUID.randomUUID())
|
return values(THREAD_LOCAL_RANDOM.nextUUID())
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun save(nbt: CompoundTag, registry: HolderLookup.Provider): CompoundTag {
|
override fun save(nbt: CompoundTag, registry: HolderLookup.Provider): CompoundTag {
|
||||||
@ -160,7 +163,7 @@ class QuantumBatteryItem(val savedataID: String, val balanceValues: EnergyBalanc
|
|||||||
|
|
||||||
fun updateValues() {
|
fun updateValues() {
|
||||||
if (!values.isServer && isServerThread()) {
|
if (!values.isServer && isServerThread()) {
|
||||||
values = serverData.values(stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: UUID.randomUUID().also { stack[MDataComponentTypes.QUANTUM_LINK_ID] = it })
|
values = serverData.values(stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: THREAD_LOCAL_RANDOM.nextUUID().also { stack[MDataComponentTypes.QUANTUM_LINK_ID] = it })
|
||||||
} else if (isClientThread()) {
|
} else if (isClientThread()) {
|
||||||
val id = stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: return
|
val id = stack[MDataComponentTypes.QUANTUM_LINK_ID] ?: return
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user